-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_scite_setup.py
449 lines (323 loc) · 12.4 KB
/
make_scite_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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!python3
'''Build a new wscite or sc1 setup.'''
import glob, os, shutil, subprocess, urllib.request, zipfile, zlib
import common
def build_global_properties(bitness):
'''Build content to use for the SciTEGlobal override file.'''
global_content = (
'# SciTE override file for global initialization.\n\n\n'
'# Behaviour\n'
'are.you.sure.on.reload=1\n'
'clear.before.execute=1\n'
'load.on.activate=1\n\n'
'# Define the Lexer menu\n'
'keyIndent=\n'
'keyText=\n'
'keyMake=\n'
'keyHTML=\n'
'keyXML=\n\n'
'# Remove these languages from the menu\n'
'*language.errorlist=\n'
'*language.java=\n'
'*language.makefile=\n'
'*language.perl=\n'
'*language.vb=\n'
'*language.yaml=\n\n'
'# Scripting\n'
'ext.lua.startup.script=$(SciteDefaultHome)' + os.sep + 'SciTEStartup.lua\n'
'if PLAT_WIN\n'
'\tcreate.hidden.console=1\n')
if bitness == 'x64':
global_content += (
'\n# 48 DebugPrintSelection\n'
'#command.name.48.*=DebugPrintSelection\n'
'command.48.*=DebugPrintSelection\n'
'command.subsystem.48.*=3\n'
'command.save.before.48.*=2\n\n'
'# 49 GlobalTools\n'
'command.name.49.*=GlobalTools\n'
'command.49.*=GlobalTools\n'
'command.subsystem.49.*=3\n'
'command.save.before.49.*=2\n'
'command.shortcut.49.*=F12\n')
if selection == 'sc1':
global_content += '\nimport *\n'
return global_content
# Read all override files and include for wscite to keep imports small.
for item in glob.iglob(os.path.join(base, 'overrides', '*.properties')):
with open(item) as r:
content = r.read().strip()
if content:
name = os.path.basename(item)[:-11]
length = len(name) + 6
global_content += '\n' + ('#' * length) + '\n' \
'# ' + name + ' #\n' \
+ ('#' * length) + '\n\n' \
+ content + '\n'
global_content = global_content.strip('\n') + '\n'
return global_content
def compile_au3(file, bitness):
'''Compile au3 file to an executable.'''
def _download_range(url, offset, length):
'''Download a range of bytes from a url.'''
with urllib.request.urlopen(urllib.request.Request(
url, headers={'Range': 'bytes={}-'.format(offset)})) as r:
content = r.read(length)
return content
# Check au3 file exist.
if not os.path.isfile(file):
return
# Check Aut2exe.exe exist.
for item in ('ProgramFiles', 'ProgramFiles(x86)'):
if item in os.environ:
compiler = os.path.join(os.environ[item], 'AutoIt3',
'Aut2Exe', 'Aut2exe.exe')
if os.path.isfile(compiler):
break
else:
bin_dir = common.settings['bin']
compiler = os.path.join(bin_dir, 'Aut2exe.exe')
# Download Aut2exe.exe if not exist.
if not os.path.isfile(compiler):
reply = input('Download Aut2Exe.exe to compile "{}"? [n|y]: '.format(file))
if reply.lower() != 'y':
return
if not os.path.isdir(bin_dir):
os.makedirs(bin_dir)
url = ('https://www.autoitscript.com/autoit3/'
'files/archive/autoit/autoit-v3.3.14.5.zip')
compressed = _download_range(url, 256320, 1117562)
uncompressed = zlib.decompress(compressed, -15)
with open(compiler, 'wb') as w:
w.write(uncompressed)
# Compile au3 file to executable.
bit = '/x64' if bitness == 'x64' else '/x86'
with subprocess.Popen([compiler, '/in', file, '/nopack', bit]) as p:
p.wait()
def copy_base_files(bitness):
'''Copy base files to a setup directory.'''
rewrite = []
for root, dirs, files in os.walk(base):
relroot = os.path.relpath(root, base)
# Make dirs.
makedirs = True
if not os.path.isdir(relroot):
if selection == 'sc1' and relroot.lower() == 'overrides':
makedirs = False
if makedirs and not os.path.isdir(relroot):
os.makedirs(relroot)
# Copy files.
for item in files:
if bitness != 'x64':
if item in ('about_global_tools.hta',
'rluawfx.lua',
'SciTEStartup.lua'):
rewrite.append('SciTEStartup.lua')
continue
if selection == 'sc1' and relroot.lower() == 'overrides':
relroot = '.'
src = os.path.join(root, item)
dst = os.path.join(relroot, item)
shutil.copyfile(src, dst)
print(' ', os.path.basename(dst))
# Write global override properties.
content = build_global_properties(bitness)
if selection == 'sc1':
file = 'SciTEGlobal.properties'
else:
file = '~overrides.properties'
with open(file, 'w') as w:
w.write(content)
# Write startup script with the require calls without rluawfx.
if 'SciTEStartup.lua' in rewrite:
with open(os.path.join(base, 'SciTEStartup.lua')) as r, \
open('SciTEStartup.lua', 'w') as w:
for line in r:
if line.startswith("require('rluawfx')"):
break
w.write(line)
def copy_output_files(bitness):
'''Copy files from output directory to a setup directory.'''
# Copy api files.
items = glob.glob(os.path.join(output, '*.api'))
if items:
if not os.path.isdir('api'):
os.mkdir('api')
for item in items:
print(' ', os.path.basename(item))
shutil.copyfile(item, os.path.join('api', os.path.basename(item)))
# Copy files and remove some files if not x64.
items = [(output, 'scite.chm'),
(output, 'lua/iface.hta'),
(output, 'lua/lua.chm'),
(output, 'lua/lua.json'),
(output, 'properties/modules.json')]
if bitness == 'x64':
items.extend([(download, 'rluawfx-en.dll'),
(output, 'lua/rluawfx_functions.hta')])
else:
for item in ('rluawfx-en.dll', 'lua/rluawfx_functions.hta'):
if os.path.isfile(item):
os.remove(item)
for item in items:
path, name = os.path.split(item[1])
if path and not os.path.isdir(path):
os.makedirs(path)
infile = os.path.join(os.path.join(item[0], name))
outfile = os.path.normpath(item[1])
if os.path.isfile(infile):
print(' ', name)
shutil.copyfile(infile, outfile)
def get_exe_bitness(file):
'''Get executable bitness as x86 or x64.'''
with open(file, 'rb') as r:
pos = r.read(1024).find(b'PE')
r.seek(pos + 4, 0)
char = r.read(1)
if char == b'd':
return 'x64'
elif char == b'L':
return 'x86'
def get_scite_to_copy(glob_pattern_list):
'''List SciTE choices and get the one selected from input.'''
scite = []
for pattern in glob_pattern_list:
scite.extend(glob.glob(pattern))
if not scite:
print('No files match the pattern')
return
if len(scite) > 1:
print('Select file to copy or extract the SciTE executable:')
print(' 0 Quit')
for index, item in enumerate(scite, 1):
print(' {} {}'.format(index, os.path.basename(item)))
reply = input('Enter a number: ').strip()
if reply in ('', '0'):
return
try:
index = int(reply) - 1
except ValueError as err_msg:
print(err_msg)
return
else:
index = 0
return scite[index]
def make_sc1(output):
'''Main function to make a sc1 setup.'''
# Prepare sc1 dir.
if not os.path.isdir(selection):
os.mkdir(selection)
os.chdir(selection)
# Get sc1 file.
scite_exe = get_scite_to_copy([os.path.join(output, 'sc*.exe')])
if not scite_exe:
return
# Remove old SciTE executables.
for item in glob.iglob('sc*.exe'):
os.remove(item)
# Copy SciTE executable.
shutil.copyfile(scite_exe, os.path.basename(scite_exe))
bitness = get_exe_bitness(scite_exe)
# Copy, extract... various files.
print('copy:')
copy_base_files(bitness)
copy_output_files(bitness)
# Make a relative user directory.
if not os.path.isdir('user'):
os.mkdir('user')
# Compile SciTE Launcher.
if not os.path.isfile('SciTELauncher.exe'):
if os.path.isfile('SciTELauncher.au3'):
compile_au3('SciTELauncher.au3', bitness)
os.remove('SciTELauncher.au3')
os.chdir(initial_dir)
def make_wscite(output):
'''Main function to make a wscite setup.'''
# Check modified external properties exist to continue.
output = os.path.join(output, 'external_properties')
if not os.path.exists(output):
exit('No external_properties dir found')
# Prepare wscite dir.
if not os.path.isdir(selection):
os.mkdir(selection)
# Get wscite zipfiles.
scite_exe = get_scite_to_copy([os.path.join(download, 'wscite*.zip'),
os.path.join('scite', 'bin', 'SciTE*.exe')])
if not scite_exe:
return
# Remove old SciTE executables.
for item in glob.iglob(os.path.join(selection, 'sc*.exe')):
os.remove(item)
# Unzip files from wscite zipfile.
if scite_exe.endswith('.zip'):
print('unzip:')
with zipfile.ZipFile(scite_exe) as r:
members = ('wscite/SciTE32.exe',
'wscite/SciTE.exe',
'wscite/Lexilla.dll',
'wscite/Scintilla.dll')
for item in members:
if item in r.namelist():
print(' ', os.path.basename(item))
r.extract(item)
if item.endswith('.exe'):
bitness = get_exe_bitness(item)
print('copy:')
else:
print('copy:')
scite_files = (os.path.join('scite', 'bin', 'lexilla.dll'),
os.path.join('scite', 'bin', 'Scintilla.dll'),
scite_exe)
for item in scite_files:
if os.path.isfile(item):
print(' ', os.path.basename(item))
shutil.copyfile(item, os.path.join('wscite',
os.path.basename(item)))
bitness = get_exe_bitness(scite_exe)
# Change to root of the extract dir.
os.chdir(selection)
# Copy, extract... various files.
copy_base_files(bitness)
copy_output_files(bitness)
# Make a relative user directory.
if not os.path.isdir('user'):
os.mkdir('user')
# Copy properties files.
items = glob.glob(os.path.join(output, '*.properties'))
if len(items) > 10:
print(' {}\n [...]\n {}'.format(os.path.basename(items[0]),
os.path.basename(items[-1])))
print_items = False
else:
print_items = True
for item in glob.iglob(os.path.join(output, '*.properties')):
if print_items:
print(' ', os.path.basename(item))
shutil.copyfile(item, os.path.basename(item))
# Compile SciTE Launcher.
if not os.path.isfile('SciTELauncher.exe'):
if os.path.isfile('SciTELauncher.au3'):
compile_au3('SciTELauncher.au3', bitness)
os.remove('SciTELauncher.au3')
os.chdir(initial_dir)
if __name__ == '__main__':
initial_dir = os.getcwd()
base = common.settings['base']
download = common.settings['download']
output = common.settings['output']
for item in ('sc1', 'wscite'):
reply = input('Make {} setup? [n|y]: '.format(item))
if reply.lower() == 'y':
# Ask to remove current setup.
if os.path.isdir(item):
reply = input(('Delete {0} dir before '
'make {0} setup? [n|y]: ').format(item))
if reply.lower() == 'y':
shutil.rmtree(item)
# Make the setup.
selection = item
if item == 'sc1':
make_sc1(output)
else:
make_wscite(output)
print('done')