-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenmirrorz.py
executable file
·155 lines (130 loc) · 4.47 KB
/
genmirrorz.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import sys
import subprocess
import sys
import os
import logging
from utils import CONFIG_FOLDER, get_mirrorz_cname, get_resp_with_timeout
from gencontent import USER_CONFIG as gencontent_config
cname = get_mirrorz_cname()
logger = logging.getLogger(__name__)
with open(CONFIG_FOLDER / "genmirrorz.json") as f:
options = json.load(f)
base = options["base"]
skiplist = options.get("skip", [])
metas = []
for yuki_endpoint in gencontent_config.get("yuki", []):
if yuki_endpoint.get("mirrorz"):
url = yuki_endpoint["url"]
this_metas = get_resp_with_timeout(url)
if not this_metas:
logger.error(f"failed to get {url}")
continue
try:
this_metas = this_metas.json()
except json.JSONDecodeError:
logger.error(f"failed to decode json from {url}")
continue
if not this_metas:
logger.error(f"empty json from {url}")
continue
metas.extend(this_metas)
def name_func(name: str) -> str:
if name in cname:
return cname[name]
else:
return name
def iso(iso_orig: list) -> None:
# modify iso_orig inplace
for i in iso_orig:
i["distro"] = name_func(i["distro"])
if not i.get("category"):
# fallback to OS (iso) if not exist.
i["category"] = "os"
def size(bytes: int) -> str:
mib = bytes / 1024 / 1024
if mib < 1024:
return f"{mib:.2f} MiB"
gib = mib / 1024
if gib < 1024:
return f"{gib:.2f} GiB"
tib = gib / 1024
return f"{tib:.2f} TiB"
def parse_repo_with_meta(repolist: list, meta: dict) -> dict:
content_list = []
content_hash = {}
for i in repolist:
_, help_url, _, name = i
cname = name_func(name)
content_hash[cname.lower()] = len(content_list)
content_list.append(
{
"cname": cname,
"desc": "", # now we don't have desc yet...
"url": f"/{name}",
"status": "U",
"help": help_url,
"upstream": "",
}
)
# now we add data to content_list with meta!
for i in meta:
name = i["name"]
if name in skiplist:
continue
name = name_func(name)
try:
try:
ind = content_hash[name.lower()]
except KeyError:
ind = content_hash[
name.lower().split(".")[0]
] # fix repo name like "kubernetes.apt"
next_run = i.get("nextRun")
last_success = i.get("lastSuccess")
if next_run < 0:
content_list[ind][
"status"
] = "P" # negative next_run means cron date set to 2/30 or 2/31 (unreachable)
elif i["syncing"]:
content_list[ind]["status"] = "Y" + str(i.get("prevRun"))
if last_success:
content_list[ind]["status"] += "O" + str(last_success)
elif i["exitCode"] == 0:
content_list[ind]["status"] = "S" + str(last_success)
else:
content_list[ind]["status"] = "F" + str(i.get("prevRun"))
if last_success:
content_list[ind]["status"] += "O" + str(last_success)
if next_run > 0:
content_list[ind]["status"] += "X" + str(next_run)
content_list[ind]["size"] = size(i["size"])
content_list[ind]["upstream"] = i["upstream"]
except KeyError:
print(f"failed to parse {i['name']}", file=sys.stderr)
return content_list
def disk_info(site: dict) -> None:
# TODO(portability): This now only works on mirrors4
repo_zfs = subprocess.check_output(
"zfs get -Hp -o value used,available pool0/repo", shell=True
).decode("utf-8")
used, available = repo_zfs.split()
used = int(used)
available = int(available)
site["disk"] = f"{size(used)} / {size(used + available)}"
def getMirrorzJson(repolist, isolist):
disk_info(base["site"])
iso(isolist)
mirrors = parse_repo_with_meta(repolist, metas)
mirrorz = base
mirrorz["info"] = isolist
mirrorz["mirrors"] = mirrors
return json.dumps(mirrorz)
if __name__ == "__main__":
from utils import get_isolist
import gencontent
isolist = get_isolist()
repolist = gencontent.genRepoList()
print(getMirrorzJson(repolist, isolist))