-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathchunk-native-libraries.py
More file actions
184 lines (159 loc) · 6.4 KB
/
Copy pathchunk-native-libraries.py
File metadata and controls
184 lines (159 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright (c) 2026, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import shutil
from java.io import FileInputStream, FileOutputStream
from java.util.zip import CRC32
from jarray import zeros
COPY_BUFFER_SIZE = 1024 * 1024
MANIFEST_SUFFIX = ".chunks.properties"
CHUNK_DIRECTORY_SUFFIX = ".chunks"
NATIVE_SUFFIXES = (".so", ".dylib", ".dll")
EMPTY_INCLUDE_PATTERN = "__no_native_chunks__"
def ensure_directory(path):
if not os.path.isdir(path):
os.makedirs(path)
def remove_path(path):
if os.path.isdir(path):
shutil.rmtree(path)
elif os.path.exists(path):
os.remove(path)
def native_libraries(root_dir, minimum_size):
candidates = []
for arch in sorted(os.listdir(root_dir)):
arch_dir = os.path.join(root_dir, arch)
if not os.path.isdir(arch_dir):
continue
for operating_system in sorted(os.listdir(arch_dir)):
os_dir = os.path.join(arch_dir, operating_system)
if not os.path.isdir(os_dir):
continue
for name in sorted(os.listdir(os_dir)):
path = os.path.join(os_dir, name)
if (os.path.isfile(path) and name.endswith(NATIVE_SUFFIXES)
and os.path.getsize(path) >= minimum_size):
candidates.append(path)
return candidates
def split_library(root_dir, library_path, chunk_size):
relative_library = os.path.relpath(library_path, root_dir).replace(os.sep, "/")
source_stat = os.stat(library_path)
library_size = source_stat.st_size
chunk_dir = library_path + CHUNK_DIRECTORY_SUFFIX
temporary_chunk_dir = chunk_dir + ".tmp"
manifest_path = library_path + MANIFEST_SUFFIX
temporary_manifest = manifest_path + ".tmp"
remove_path(temporary_chunk_dir)
remove_path(temporary_manifest)
if os.path.exists(chunk_dir) or os.path.exists(manifest_path):
raise RuntimeError("Chunk output already exists for %s" % relative_library)
ensure_directory(temporary_chunk_dir)
crc = CRC32()
source = FileInputStream(library_path)
buffer = zeros(COPY_BUFFER_SIZE, "b")
deflated_entries = []
chunk_crc32 = []
chunk_count = 0
total_bytes = 0
try:
while total_bytes < library_size:
chunk_name = "%05d" % chunk_count
chunk_path = os.path.join(temporary_chunk_dir, chunk_name)
chunk_output = FileOutputStream(chunk_path)
chunk_crc = CRC32()
chunk_bytes = 0
expected = min(chunk_size, library_size - total_bytes)
try:
while chunk_bytes < expected:
requested = int(min(COPY_BUFFER_SIZE, expected - chunk_bytes))
count = source.read(buffer, 0, requested)
if count < 0:
raise RuntimeError("Unexpected end of native library %s" % relative_library)
if count:
chunk_output.write(buffer, 0, count)
crc.update(buffer, 0, count)
chunk_crc.update(buffer, 0, count)
chunk_bytes += count
total_bytes += count
finally:
chunk_output.close()
os.utime(chunk_path, (source_stat.st_atime, source_stat.st_mtime))
final_relative = (
relative_library + CHUNK_DIRECTORY_SUFFIX + "/" + chunk_name)
deflated_entries.append(final_relative)
chunk_crc32.append(chunk_crc.getValue())
chunk_count += 1
except:
remove_path(temporary_chunk_dir)
remove_path(temporary_manifest)
raise
finally:
source.close()
if total_bytes != library_size:
remove_path(temporary_chunk_dir)
raise RuntimeError(
"Native library changed while chunking %s: expected %d bytes, read %d"
% (relative_library, library_size, total_bytes))
manifest = (
"format.version=1\n"
"library.size=%d\n"
"library.crc32=%08x\n"
"chunk.size=%d\n"
"chunk.count=%d\n"
% (library_size, crc.getValue(), chunk_size, chunk_count))
manifest += "".join(
"chunk.%05d.crc32=%08x\n" % (index, value)
for index, value in enumerate(chunk_crc32))
manifest_output = open(temporary_manifest, "w")
try:
manifest_output.write(manifest)
finally:
manifest_output.close()
os.utime(temporary_manifest, (source_stat.st_atime, source_stat.st_mtime))
os.rename(temporary_chunk_dir, chunk_dir)
os.rename(temporary_manifest, manifest_path)
os.remove(library_path)
return relative_library, deflated_entries
def write_lines(path, values):
output = open(path, "w")
try:
if not values:
output.write(EMPTY_INCLUDE_PATTERN)
output.write("\n")
for value in sorted(values):
output.write(value)
output.write("\n")
finally:
output.close()
root_dir = attributes.get("root_dir")
metadata_dir = attributes.get("metadata_dir")
minimum_size = long(attributes.get("minimum_size"))
chunk_size = long(attributes.get("chunk_size"))
if minimum_size <= 0 or chunk_size <= 0:
raise RuntimeError("Native chunk sizes must be positive")
ensure_directory(metadata_dir)
deflated_entries = []
manifests = []
for library_path in native_libraries(root_dir, minimum_size):
relative_library, library_deflated = split_library(
root_dir, library_path, chunk_size)
deflated_entries.extend(library_deflated)
manifests.append(relative_library + MANIFEST_SUFFIX)
self.log(
"Chunked %s into %d DEFLATED entries"
% (relative_library, len(library_deflated)))
write_lines(os.path.join(metadata_dir, "deflated-chunks.list"), deflated_entries)
write_lines(os.path.join(metadata_dir, "chunk-manifests.list"), manifests)
if manifests:
marker = open(os.path.join(metadata_dir, "enabled"), "w")
marker.close()