Spaces:
Sleeping
Sleeping
Update bootstrap_upstream.py
Browse files- bootstrap_upstream.py +43 -12
bootstrap_upstream.py
CHANGED
|
@@ -1,16 +1,47 @@
|
|
| 1 |
import os, io, zipfile, shutil, requests
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
def _dl_and_extract(owner, repo, ref):
|
| 4 |
-
url=f"https://github.com/{owner}/{repo}/archive/refs/heads/{ref}.zip"
|
| 5 |
-
r=requests.get(url,timeout=60)
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def fetch_upstream():
|
| 11 |
-
os.makedirs("external",exist_ok=True)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return results
|
|
|
|
| 1 |
import os, io, zipfile, shutil, requests
|
| 2 |
+
|
| 3 |
+
GITHUB_SOURCES = [
|
| 4 |
+
("RTLucassen", "B-line_detection"),
|
| 5 |
+
("ljhowell", "LUS-Segmentation-RT"),
|
| 6 |
+
("GuoJiaqi-1020", "MeDiVLAD"),
|
| 7 |
+
]
|
| 8 |
+
|
| 9 |
def _dl_and_extract(owner, repo, ref):
|
| 10 |
+
url = f"https://github.com/{owner}/{repo}/archive/refs/heads/{ref}.zip"
|
| 11 |
+
r = requests.get(url, timeout=60)
|
| 12 |
+
r.raise_for_status()
|
| 13 |
+
z = zipfile.ZipFile(io.BytesIO(r.content))
|
| 14 |
+
|
| 15 |
+
tmp = f"_tmp_{repo}"
|
| 16 |
+
os.makedirs(tmp, exist_ok=True)
|
| 17 |
+
z.extractall(tmp)
|
| 18 |
+
|
| 19 |
+
top = [os.path.join(tmp, d) for d in os.listdir(tmp) if os.path.isdir(os.path.join(tmp, d))]
|
| 20 |
+
if not top:
|
| 21 |
+
raise RuntimeError(f"Could not find extracted folder for {owner}/{repo}")
|
| 22 |
+
|
| 23 |
+
src = top[0]
|
| 24 |
+
dst = os.path.join("external", repo)
|
| 25 |
+
os.makedirs("external", exist_ok=True)
|
| 26 |
+
if os.path.exists(dst):
|
| 27 |
+
shutil.rmtree(dst)
|
| 28 |
+
shutil.move(src, dst)
|
| 29 |
+
shutil.rmtree(tmp, ignore_errors=True)
|
| 30 |
+
return dst
|
| 31 |
+
|
| 32 |
def fetch_upstream():
|
| 33 |
+
os.makedirs("external", exist_ok=True)
|
| 34 |
+
results = {}
|
| 35 |
+
for owner, repo in GITHUB_SOURCES:
|
| 36 |
+
dst = os.path.join("external", repo)
|
| 37 |
+
if os.path.isdir(dst):
|
| 38 |
+
results[repo] = "exists"
|
| 39 |
+
continue
|
| 40 |
+
# try 'main' then 'master'
|
| 41 |
+
try:
|
| 42 |
+
_dl_and_extract(owner, repo, "main")
|
| 43 |
+
results[repo] = "downloaded-main"
|
| 44 |
+
except Exception:
|
| 45 |
+
_dl_and_extract(owner, repo, "master")
|
| 46 |
+
results[repo] = "downloaded-master"
|
| 47 |
return results
|