-
Notifications
You must be signed in to change notification settings - Fork 65
/
indexer.py
281 lines (229 loc) · 8.69 KB
/
indexer.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import json
import yaml
import re
import sys
from gzip import GzipFile
from pathlib import Path
from hashlib import sha1
CACHE = Path("cache")
RX_PROTOCOL = 1 # This should be incremented when breaking changes to the format are implemented
GEN_PATH = Path("index")
GEN_FILE = GEN_PATH / Path(f"{RX_PROTOCOL}.json") # Pretty, for QA checking
GEN_MIN_FILE = GEN_PATH / Path(f"{RX_PROTOCOL}-min.json") # Minified, for user download
GEN_GZ_FILE = GEN_PATH / Path(f"{RX_PROTOCOL}-min.json.gz") # Gzipped
GEN_ERROR_LOG = GEN_PATH / Path(f"{RX_PROTOCOL}-errors.yaml") # Error log
class CustomEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, "__json__"):
return obj.__json__()
else:
return json.JSONEncoder.default(self, obj)
class Repo:
def __init__(self, url: str, category: str):
"""Anything exposed here will be serialized later
Attributes starting with rx_ deviate from the info.json spec
and as such they have this prefix to avoid future conflicts"""
self.rx_category = category # approved / unapproved
self._owner_repo = ""
self._error = ""
self._path = None
self.rx_cogs = []
self.author = []
self.description = ""
self.short = ""
self._url = url
self.name = ""
self.rx_branch = ""
try:
self.parse_name_branch_url(url)
except:
self._error = ("Something went wrong while parsing the url. "
"Is it a valid address?")
def parse_name_branch_url(self, url):
branch = ""
name = url.split("/")[4]
# Owner/RepoName will be useful when it's time to exclude cogs
self._owner_repo = url.split("/")[3] + "/" + url.split("/")[4]
if "@" in name:
name, branch = name.split("@")
if name.endswith("/"):
name = name[:-1]
url = url.replace("/@", "@")
if url.endswith("/"):
url = url[:-1]
self.name = name
self.rx_branch = branch
self._url = url
def folder_check_and_get_info(self):
if self._error:
return
safe_name = re.sub(r"[^a-zA-Z0-9_\-\.]", "", self.name).strip(".")
prefix = f"{safe_name}_" if safe_name else ""
base_path = CACHE / Path(f"{prefix}{sha1_digest(self._url)}")
if not base_path.is_dir():
self._error = "Repo path does not exist. Cloning failed?"
return
self._path = base_path
infofile = base_path / Path("info.json")
if not infofile.is_file():
self._error = "No repo info.json found."
return
try:
with open(str(infofile)) as f:
info = json.load(f)
except:
self._error = "Error reading repo info.json. Possibly invalid."
return
self.author = info.get("author", [])
self.description = info.get("description", "")
self.short = info.get("short", "")
def populate_cogs(self):
if self._error:
return
sub_dirs = [p for p in self._path.iterdir() if p.is_dir() and not p.name.startswith(".")]
for d in sub_dirs:
path = d / Path("info.json")
if path.is_file(): # Dirs with no info.json inside are simply ignored
self.rx_cogs.append(Cog(d.name, d))
if not self.rx_cogs:
self._error = "Repo contains no valid cogs"
def process_cogs(self):
if self._error:
return
for cog in self.rx_cogs:
cog.get_info()
cog.check_cog_validity()
def __json__(self):
return {k:v for (k, v) in self.__dict__.items() if not k.startswith("_") and not callable(k)}
class Cog:
def __init__(self, name: str, path: Path):
"""Anything exposed here will be serialized later
Attributes starting with rx_ deviate from the info.json spec
and as such they have this prefix to avoid future conflicts"""
self._name = name
self._path = path
self.author = []
self.description = ""
self.end_user_data_statement = ""
self.permissions = []
self.short = ""
self.min_bot_version = ""
self.max_bot_version = ""
self.min_python_version = ""
self.hidden = False
self.disabled = False
self.required_cogs = {}
self.requirements = []
self.tags = []
self.type = "" # Still a thing?
self._error = ""
def check_cog_validity(self):
if self._error:
return
initpath = self._path / Path("__init__.py")
if not initpath.exists():
self._error = "Info.json is present but no __init__.py was found. Invalid cog package."
def get_info(self):
if self._error:
return
info_path = self._path / Path("info.json")
try:
with open(str(info_path)) as f:
data = json.load(f)
except:
self._error = "Error reading cog info.json. Possibly invalid."
return
self.author = data.get("author", [])
self.description = data.get("description", "")
self.end_user_data_statement = data.get("end_user_data_statement", "")
self.short = data.get("short", "")
self.permissions = data.get("permissions", [])
self.min_bot_version = data.get("min_bot_version", "")
self.max_bot_version = data.get("max_bot_version", "")
self.min_python_version = data.get("min_python_version", "")
self.hidden = data.get("hidden", False)
self.disabled = data.get("disabled", False)
self.required_cogs = data.get("required_cogs", {})
self.requirements = data.get("requirements", [])
self.tags = data.get("tags", [])
self.type = data.get("type", "")
def __json__(self):
return {k:v for (k, v) in self.__dict__.items() if not k.startswith("_") and not callable(k)}
def sha1_digest(url):
return sha1(url.encode('utf-8')).hexdigest()
def make_error_log(repos):
log = {}
for r in repos:
if r._error:
log[r._url] = r._error
continue
for c in r.rx_cogs:
if not c._error:
continue
if r._url not in log:
log[r._url] = {}
log[r._url][c._name] = c._error
if log:
return yaml.safe_dump(log, sort_keys=True, default_flow_style=False)
else:
return ""
def main():
yamlfile = sys.argv[1]
with open(yamlfile) as f:
data = yaml.safe_load(f.read())
repos = []
for k in ("approved", "unapproved"):
if data[k]: # Can be None if empty
for url in data[k]:
repos.append(Repo(url, k))
for r in repos:
r.folder_check_and_get_info()
r.populate_cogs()
r.process_cogs()
# Remove errored repos and cogs.
error_log = make_error_log(repos)
repos = [r for r in repos if not r._error]
for r in repos:
r.rx_cogs = [c for c in r.rx_cogs if not c._error]
if data["flagged-cogs"]:
for url, flagged_cogs in data["flagged-cogs"].items():
for r in repos:
# I'm doing this instead of comparing URLs in case of
# slashes and such. Owner/Reponame is close enough.
if r._owner_repo not in url:
continue
to_remove = []
for c in r.rx_cogs:
if c._name in flagged_cogs:
to_remove.append(c)
if to_remove:
r.rx_cogs = [c for c in r.rx_cogs if c not in to_remove]
if repos:
# Final format URL : Repo...
repos_index = {}
for r in repos:
cogs_dict = {}
for c in r.rx_cogs:
cogs_dict[c._name] = c
# ... and CogName : Cog
r.rx_cogs = cogs_dict
repos_index[r._url] = r
if not GEN_PATH.exists():
GEN_PATH.mkdir()
minified_str = json.dumps(repos_index, separators=(',', ':'), sort_keys=True, cls=CustomEncoder)
# Minified json file
with open(str(GEN_MIN_FILE), "w") as f:
f.write(minified_str)
# Gzipped minified json file
with open(str(GEN_GZ_FILE), "wb") as f:
gz = GzipFile(GEN_MIN_FILE.name, "wb", 9, f, 0.)
gz.write(minified_str.encode())
gz.close()
# Pretty json file
with open(str(GEN_FILE), "w") as f:
json.dump(repos_index, f, indent=4, sort_keys=True, cls=CustomEncoder)
# YAML error log
with open(str(GEN_ERROR_LOG), "w") as f:
f.write(error_log)
if __name__ == "__main__":
main()