-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmake_wheels.py
346 lines (293 loc) · 13 KB
/
make_wheels.py
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
import hashlib
import pathlib
import urllib.request
import libarchive
from email.message import EmailMessage
from wheel.wheelfile import WheelFile
from zipfile import ZipInfo, ZIP_DEFLATED
from inspect import cleandoc
# Versions to build if run as a script:
BUILD_VERSIONS = ('14.19.3', '16.15.1', '18.4.0')
# Suffix to append to the Wheel
# For pre release versions this should be 'aN', e.g. 'a1'
# For release versions this should be ''
# See https://peps.python.org/pep-0427/#file-name-convention for details.
BUILD_SUFFIX = 'a3'
# Main binary for node
# Path of binary inn downloaded distribution to match
NODE_BINS = ('bin/node', 'node.exe')
# Other binaries
# key: path of binary inn downloaded distribution to match
# value: tuple of (
# <name>,
# <True = is a link to script to run with node, False = is executable>
# )
NODE_OTHER_BINS = {
'bin/npm': ('npm', True),
'npm.cmd': ('npm', False),
'bin/npx': ('npx', True),
'npx.cmd': ('npx', False),
'bin/corepack': ('corepack', True),
'corepack.cmd': ('corepack', False),
}
# Mapping of node platforms to Python platforms
PLATFORMS = {
'win-x86': 'win32',
'win-x64': 'win_amd64',
'darwin-x64': 'macosx_10_9_x86_64',
'darwin-arm64': 'macosx_11_0_arm64',
'linux-x64': 'manylinux_2_12_x86_64.manylinux2010_x86_64',
'linux-armv7l': 'manylinux_2_17_armv7l.manylinux2014_armv7l',
'linux-arm64': 'manylinux_2_17_aarch64.manylinux2014_aarch64',
'linux-x64-musl': 'musllinux_1_1_x86_64'
}
# https://github.com/nodejs/unofficial-builds/
# Versions added here should match the keys above
UNOFFICIAL_NODEJS_BUILDS = {'linux-x64-musl'}
_mismatched_versions = UNOFFICIAL_NODEJS_BUILDS - set(PLATFORMS.keys())
if _mismatched_versions:
raise Exception(f"A version mismatch occurred. Check the usage of {_mismatched_versions}")
class ReproducibleWheelFile(WheelFile):
def writestr(self, zinfo, *args, **kwargs):
if not isinstance(zinfo, ZipInfo):
raise ValueError("ZipInfo required")
zinfo.date_time = (1980, 1, 1, 0, 0, 0)
zinfo.create_system = 3
super().writestr(zinfo, *args, **kwargs)
def make_message(headers, payload=None):
msg = EmailMessage()
for name, value in headers.items():
if isinstance(value, list):
for value_part in value:
msg[name] = value_part
else:
msg[name] = value
if payload:
msg.set_payload(payload)
return msg
def write_wheel_file(filename, contents):
with ReproducibleWheelFile(filename, 'w') as wheel:
for member_info, member_source in contents.items():
if not isinstance(member_info, ZipInfo):
member_info = ZipInfo(member_info)
member_info.external_attr = 0o644 << 16
member_info.file_size = len(member_source)
member_info.compress_type = ZIP_DEFLATED
wheel.writestr(member_info, bytes(member_source))
return filename
def write_wheel(out_dir, *, name, version, tag, metadata, description, contents, entry_points):
name_snake = name.replace('-', '_')
wheel_name = f'{name_snake}-{version}-{tag}.whl'
dist_info = f'{name_snake}-{version}.dist-info'
if entry_points:
contents[f'{dist_info}/entry_points.txt'] = (cleandoc("""
[console_scripts]
{entry_points}
""").format(entry_points='\n'.join([f'{k} = {v}' for k, v in entry_points.items()] if entry_points else []))).encode('ascii'),
return write_wheel_file(os.path.join(out_dir, wheel_name), {
**contents,
f'{dist_info}/METADATA': make_message({
'Metadata-Version': '2.1',
'Name': name,
'Version': version,
**metadata,
}, description),
f'{dist_info}/WHEEL': make_message({
'Wheel-Version': '1.0',
'Generator': 'nodejs-pypi make_wheels.py',
'Root-Is-Purelib': 'false',
'Tag': tag,
}),
})
def write_nodejs_wheel(out_dir, *, node_version, version, platform, archive):
contents = {}
entry_points = {}
init_imports = []
# Create the output directory if it does not exist
out_dir_path = pathlib.Path(out_dir)
if not out_dir_path.exists():
out_dir_path.mkdir(parents=True)
with libarchive.memory_reader(archive) as archive:
for entry in archive:
entry_name = '/'.join(entry.name.split('/')[1:])
if entry.isdir or not entry_name:
continue
zip_info = ZipInfo(f'nodejs/{entry_name}')
zip_info.external_attr = (entry.mode & 0xFFFF) << 16
contents[zip_info] = b''.join(entry.get_blocks())
if entry_name in NODE_BINS:
# entry_points['node'] = 'nodejs.node:main'
init_imports.append('from . import node as node')
contents['nodejs/node.py'] = cleandoc(f"""
import os, sys, subprocess
from typing import TYPE_CHECKING
path = os.path.join(os.path.dirname(__file__), "{entry_name}")
if TYPE_CHECKING:
call = subprocess.call
run = subprocess.run
Popen = subprocess.Popen
else:
def call(args, **kwargs):
return subprocess.call([
path,
*args
], **kwargs)
def run(args, **kwargs):
return subprocess.run([
path,
*args
], **kwargs)
def Popen(args, **kwargs):
return subprocess.Popen([
path,
*args
], **kwargs)
def main() -> None:
sys.exit(call(sys.argv[1:]))
if __name__ == '__main__':
main()
""").encode('ascii')
contents['nodejs/__main__.py'] = cleandoc(f"""
from .node import main
if __name__ == '__main__':
main()
""").encode('ascii')
elif entry_name in NODE_OTHER_BINS and NODE_OTHER_BINS[entry_name][1]:
other_bin = NODE_OTHER_BINS[entry_name][0]
init_imports.append(f'from . import {other_bin} as {other_bin}')
script_name = '/'.join(os.path.normpath(os.path.join(os.path.dirname(entry.name), entry.linkpath)).split('/')[1:])
contents[f'nodejs/{NODE_OTHER_BINS[entry_name][0]}.py'] = cleandoc(f"""
import os, sys
from typing import TYPE_CHECKING
from . import node
if TYPE_CHECKING:
call = subprocess.call
run = subprocess.run
Popen = subprocess.Popen
else:
def call(args, **kwargs):
return node.call([
os.path.join(os.path.dirname(__file__), "{script_name}"),
*args
], **kwargs)
def run(args, **kwargs):
return node.run([
os.path.join(os.path.dirname(__file__), "{script_name}"),
*args
], **kwargs)
def Popen(args, **kwargs):
return node.Popen([
os.path.join(os.path.dirname(__file__), "{script_name}"),
*args
], **kwargs)
def main() -> None:
sys.exit(call(sys.argv[1:]))
if __name__ == '__main__':
main()
""").encode('ascii')
elif entry_name in NODE_OTHER_BINS:
other_bin = NODE_OTHER_BINS[entry_name][0]
init_imports.append(f'from . import {other_bin} as {other_bin}')
contents[f'nodejs/{NODE_OTHER_BINS[entry_name][0]}.py'] = cleandoc(f"""
import os, sys, subprocess
from typing import TYPE_CHECKING
if TYPE_CHECKING:
call = subprocess.call
run = subprocess.run
Popen = subprocess.Popen
else:
def call(args, **kwargs):
return subprocess.call([
os.path.join(os.path.dirname(__file__), "{entry_name}"),
*args
], **kwargs)
def run(args, **kwargs):
return subprocess.run([
os.path.join(os.path.dirname(__file__), "{entry_name}"),
*args
], **kwargs)
def Popen(args, **kwargs):
return subprocess.Popen([
os.path.join(os.path.dirname(__file__), "{entry_name}"),
*args
], **kwargs)
def main() -> None:
sys.exit(call(sys.argv[1:]))
if __name__ == '__main__':
main()
""").encode('ascii')
contents['nodejs/__init__.py'] = (cleandoc("""
import sys
from .node import path as path, main as main, call as call, run as run, Popen as Popen
if not '-m' in sys.argv:
{init_imports}
__version__ = "{version}"
node_version = "{node_version}"
""")).format(
# Note: two space indentation above and below is necessary to align
init_imports='\n '.join(init_imports),
version=version,
node_version=node_version,
).encode('ascii')
contents['nodejs/py.typed'] = b''
with open('README.md') as f:
description = f.read()
return write_wheel(out_dir,
name='nodejs-bin',
version=version,
tag=f'py3-none-{platform}',
metadata={
'Summary': 'Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.',
'Description-Content-Type': 'text/markdown',
'License': 'MIT',
'Classifier': [
'License :: OSI Approved :: MIT License',
],
'Project-URL': [
'Project Homepage, https://github.com/samwillis/nodejs-pypi',
'Node.js Homepage, https://nodejs.org',
],
'Requires-Python': '~=3.5',
'Provides-Extra': 'cmd',
'Requires-Dist': "nodejs-cmd; extra == 'cmd'",
},
description=description,
contents=contents,
entry_points=entry_points,
)
def make_nodejs_version(node_version, suffix=''):
wheel_version = f'{node_version}{suffix}'
print('--')
print('Making Node.js Wheels for version', node_version)
if suffix:
print('Suffix:', suffix)
for node_platform, python_platform in PLATFORMS.items():
filetype = 'zip' if node_platform.startswith('win-') else 'tar.xz'
if node_platform in UNOFFICIAL_NODEJS_BUILDS:
node_url = f'https://unofficial-builds.nodejs.org/download/release/v{node_version}/node-v{node_version}-{node_platform}.{filetype}'
else:
node_url = f'https://nodejs.org/dist/v{node_version}/node-v{node_version}-{node_platform}.{filetype}'
print(f'- Making Wheel for {node_platform} from {node_url}')
try:
with urllib.request.urlopen(node_url) as request:
node_archive = request.read()
print(f' {node_url}')
print(f' {hashlib.sha256(node_archive).hexdigest()}')
except urllib.error.HTTPError as e:
print(f' {e.code} {e.reason}')
print(f' Skipping {node_platform}')
continue
wheel_path = write_nodejs_wheel('dist/',
node_version=node_version,
version=wheel_version,
platform=python_platform,
archive=node_archive)
with open(wheel_path, 'rb') as wheel:
print(f' {wheel_path}')
print(f' {hashlib.sha256(wheel.read()).hexdigest()}')
def main():
for node_version in BUILD_VERSIONS:
make_nodejs_version(node_version, suffix=BUILD_SUFFIX)
if __name__ == '__main__':
main()