-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_pyz.py
52 lines (40 loc) · 1.44 KB
/
to_pyz.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
import itertools
import time
import zipfile
from pathlib import Path
from typing import Optional
from and_beyond.common import VERSION_DISPLAY_NAME
OUTPUT_DIR = Path('dist')
OUTPUT_DIR.mkdir(exist_ok=True)
DEST_FILE = OUTPUT_DIR / f'and-Beyond-{VERSION_DISPLAY_NAME}.pyz'
AND_BEYOND_DIR = Path('and_beyond')
ASSETS_DIR = Path('assets')
def copy_to_zip(zfp: zipfile.ZipFile, file: Path, arcname: Optional[str] = None) -> None:
global counter
if arcname is None:
arcname = file.relative_to('.').as_posix()
print('Writing', arcname)
zfp.write(file, arcname)
counter += 1
start = time.perf_counter()
counter = 0
with open(DEST_FILE, 'wb') as fp:
print('Writing shebang')
fp.write(b'#!/usr/bin/env python3\n')
with zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED) as zfp:
for child in itertools.chain(
AND_BEYOND_DIR.rglob('*'),
ASSETS_DIR.rglob('*')
):
rel = child.relative_to('.').as_posix()
if '__pycache__' in (child.name, child.parent.name) or child.name.endswith('.pdn'):
print('Skipping', rel)
continue
copy_to_zip(zfp, child, rel)
print('Writing __main__.py')
zfp.write('run_game.py', '__main__.py')
print('Writing LICENSE')
zfp.write('LICENSE')
end = time.perf_counter()
print('Successfully zipped', counter, 'files in', end - start, 'seconds')
print('Result stored in', DEST_FILE)