forked from OpenObservatory/ooni-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
356 lines (306 loc) · 10.4 KB
/
run.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import os
import json
import shutil
import hashlib
import argparse
from datetime import datetime
from glob import glob
import git
import requests
TESTING = True if os.getenv('OONI_RESOURCES_TESTING') else False
GEOIP_ASN_URL = "https://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz"
GEOIP_ASN_FILE = "working_dir/GeoIPASNum.dat.gz"
GEOIP_URL = "https://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"
GEOIP_FILE = "working_dir/GeoIP.dat.gz"
CITIZENLAB_TEST_LISTS_REPO_URL = "https://github.com/citizenlab/test-lists.git"
CITIZENLAB_TEST_LISTS_REPO = "working_dir/test-lists/"
CITIZENLAB_TEST_LISTS = "working_dir/test-lists/lists/*.csv"
BRIDGE_REACHABILITY_LISTS = "bridge_reachability/*.csv"
CWD = os.path.dirname(__file__)
MANIFEST_FILE = "assets/manifest.json"
try:
GITHUB_TOKEN = open("GITHUB_TOKEN").read().strip()
except Exception:
print("You must write your github token to a file called \"GITHUB_TOKEN\"")
raise
RESOURCES = [
{"maxmind-geoip": [GEOIP_ASN_FILE, GEOIP_FILE]},
{"citizenlab-test-lists": [CITIZENLAB_TEST_LISTS]},
{"tor-bridges": [BRIDGE_REACHABILITY_LISTS]}
]
GH_BASE_URL = "https://api.github.com/repos/OpenObservatory/ooni-resources"
if TESTING:
GH_BASE_URL = "https://api.github.com/repos/OpenObservatory/ooni-resources.testing"
REMOTE = "origin"
if TESTING:
REMOTE = "testing"
def _get_latest_release_tag():
params = {
"access_token": GITHUB_TOKEN
}
r = requests.get(GH_BASE_URL + "/releases/latest",
params=params)
return r.json()['tag_name']
def _create_latest_version():
params = {
"access_token": GITHUB_TOKEN
}
data = {
"tag_name": "latest",
"target_commitish": "master",
"name": "latest",
"body": "This tag is used to obtain the latest version of resources",
"draft": False,
"prerelease": False
}
r = requests.post(GH_BASE_URL + "/releases",
params=params, json=data)
try:
assert r.status_code == 201
except:
print r.text
raise
return r.json()["id"]
def _upload_asset(upload_url, name, content_type, data):
headers = {
"Content-Type": content_type
}
params = {
"access_token": GITHUB_TOKEN,
"name": name
}
print("Uploading asset {0}".format(params["name"]))
print("to: {0}".format(upload_url))
r = requests.post(upload_url,
params=params,
headers=headers,
data=data)
if r.status_code != 201:
raise Exception("Could not upload asset")
return r.json()
def _delete_all_assets(release_id):
params = {
"access_token": GITHUB_TOKEN,
}
r = requests.get(GH_BASE_URL + "/releases/{0}/assets".format(release_id),
params=params)
for asset in r.json():
requests.delete(GH_BASE_URL + "/releases/assets/{0}".format(asset["id"]),
params=params)
assert r.status_code/100 == 2
def update_latest_version(tag_name):
params = {
"access_token": GITHUB_TOKEN
}
r = requests.get(GH_BASE_URL + "/releases/tags/latest",
params=params)
if r.status_code == 404:
release_id = _create_latest_version()
elif r.status_code == 200:
release_id = r.json()["id"]
data = {
"target_commitish": "master"
}
r = requests.patch(GH_BASE_URL + "/releases/{0}".format(release_id),
params=params, json=data)
assert r.status_code == 200
upload_url = r.json()['upload_url'].replace("{?name,label}", "")
_delete_all_assets(release_id)
_upload_asset(upload_url, "version", "text/plain", tag_name)
def create_new_release(tag_name):
params = {
"access_token": GITHUB_TOKEN
}
data = {
"tag_name": tag_name,
"target_commitish": "master",
"name": tag_name,
"body": "Update for ooni-resources {0}".format(
datetime.now().strftime("%Y-%m-%d")
),
"draft": False,
"prerelease": False
}
r = requests.post(GH_BASE_URL + "/releases",
params=params, json=data)
try:
assert r.status_code == 201
except:
print r.text
return
j = r.json()
upload_url = j["upload_url"].replace("{?name,label}", "")
for asset in glob("assets/*"):
if asset.endswith(".csv"):
content_type = "text/csv"
elif asset.endswith(".gz"):
content_type = "application/gzip"
data = open(asset, "r").read()
_upload_asset(upload_url,
name=os.path.basename(asset),
content_type=content_type,
data=data)
update_latest_version(tag_name)
def _resolve_asset_dst(path):
"""
We write assets to their path by replacing / with .
"""
return os.path.join("assets", path.replace("/", "."))
def _resolve_path(path):
"""
Resolves a path in the manifest to a path relative to the working
directory.
"""
prepath, filename = path.split("/")
if prepath == "citizenlab-test-lists":
real_prepath = "working_dir/test-lists/lists/"
elif prepath == "maxmind-geoip":
real_prepath = "working_dir/"
elif prepath == "tor-bridges":
real_prepath = "bridge_reachability"
else:
raise Exception("Invalid prepath")
return os.path.join(real_prepath, filename)
def _list_resources():
return [
(name, filename)
for resource in RESOURCES
for name, file_globs in resource.items()
for file_glob in file_globs
for filename in glob(file_glob)
]
def download_file(url, dst):
print("Downloading %s" % url)
r = requests.get(url, stream=True)
with open(dst, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
return dst
def sha256_sum(path):
h = hashlib.sha256()
with open(path) as f:
for chunk in iter(lambda: f.read(4096), b""):
h.update(chunk)
return h.hexdigest()
def _format_resource(prepath, filepath):
filename = os.path.basename(filepath)
path = "{0}/{1}".format(prepath, filename)
country_code = "ALL"
if prepath == "citizenlab-test-lists":
cc = filename.split(".")[0]
if len(cc) == 2:
country_code = cc.upper()
sha256 = sha256_sum(filepath)
return {
"path": path,
"sha256": sha256,
"version": 0,
"country_code": country_code
}
def write_manifest(manifest):
with open(MANIFEST_FILE, "wb") as f:
json.dump(manifest, f, indent=2, sort_keys=True)
def _initialize_test_lists():
# Git clone etc.
if os.path.exists(CITIZENLAB_TEST_LISTS_REPO):
print("Repo already exists. Skipping.")
return
git.Repo.clone_from(CITIZENLAB_TEST_LISTS_REPO_URL,
CITIZENLAB_TEST_LISTS_REPO,
branch="master")
def _initialize_geoip():
if not os.path.exists(GEOIP_ASN_FILE):
download_file(GEOIP_ASN_URL, GEOIP_ASN_FILE)
if not os.path.exists(GEOIP_FILE):
download_file(GEOIP_URL, GEOIP_FILE)
def initialize(args):
resources = []
_initialize_geoip()
_initialize_test_lists()
for name, filepath in _list_resources():
resources.append(_format_resource(name, filepath))
if os.path.exists(MANIFEST_FILE):
with open(MANIFEST_FILE) as f:
manifest = json.load(f)
else:
manifest = {
"version": 1
}
manifest["resources"] = resources
copy_assets(resources)
write_manifest(manifest)
update_repo(manifest["version"])
def _update_test_lists():
repo = git.Repo(CITIZENLAB_TEST_LISTS_REPO)
repo.remotes.origin.pull()
def update_repo(version):
print("Updating the repo")
repo = git.Repo(CWD)
repo.git.add("assets/")
if repo.is_dirty():
repo.git.commit("-a", m="Automatic update")
print("Pushing changes to remote")
repo.git.push("-u", "origin", "master")
print("Creating a new release with version {0}".format(version))
create_new_release(str(version))
def copy_assets(resources):
print("Copying assets")
for resource in resources:
shutil.copy(_resolve_path(resource['path']),
_resolve_asset_dst(resource['path']))
def update(args):
print("Updating manifest")
if args.no_push:
print(" - will not push to remote")
changed = False
with open(MANIFEST_FILE) as f:
manifest = json.load(f)
_update_test_lists()
resource_paths = set(["{0}/{1}".format(name, os.path.basename(path))
for (name, path) in _list_resources()])
manifest_paths = set([resource['path']
for resource in manifest['resources']])
# Check for changed paths
for resource in manifest['resources']:
if resource['path'] not in resource_paths:
print("Removing %s from manifest" % resource['path'])
manifest['resources'].remove(resource)
changed = True
continue
new_hash = sha256_sum(_resolve_path(resource['path']))
if new_hash != resource["sha256"]:
changed = True
resource["sha256"] = new_hash
resource["version"] += 1
# Add the new resource paths
new_paths = resource_paths - manifest_paths
for new_path in new_paths:
prepath = new_path.split("/")[0]
filepath = _resolve_path(new_path)
manifest['resources'].append(_format_resource(prepath, filepath))
if changed:
manifest['version'] += 1
write_manifest(manifest)
copy_assets(manifest['resources'])
if not args.no_push:
update_repo(manifest['version'])
else:
print("No update required")
return changed
def parse_args():
if TESTING:
print("WE ARE IN TESTING")
parser = argparse.ArgumentParser(description="Handle the workflow for updating ooni resources")
subparsers = parser.add_subparsers()
parser_update = subparsers.add_parser("update")
parser_update.add_argument('--no-push', action='store_true')
parser_update.set_defaults(func=update)
parser_initialize= subparsers.add_parser("initialize")
parser_initialize.set_defaults(func=initialize)
args = parser.parse_args()
args.func(args)
def main():
parse_args()
if __name__ == "__main__":
main()