forked from napari/napari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbundle.py
222 lines (175 loc) · 6.5 KB
/
bundle.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
import configparser
import os
import re
import shutil
import subprocess
import sys
import time
import tomlkit
APP = 'napari'
# EXTRA_REQS will be added to the bundle, in addition to those specified in
# setup.cfg. To add additional packages to the bundle, or to override any of
# the packages listed here or in `setup.cfg, use the `--add` command line
# argument with a series of "pip install" style strings when running this file.
# For example, the following will ADD ome-zarr, and CHANGE the version of
# PySide2:
# python bundle.py --add 'PySide2==5.15.0' 'ome-zarr'
EXTRA_REQS = ["pip", "PySide2==5.14.2.2", "scikit-image", "zarr"]
WINDOWS = os.name == 'nt'
MACOS = sys.platform == 'darwin'
LINUX = sys.platform.startswith("linux")
HERE = os.path.abspath(os.path.dirname(__file__))
PYPROJECT_TOML = os.path.join(HERE, 'pyproject.toml')
SETUP_CFG = os.path.join(HERE, 'setup.cfg')
if WINDOWS:
BUILD_DIR = os.path.join(HERE, 'windows')
elif LINUX:
BUILD_DIR = os.path.join(HERE, 'linux')
elif MACOS:
BUILD_DIR = os.path.join(HERE, 'macOS')
APP_DIR = os.path.join(BUILD_DIR, APP, f'{APP}.app')
with open(PYPROJECT_TOML, 'r') as f:
original_toml = f.read()
with open(os.path.join(HERE, "napari", "_version.py")) as f:
match = re.search(r'version\s?=\s?\'([^\']+)', f.read())
if match:
VERSION = match.groups()[0].split('+')[0]
def patch_toml():
parser = configparser.ConfigParser()
parser.read(SETUP_CFG)
requirements = parser.get("options", "install_requires").splitlines()
requirements = [r.split('#')[0].strip() for r in requirements if r]
requirements += EXTRA_REQS
toml = tomlkit.parse(original_toml)
# parse command line arguments
if '--add' in sys.argv:
for item in sys.argv[sys.argv.index('--add') + 1 :]:
if item.startswith('-'):
break
_base = re.split('<|>|=', item, maxsplit=1)[0]
for r in requirements:
if r.startswith(_base):
requirements.remove(r)
break
if _base.lower().startswith('pyqt5'):
try:
i = next(x for x in requirements if x.startswith('PySide'))
requirements.remove(i)
except StopIteration:
pass
requirements.append(item)
toml['tool']['briefcase']['app'][APP]['requires'] = requirements
toml['tool']['briefcase']['version'] = VERSION
print("patching pyroject.toml to version: ", VERSION)
print(
"patching pyroject.toml requirements to : \n",
"\n".join(toml['tool']['briefcase']['app'][APP]['requires']),
)
with open(PYPROJECT_TOML, 'w') as f:
f.write(tomlkit.dumps(toml))
def patch_dmgbuild():
if not MACOS:
return
from dmgbuild import core
# will not be required after dmgbuild > v1.3.3
# see https://github.com/al45tair/dmgbuild/pull/18
with open(core.__file__, 'r') as f:
src = f.read()
if 'max(total_size / 1024' not in src:
return
with open(core.__file__, 'w') as f:
f.write(src.replace('max(total_size / 1024', 'max(total_size / 1000'))
print("patched dmgbuild.core")
def add_site_packages_to_path():
# on mac, make sure the site-packages folder exists even before the user
# has pip installed, so it is in sys.path on the first run
# (otherwise, newly installed plugins will not be detected until restart)
if MACOS:
pkgs_dir = os.path.join(
APP_DIR,
'Contents',
'Resources',
'Support',
'lib',
f'python{sys.version_info.major}.{sys.version_info.minor}',
'site-packages',
)
os.makedirs(pkgs_dir)
print("created site-packages at", pkgs_dir)
# on windows, briefcase uses a _pth file to determine the sys.path at
# runtime. https://docs.python.org/3/using/windows.html#finding-modules
# We update that file with the eventual location of pip site-packages
elif WINDOWS:
py = "".join(map(str, sys.version_info[:2]))
python_dir = os.path.join(BUILD_DIR, APP, 'src', 'python')
pth = os.path.join(python_dir, f'python{py}._pth')
with open(pth, "a") as f:
# Append 'hello' at the end of file
f.write(".\\\\Lib\\\\site-packages\n")
print("added bundled site-packages to", pth)
pkgs_dir = os.path.join(python_dir, 'Lib', 'site-packages')
os.makedirs(pkgs_dir)
print("created site-packages at", pkgs_dir)
with open(os.path.join(pkgs_dir, 'readme.txt'), 'w') as f:
f.write("this is where plugin packages will go")
def patch_wxs():
# must run after briefcase create
fname = os.path.join(BUILD_DIR, APP, f'{APP}.wxs')
if os.path.exists(fname):
with open(fname, 'r') as f:
source = f.read()
with open(fname, 'w') as f:
f.write(source.replace('pythonw.exe', 'python.exe'))
print("patched pythonw.exe -> python.exe")
def make_zip():
import glob
import zipfile
if WINDOWS:
ext, OS = '*.msi', 'Windows'
elif LINUX:
ext, OS = '*.AppImage', 'Linux'
elif MACOS:
ext, OS = '*.dmg', 'macOS'
artifact = glob.glob(os.path.join(BUILD_DIR, ext))[0]
dest = f'napari-{VERSION}-{OS}.zip'
with zipfile.ZipFile(dest, 'w', zipfile.ZIP_DEFLATED) as zf:
zf.write(artifact, arcname=os.path.basename(artifact))
print("created zipfile: ", dest)
return dest
def clean():
shutil.rmtree(BUILD_DIR, ignore_errors=True)
def bundle():
clean()
if MACOS:
patch_dmgbuild()
# smoke test, and build resources
subprocess.check_call([sys.executable, '-m', APP, '--info'])
patch_toml()
# create
cmd = ['briefcase', 'create'] + (['--no-docker'] if LINUX else [])
subprocess.check_call(cmd)
time.sleep(0.5)
add_site_packages_to_path()
if WINDOWS:
patch_wxs()
# build
cmd = ['briefcase', 'build'] + (['--no-docker'] if LINUX else [])
subprocess.check_call(cmd)
# package
cmd = ['briefcase', 'package']
cmd += ['--no-sign'] if MACOS else (['--no-docker'] if LINUX else [])
subprocess.check_call(cmd)
# compress
dest = make_zip()
clean()
with open(PYPROJECT_TOML, 'w') as f:
f.write(original_toml)
return dest
if __name__ == "__main__":
if '--clean' in sys.argv:
clean()
sys.exit()
if '--version' in sys.argv:
print(VERSION)
sys.exit()
print('created', bundle())