-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.py
143 lines (111 loc) · 4.4 KB
/
setup.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
import os
import json
import urllib.request
import shutil
import re
import sys
repo_url = "https://api.github.com/repos/CaptainDario/DaKanji-Dependencies/releases/tags/"
tmp_dir = "tmp"
move_to_blobs = ["libtensorflow", "libmecab"]
move_to_dict = ["dict", "examples", "krad", "radk"]
move_to_tf_lite = ["CNN_single_char.tflite"]
files_to_exclude = ["audios.zip", "libtensorflowlite_c_arm64.dylib", "libtensorflowlite_c_x86_64.dylib"]
files_to_exclude_win = [
"libtensorflowlite_c-mac.dylib",
"libtensorflowlite_c_arm64.so", "libtensorflowlite_c_x86.so",
]
files_to_exclude_mac = [
"libmecab_arm64.dll", "libmecab_x86.dll",
"libtensorflowlite_c_x86.so", "libtensorflowlite_c_arm64.so",
"libtensorflowlite_c_x86.dll", "libtensorflowlite_c_arm64.dll"
]
files_to_exclude_lin = [
"libmecab_arm64.dll", "libmecab_x86.dll",
"libtensorflowlite_c_arm64.dll", "libtensorflowlite_c_x86.dll",
"libtensorflowlite_c-mac.dylib",
]
release_url = None
assets_version = None
def exclude_files_per_platform():
""" Excludes files that are not needed for the current platform
"""
if(sys.platform.startswith("win32")):
files_to_exclude.extend(files_to_exclude_win)
elif(sys.platform.startswith("darwin")):
files_to_exclude.extend(files_to_exclude_mac)
elif(sys.platform.startswith("linux")):
files_to_exclude.extend(files_to_exclude_lin)
def get_release_url():
""" gets the url to the latest assets release of DaKanji
"""
with open("pubspec.yaml", mode="r") as f:
content = f.read()
m = re.search(r'version: (.*)\+', content)
assets_version = m.group(1)
print("Downloading assets for version: ", assets_version)
return repo_url + "v" + assets_version
def download_assets(url: str):
""" Downloads all assets for DaKanji from the given url
url : url to the assets release of DaKanji
"""
# get url to latest download
req = urllib.request.Request(url)
asset_names, asset_urls = [], []
with urllib.request.urlopen(req) as response:
the_page = json.loads(response.read())
print(json.dumps(the_page, sort_keys=True, indent=4))
for k, v in the_page.items():
if(k == "assets"):
for asset in the_page[k]:
for aK, aV in asset.items():
if(aK == "browser_download_url"):
asset_urls.append(aV)
if(aK == "name"):
asset_names.append(aV)
# download to temp dir and unzip
if not os.path.exists(tmp_dir):
os.mkdir(tmp_dir)
for name, url in zip(asset_names, asset_urls):
if(name in files_to_exclude):
continue
print(f"Downloading: {name}")
urllib.request.urlretrieve(url, f"{tmp_dir}/{name}")
def move_assets():
""" Moves the downloaded assets to their destination
"""
# move files to correct location
print("Moving downloaded assets")
for f in os.listdir(tmp_dir):
# move dynamic libraries to blobs
if(f.startswith(tuple(move_to_blobs))):
shutil.copy(f"{tmp_dir}/{f}", "blobs/")
# move ipadic assets
if(f.startswith("ipadic")):
shutil.copy(f"{tmp_dir}/ipadic.zip", "assets/")
# move tf lite assets
if(f.startswith(tuple(move_to_tf_lite))):
shutil.copy(f"{tmp_dir}/{f}", "assets/tflite_models/")
# move the dictionary related assets
if(f.startswith(tuple(move_to_dict))):
shutil.copy(f"{tmp_dir}/{f}", "assets/dict/")
if __name__ == "__main__":
print("Setting up DaKanji")
args = sys.argv[1:]
if("--help" in args or "-h" in args):
print("""
--download-all : download all assets, this includes assets that are not needed to run dakanji on THIS platform
--no_download : Does NOT download any assets and expects to find all assets in a folder called 'tmp'
--no-delete : Do not delete the tmp folder
""")
sys.exit(0)
if("--download-all" not in args):
exclude_files_per_platform()
if("--no-download" not in args):
release_url = get_release_url()
download_assets(release_url)
move_assets()
if("--no-delete" not in args):
# delete temp dir
print("Deleting temporary folder")
shutil.rmtree(tmp_dir)
print("Setup done! Run: \n flutter run")