-
Notifications
You must be signed in to change notification settings - Fork 3
/
feetmaker.py
265 lines (214 loc) · 7.72 KB
/
feetmaker.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
#!/usr/bin/env python3
import argparse
import fnmatch
import json
import logging
import os
import shutil
import subprocess
import sys
import zipfile
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='command')
build_parser = subparsers.add_parser('build')
build_parser.add_argument('--debug', action='store_true')
build_parser.add_argument('-o', action='store', default=None, dest='output')
build_parser.add_argument('-a', '--arch', dest='arch', action='store', default='win32', choices=['win32', 'amd64'])
clean_parser = subparsers.add_parser('clean')
setup_parser = subparsers.add_parser('setup')
python_parser = subparsers.add_parser('python')
python_parser.add_argument('-p', dest='pyversion', action='store', default='v3.8.2')
python_parser.add_argument('-a', '--arch', dest='arch', action='store', default='win32', choices=['win32', 'amd64'])
version = open("VERSION.txt").read()
python_loc_default = "cpython"
python_loc = os.getenv("FEET_PYTHON_DIR", python_loc_default)
# These patterns will be excluded from the generated Zip archives
zip_excludes = [
'**/__pycache__',
'**/*.pyc',
'**/*.pdb',
'**/*.exp',
'*venv*.exe',
'**/pythonw.exe',
'**/_test*',
'**/py.exe',
'**/pyw.exe',
'*test_*',
]
# These third-party packages will be included in the build
py_deps = (
'pip',
# 'requirements-parser',
'setuptools',
# 'pkg_resources',
)
# These first-party modules will be included outside the stdlib archive
non_zip_modules = (
'importlib',
'collections',
'_weakref',
'_io',
'encodings',
'codecs',
'_codecs',
'_signal',
'__main__',
'io',
'abc',
'_abc',
'site',
'os',
'stat',
'_stat',
'ntpath',
'genericpath',
'_collections_abc',
'_sitebuiltins',
'_bootlocale',
'_locale',
)
def clean():
if os.path.exists("feet/cpython"):
shutil.rmtree("feet/cpython")
for name in os.listdir('build'):
path = os.path.join('build', name)
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.unlink(path)
def is_excluded(name):
for pattern in zip_excludes:
if fnmatch.fnmatch(name, pattern):
return True
return False
def ignore_excludes(dirname, names):
if is_excluded(dirname):
print("IGNORE", dirname)
return []
return [
name for name in names
if is_excluded(os.path.join(dirname, name))
]
def zipdir(path, relto, dest, compression):
zipf = zipfile.ZipFile(dest, 'w', compression, compresslevel=9)
relto = relto or path
if path.endswith('*'):
path = path[:-2]
print("Writing zip file", dest, "from", path)
for root, _, files in os.walk(path):
for file in files:
src = os.path.join(root, file)
name = os.path.relpath(src, relto)
if not is_excluded(name):
zipf.write(src, name)
zipf.close()
def main(args):
if not os.path.exists("feetmaker.py"):
print("You are not in the feet repo. Cannot build.")
sys.exit(1)
if args.command == "python":
if not os.path.exists(python_loc):
assert python_loc_default == python_loc, "No python checkout found at FEET_PYTHON_DIR"
subprocess.check_call(f"git clone https://github.com/python/cpython.git {python_loc}")
os.chdir(python_loc)
subprocess.check_call("git fetch")
subprocess.check_call("git reset --hard HEAD")
subprocess.check_call(f"git checkout {args.pyversion}")
os.chdir('..')
assert os.path.exists(f"./{python_loc}/PCBuild/build.bat")
if args.arch == "amd64":
p = "x64"
else:
p = args.arch
print(f"./{python_loc}/PCBuild/build.bat -c Release -p {p} -t Build")
subprocess.check_call(f"{python_loc}\\PCBuild\\build.bat -c Release -p {p} -t Build")
elif not args.command or args.command == "build":
if os.path.exists("feet/cpython"):
shutil.rmtree("feet/cpython")
print("Compiling bootloader...")
subprocess.check_call("cargo build --release")
print("Creating runtime archive...")
py_exe = os.path.join(python_loc, "PCbuild", args.arch, 'python.exe')
subprocess.run([py_exe, '-m', 'lib2to3'], stdout=subprocess.PIPE)
shutil.copytree(
os.path.join(python_loc, "PCbuild", args.arch),
"feet/cpython",
ignore=ignore_excludes,
)
os.makedirs("feet/cpython/lib")
for name in os.listdir(os.path.join(python_loc, 'Lib')):
src = os.path.join(python_loc, "lib", name)
if os.path.splitext(name)[0] in non_zip_modules:
if not os.path.exists(src):
name += ".py"
src = os.path.join(python_loc, "lib", name)
if not os.path.exists(src):
print("Missing", src)
else:
print(f"Copying {src}")
if os.path.isdir(src):
shutil.copytree(src, f"feet/cpython/{name}")
else:
shutil.copy(
src,
"feet/cpython/",
)
feet_py = "feet/cpython/python.exe"
# Create the stdlib zip to make unpacking faster
zipdir(
os.path.join(python_loc, "lib"),
None,
os.path.join("feet", "cpython", "python38.zip"),
zipfile.ZIP_DEFLATED,
)
# Create the archive to attach to feet.exe to self-extract
subprocess.check_call([feet_py, '-m', 'ensurepip'])
for name in py_deps:
try:
p = subprocess.run([feet_py, '-m', 'pip', 'install', '-U', name], text=True)
print(f"Installing package '{name}'")
if p.stdout:
print(p.stdout)
except FileNotFoundError:
logger.error(f"Could not find python executable to install deps: {feet_py}")
raise
# Create archive to attach to runtime
zipdir(
'./feet/',
'.',
'./feetruntime.zip',
zipfile.ZIP_BZIP2,
)
print("Combining...")
if not os.path.exists('build'):
os.mkdir('build')
base = open('target/release/feet.exe', 'rb')
archive = open('feetruntime.zip', 'rb')
output = getattr(args, 'output', None) or f'build/feet-{args.arch}-{version}'
if not output.endswith('.exe'):
output += '.exe'
final = open(output, 'wb')
final.write(base.read())
final.write(archive.read())
final.close()
base.close()
archive.close()
# add metadata
final = zipfile.ZipFile(output, 'a')
final.comment = json.dumps({
'feet_format': '1',
'feet_arch': args.arch,
'feet_runner_size': os.stat('target/release/feet.exe').st_size,
'feet_archive_size': os.stat('feetruntime.zip').st_size,
}).encode('utf8')
print("Done.")
elif args.command == "clean":
clean()
elif args.command == "setup":
subprocess.check_call(["pip", "install", "-r", "requirements.txt"])
if __name__ == '__main__':
argv = sys.argv[1:]
args = parser.parse_args(argv)
main(args)