forked from pygame-web/pygbag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pack.py
126 lines (95 loc) · 3.59 KB
/
pack.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
import sys, os
import zipfile
from pathlib import Path
from .gathering import gather
from .filtering import filter
from .optimizing import optimize
from .html_embed import html_embed
COUNTER = 0
class REPLAY:
HTML = False
LIST = []
APK = ""
TARGET = ""
async def pack_files(zf, packlist, zfolders, target_folder):
global COUNTER
for asset in packlist:
zpath = list(zfolders)
zpath.insert(0, str(target_folder))
zpath.append(str(asset)[1:])
zip_content = target_folder / str(asset)[1:]
print(f"\t{target_folder} : {str(asset)[1:]}")
zpath = list(zfolders)
zpath.append(str(asset)[1:].replace("-pygbag.", "."))
if not zip_content.is_file():
print("32: ERROR", zip_content)
break
zip_name = Path("/".join(zpath))
# TODO: TEST SHEBANG for .html -> .py extension
COUNTER += 1
zf.write(zip_content, zip_name)
def stream_pack_replay():
global COUNTER, REPLAY
if os.path.isfile(REPLAY.APK):
os.unlink(REPLAY.APK)
zfolders = ["assets"]
with zipfile.ZipFile(REPLAY.APK, mode="x", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zf:
for asset in REPLAY.LIST:
zpath = list(zfolders)
zpath.insert(0, str(REPLAY.TARGET))
zpath.append(str(asset)[1:])
zip_content = REPLAY.TARGET / str(asset)[1:]
zpath = list(zfolders)
zpath.append(str(asset)[1:].replace("-pygbag.", "."))
if not zip_content.is_file():
print("59: ERROR", zip_content)
break
zip_name = Path("/".join(zpath))
zf.write(zip_content, zip_name)
print(f"replay packing {len(REPLAY.LIST)=} files complete for {REPLAY.APK}")
async def archive(apkname, target_folder, build_dir=None):
global COUNTER, REPLAY
COUNTER = 0
if build_dir:
apkname = build_dir.joinpath(apkname).as_posix()
walked = []
for folder, filenames in gather(target_folder):
walked.append([folder, filenames])
sched_yield()
filtered = []
last = ""
for infolder, fullpath in filter(walked):
if last != infolder:
print(f"Now in {infolder}")
last = infolder
print(" " * 4, fullpath)
filtered.append(fullpath)
sched_yield()
packlist = []
for filename in optimize(target_folder, filtered):
packlist.append(filename)
sched_yield()
REPLAY.LIST = packlist
REPLAY.APK = apkname
REPLAY.TARGET = target_folder
if "--html" in sys.argv:
REPLAY.HTML = True
html_embed(target_folder, packlist, apkname[:-4] + ".html")
return
try:
with zipfile.ZipFile(apkname, mode="x", compression=zipfile.ZIP_DEFLATED, compresslevel=9) as zf:
# pack_files(zf, Path.cwd(), ["assets"], target_folder)
await pack_files(zf, packlist, ["assets"], target_folder)
except TypeError:
# 3.6 does not support compresslevel
with zipfile.ZipFile(apkname, mode="x", compression=zipfile.ZIP_DEFLATED) as zf:
# pack_files(zf, Path.cwd(), ["assets"], target_folder)
await pack_files(zf, packlist, ["assets"], target_folder)
print(f"packing {COUNTER} files complete")
async def web_archive(apkname, build_dir):
archfile = build_dir.with_name("web.zip")
if archfile.is_file():
archfile.unlink()
with zipfile.ZipFile(archfile, mode="x", compression=zipfile.ZIP_STORED) as zf:
for f in ("index.html", "favicon.png", apkname):
zf.write(build_dir.joinpath(f), f)