-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.py
executable file
·205 lines (185 loc) · 6.77 KB
/
log.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
#!/usr/bin/env python3
import binascii
import hashlib
import io
import json
import os
import re
import requests
import sys
import time
import zipfile
from bs4 import BeautifulSoup
from pathlib import Path
def process_url(status_codes, url, checksum_type, checksum):
sha1_hasher = hashlib.sha1()
sha256_hasher = hashlib.sha256()
sha512_hasher = hashlib.sha512()
r = requests.get(url, stream=True)
status_codes.append([url, r.status_code])
if r.status_code != 200:
return
d = None
with io.BytesIO() as urlfp:
for chunk in r.iter_content(chunk_size=8192):
urlfp.write(chunk)
urlfp.flush()
if chunk: # filter out keep-alive new chunks
sha1_hasher.update(chunk)
sha256_hasher.update(chunk)
sha512_hasher.update(chunk)
d = {
'sha1': binascii.hexlify(sha1_hasher.digest()).decode(),
'sha256': binascii.hexlify(sha256_hasher.digest()).decode(),
'sha512': binascii.hexlify(sha512_hasher.digest()).decode(),
}
with zipfile.ZipFile(urlfp) as zfp:
for name in zfp.namelist():
segments = name.rstrip('/').split('/')
for filename in (
'RELEASE.TXT',
'source.properties',
'build.prop',
'sdk.properties',
'runtime.properties',
):
if filename not in d and (
(len(segments) == 1 and segments[0] == filename)
or (len(segments) == 2 and segments[1] == filename)
):
with zfp.open(name) as fp:
d[filename] = fp.read().decode()
for entry in checksums.get(url, []):
for t in ('sha1', 'sha256', 'sha512'):
if entry.get(t) == d.get(t):
for k, v in d.items():
entry[k] = v
return # alread filled in existing, no need append to list
return d
def check_file(url, checksum_type, checksum):
print(url, checksum_type, checksum)
if url not in checksums:
checksums[url] = []
found = False
d = None
for entry in checksums[url]:
if checksum and entry.get(checksum_type) == checksum:
found = True
d = entry
break
if not found:
d = process_url(status_codes, url, checksum_type, checksum)
if d:
checksums[url].append(d)
def write_status_codes(l):
write_json(sorted(status_codes), 'status_codes.json')
def write_json(l, f):
with open(f, 'w') as fp:
json.dump(l, fp, indent=2, sort_keys=True)
def write_repository_xml(url):
while True:
try:
r = requests.get(url)
break
except Exception as e:
print(url, 'retry', e)
time.sleep(60)
status_codes.append([url, r.status_code])
write_status_codes(status_codes)
if r.status_code == 200:
path = Path(url.replace('https://dl.google.com/', ''))
path.parent.mkdir(exist_ok=True)
with open(path, 'w') as fp:
fp.write(r.text)
return r.status_code
status_codes = []
BASE_URL = 'https://dl.google.com/android/repository/'
URLS = [
'https://dl.google.com/android/repository/addon-6.xml',
'https://dl.google.com/android/repository/addon.xml',
'https://dl.google.com/android/repository/repository-10.xml',
'https://dl.google.com/android/repository/sys-img/android/sys-img.xml',
'https://dl.google.com/android/repository/sys-img/x86/addon-x86.xml',
]
for url in URLS:
write_repository_xml(url)
VERSIONED_URLS = (
'https://dl.google.com/android/repository/addon2-%s.xml',
'https://dl.google.com/android/repository/addons_list-%s.xml',
'https://dl.google.com/android/repository/repository-1%s.xml',
'https://dl.google.com/android/repository/repository2-%s.xml',
'https://dl.google.com/android/repository/sys-img/android/sys-img2-%s.xml',
'https://dl.google.com/android/repository/sys-img/android-desktop/sys-img2-%s.xml',
)
for vu in VERSIONED_URLS:
for i in range(1, 10):
if write_repository_xml(vu % i) == 404:
break
checksums_file = os.path.join(os.path.dirname(__file__), 'checksums.json')
if os.path.exists(checksums_file):
with open(checksums_file) as fp:
checksums = json.load(fp)
else:
checksums = dict()
with open('android/repository/addon.xml') as fp:
soup = BeautifulSoup(fp.read(), "xml")
for archive in soup.find_all('archive'):
filename = archive.url.string
if filename.startswith('android_m2repository_r'):
check_file(
BASE_URL + filename,
archive.checksum['type'].lower().strip(),
archive.checksum.string.lower().strip(),
)
with open('android/repository/repository-12.xml') as fp:
soup = BeautifulSoup(fp.read(), "xml")
for archive in soup.find_all('archive'):
host_os = archive.find('host-os')
host_bits = archive.find('host-bits')
if (
host_os
and host_os.string == 'linux'
and (not host_bits or host_bits.string == '64')
):
check_file(
BASE_URL + archive.url.string,
archive.checksum['type'].lower().strip(),
archive.checksum.string.lower().strip(),
)
with open('android/repository/repository2-3.xml') as fp:
soup = BeautifulSoup(fp.read(), "xml")
for remotePackage in soup.find_all('remotePackage'):
path = remotePackage.attrs.get('path', '')
path0 = path.split(';')[0]
if path0 in (
'build-tools',
'cmake',
'cmdline-tools',
'emulator',
'ndk',
'ndk-bundle',
'patcher',
'platforms',
'platform-tools',
'skiaparser',
'sources',
'tools',
):
for archive in remotePackage.find_all('archive'):
host_os = archive.find('host-os')
host_bits = archive.find('host-bits')
if (
host_os
and host_os.string == 'linux'
and (not host_bits or host_bits.string == '64')
) or (
host_os is None
and path0 in ('platforms', 'sources') # host-platform-neutral
):
check_file(
BASE_URL + archive.complete.url.string,
'sha1',
archive.complete.checksum.string.lower().strip(),
)
with open('checksums.json', 'w') as fp:
json.dump(checksums, fp, indent=2, sort_keys=True)