forked from EDCD/EDMarketConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
executable file
·318 lines (282 loc) · 10.7 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script to build to .exe and .msi package.
.exe build is via py2exe on win32.
.msi packaging utilises Windows SDK.
"""
import codecs
import os
import platform
import re
import shutil
import sys
from distutils.core import setup
from os.path import exists, isdir, join
from tempfile import gettempdir
from typing import Any, Generator, Set
from config import (
appcmdname, applongname, appname, appversion, appversion_nobuild, copyright, git_shorthash_from_head, update_feed,
update_interval
)
from constants import GITVERSION_FILE
if sys.version_info[0:2] != (3, 9):
raise AssertionError(f'Unexpected python version {sys.version}')
###########################################################################
# Retrieve current git short hash and store in file GITVERSION_FILE
git_shorthash = git_shorthash_from_head()
if git_shorthash is None:
exit(-1)
with open(GITVERSION_FILE, 'w+', encoding='utf-8') as gvf:
gvf.write(git_shorthash)
print(f'Git short hash: {git_shorthash}')
###########################################################################
if sys.platform == 'win32':
assert platform.architecture()[0] == '32bit', 'Assumes a Python built for 32bit'
import py2exe # noqa: F401 # Yes, this *is* used
dist_dir = 'dist.win32'
elif sys.platform == 'darwin':
dist_dir = 'dist.macosx'
else:
assert False, f'Unsupported platform {sys.platform}'
# Split version, as py2exe wants the 'base' for version
semver = appversion()
appversion_str = str(semver)
base_appversion = str(semver.truncate('patch'))
if dist_dir and len(dist_dir) > 1 and isdir(dist_dir):
shutil.rmtree(dist_dir)
# "Developer ID Application" name for signing
macdeveloperid = None
# Windows paths
WIXPATH = r'C:\Program Files (x86)\WiX Toolset v3.11\bin'
SDKPATH = r'C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x86'
# OSX paths
SPARKLE = '/Library/Frameworks/Sparkle.framework'
if sys.platform == 'darwin':
# Patch py2app recipe enumerator to skip the sip recipe since it's too
# enthusiastic - we'll list additional Qt modules explicitly
import py2app.build_app
from py2app import recipes
# NB: 'Any' is because I don't have MacOS docs
def iter_recipes(module=recipes) -> Generator[str, Any]:
"""Enumerate recipes via alternate method."""
for name in dir(module):
if name.startswith('_') or name == 'sip':
continue
check = getattr(getattr(module, name), 'check', None)
if check is not None:
yield (name, check)
py2app.build_app.iterRecipes = iter_recipes
APP = 'EDMarketConnector.py'
APPCMD = 'EDMC.py'
PLUGINS = [
'plugins/coriolis.py',
'plugins/eddb.py',
'plugins/eddn.py',
'plugins/edsm.py',
'plugins/edsy.py',
'plugins/inara.py',
]
if sys.platform == 'darwin':
def get_cfbundle_localizations() -> Set:
"""
Build a set of the localisation files.
See https://github.com/sparkle-project/Sparkle/issues/238
"""
return sorted(
(
[x[:-len('.lproj')] for x in os.listdir(join(SPARKLE, 'Resources')) if x.endswith('.lproj')]
) | (
[x[:-len('.strings')] for x in os.listdir('L10n') if x.endswith('.strings')]
)
)
OPTIONS = {
'py2app': {
'dist_dir': dist_dir,
'optimize': 2,
'packages': [
'requests',
'sqlite3', # Included for plugins
],
'includes': [
'shutil', # Included for plugins
'zipfile', # Included for plugins
],
'frameworks': [
'Sparkle.framework'
],
'excludes': [
'distutils',
'_markerlib',
'PIL',
'pkg_resources',
'simplejson',
'unittest'
],
'iconfile': f'{appname}.icns',
'include_plugins': [
('plugins', x) for x in PLUGINS
],
'resources': [
'commodity.csv',
'rare_commodity.csv',
'snd_good.wav',
'snd_bad.wav',
'modules.p',
'ships.p',
],
'site_packages': False,
'plist': {
'CFBundleName': applongname,
'CFBundleIdentifier': f'uk.org.marginal.{appname.lower()}',
'CFBundleLocalizations': get_cfbundle_localizations(),
'CFBundleShortVersionString': appversion_str,
'CFBundleVersion': appversion_str,
'CFBundleURLTypes': [
{
'CFBundleTypeRole': 'Viewer',
'CFBundleURLName': f'uk.org.marginal.{appname.lower()}.URLScheme',
'CFBundleURLSchemes': [
'edmc'
],
}
],
'LSMinimumSystemVersion': '10.10',
'NSAppleScriptEnabled': True,
'NSHumanReadableCopyright': copyright,
'SUEnableAutomaticChecks': True,
'SUShowReleaseNotes': True,
'SUAllowsAutomaticUpdates': False,
'SUFeedURL': update_feed,
'SUScheduledCheckInterval': update_interval,
},
'graph': True, # output dependency graph in dist
}
}
DATA_FILES = []
elif sys.platform == 'win32':
OPTIONS = {
'py2exe': {
'dist_dir': dist_dir,
'optimize': 2,
'packages': [
'sqlite3', # Included for plugins
],
'includes': [
'dataclasses',
'shutil', # Included for plugins
'timeout_session',
'zipfile', # Included for plugins
],
'excludes': [
'distutils',
'_markerlib',
'optparse',
'PIL',
'simplejson',
'unittest'
],
}
}
DATA_FILES = [
('', [
'.gitversion', # Contains git short hash
'WinSparkle.dll',
'WinSparkle.pdb', # For debugging - don't include in package
'EUROCAPS.TTF',
'Changelog.md',
'commodity.csv',
'rare_commodity.csv',
'snd_good.wav',
'snd_bad.wav',
'modules.p',
'ships.p',
f'{appname}.VisualElementsManifest.xml',
f'{appname}.ico',
'EDMarketConnector - TRACE.bat',
'EDMarketConnector - localserver-auth.bat',
'EDMarketConnector - reset-ui.bat',
]),
('L10n', [join('L10n', x) for x in os.listdir('L10n') if x.endswith('.strings')]),
('plugins', PLUGINS),
]
setup(
name=applongname,
version=appversion_str,
windows=[
{
'dest_base': appname,
'script': APP,
'icon_resources': [(0, f'{appname}.ico')],
'company_name': 'EDCD', # Used by WinSparkle
'product_name': appname, # Used by WinSparkle
'version': base_appversion,
'product_version': appversion_str,
'copyright': copyright,
'other_resources': [(24, 1, open(f'{appname}.manifest').read())],
}
],
console=[
{
'dest_base': appcmdname,
'script': APPCMD,
'company_name': 'EDCD',
'product_name': appname,
'version': base_appversion,
'product_version': appversion_str,
'copyright': copyright,
'other_resources': [(24, 1, open(f'{appcmdname}.manifest').read())],
}
],
data_files=DATA_FILES,
options=OPTIONS,
)
package_filename = None
if sys.platform == 'darwin':
if isdir(f'{dist_dir}/{applongname}.app'): # from CFBundleName
os.rename(f'{dist_dir}/{applongname}.app', f'{dist_dir}/{appname}.app')
# Generate OSX-style localization files
for x in os.listdir('L10n'):
if x.endswith('.strings'):
lang = x[:-len('.strings')]
path = f'{dist_dir}/{appname}.app/Contents/Resources/{lang}.lproj'
os.mkdir(path)
codecs.open(
f'{path}/Localizable.strings',
'w',
'utf-16'
).write(codecs.open(f'L10n/{x}', 'r', 'utf-8').read())
if macdeveloperid:
os.system(f'codesign --deep -v -s "Developer ID Application: {macdeveloperid}" {dist_dir}/{appname}.app')
# Make zip for distribution, preserving signature
package_filename = f'{appname}_mac_{appversion_nobuild()}.zip'
os.system(f'cd {dist_dir}; ditto -ck --keepParent --sequesterRsrc {appname}.app ../{package_filename}; cd ..')
elif sys.platform == 'win32':
os.system(rf'"{WIXPATH}\candle.exe" -out {dist_dir}\ {appname}.wxs')
if not exists(f'{dist_dir}/{appname}.wixobj'):
raise AssertionError(f'No {dist_dir}/{appname}.wixobj: candle.exe failed?')
package_filename = f'{appname}_win_{appversion_nobuild()}.msi'
os.system(rf'"{WIXPATH}\light.exe" -sacl -spdb -sw1076 {dist_dir}\{appname}.wixobj -out {package_filename}')
if not exists(package_filename):
raise AssertionError(f'light.exe failed, no {package_filename}')
# Seriously, this is how you make Windows Installer use the user's display language for its dialogs. What a crock.
# http://www.geektieguy.com/2010/03/13/create-a-multi-lingual-multi-language-msi-using-wix-and-custom-build-scripts
lcids = [
int(x) for x in re.search( # type: ignore
r'Languages\s*=\s*"(.+?)"',
open(f'{appname}.wxs').read()
).group(1).split(',')
]
assert lcids[0] == 1033, f'Default language is {lcids[0]}, should be 1033 (en_US)'
shutil.copyfile(package_filename, join(gettempdir(), f'{appname}_1033.msi'))
for lcid in lcids[1:]:
shutil.copyfile(
join(gettempdir(), f'{appname}_1033.msi'),
join(gettempdir(), f'{appname}_{lcid}.msi')
)
# Don't care about codepage because the displayed strings come from msiexec not our msi
os.system(rf'cscript /nologo "{SDKPATH}\WiLangId.vbs" {gettempdir()}\{appname}_{lcid}.msi Product {lcid}')
os.system(rf'"{SDKPATH}\MsiTran.Exe" -g {gettempdir()}\{appname}_1033.msi {gettempdir()}\{appname}_{lcid}.msi {gettempdir()}\{lcid}.mst') # noqa: E501 # Not going to get shorter
os.system(rf'cscript /nologo "{SDKPATH}\WiSubStg.vbs" {package_filename} {gettempdir()}\{lcid}.mst {lcid}')
else:
raise AssertionError('Unsupported platform')