-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxpack.py
723 lines (630 loc) · 21 KB
/
xpack.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
#!/usr/bin/env python3
"""
Token (PYTHONHOME/lib/pythonX.Y/token.py):
0 : ENDMARKER
1 : NAME
2 : NUMBER
3 : STRING
4 : NEWLINE
5 : INDENT
6 : DEDENT
54 : OP
61 : COMMENT
62 : NL (pure new line)
Logical:
define: VALUE = NAME | NUMBER | STRING
code: a = b if c else d
parse: NAME OP VALUE OP NAME OP VALUE
OP : = += *= [ { ( or any operator like "," or "." separator
NAME : None/True/False or any valid name
"""
import os
import sys
import io
import re
import argparse
import tokenize
import py_compile
FEATURES = [
'version 0.1.0 : start',
'version 0.2.0 : add local modules filter for `builtins`',
'version 0.3.0 : add option `--no-precompile` for `pyc` support',
'version 0.4.0 : change option `--no-precompile` to `precompile`',
'version 0.5.0 : add parser for non-py file',
'version 0.5.1 : avoid duplicate file',
'version 0.6.0 : add `pyminifier`',
'version 0.7.0 : add `--mini-files`',
'version 0.8.0 : add `--show-source-stdout`',
'version 0.9.0 : make "minification" be default',
'version 0.10.0 : fully remove blank lines',
'version 0.11.0 : fix errors in continuous OP',
'version 0.12.0 : rewrite `reduce_operators`',
]
__version__ = FEATURES[-1].split()[1]
__author__ = 'Xiang Zhong'
builtins = [
'abs', 'all', 'any', 'bin', 'bool', 'bytearray', 'bytes', 'callable',
'chr', 'classmethod', 'compile', 'complex', 'delattr', 'dict', 'dir',
'divmod', 'enumerate', 'eval', 'filter', 'float', 'format', 'frozenset',
'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id', 'input', 'int',
'isinstance', 'issubclass', 'iter', 'len', 'list', 'locals', 'map',
'max', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print',
'property', 'quit', 'range', 'raw_input', 'repr', 'reversed', 'round',
'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum',
'super', 'tuple', 'type', 'vars', 'zip',
'os', 'sys', 'argparse', 'import',
'is', 'None', 'if', 'else', 'elif', 'and', 'or',
]
def xuntokenize(tokens):
"""tokens: [[tokentype, string, (srow, scol), (erow, ecol, line)], ...]"""
out = ""
last_lineno = -1
last_col = 0
for tok in tokens:
token_string = tok[1]
start_line, start_col = tok[2]
end_line, end_col = tok[3]
# The following two conditionals preserve indentation:
if start_line > last_lineno:
last_col = 0
if start_col > last_col and token_string != '\n':
out += (" " * (start_col - last_col))
out += token_string
last_col = end_col
last_lineno = end_line
return out
def xtokenize(source):
"""Tokenizes *source* and returns the tokens as a list of lists"""
if isinstance(source,str):
if os.path.isfile(source):
with open(source,'rt') as f:
source = f.read()
else:
print('Fatal: tokenizer: only file or string input supported')
return []
sio = io.StringIO(source)
return [list(a) for a in tokenize.generate_tokens(sio.readline)]
def remove_blank_lines_and_spaces(source):
bo = False
if isinstance(source,(list,tuple)):
source = xuntokenize(source)
bo = True
result = ''
for line in source.split('\n'):
l = line.rstrip()
if len(l):
result += l + '\n'
if bo:
return xtokenize(result)
return result
def remove_blank_lines_and_spaces2(source):
"""to use, comments should be removed firstly"""
bo = False
if isinstance(source,(list,tuple)):
source = xuntokenize(source)
bo = True
result = ''
for line in source.split('\n'):
result += line.rstrip() + '\n' # only remove right whitespace
tokens = xtokenize(result)
for tok in tokens:
if tok[0] == tokenize.NL:
tok[1] = ''
tok[3] = (tok[2][0], tok[2][1])
if bo:
return tokens
return xuntokenize(tokens)
def remove_comments(tokens):
for tok in tokens:
if tok[0] == tokenize.COMMENT:
tok[1] = ''
tok[3] = (tok[2][0], tok[2][1]) # tuple
def remove_docstrings(tokens):
"""must be docstring, for token type:
> token.STRING: 3
> token.NEWLINE: 4
> token.INDENT: 5
> token.DEDENT: 6
> token.EXCLAMATION: 54
> token.FSTRING_START: 61 # pure new line, changed from "NL"
> token.NT_OFFSET: 256
1) (filebeginning), {3}
2) 61 -> {3}
3) 6 -> {3}
4) 54 -> 4 -> [61] -> 5 -> {3} # `61` is optional, can be multiple
"""
if not tokens: return tokens
sall = ''.join(['{:3}'.format(i[0]) for i in tokens])
sall = sall.replace(' ','@')
if sall[:3] == '@@3':
tokens[0][1] = ''
tokens[0][3] = (tokens[0][3][0], tokens[0][2][1]) # tuple
# search pattern: @@4@@3, @61@@3, @@6@@3, @54@@4{@61}*@@5@@3
p = re.compile(r'@@4@@3|@61@@3|@@6@@3|@54@@4(@61)*@@5@@3')
for i in p.finditer(sall):
k = i.span()[1] // 3 - 1 # starts from 1
tokens[k][1] = ''
tokens[k][3] = (tokens[k][2][0], tokens[k][2][1]) # tuple
def fix_empty_methods(tokens):
"""
empty method will become:
def func(): pass
class cls(): pass
"""
bo = False
if isinstance(tokens,(list,tuple)):
tokens = xuntokenize(tokens)
bo = True
lines = tokens.split('\n')
if not lines[-1]:
lines.pop(len(lines)-1)
if not lines: return ''
for k in range(len(lines)-1):
if not lines[k]: continue # inline docstring may have empty line
if lines[k][-1] == ':':
p = lines[k].lstrip() # may use `\t` or spaces
if p[:3] == 'def' or p[:5] == 'class':
offset = len(lines[k]) - len(p)
u = lines[k+1].lstrip()
offnew = len(lines[k+1]) - len(u)
if offset == offnew and (u[:3] == 'def' or u[:5] == 'class'):
lines[k] += ' pass'
p = lines[-1].lstrip()
if (p[:3] == 'def' or p[:5] == 'class') and p[-1] == ':':
lines[-1] += ' pass'
result = '\n'.join(lines)
if bo:
return xtokenize(result)
return result
def reduce_operators(source):
"""Remove spaces between operators in *source* and returns the result
Example codes:
def foo(a, b, c):
e = 'test: %s' % a
if True:
[1]
[2] # continuous OP
a = [1, 2, 3]
for i in (1,): print(i) # trailing comma
for i in [2, 3, ]: print(i)
v = 4
g = (f"{v} " "k: 1" "t=3" \
"b=4"
) # new line will be kept
bo = True
num = 1 if bo else 0
Results:
def foo(a,b,c):
e= ('test: %s') %a
if True:
[1]
[2]# continuous OP
a=[1,2,3]
for i in(1,):print(i)# trailing comma
for i in[2,3]:print(i)
v=4
g=( (f"{v} " "k: 1" "t=3" "b=4")
)# new line will be kept
bo=True
num=1 if bo else 0
"""
prev_tok = [-99, '', (0,0), (0,0), '']
out = ''
newlines = (tokenize.NL, tokenize.NEWLINE)
operator = (tokenize.NAME, tokenize.NUMBER, tokenize.STRING)
scnt = 0
new = ''
for tok in tokenize.generate_tokens(io.StringIO(source).readline):
beg_row, beg_col = tok[2]
end_row, end_col = tok[3]
if prev_tok[0] in newlines:
if beg_col > 0: # for continuous OP
out += ' ' * beg_col
else:
if tok[0] in operator and prev_tok[0] in operator:
if end_col > prev_tok[3][1]:
out += ' ' # reduced happens at here!
if tok[0] == tokenize.OP:
if tok[1] in ('}', ']'): # for trailing comma
if prev_tok[1] == ',':
out = out[:-1]
if scnt > 0: # join string
if scnt > 1:
out += ' (' + new + ') '
else:
out += new
new = ''
scnt = 0
out += tok[1]
elif tok[0] == tokenize.STRING:
scnt += 1
if prev_tok[0] == tokenize.STRING:
new += ' ' + tok[1]
else:
new += tok[1]
else:
if scnt > 0: # join string
if scnt > 1:
out += ' (' + new + ') '
else:
out += new
new = ''
scnt = 0
out += tok[1]
prev_tok = tok
return out
def join_multiline_pairs(source, pair="()"):
"""
Idea is to remove `\n` in every defined pair `OP`
Finds and removes newlines in multiline matching pairs of characters in
*source*.
By default it joins parens () but it will join any two characters given via
the *pair* variable.
.. note::
Doesn't remove extraneous whitespace that ends up between the pair.
Use `reduce_operators()` for that.
Example::
test = (
"This is inside a multi-line pair of parentheses"
)
Will become::
test = ( "This is inside a multi-line pair of parentheses" )
"""
opener = pair[0]
closer = pair[1]
out_tokens = []
open_count = 0
for tok in tokenize.generate_tokens(io.StringIO(source).readline):
token_type = tok[0]
token_string = tok[1]
if token_type == tokenize.OP and token_string in pair:
if token_string == opener:
open_count += 1
elif token_string == closer:
open_count -= 1
out_tokens.append(tok)
elif token_type in (tokenize.NL, tokenize.NEWLINE):
if open_count == 0:
out_tokens.append(tok)
else:
out_tokens.append(tok)
return xuntokenize(out_tokens)
def dedent(source, use_tabs=False):
"""
Minimizes indentation to save precious bytes. Optionally, *use_tabs*
may be specified if you want to use tabulators (\t) instead of spaces.
Example::
def foo(bar):
test = "This is a test"
Will become::
def foo(bar):
test = "This is a test"
"""
indent_char = '\t' if use_tabs else ' '
out = ""
last_lineno = -1
last_col = 0
prev_start_line = 0
indentation_level = 0
for tok in tokenize.generate_tokens(io.StringIO(source).readline):
if tok[0] == tokenize.INDENT:
indentation_level += 1
continue
if tok[0] == tokenize.DEDENT:
indentation_level -= 1
continue
token_string = tok[1]
start_line, start_col = tok[2]
end_line, end_col = tok[3]
if start_line > last_lineno:
last_col = 0
if start_line > prev_start_line:
if token_string in (',', '.'):
out += token_string
else:
indentation = indent_char * indentation_level
out += indentation + token_string
elif start_col > last_col:
out += indent_char + token_string
else:
out += token_string
prev_start_line = start_line
last_col = end_col
last_lineno = end_line
return out
def minify(tokens, use_tabs=None):
"""tokens: file-contents or tokens"""
if not isinstance(tokens, (list,tuple)):
tokens = xtokenize(tokens)
remove_comments(tokens)
result = xuntokenize(tokens)
result = remove_blank_lines_and_spaces2(result)
result = join_multiline_pairs(result)
result = join_multiline_pairs(result, '[]')
result = join_multiline_pairs(result, '{}')
tokens = xtokenize(result)
remove_docstrings(tokens)
result = xuntokenize(tokens)
result = remove_blank_lines_and_spaces2(result)
result = result.lstrip('\n')
result = fix_empty_methods(result)
result = reduce_operators(result)
result = dedent(result, use_tabs)
return result
def enumerate_local_modules(path,include_files=None,strip=False):
"""return all python source and include_files in `path`"""
local_modules = set()
old = os.getcwd()
full = os.path.abspath(path)
cwd = os.path.basename(full) if os.path.isfile(full) else full
os.chdir(cwd)
# Now check the local dir for matching modules
for root, dirs, files in os.walk('./'):
for f in files:
bo = False
if f.endswith('.py'):
bo = True
if include_files and f in include_files:
bo = True
if bo:
if strip:
f = f[:-3]
module = os.path.join(root,f)
local_modules.add(module)
os.chdir(old)
return list(local_modules)
def _zip_packall(fileobjs,precompile=True):
"""
Args:
fileobjs : [(fobj, modulename), filename, ...] # tuple 2 | file
* The first item will be used as `__main__.py`
"""
import zipfile
import tempfile
finals = []
for t in fileobjs:
bo = False
if isinstance(t,str) and os.path.isfile(t):
finals.append((t,t))
elif isinstance(t,(list,tuple)) and len(t) == 2:
if isinstance(t[1],str):
if (os.path.isfile(t[0]) and hasattr(t[0],'read')):
finals.append((t[0].read(),t[1]))
elif isinstance(t[0],str): # str, source contents
finals.append(t)
else:
bo = True
else:
bo = True
else:
bo = True
if bo:
print(f'Warning: zip_py: not valid: ignoring: {t}')
if not finals:
print('Fatal: zip_py: no inputs')
return
# This is so it will still execute as a zip
if finals[0][0].endswith('.py'):
finals[0] = (finals[0][0],'__main__.py')
elif finals[0][0].endswith('.pyc'):
finals[0] = (finals[0][0],'__main__.pyc')
else:
print('Note: use python source: zipfile::__main__')
finals[0] = (finals[0][0],'__main__.py')
i = 1
while True:
dest = 'xz' + str(i) + '.zip'
if not os.path.isfile(dest):
break
i += 1
print(f'Note: zipfile will be saved to: {dest}')
# Hopefully some day we'll be able to use ZIP_LZMA to save even more space
z = zipfile.ZipFile(dest, 'w', zipfile.ZIP_DEFLATED)
total_old_size = 0.0
for t in finals:
if os.path.isfile(t[0]):
file = t[0]
else:
w = tempfile.NamedTemporaryFile(mode='w',suffix='.py')
w.write(t[0])
w.flush()
file = w.name
total_old_size += os.path.getsize(file)
if precompile and file.endswith('.py'):
file = py_compile.compile(file,file+'c')
m = t[1] if t[1].endswith('.pyc') else t[1]+'c'
else:
m = t[1]
print(f'Note: zipfile writing::: {m}')
z.write(file, m)
if not os.path.isfile(t[0]):
w.close()
z.close()
i = 1
while True:
exe = 'xp' + str(i)
if not os.path.isfile(exe):
break
i += 1
print(f'Note: executable will be saved to: {exe}')
s = 'Has' if precompile else 'No'
print(f'>>> {s} precompile used')
with open(exe, 'wb') as f:
f.write(b'#!/usr/bin/env python3\n')
with open(dest, 'rb') as r:
f.write(r.read())
# Make it executable since shebang added
os.chmod(exe, 0o755)
p = round(os.path.getsize(exe)/total_old_size, 4)
print(f'Note: overall size reduction: {p} of original size\n')
def get_module_variables(file):
if not file or not (isinstance(file,str) and os.path.isfile(file) and file.endswith('.py')):
return []
results = set()
indent = 0
operator = False
prevstr = ''
for tok in tokenize.generate_tokens(open(file,'rt').readline):
if tok[0] == tokenize.INDENT:
indent += 1
elif tok[0] == tokenize.DEDENT:
indent -= 1
elif tok[0] == tokenize.OP:
if tok[1] in ['(', '[','{']:
operator = True
elif tok[1] in [')', ']', '}']:
operator = False
elif tok[0] == tokenize.NAME and indent == 0 and not operator and prevstr != '.':
results.add(tok[1])
prevstr = tok[1]
both = results.intersection(builtins)
diff = results.difference(builtins)
return sorted(list(diff))+sorted(list(both))
def main():
parser = argparse.ArgumentParser(
usage="""xpack.py:
# check going to be processed files
>>> xpack.py -s scrfile1 [scrfile2, dir, ...] -i add1.dat add2.md -L
# pack all files
>>> xpack.py -s scrfile1 [scrfile2, dir, ...] -i add1.dat add2.md
""",
allow_abbrev=False,
)
parser.add_argument(
'-v','--version',
action='version',
version=__version__
)
parser.add_argument(
'-s', '--srcfiles',
dest='srcfiles',
nargs='+',
help='source files or folders need to be processed'
)
parser.add_argument(
'-N', '--not-mini',
action='store_true',
dest='mini',
help='do not minification by removing docstrings, comments, white spaces, empty lines, and indentions'
)
parser.add_argument(
'-O', '--show-source-stdout',
action='store_true',
help='show sources to stdout, valid when `--mini` is used'
)
parser.add_argument(
'-mf', '--mini-files',
action='store_true',
help='minification files only, but not create executable'
)
parser.add_argument(
'-C', '--precompile',
dest='precompile',
action='store_true',
help='precompile python source before writing to zip executable'
)
parser.add_argument(
'-i', '--include-files',
dest='include_files',
nargs='+',
help='include additional files, such as README or data file'
)
parser.add_argument(
'-L', '--list-modules',
dest='list_modules',
action='store_true',
help='tool, only list including python modules (higher priority)'
)
parser.add_argument(
'-M', '--list-module-variables',
dest='list_module_variables',
action='store_true',
help='tool, only list python module type variables'
)
parser.add_argument(
'--features',
action='store_true',
help='show development features'
)
if len(sys.argv) <= 1:
parser.print_help()
return
args = parser.parse_args()
if args.features:
for i in FEATURES: print(i)
return
if args.srcfiles:
if not os.path.isfile(args.srcfiles[0]):
print(f'Fatal: the first input is not a file: {args.srcfiles[0]}')
return
else:
print('Fatal: no inputs')
return
args.mini = False if args.mini else True # reverse
files = []
for i in args.srcfiles:
if os.path.isfile(i):
v = os.path.normpath(i)
if v not in files:
files.append(v)
for i in args.srcfiles:
if os.path.isdir(i):
m = enumerate_local_modules(i,include_files=args.include_files,strip=False)
for j in m:
k = os.path.normpath(os.path.join(i,j))
if k not in files:
files.append(k)
if args.list_modules:
print('\nNote: going to include files:')
for i in files:
print(f' -> {i}')
if args.mini:
print('>>> minification will be performed')
if args.precompile:
print('>>> precompile will be performed')
print()
return
if args.list_module_variables:
for f in files:
results = get_module_variables(f)
print(f'\n>>>:: module variables for file: {f}')
if results:
print(' '.join(results))
print()
return
if args.mini or args.mini_files:
new = []
for f in files:
if f.endswith('.py'):
n = minify(f)
new.append((n,f))
else:
new.append((f,f))
files = new
print('\n>>> minification used')
if args.show_source_stdout:
if not args.mini:
print('Warning: show source to stdout only valid when minification is performed')
return
for c,f in files:
print('\n\n# ' + f)
print(c,end='')
print()
print()
return
if args.mini_files:
dest = '__xpackmini'
os.makedirs(dest,exist_ok=True)
pures = [t for t in files if not os.path.isfile(t[0])]
cwd = os.getcwd()
os.chdir(dest)
for t in pures:
base = os.path.basename(t[1])
with open(base,'wt') as f: f.write(t[0])
os.chdir(cwd)
print(f'Note: check folder for results: {dest}\n')
return
bo = True if args.precompile else False
_zip_packall(files,precompile=bo)
if __name__ == '__main__':
main()