-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlist_builder.py
executable file
·266 lines (219 loc) · 8.43 KB
/
list_builder.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
import argparse
import copy
import json
import logging
import multiprocessing
import shutil
import subprocess
import time
from collections import OrderedDict
from functools import cache
from itertools import repeat
from pathlib import Path
from typing import Any, Optional
import toml
import tqdm
from tqdm.contrib.logging import logging_redirect_tqdm
from git import Repo
import appslib.logging_sender # pylint: disable=import-error
from appslib.utils import (
get_antifeatures, # pylint: disable=import-error
get_catalog,
get_categories,
)
import appslib.get_apps_repo as get_apps_repo
now = time.time()
@cache
def categories_list():
# Load categories and reformat the structure to have a list with an "id" key
new_categories = get_categories()
for category_id, infos in new_categories.items():
infos["id"] = category_id
for subtag_id, subtag_infos in infos.get("subtags", {}).items():
subtag_infos["id"] = subtag_id
infos["subtags"] = list(infos.get("subtags", {}).values())
return list(new_categories.values())
@cache
def antifeatures_list():
# (Same for antifeatures)
new_antifeatures = get_antifeatures()
for antifeature_id, infos in new_antifeatures.items():
infos["id"] = antifeature_id
return list(new_antifeatures.values())
################################
# Actual list build management #
################################
def __build_app_dict(data) -> Optional[tuple[str, dict[str, Any]]]:
(name, info), cache_path = data
try:
return name, build_app_dict(name, info, cache_path)
except Exception as err:
logging.error("[List builder] Error while updating %s: %s", name, err)
return None
def build_base_catalog(
catalog: dict[str, dict[str, Any]], cache_path: Path, nproc: int
):
result_dict = {}
with multiprocessing.Pool(processes=nproc) as pool:
with logging_redirect_tqdm():
tasks = pool.imap(
__build_app_dict, zip(catalog.items(), repeat(cache_path))
)
for result in tqdm.tqdm(tasks, total=len(catalog.keys()), ascii=" ·#"):
if result is not None:
name, info = result
result_dict[name] = info
return result_dict
def write_catalog_v3(base_catalog, apps_path: Path, target_dir: Path) -> None:
logos_dir = target_dir / "logos"
logos_dir.mkdir(parents=True, exist_ok=True)
def infos_for_v3(app_id: str, infos: Any) -> Any:
# We remove the app install question and resources parts which aint
# needed anymore by webadmin etc (or at least we think ;P)
if "manifest" in infos and "install" in infos["manifest"]:
del infos["manifest"]["install"]
if "manifest" in infos and "resources" in infos["manifest"]:
del infos["manifest"]["resources"]
app_id = app_id.lower()
logo_source = apps_path / "logos" / f"{app_id}.png"
if logo_source.exists():
logo_hash = (
subprocess.check_output(["sha256sum", logo_source])
.strip()
.decode("utf-8")
.split()[0]
)
shutil.copyfile(logo_source, logos_dir / f"{logo_hash}.png")
# FIXME: implement something to cleanup old logo stuf in the builds/.../logos/ folder somehow
else:
logo_hash = None
infos["logo_hash"] = logo_hash
return infos
full_catalog = {
"apps": {app: infos_for_v3(app, info) for app, info in base_catalog.items()},
"categories": categories_list(),
"antifeatures": antifeatures_list(),
}
target_file = target_dir / "apps.json"
target_file.parent.mkdir(parents=True, exist_ok=True)
target_file.open("w", encoding="utf-8").write(
json.dumps(full_catalog, sort_keys=True)
)
def write_catalog_doc(base_catalog, target_dir: Path) -> None:
def infos_for_doc_catalog(infos):
level = infos.get("level")
if not isinstance(level, int):
level = -1
return {
"id": infos["id"],
"category": infos["category"],
"url": infos["git"]["url"],
"name": infos["manifest"]["name"],
"description": infos["manifest"]["description"],
"state": infos["state"],
"level": level,
"broken": level <= 0,
"good_quality": level >= 8,
"bad_quality": level <= 5,
"antifeatures": infos.get("antifeatures"),
"potential_alternative_to": infos.get("potential_alternative_to", []),
}
result_dict_doc = {
k: infos_for_doc_catalog(v)
for k, v in base_catalog.items()
if v["state"] == "working"
}
full_catalog = {"apps": result_dict_doc, "categories": categories_list()}
target_file = target_dir / "apps.json"
target_file.parent.mkdir(parents=True, exist_ok=True)
target_file.open("w", encoding="utf-8").write(
json.dumps(full_catalog, sort_keys=True)
)
def build_app_dict(app, infos, cache_path: Path):
# Make sure we have some cache
this_app_cache = cache_path / app
assert this_app_cache.exists(), f"No cache yet for {app}"
repo = Repo(this_app_cache)
# If added_date is not present, we are in a github action of the PR that adds it... so default to a bad value.
infos["added_in_catalog"] = infos.get("added_date", 0)
# int(commit_timestamps_for_this_app_in_catalog.split("\n")[0])
infos["branch"] = infos.get("branch", "master")
infos["revision"] = infos.get("revision", "HEAD")
# If using head, find the most recent meaningful commit in logs
if infos["revision"] == "HEAD":
infos["revision"] = repo.head.commit.hexsha
# Otherwise, validate commit exists
else:
try:
_ = repo.commit(infos["revision"])
except ValueError as err:
raise RuntimeError(
f"Revision ain't in history ? {infos['revision']}"
) from err
# Find timestamp corresponding to that commit
timestamp = repo.commit(infos["revision"]).committed_date
# Build the dict with all the infos
if (this_app_cache / "manifest.toml").exists():
manifest = toml.load(
(this_app_cache / "manifest.toml").open("r"), _dict=OrderedDict
)
else:
manifest = json.load((this_app_cache / "manifest.json").open("r"))
return {
"id": manifest["id"],
"git": {
"branch": infos["branch"],
"revision": infos["revision"],
"url": infos["url"],
},
"added_in_catalog": infos["added_in_catalog"],
"lastUpdate": timestamp,
"manifest": manifest,
"state": infos["state"],
"level": infos.get("level", "?"),
"maintained": "package-not-maintained" not in infos.get("antifeatures", []),
"high_quality": infos.get("high_quality", False),
"featured": infos.get("featured", False),
"category": infos.get("category", None),
"subtags": infos.get("subtags", []),
"potential_alternative_to": infos.get("potential_alternative_to", []),
"antifeatures": list(
set(
list(manifest.get("antifeatures", {}).keys())
+ infos.get("antifeatures", [])
)
),
}
def main() -> None:
parser = argparse.ArgumentParser()
get_apps_repo.add_args(parser)
parser.add_argument(
"target_dir",
type=Path,
nargs="?",
help="The directory to write the catalogs to. Defaults to apps/builds/default",
)
parser.add_argument(
"-j",
"--jobs",
type=int,
default=multiprocessing.cpu_count(),
metavar="N",
help="Allow N threads to run in parallel",
)
args = parser.parse_args()
appslib.logging_sender.enable()
apps_dir = get_apps_repo.from_args(args)
cache_path = get_apps_repo.cache_path(args)
cache_path.mkdir(exist_ok=True, parents=True)
target_dir = args.target_dir or apps_dir / "builds" / "default"
catalog = get_catalog(apps_dir)
print("Retrieving all apps' information to build the catalog...")
base_catalog = build_base_catalog(catalog, cache_path, args.jobs)
print(f"Writing the catalogs to {target_dir}...")
write_catalog_v3(base_catalog, apps_dir, target_dir / "v3")
write_catalog_doc(base_catalog, target_dir / "doc_catalog")
print("Done!")
if __name__ == "__main__":
main()