-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
setup.py
342 lines (263 loc) · 11 KB
/
setup.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
import setuptools
import os
import subprocess
import shutil
import re
from pathlib import Path
from setuptools import Command
with open('ethwizard/__init__.py', 'rt') as f:
version = re.search(r'__version__ = \'(.*?)\'', f.read()).group(1)
def get_python_binary():
try:
process_result = subprocess.run(['python3', '--version'])
if process_result.returncode == 0:
return 'python3'
except FileNotFoundError:
pass
try:
process_result = subprocess.run(['python', '--version'])
if process_result.returncode == 0:
return 'python'
except FileNotFoundError:
pass
return None
def include_requirements(target_path):
project_path = Path(os.getcwd())
python_binary = get_python_binary()
# Install packages from requirements.txt file into build dir
requirements_path = Path(project_path, 'requirements.txt')
subprocess.run([
python_binary, '-m', 'pip', 'install', '-r', requirements_path,
'--target', target_path
])
# Clean __pycache__ directories
dir_list = []
dir_list.append(target_path)
while len(dir_list) > 0:
next_dir = dir_list.pop()
with os.scandir(next_dir) as it:
for entry in it:
if entry.name.startswith('.'):
continue
if entry.is_dir():
if entry.name == '__pycache__':
shutil.rmtree(entry.path)
else:
dir_list.append(entry.path)
# Clean .dist-info directories
with os.scandir(target_path) as dir_it:
for entry in dir_it:
if entry.name.startswith('.') or not entry.is_dir():
continue
if entry.name.startswith('humanize'):
continue
if entry.name.startswith('setuptools'):
continue
if entry.name.endswith('.dist-info'):
shutil.rmtree(entry.path)
def create_zipapp(for_windows=False):
project_path = Path(os.getcwd())
src_package_path = Path(project_path, 'ethwizard')
python_binary = get_python_binary()
# Create and clean the build dir
build_path = Path(project_path, 'build')
if build_path.is_dir():
shutil.rmtree(build_path)
build_path.mkdir(parents=True, exist_ok=True)
# Copy package into build dir
build_package_path = Path(build_path, 'ethwizard')
shutil.copytree(src_package_path, build_package_path)
# Copy __main__.py into build root
src_main_path = Path(src_package_path, '__main__.py')
build_main_path = Path(build_path, '__main__.py')
shutil.copyfile(src_main_path, build_main_path)
include_requirements(build_path)
# Bundle with zipapp
dist_path = Path(project_path, 'dist')
dist_path.mkdir(parents=True, exist_ok=True)
added_part = ''
if for_windows:
added_part = '-win'
bundle_name = f'ethwizard-{version}{added_part}.pyz'
bundle_path = Path(dist_path, bundle_name)
if bundle_path.is_file():
bundle_path.unlink()
command = [
python_binary, '-m', 'zipapp', build_path, '-c', '-o', bundle_path
]
if not for_windows:
command.extend(['-p', '/usr/bin/env python3'])
subprocess.run(command)
return bundle_path
class Bundle(Command):
''' Create a bundle for release
'''
description = 'create a bundle for release'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
bundle_path = create_zipapp()
project_path = Path(os.getcwd())
dist_path = Path(project_path, 'dist')
dist_path.mkdir(parents=True, exist_ok=True)
bundle_name = f'ethwizard-{version}.pyz'
# Sign bundle with GPG key
bundle_sign_name = f'{bundle_name}.asc'
bundle_sign_path = Path(dist_path, bundle_sign_name)
if bundle_sign_path.is_file():
bundle_sign_path.unlink()
subprocess.run([
'gpg', '--default-key', 'BE905564CFC0D168456BCC72D292C9F431302697', '--sign',
'--armor', '--output', bundle_sign_path, '--detach-sig', bundle_path
])
# gpg --default-key BE905564CFC0D168456BCC72D292C9F431302697 --sign --armor --output ethwizard-0.7.2.exe.asc --detach-sig ethwizard-0.7.2.exe
class BundleWinZip(Command):
''' Create a Windows zipapp bundle for release
'''
description = 'create a Windows zipapp bundle for release'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
bundle_path = create_zipapp(for_windows=True)
class BundleWinExe(Command):
''' Create a Windows bundle for release
'''
description = 'create a Windows bundle for release'
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
# Find 7-Zip directory
import winreg
sevenzip_directory = None
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\7-Zip') as key:
sevenzip_directory = winreg.QueryValueEx(key, 'Path')
if sevenzip_directory:
sevenzip_directory = Path(sevenzip_directory[0])
except OSError as exception:
print(f'Unable to find 7-Zip directory. Exception: {exception}')
return
if sevenzip_directory is None:
print('We could not find 7-Zip. Make sure to install 7-Zip from '
'https://www.7-zip.org/')
return
sevenzip_binary = sevenzip_directory.joinpath('7z.exe')
if not sevenzip_binary.is_file():
print(f'We could not find the 7-Zip binary in {sevenzip_binary}')
return
project_path = Path(os.getcwd())
src_package_path = Path(project_path, 'ethwizard')
build_path = Path(project_path, 'build')
if build_path.is_dir():
shutil.rmtree(build_path)
build_path.mkdir(parents=True, exist_ok=True)
# Create SFX config file
sfx_config_path = build_path.joinpath('config.txt')
with open(sfx_config_path, 'w', encoding='utf8') as config_file:
config_file.write(
f'''
;!@Install@!UTF-8!
Title="eth-wizard {version}"
ExecuteFile="python.exe"
ExecuteParameters="-m ethwizard"
;!@InstallEnd@!
''' )
download_path = build_path.joinpath('downloads')
download_path.mkdir(parents=True, exist_ok=True)
# Download Python embeddable package
import httpx
python_embed_url = 'https://www.python.org/ftp/python/3.12.1/python-3.12.1-embed-amd64.zip'
python_embed_name = 'python-3.12.1-embed-amd64.zip'
python_embed_archive = download_path.joinpath(python_embed_name)
try:
with open(python_embed_archive, 'wb') as binary_file:
print(f'Downloading python archive {python_embed_name}...')
with httpx.stream('GET', python_embed_url) as http_stream:
if http_stream.status_code != 200:
print(f'Cannot download python archive {python_embed_name}.\n'
f'Unexpected status code {http_stream.status_code}')
return False
for data in http_stream.iter_bytes():
binary_file.write(data)
except httpx.RequestError as exception:
print(f'Exception while downloading python archive. Exception: {exception}')
return False
archive_dir_path = build_path.joinpath('archive')
archive_dir_path.mkdir(parents=True, exist_ok=True)
# Extracting Python embeddable package
from zipfile import ZipFile
print(f'Extracting python archive {python_embed_name}...')
with ZipFile(python_embed_archive, 'r') as zip_file:
zip_file.extractall(archive_dir_path)
# Download LZMA SDK for SFX modules for installers
lzma_sdk_url = 'https://www.7-zip.org/a/lzma1900.7z'
lzma_sdk_name = 'lzma1900.7z'
lzma_sdk_archive = download_path.joinpath(lzma_sdk_name)
try:
with open(lzma_sdk_archive, 'wb') as binary_file:
print(f'Downloading LZMA SDK archive {lzma_sdk_name}...')
with httpx.stream('GET', lzma_sdk_url) as http_stream:
if http_stream.status_code != 200:
print(f'Cannot download LZMA SDK archive {lzma_sdk_name}.\n'
f'Unexpected status code {http_stream.status_code}')
return False
for data in http_stream.iter_bytes():
binary_file.write(data)
except httpx.RequestError as exception:
print(f'Exception while downloading LZMA SDK archive. Exception: {exception}')
return False
lzma_dir_path = build_path.joinpath('lzma')
lzma_dir_path.mkdir(parents=True, exist_ok=True)
# Extracting LZMA SDK
subprocess.run([
sevenzip_binary, 'x', lzma_sdk_archive, '-y'
], cwd=lzma_dir_path)
sfx_module = lzma_dir_path.joinpath('bin', '7zSD.sfx')
if not sfx_module.is_file():
print(f'We could not find the 7-Zip SFX module in {lzma_dir_path}')
return
# Copy package into archive dir
archive_package_path = archive_dir_path.joinpath('ethwizard')
shutil.copytree(src_package_path, archive_package_path)
include_requirements(archive_dir_path)
# Create archive to be used with self extracting (SFX)
sfx_archive_path = build_path.joinpath('sfx.7z')
subprocess.run([
sevenzip_binary, 'a', '-t7z', sfx_archive_path, '*'
], cwd=archive_dir_path)
# Create distribution file
dist_path = Path(project_path, 'dist')
dist_path.mkdir(parents=True, exist_ok=True)
dist_binary = dist_path.joinpath(f'ethwizard-{version}.exe')
if dist_binary.is_file():
dist_binary.unlink()
from functools import partial
chunk_size = 1024 * 64
with open(dist_binary, 'wb') as dist_file:
with open(sfx_module, 'rb') as sfx_file:
for chunk in iter(partial(sfx_file.read, chunk_size), b''):
dist_file.write(chunk)
with open(sfx_config_path, 'rb') as config_file:
for chunk in iter(partial(config_file.read, chunk_size), b''):
dist_file.write(chunk)
with open(sfx_archive_path, 'rb') as archive_file:
for chunk in iter(partial(archive_file.read, chunk_size), b''):
dist_file.write(chunk)
if __name__ == "__main__":
setuptools.setup(
version=version,
cmdclass={
'bundle': Bundle,
'bundlewinexe': BundleWinExe,
'bundlewinzip': BundleWinZip
}
)