-
Notifications
You must be signed in to change notification settings - Fork 65
/
parser.py
64 lines (51 loc) · 1.57 KB
/
parser.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
import os
import re
import sys
from hashlib import sha1
from pathlib import Path
import yaml
CACHE = Path("cache")
def executable_opener(path, flags):
return os.open(path, flags, 0o755)
def sha1_digest(url):
return sha1(url.encode('utf-8')).hexdigest()
def get_name(url):
name = url.split("/")[4]
if "@" in name:
name, _ = name.split("@")
if name.endswith("/"):
name = name[:-1]
return name
def get_clean_url(url):
branch = ""
if "@" in url:
url, branch = url.split("@")
if url.endswith("/"):
url = url[:-1]
return url, branch
if __name__ == "__main__":
infile = sys.argv[1]
outfile = sys.argv[2]
with open(infile) as f:
data = yaml.safe_load(f.read())
sh = "mkdir -p cache\n"
repos = []
if data["approved"]:
repos.extend(data["approved"])
if data["unapproved"]:
repos.extend(data["unapproved"])
for r in repos:
name = get_name(r)
url, branch = get_clean_url(r)
safe_name = re.sub(r"[^a-zA-Z0-9_\-\.]", "", name).strip(".")
prefix = f"{safe_name}_" if safe_name else ""
if branch:
sha = sha1_digest(f"{url}@{branch}")
dest = CACHE / Path(f"{prefix}{sha}")
sh += f"./git-retry.sh clone --depth=1 {url} --branch {branch} --single-branch {dest}\n"
else:
sha = sha1_digest(url)
dest = CACHE / Path(f"{prefix}{sha}")
sh += f"./git-retry.sh clone --depth=1 {url} {dest}\n"
with open(outfile, "w", opener=executable_opener) as f:
f.write(sh)