-
Notifications
You must be signed in to change notification settings - Fork 3
/
flathub.py
executable file
·334 lines (285 loc) · 10.6 KB
/
flathub.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python3
import base64
import binascii
import gzip
import json
import re
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from gi.repository import GLib
def load_cache(path):
commit_map = {}
try:
print(f"Loading cache from {path}")
with open(path) as f:
commit_map = json.loads(f.read())
except OSError:
print("Failed to load cache")
pass
return CommitCache(commit_map)
class CommitCache:
def __init__(self, commit_map):
self.commit_map: dict[str, list[str | None]] = commit_map
self.dirtree_map: dict[str | None] = {}
self.modified = False
# Backwards compat, re-resolve all commits where we don't have root dirtree info
# Also remove uninteresting things from the cache
for commit, cached_data in list(self.commit_map.items()):
if not isinstance(cached_data, list):
ref = cached_data
# Older version saved uninteresting refs in the cache, but we don't need them anymore
if ref and should_keep_ref(ref):
self.update_for_commit(commit, ref)
else:
del self.commit_map[commit]
for commit, cached_data in list(self.commit_map.items()):
dirtree = cached_data[1]
if dirtree:
self.dirtree_map[dirtree] = commit
self.summary_map = {}
url = "https://dl.flathub.org/repo/summary"
try:
response = urllib.request.urlopen(url)
summaryv = response.read()
if summaryv:
v = GLib.Variant.new_from_bytes(
GLib.VariantType.new("(a(s(taya{sv}))a{sv})"),
GLib.Bytes.new(summaryv),
False,
)
for m in v[0]:
self.summary_map[m[0]] = binascii.hexlify(
bytearray(m[1][1])
).decode("utf-8")
except OSError:
print("Failed to load summary: ")
print(sys.exc_info())
pass
def update_from_summary(self, branch: str):
commit = self.summary_map.get(branch, None)
if commit and not self.has_commit(commit):
self.update_for_commit(commit, branch)
def update_for_commit(self, commit: str | None, known_branch: str | None = None):
ref = known_branch
root_dirtree = None
url = f"https://dl.flathub.org/repo/objects/{commit[0:2]}/{commit[2:]}.commit"
print(f"Resolving {commit}", end=" ")
try:
response = urllib.request.urlopen(url)
commitv = response.read()
if commitv:
v = GLib.Variant.new_from_bytes(
GLib.VariantType.new("(a{sv}aya(say)sstayay)"),
GLib.Bytes.new(commitv),
False,
)
if "xa.ref" in v[0]:
ref = v[0]["xa.ref"]
elif "ostree.ref-binding" in v[0]:
ref = v[0]["ostree.ref-binding"][0]
root_dirtree = binascii.hexlify(bytearray(v[6])).decode("utf-8")
except OSError:
print("Failed to resolve commit")
pass
print(f"-> {ref}, {root_dirtree}")
self.modified = True
self.commit_map[commit] = [ref, root_dirtree]
if root_dirtree:
self.dirtree_map[root_dirtree] = commit
def has_commit(self, commit):
return commit in self.commit_map
def lookup_ref(self, commit):
pair = self.commit_map.get(commit, None)
if pair:
return pair[0]
def lookup_by_dirtree(self, dirtree) -> str | None:
return self.dirtree_map.get(dirtree, None)
def save(self, path):
if self.modified:
try:
with open(path, "w") as f:
json.dump(self.commit_map, f, indent=4)
except OSError:
print("Failed to save cache")
pass
self.modified = False
# Indexes for log lines
CHECKSUM = 0
DATE = 1
REF = 2
OSTREE_VERSION = 3
FLATPAK_VERSION = 4
IS_DELTA = 5
IS_UPDATE = 6
COUNTRY = 7
# 151.100.102.134 "-" "-" [05/Jun/2018:10:01:16 +0000] "GET /repo/objects/ca/717a9f713291670035f228520523cdea82811eb34521b58b7eea6d5f9e4085.filez HTTP/1.1" 200 822627 "" "libostree/2018.5 flatpak/0.11.7" "runtime/org.freedesktop.Sdk/x86_64/1.6" "" IT
fastly_log_pat = (
r""
" ?" # allow leading space in syslog message per RFC3164
"([\\da-f.:]+)" # source
'\\s"-"\\s"-"\\s'
"\\[([^\\]]+)\\]\\s" # datetime
'"(\\w+)\\s([^\\s"]+)\\s([^"]+)"\\s' # path
"(\\d+)\\s" # status
"([^\\s]+)\\s" # size
'"([^"]*)"\\s' # referrer
'"([^"]*)"\\s' # user agent
'"([^"]*)"\\s' # ref
'"([^"]*)"\\s' # update_from
"(\\w+)" # update_from
)
fastly_log_re = re.compile(fastly_log_pat)
def deltaid_to_commit(deltaid: str) -> str | None:
try:
if deltaid:
return binascii.hexlify(
base64.b64decode(deltaid.replace("_", "/") + "=")
).decode("utf-8")
except binascii.Error:
pass
return None
def should_keep_ref(ref: str) -> bool:
parts = ref.split("/")
if parts[0] == "app":
return True
return bool(
parts[0] == "runtime"
and not (
parts[1].endswith(".Debug")
or parts[1].endswith(".Locale")
or parts[1].endswith(".Sources")
)
)
def parse_log(logname: str, cache: CommitCache, ignore_deltas=False):
print(f"loading log {logname}")
downloads = []
with gzip.open(logname, "rb") if logname.endswith(".gz") else open(
logname
) as log_file:
# detect log type
first_line = log_file.readline()
if first_line == "":
return []
match = fastly_log_re.match(first_line)
if match:
line_re = fastly_log_re
else:
raise Exception("Unknown log format")
while True:
if first_line:
line = first_line
first_line = None
else:
line = log_file.readline()
if line == "":
break
match = line_re.match(line)
if not match:
sys.stderr.write(f"Warning: Can't match line: {line[:-1]}\n")
continue
op = match.group(3)
result = match.group(6)
path = match.group(4)
if op != "GET" or result != "200":
continue
target_ref: str = match.group(10)
if len(target_ref) == 0:
target_ref = None
# Early bailout for uninteresting refs (like locales) to keep work down
if target_ref is not None and not should_keep_ref(target_ref):
continue
# Ensure we have (at least) the current HEAD for this branch cached.
# We need this to have any chance to map a dirtree object to the
# corresponding ref, because unless we saw the commit id for some
# other reason before we will not have resolved it so we can do
# the reverse lookup.
if target_ref:
cache.update_from_summary(target_ref)
is_delta = False
if path.startswith("/repo/deltas/") and path.endswith("/superblock"):
if ignore_deltas:
continue
delta = path[len("/repo/deltas/") : -len("/superblock")].replace(
"/", ""
)
if delta.find("-") != -1:
is_delta = True
delta[: delta.find("-")]
target = delta[delta.find("-") + 1 :]
else:
target = delta
commit = deltaid_to_commit(target)
if not commit:
continue
elif path.startswith("/repo/objects/") and path.endswith(".dirtree"):
dirtree = path[len("/repo/objects/") : -len(".dirtree")].replace(
"/", ""
)
# Look up via the reverse map for all the commits we've seen so far
commit = cache.lookup_by_dirtree(dirtree)
if not commit:
continue # No match, probably not a root dirtree (although could be commit we never saw before)
else:
# Some other kind of log line, ignore
continue
# Maybe this is a new commit, if so cache it for future use
if not cache.has_commit(commit):
cache.update_for_commit(commit, target_ref)
# Some log entries have no ref specified, if so look it up via the cache
if not target_ref:
target_ref = cache.lookup_ref(commit)
if not target_ref:
print("Unable to figure out ref for commit " + str(commit))
continue
# Late bailout, as we're now sure of the ref
if not should_keep_ref(target_ref):
continue
date_str = match.group(2)
if not date_str.endswith(" +0000"):
sys.stderr.write(f"Unhandled date timezone: {date_str}\n")
continue
date_str = date_str[:-6]
date_struct = time.strptime(date_str, "%d/%b/%Y:%H:%M:%S")
date = "%d/%02d/%02d" % (
date_struct.tm_year,
date_struct.tm_mon,
date_struct.tm_mday,
)
user_agent = match.group(9)
uas = user_agent.split(" ")
ostree_version = (
"2017.15" # This is the last version that didn't list version
)
flatpak_version = None
for ua in uas:
if ua.startswith("libostree/"):
ostree_version = ua[10:]
if ua.startswith("flatpak/"):
flatpak_version = ua[8:]
update_from = match.group(11)
if len(update_from) == 0:
update_from = None
country = match.group(12)
is_update = is_delta or update_from
download = (
commit,
date,
target_ref,
ostree_version,
flatpak_version,
is_delta,
is_update,
country,
)
downloads.append(download)
return downloads
if __name__ == "__main__":
logs = []
for logname in sys.argv[1:]:
log = parse_log(logname, CommitCache({}))
logs = logs + log
for log in logs:
print(log)