generate-asset-list.py
1.6 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
#!/usr/bin/env python3
"""
This file assumes that
assets:
is the last line in ./pubspec.yaml
It reads the file names of all files from the ./assets folder
and turns them as assets and writes them into ./pubspec.yaml
"""
import os
def main():
target = "./assets"
excluded_ext = [
".gitkeep",
".onnx.json",
".py",
".sh",
"*.md",
"MODEL_CARD",
]
sep = " "
ss = []
for root, d, files in os.walk(target):
for f in files:
skip = False
for p in excluded_ext:
if f.endswith(p):
skip = True
break
if skip:
continue
t = os.path.join(root, f).replace("\\", "/")
ss.append("{sep}- {t}".format(sep=sep, t=t))
# read pub.spec.yaml
with open("./pubspec.yaml", encoding="utf-8") as f:
lines = f.readlines()
found_assets = False
with open("./pubspec.yaml", "w", encoding="utf-8") as f:
for line in lines:
if line == " assets:\n":
assert found_assets is False
found_assets = True
if len(ss) > 0:
f.write(line)
if not found_assets:
f.write(line)
continue
for s in ss:
f.write("{s}\n".format(s=s))
break
if not found_assets and ss:
f.write(" assets:\n")
for s in ss:
f.write("{s}\n".format(s=s))
if __name__ == "__main__":
main()