-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathdep.py
174 lines (141 loc) · 4.94 KB
/
dep.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
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
# -*- coding: utf-8 -*-
# -- stdlib --
import shutil
import zipfile
from pathlib import Path
from urllib.parse import urlparse
# -- third party --
import requests
# -- own --
from .misc import get_cache_home, info
from .tinysh import bash, sh, sudo, tar
# -- code --
def unzip(filename, extract_dir, strip=0):
"""
Unpack zip `filename` to `extract_dir`, optionally stripping `strip` components.
"""
if not zipfile.is_zipfile(filename):
raise Exception(f"{filename} is not a zip file")
extract_dir = Path(extract_dir)
ar = zipfile.ZipFile(filename)
try:
for info in ar.infolist():
name = info.filename
# don't extract absolute paths or ones with .. in them
if name.startswith("/") or ".." in name:
continue
target = extract_dir.joinpath(*name.split("/")[strip:]).resolve()
if not target:
continue
target.parent.mkdir(parents=True, exist_ok=True)
if not name.endswith("/"):
# file
data = ar.read(info.filename)
f = open(target, "wb")
try:
f.write(data)
finally:
f.close()
del data
finally:
ar.close()
def escape_url(url):
return url.replace("/", "_").replace(":", "_")
SHOULD_USE_MIRROR = {
"ci": False,
"aliyun": False,
"_probed": False,
}
def probe_mirrors():
if SHOULD_USE_MIRROR["_probed"]:
return
try:
resp = requests.get("http://botmaster.tgr:9000/misc/canary.txt", timeout=0.5)
if resp.ok and resp.text.strip() == "in-taichi-ci-environment":
info("Enabling Taichi CI cluster mirror")
SHOULD_USE_MIRROR["ci"] = True
except Exception:
pass
google_ok = True
try:
resp = requests.head("https://google.com", timeout=2)
google_ok = resp.ok
except Exception:
google_ok = False
if not google_ok:
info("Enabling Aliyun mirror")
SHOULD_USE_MIRROR["aliyun"] = True
SHOULD_USE_MIRROR["_probed"] = True
def download_dep(url, outdir, *, strip=0, force=False, args=None, plain=False, elevate=False):
"""
Download a dependency archive from `url` and expand it to `outdir`,
optionally stripping `strip` components.
"""
outdir = Path(outdir)
if outdir.exists() and len(list(outdir.glob("*"))) > 0 and not force:
return
shutil.rmtree(outdir, ignore_errors=True)
parsed = urlparse(url)
name = Path(parsed.path).name
escaped = escape_url(url)
depcache = get_cache_home() / "deps"
depcache.mkdir(parents=True, exist_ok=True)
local_cached = depcache / escaped
probe_mirrors()
urls = [url]
if SHOULD_USE_MIRROR["aliyun"]:
urls.append(f"https://taichi-bots.oss-cn-beijing.aliyuncs.com/depcache/{escaped}/{name}")
if SHOULD_USE_MIRROR["ci"]:
urls.append(f"http://botmaster.tgr:9000/misc/depcache/{escaped}/{name}")
size = -1
for u in reversed(urls):
try:
resp = requests.head(u, headers={"Accept-Encoding": "identity"}, timeout=1)
if resp.ok:
url = u
size = int(resp.headers["Content-Length"])
break
except Exception:
pass
if not local_cached.exists() or local_cached.stat().st_size != size:
import tqdm
with requests.get(url, stream=True) as r:
r.raise_for_status()
total_size = int(r.headers.get("content-length", 0))
prog = tqdm.tqdm(
unit="B",
unit_scale=True,
unit_divisor=1024,
total=total_size,
desc=name,
)
with prog, open(str(local_cached) + ".download", "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
sz = f.write(chunk)
prog.update(sz)
shutil.move(str(local_cached) + ".download", local_cached)
if name.endswith(".zip"):
outdir.mkdir(parents=True, exist_ok=True)
unzip(local_cached, outdir, strip=strip)
elif name.endswith(".tar.gz") or name.endswith(".tgz"):
outdir.mkdir(parents=True, exist_ok=True)
tar("-xzf", local_cached, "-C", outdir, f"--strip-components={strip}")
elif name.endswith(".sh"):
bash(local_cached, *args)
elif "." not in name and args is not None:
local_cached.chmod(0o755)
sh.bake(local_cached)(*args)
elif name.endswith(".exe") and args is not None:
local_cached.chmod(0o755)
cmd = sh.bake(local_cached)
if elevate:
with sudo():
cmd(*args)
else:
cmd(*args)
# start(local_cached, *args)
elif plain:
outdir.mkdir(parents=True, exist_ok=True)
shutil.copy(local_cached, outdir / name)
else:
raise RuntimeError(f"Unknown file type: {name}")