-
Notifications
You must be signed in to change notification settings - Fork 85
/
Deploy.py
367 lines (297 loc) · 10.1 KB
/
Deploy.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Copyright (C) 2024 Loren Eteval <loren.eteval@proton.me>
#
# This file is part of Furious.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations
from Furious.Utility import *
import os
import sys
import shutil
import logging
import argparse
import subprocess
logging.basicConfig(
format='[%(asctime)s] [%(name)s] [%(levelname)s] %(message)s',
level=logging.INFO,
)
logging.raiseExceptions = False
logger = logging.getLogger('Deploy')
# Exit status
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
DEPLOY_DIR_NAME = f'{APPLICATION_NAME}-Deploy'
if PLATFORM == 'Windows':
NUITKA_BUILD = (
f'python -m nuitka '
f'--standalone --plugin-enable=pyside6 '
f'--disable-console '
f'--assume-yes-for-downloads '
f'--include-package-data=Furious '
f'--windows-icon-from-ico=\"Icons/png/rocket-takeoff-window.png\" '
f'--force-stdout-spec=^%TEMP^%/_Furious_Enable_Stdout '
f'--force-stderr-spec=^%TEMP^%/_Furious_Enable_Stderr '
f'Furious '
f'--output-dir=\"{ROOT_DIR / DEPLOY_DIR_NAME}\"'
)
elif PLATFORM == 'Darwin':
NUITKA_BUILD = (
f'python -m nuitka '
f'--standalone --plugin-enable=pyside6 '
f'--disable-console '
f'--assume-yes-for-downloads '
f'--include-package-data=Furious '
f'--macos-create-app-bundle '
f'--macos-app-icon=\"Icons/png/rocket-takeoff-window.png\" '
f'--macos-app-name=\"Furious\" '
f'Furious-GUI.py '
f'--output-dir=\"{ROOT_DIR / DEPLOY_DIR_NAME}\"'
)
else:
# Deploy: Not implemented
NUITKA_BUILD = ''
if PLATFORM == 'Windows':
if PLATFORM_RELEASE.endswith('Server'):
# Windows server. Fixed to windows10
winVerCompatible = f'{PLATFORM.lower()}10'
else:
winVerCompatible = f'{PLATFORM.lower()}{PLATFORM_RELEASE}'
ARTIFACT_NAME = (
f'{APPLICATION_NAME}-{APPLICATION_VERSION}-'
f'{winVerCompatible}-{PLATFORM_MACHINE.lower()}'
)
WIN_UNZIPPED = f'{APPLICATION_NAME}-{APPLICATION_VERSION}-{winVerCompatible}'
elif PLATFORM == 'Darwin':
if versionToValue(PYSIDE6_VERSION) <= versionToValue('6.4.3'):
macVerCompatible = 'macOS-10.9'
else:
macVerCompatible = 'macOS-11.0'
ARTIFACT_NAME = (
f'{APPLICATION_NAME}-{APPLICATION_VERSION}-'
f'{macVerCompatible}-{PLATFORM_MACHINE.lower()}'
)
MAC_APP_DIR = ROOT_DIR / 'app'
MAC_DMG_FILENAME = f'{ARTIFACT_NAME}.dmg'
MAC_CREATE_DMG_CMD = (
f'create-dmg '
f'--volname \"Furious\" '
f'--volicon \"Icons/png/rocket-takeoff-window.png\" '
f'--window-pos 200 120 '
f'--window-size 600 300 '
f'--icon-size 100 '
f'--icon \"Furious-GUI.app\" 175 120 '
f'--hide-extension \"Furious-GUI.app\" '
f'--app-drop-link 425 120 '
f'\"{ROOT_DIR / MAC_DMG_FILENAME}\" '
f'\"{MAC_APP_DIR}\"'
)
else:
# Deploy: Not implemented
ARTIFACT_NAME = ''
def downloadXrayAssets(url, filename):
try:
import requests
except ImportError:
raise ModuleNotFoundError('missing requests module')
try:
# Make sure the save directory exists
if not os.path.exists(XRAY_ASSET_DIR):
os.makedirs(XRAY_ASSET_DIR)
# Full path where the file will be saved
filepath = os.path.join(XRAY_ASSET_DIR, filename)
# Send an HTTP GET request to the URL
response = requests.get(url)
response.raise_for_status() # Check if the request was successful
# Open the save_path in write-binary mode and write the content of the response
with open(filepath, 'wb') as file:
file.write(response.content)
except Exception as ex:
# Any non-exit exceptions
logger.error(
f'failed to download file from {url}. Status code: {response.status_code}'
)
return False
else:
logger.info(f'file downloaded successfully and saved to {filepath}')
return True
def cleanup():
try:
shutil.rmtree(ROOT_DIR / DEPLOY_DIR_NAME)
except Exception as ex:
# Any non-exit exceptions
logger.error(f'remove deployment dir failed: {ex}')
else:
logger.info(f'remove deployment dir success')
if PLATFORM == 'Windows':
# More cleanup on Windows
try:
# Remove artifact
os.remove(ROOT_DIR / f'{ARTIFACT_NAME}.zip')
except Exception as ex:
# Any non-exit exceptions
logger.error(f'remove artifact failed: {ex}')
else:
logger.info(f'remove artifact success')
try:
# Remove unzipped folder
shutil.rmtree(ROOT_DIR / WIN_UNZIPPED)
except Exception as ex:
# Any non-exit exceptions
logger.error(f'remove unzipped dir failed: {ex}')
else:
logger.info('remove unzipped dir success')
elif PLATFORM == 'Darwin':
# More cleanup on Darwin
try:
# Remove artifact
os.remove(ROOT_DIR / f'{ARTIFACT_NAME}.dmg')
except Exception as ex:
# Any non-exit exceptions
logger.error(f'remove artifact failed: {ex}')
else:
logger.info(f'remove artifact success')
try:
# Remove app folder
shutil.rmtree(MAC_APP_DIR)
except Exception as ex:
# Any non-exit exceptions
logger.error(f'remove app dir failed: {ex}')
else:
logger.info(f'remove app dir success')
else:
# Deploy: Not implemented
pass
def download():
# URLs of geosite and geoip assets
url_geosite = 'https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geosite.dat'
url_geoip = 'https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/geoip.dat'
# Xray assets names
filename_geosite = 'geosite.dat'
filename_geoip = 'geoip.dat'
return all(
[
downloadXrayAssets(url_geosite, filename_geosite),
downloadXrayAssets(url_geoip, filename_geoip),
]
)
def printStandardStream(stdout, stderr):
if isinstance(stdout, bytes):
decoded_stdout = stdout.decode('utf-8', 'replace')
else:
decoded_stdout = stdout
if isinstance(stderr, bytes):
decoded_stderr = stderr.decode('utf-8', 'replace')
else:
decoded_stderr = stderr
print(f'stdout:\n{decoded_stdout}stderr:\n{decoded_stderr}', flush=True)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-d',
'--download',
action='store_true',
help='Download latest asset files',
)
parser.add_argument(
'-c',
'--cleanup',
action='store_true',
help='Cleanup deployment files',
)
args = parser.parse_args()
if args.cleanup:
cleanup()
logger.info('cleanup done')
sys.exit(EXIT_SUCCESS)
if args.download:
success = EXIT_SUCCESS if download() else EXIT_FAILURE
sys.exit(success)
try:
import nuitka
except ImportError:
raise ModuleNotFoundError('please install nuitka to run this script')
logger.info('building')
try:
result = runExternalCommand(
NUITKA_BUILD,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
check=True,
)
except subprocess.CalledProcessError as err:
logger.error(f'build failed with returncode {err.returncode}')
printStandardStream(err.stdout, err.stderr)
sys.exit(EXIT_FAILURE)
else:
logger.info(f'build success')
printStandardStream(result.stdout, result.stderr)
if PLATFORM == 'Windows':
try:
shutil.rmtree(ROOT_DIR / DEPLOY_DIR_NAME / WIN_UNZIPPED)
except FileNotFoundError:
pass
except Exception:
# Any non-exit exceptions
raise
shutil.copytree(
ROOT_DIR / DEPLOY_DIR_NAME / f'{APPLICATION_NAME}.dist',
ROOT_DIR / DEPLOY_DIR_NAME / WIN_UNZIPPED,
)
shutil.make_archive(
ARTIFACT_NAME,
'zip',
ROOT_DIR / DEPLOY_DIR_NAME,
WIN_UNZIPPED,
logger=logger,
)
elif PLATFORM == 'Darwin':
try:
shutil.rmtree(MAC_APP_DIR)
except FileNotFoundError:
pass
except Exception:
# Any non-exit exceptions
raise
try:
os.mkdir(MAC_APP_DIR)
except Exception:
# Any non-exit exceptions
raise
shutil.copytree(
ROOT_DIR / DEPLOY_DIR_NAME / 'Furious-GUI.app',
MAC_APP_DIR / 'Furious-GUI.app',
)
logger.info('generating dmg')
try:
result = runExternalCommand(
MAC_CREATE_DMG_CMD,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
check=True,
)
except subprocess.CalledProcessError as err:
logger.error(f'generate dmg failed with returncode {err.returncode}')
printStandardStream(err.stdout, err.stderr)
sys.exit(EXIT_FAILURE)
else:
logger.info(f'generate dmg success: {MAC_DMG_FILENAME}')
printStandardStream(result.stdout, result.stderr)
else:
# Deploy: Not implemented
pass
sys.exit(EXIT_SUCCESS)
if __name__ == '__main__':
main()