-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpackage.py
More file actions
344 lines (281 loc) · 10 KB
/
package.py
File metadata and controls
344 lines (281 loc) · 10 KB
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
#!/usr/bin/env python3
import io
import utils
import string
import base64
import requests
import tarfile
import zipfile
import hashlib
import tempfile
import subprocess
from git import Repo
from loguru import logger
from pathlib import Path
def explore_file(src: Path):
assert src.exists()
if src.is_dir():
for root, dirs, files in src.walk():
rel = root.relative_to(src)
for it in dirs:
yield rel/it
for it in files:
yield rel/it
def explore_git(src: Path):
assert src.is_dir()
for it in Repo(src).tree().traverse():
yield it.path
git = src/'.git'
for it in explore_file(git):
yield '.git'/it
def emit(out, src, git):
assert isinstance(src, (Path, bytes, list)), src
if isdir := isinstance(src, list):
yield {'out': out}
if isinstance(src, bytes):
yield {'out': out, 'src': src}
return
for src, it in explore(src, git):
yield {
'out': out/src.name/it if isdir else out/it,
'src': src/it}
def explore(src, git):
explore = explore_git if git else explore_file
if not isinstance(src, list):
src = [src]
for src in src:
src = src.absolute()
if not src.exists():
logger.warning(f'source not found: "{src}"')
continue
yield src, Path('.')
for it in explore(src):
yield src, it
def reset(info):
info.uid = 0
info.gid = 0
info.mtime = 0
info.uname = 'root'
info.gname = 'root'
info.mode |= 0o200
def add_bin(tar, out, src, mod=None):
assert tar, out and isinstance(src, bytes)
# Create parent directories first
add_dir(tar, out.parent)
info = tarfile.TarInfo(str(out))
info.mode = mod or 0o644
info.size = len(src)
reset(info)
tar.addfile(info, io.BytesIO(src))
def add_file(tar, out, src, mod=None):
assert tar, out and src.exists()
# Create parent directories first
add_dir(tar, out.parent)
info = tar.gettarinfo(src, out)
info.mode = mod or info.mode
reset(info)
with open(src, 'rb') as f:
tar.addfile(info, f)
def add_dir(tar, out, mod=None):
assert tar, out
cache = getattr(tar, '__cache__', set())
tar.__cache__ = cache
if out.parent == Path('.') or out in cache:
return
add_dir(tar, out.parent)
info = tarfile.TarInfo(f'{out}/')
info.type = tarfile.DIRTYPE
info.mode = mod or 0o755
reset(info)
tar.addfile(info)
cache.add(out)
def tar(path, data):
if not data:
logger.warning('no work to do.')
return
if isinstance(data, dict):
data = [data]
assert hasattr(data, '__iter__'), f'bad data format: "{data}"'
with tarfile.open(path, mode='w:xz', format=tarfile.GNU_FORMAT, dereference=True) as tar:
for it in data:
out = it.get('out')
src = it.get('src')
mod = it.get('mod')
assert out, f'bad out field: "{out}"'
out = Path(out)
assert mod is None or isinstance(mod, int)
if isinstance(src, bytes):
add_bin(tar, out, src, mod)
elif not src or src.is_dir():
add_dir(tar, out, mod)
elif src.exists():
add_file(tar, out, src, mod)
else:
raise FileNotFoundError(src)
def base64_md5_file(path):
md5 = hashlib.md5()
with open(path, 'rb') as f:
while s := f.read(8192):
md5.update(s)
return base64.b64encode(md5.digest()).decode('utf8')
def download(url, out):
assert url, out
with requests.get(url, allow_redirects=True, stream=True) as resp:
if resp.status_code != 200:
return None
if hash := resp.headers.get('x-goog-hash'):
hash = dict([it.strip().split('=', 1) for it in hash.split(',')])
if (dst := Path(out)) and dst.is_dir():
dst = dst/url.split('?')[0].split('/')[-1]
if dst.is_file() and (md5 := base64_md5_file(dst)):
if md5 == hash.get('md5'):
return dst
resp = requests.get(url)
# TODO: check md5
with open(dst, "wb") as f:
for chunk in resp.iter_content(chunk_size=8192):
if not chunk:
continue
f.write(chunk)
return dst
class Output(object):
def __init__(self, root, arch):
self.any = None
for it in utils.__MODE__:
out = utils.target_output(root, arch, it)
self.__dict__[it] = out
if not self.any and Path(out).is_dir():
self.any = out
assert self.any, 'no valid out path found.'
@utils.record
class Package(object):
def __init__(self, root, arch, control, resource, define=None):
root = Path(root).resolve()
assert root.is_dir(), f'bad flutter root path: "{root}"'
self.globals = {
'tag': utils.flutter_tag(root),
'root': root,
'arch': arch,
'output': Output(root, arch),
'version': utils.engine_version(root),
'architecture': utils.termux_arch(arch),
}
self.defines = {
k: eval(v, self.globals) for k, v in define.items()
}
self.control = control
self.resource = resource
self.__dict__.update(self.globals)
self.__dict__.update(self.defines)
def __format__(self, s, **extra):
return string.Template(s).safe_substitute(
**self.globals,
**self.defines,
**extra)
def gen_control(self):
bin = io.BytesIO()
for k, v in self.control.items():
bin.write(self.__format__(f'{k}: {v}\n').encode('utf8'))
return {'out': 'control', 'src': bin.getvalue()}
def gen_resource(self, name=None):
if isinstance(name, str):
yield from self.gen_resource_internal(name)
elif isinstance(name, list):
for it in name:
yield from self.gen_resource_internal(it)
elif not name:
for it in self.resource.keys():
yield from self.gen_resource_internal(it)
else:
raise ValueError(f'bad name: "{name}"')
def gen_resource_internal(self, name=None):
if not (data := self.resource.get(name)):
raise ValueError(f'unknown resource name: "{name}"')
git = data.get('git', False)
src = data.get('source', [])
out = data.get('output')
bin = data.get('binary', False)
mod = data.get('mode')
dep = data.get('define', {})
ext = {}
for k, v in dep.items():
dep[k] = eval(v, self.globals, self.defines)
# expect None, str, int
if isinstance(mod, str):
mod = int(mod, 8)
if isinstance(mod, int):
ext['mod'] = mod
elif mod is not None:
raise ValueError(f'bad mode type: "{type(mod)}"')
# expect str, list
if isinstance(out, str):
out = [out]
if isinstance(out, list):
out = (Path(self.__format__(it, **dep)) for it in out)
else:
raise ValueError(f'bad output type: "{type(out)}"')
# expect None, str, list
if isinstance(src, str):
src = self.__format__(src, **dep)
src = src.encode('utf8') if bin else Path(src)
if isinstance(src, list) and not bin:
src = [Path(self.__format__(it, **dep)) for it in src]
elif not isinstance(src, (bytes, Path)):
raise ValueError(f'bad source type: "{type(src)}"')
for out in out:
for it in emit(out, src, git):
yield it | ext
def test_resource(self, name=None):
if isinstance(name, str):
yield self.test_resource_internal(name)
elif isinstance(name, list):
for it in name:
yield self.test_resource_internal(it)
elif not name:
for it in self.resource.keys():
yield self.test_resource_internal(it)
else:
raise ValueError(f'bad name: "{name}"')
def test_resource_internal(self, name):
if not (data := self.resource.get(name)):
raise ValueError(f'unknown resource name: "{name}"')
if not (test := data.get('test', {})):
return None
deps = data.get('define', {}).items()
deps = {k: eval(v, self.globals, self.defines) for k, v in deps}
file = self.__format__(test['file'], **deps)
path = self.__format__(test['path'], **deps)
if not (dest := download(file, Path('~/storage/downloads/1DMP/General').expanduser())):
logger.warning(f'test file not found: "{file}"')
data = {it['out'] for it in self.gen_resource(name)}
with zipfile.ZipFile(dest) as f:
for it in f.namelist():
if not it.endswith('.md') and Path(path, it) not in data:
logger.error(f'missing file: {path}/{it}')
return False
return True
def debuild(self, output, section=None):
output = Path(output or '.').expanduser().resolve()
if not output.parent.is_dir() or output.is_dir():
raise ValueError(f'bad output path: "{output}"')
with tempfile.TemporaryDirectory() as tmp:
info = Path(tmp, 'debian-binary')
ctrl = Path(tmp, 'control.tar.xz')
data = Path(tmp, 'data.tar.xz')
with open(info, 'wb+') as f:
f.write(b'2.0\n')
tar(ctrl, self.gen_control())
tar(data, self.gen_resource(section))
subprocess.run(
['ar', 'rc', output, info, ctrl, data],
check=True,
stderr=True,
stdout=True)
logger.info(f'✓ 构建完成 {output}')
if __name__ == '__main__':
import fire
import yaml
with open('package.yaml', 'rb') as f:
src = yaml.safe_load(f)
pkg = Package(root='flutter', arch='arm64', **src)
fire.Fire(pkg)