forked from Nriver/trilium-translation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.make_release.py
382 lines (296 loc) · 10 KB
/
3.make_release.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import os
import re
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from settings import DEBUG, PATCH_FOLDER, LANG, TRANS_RELEASE_FOLDER, USE_PROXY, PROXIES, VERSION_INFO_OVERRIDE, \
force_version_info_full, VERSION_INFO_OVERRIDE_BETA, force_version_info_full_beta
# disable warning if we use proxy
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
COMPRESS_TOOL = '7z'
COMPRESS_LEVEL = 9
REPO_NAME = 'zadam/trilium'
# TRANS_RELEASE_FOLDER = TRANS_RELEASE_FOLDER
# PATCH_FOLDER = PATCH_FOLDER
# 是否从GitHub下载文件
# whether download files from GitHub
DO_DOWNLOAD = True
# DO_DOWNLOAD = False
# 是否删除临时文件
# whether delete template files
DO_DELETE = True
# DO_DELETE = False
def requests_get(url):
ret = None
try:
ret = requests.get(url, proxies=PROXIES, verify=not USE_PROXY)
except Exception as e:
print('If github is not available, you can set USE_PROXY to True and set PROXIES.')
print('Exception', e)
return ret
def get_latest_version():
"""get latest version info"""
print('get latest version info')
url = f'https://api.github.com/repos/{REPO_NAME}/releases/latest'
print(url)
res = requests_get(url)
version_info = {'name': res.json()['name']}
# zipball_url 就是源码
# version_info['zipball_url'] = res.json()['zipball_url']
patterns = {
'linux': r'trilium-linux-x64-[0-9\.]+.tar.xz',
'linux-server': r'trilium-linux-x64-server-[0-9\.]+.tar.xz',
'windows': r'trilium-windows-x64-[0-9\.]+.zip',
'mac': r'trilium-mac-x64-[0-9\.]+.zip',
}
releases = {}
for x in res.json()['assets']:
for package_type in patterns:
if re.match(patterns[package_type], x['name']):
releases[package_type] = {
'name': x['name'],
'url': x['browser_download_url']
}
version_info['releases'] = releases
return version_info
def download_file(url, file_name=None):
"""download file"""
print('download file')
if not file_name:
file_name = url.split('/')[-1]
print('downloading ...')
if DO_DOWNLOAD:
with requests.get(url, proxies=PROXIES, verify=False, stream=True) as r:
r.raise_for_status()
with open(file_name, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f'download complete, file saved as {file_name}')
return file_name
def download_releases(releases):
for package_type in releases:
if DEBUG:
if package_type != 'linux':
continue
release = releases[package_type]
download_file(release['url'], TRANS_RELEASE_FOLDER + release['name'])
def download_release(releases, package_type):
for x in releases:
if x == package_type:
release = releases[x]
download_file(release['url'], TRANS_RELEASE_FOLDER + release['name'])
return
def download_and_patch(releases, package_type):
download_release(releases, package_type)
file_name = releases[package_type]['name']
if package_type == 'linux':
patch_linux(file_name)
elif package_type == 'windows':
patch_windows(file_name)
elif package_type == 'linux-server':
patch_linux_server(file_name)
elif package_type == 'mac':
patch_mac(file_name)
def decompress_package(file_name):
print(f'decompress {file_name}')
if file_name.endswith('tar.xz'):
try:
os.system(f'xz -d {file_name}')
except:
pass
try:
os.system(f'tar -xf {file_name[:-3]}')
if DO_DELETE:
os.system(f'rm -f {file_name[:-3]}')
except:
pass
elif file_name.endswith('.zip'):
try:
os.system(f'unzip -o {file_name}')
if DO_DELETE:
os.system(f'rm -f {file_name}')
except:
pass
def patch_linux(file_name):
if not file_name.endswith('.tar.xz'):
print('linux客户端文件名有问题')
print('linux client wrong file name')
exit()
file_path = TRANS_RELEASE_FOLDER + file_name
print('file_path', file_path)
decompress_package(file_path)
asar_folder = TRANS_RELEASE_FOLDER + 'trilium-linux-x64/resources'
asar_path = asar_folder + '/app.asar'
print(asar_path)
# asar解包
# asar unpack
os.chdir(asar_folder)
os.system('asar extract app.asar ./app/')
# 打补丁
# apply patch
os.system(f'cp -rf {PATCH_FOLDER}* "{asar_folder}/app/"')
# asar封包
# asar pack
os.system('asar pack app/ app.asar')
if not DEBUG:
# 删除解包文件
# remove unpacked files
os.system(f'rm -rf {asar_folder}/app/')
# 打zip包
# make zip
new_name = f'trilium-{LANG}-linux-x64.zip'
print('new_name', new_name)
os.system(f'rm -f {new_name}')
patched_root_folder = 'trilium-linux-x64'
os.chdir(TRANS_RELEASE_FOLDER)
if COMPRESS_TOOL == '7z':
cmd = f'7z a {new_name} -r {patched_root_folder}'
else:
cmd = f'zip -{COMPRESS_LEVEL} -r {new_name} {patched_root_folder}'
print(cmd)
os.system(cmd)
if DO_DELETE:
os.system('rm -rf trilium-linux-x64')
return new_name
def patch_linux_server(file_name):
if not file_name.endswith('.tar.xz'):
print('linux server 文件名有问题')
print('linux server wrong file name')
exit()
file_path = TRANS_RELEASE_FOLDER + file_name
print('file_path', file_path)
decompress_package(file_path)
# 打补丁
# apply patch
cmd = f'cp -rf {PATCH_FOLDER}* {TRANS_RELEASE_FOLDER}/trilium-linux-x64-server/'
print('cmd', cmd)
os.system(cmd)
# 打zip包
# make zip package
new_name = 'trilium-cn-linux-x64-server.zip'
print('new_name', new_name)
os.system(f'rm -f {new_name}')
patched_root_folder = 'trilium-linux-x64-server'
os.chdir(TRANS_RELEASE_FOLDER)
if COMPRESS_TOOL == '7z':
cmd = f'7z a {new_name} -r {patched_root_folder}'
else:
cmd = f'zip -{COMPRESS_LEVEL} -r {new_name} {patched_root_folder}'
print(cmd)
os.system(cmd)
if DO_DELETE:
os.system('rm -rf trilium-linux-x64-server')
return new_name
def patch_windows(file_name):
if not file_name.endswith('.zip'):
print('windows 文件名有问题')
exit()
file_path = TRANS_RELEASE_FOLDER + file_name
print('file_path', file_path)
decompress_package(file_path)
asar_folder = TRANS_RELEASE_FOLDER + 'trilium-windows-x64/resources'
asar_path = asar_folder + '/app.asar'
print(asar_path)
# asar解包
os.chdir(asar_folder)
os.system('asar extract app.asar ./app/')
# 打补丁
# apply patch
os.system(f'cp -rf {PATCH_FOLDER}* {asar_folder}/app/')
# asar封包
# asar pack
os.system('asar pack app/ app.asar')
# 删除解包文件
# remove unpacked files
os.system(f'rm -rf {asar_folder}/app/')
# 打zip包
# make zip package
new_name = f'trilium-{LANG}-windows-x64.zip'
print('new_name', new_name)
os.system(f'rm -f {new_name}')
patched_root_folder = 'trilium-windows-x64'
os.chdir(TRANS_RELEASE_FOLDER)
if COMPRESS_TOOL == '7z':
cmd = f'7z a {new_name} -r {patched_root_folder}'
else:
cmd = f'zip -{COMPRESS_LEVEL} -r {new_name} {patched_root_folder}'
print(cmd)
os.system(cmd)
if DO_DELETE:
os.system('rm -rf trilium-windows-x64')
return new_name
def patch_mac(file_name):
if not file_name.endswith('.zip'):
print('windows 文件名有问题')
exit()
file_path = TRANS_RELEASE_FOLDER + file_name
print('file_path', file_path)
decompress_package(file_path)
asar_folder = TRANS_RELEASE_FOLDER + 'trilium-mac-x64/Trilium Notes.app/Contents/Resources'
asar_path = asar_folder + '/app.asar'
print(asar_path)
# asar解包
# asar unpack
os.chdir(asar_folder)
os.system('asar extract app.asar ./app/')
# 打补丁
# apply patch
os.system(f'cp -rf {PATCH_FOLDER}* "{asar_folder}/app/"')
# asar封包
# asar pack
os.system('asar pack app/ app.asar')
# 删除解包文件
# remove unpacked files
cmd = f'rm -rf "{asar_folder}/app/"'
print('cmd', cmd)
os.system(cmd)
# 打zip包
# make zip package
new_name = f'trilium-{LANG}-mac-x64.zip'
print('new_name', new_name)
os.system(f'rm -f {new_name}')
patched_root_folder = 'trilium-mac-x64'
os.chdir(TRANS_RELEASE_FOLDER)
if COMPRESS_TOOL == '7z':
cmd = f'7z a {new_name} -r {patched_root_folder}'
else:
cmd = f'zip -{COMPRESS_LEVEL} -r {new_name} {patched_root_folder}'
print('压缩命令', cmd)
os.system(cmd)
if DO_DELETE:
os.system('rm -rf trilium-mac-x64')
return new_name
if __name__ == '__main__':
print(f'DEBUG is {DEBUG}')
a = input(f'Delete folder {TRANS_RELEASE_FOLDER}, continue?(y)')
if a not in ['y', ]:
exit()
os.system(f'rm -rf {TRANS_RELEASE_FOLDER}')
os.makedirs(f'{TRANS_RELEASE_FOLDER}')
os.chdir(TRANS_RELEASE_FOLDER)
# 获取更新
# get update info
if VERSION_INFO_OVERRIDE:
if VERSION_INFO_OVERRIDE_BETA:
version_info = force_version_info_full_beta
else:
version_info = force_version_info_full
else:
version_info = get_latest_version()
print('version_info', version_info)
# 下载
# download
releases = version_info['releases']
# download_releases(releases)
# 打补丁
# patch
# linux
download_and_patch(releases, 'linux')
if not DEBUG:
# linux-server
download_and_patch(releases, 'linux-server')
# windows
download_and_patch(releases, 'windows')
# mac
download_and_patch(releases, 'mac')
os.system(f'xdg-open {TRANS_RELEASE_FOLDER}')
print('finished')