update_files 2.21 KB
Newer Older
1
2
#!/usr/bin/env python3

Cecylia Bocovich's avatar
Cecylia Bocovich committed
3
4
#pip3 install PyGithub

5
6
7
8
9
10
11
12
13
from github import Github

import sys
import json
import urllib
import subprocess

REPO_NAME = "TheTorProject/gettorbrowser"

14
15
failure = False

Cecylia Bocovich's avatar
Cecylia Bocovich committed
16
17
failed_uploads = []

18
19
def delete_old_releases(repo):
    for release in repo.get_releases():
Cecylia Bocovich's avatar
Cecylia Bocovich committed
20
21
        for asset in release.get_assets():
            asset.delete_asset()
22
23
24
25
26
27
28
29
30
31
32
        release.delete_release()

#Download list of tor browser releases and upload them to github
def upload_files(release):
    url = urllib.request.urlopen("https://aus1.torproject.org/torbrowser/update_3/release/downloads.json")
    data = json.loads(url.read().decode())
    for arch in data['downloads']:
        for locale in data['downloads'][arch]:
            for asset in data['downloads'][arch][locale]:
                url = data['downloads'][arch][locale][asset]
                filename = url.split('/')[-1]
33
34
35
36
                try:
                    subprocess.check_call(["/usr/bin/wget", "--quiet", url])
                    release.upload_asset(filename)
                except:
Cecylia Bocovich's avatar
Cecylia Bocovich committed
37
38
39
40
41
42
43
44
45
46
47
                    print("Error: failed to download "+url+". Will retry later.")
                    failed_uploads.append(url)
    #Retry failed uploads
    for url in failed_uploads:
        filename = url.split('/')[-1]
        try:
            subprocess.check_call(["/usr/bin/wget", "--quiet", url])
            release.upload_asset(filename)
        except:
            print("Error: failed to download "+url+". Please upload this file manually.")
            failure = True
48
49
50
51
52
53
54
55
56
57
58
59
60
61


def main(token):

    #Initialize a new release
    g = Github(token)
    repo = g.get_repo(REPO_NAME)

    delete_old_releases(repo)

    #Create a new release
    release = repo.create_git_release("torbrowser-release", "Tor Browser releases", "These releases were uploaded to be distributed with gettor.")
    upload_files(release)

62
63
64
    if failure:
        sys.exit(1)

65
66
67
68
69
70
71
72
73
74

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: {} AUTH_TOKEN".format(sys.argv[0]), file=sys.stderr)
        print("\nAUTH_TOKEN should be an authentication token for a user"
              "with access to the gettor repository.", file=sys.stderr)
        sys.exit(1)
    token = sys.argv[1]

    sys.exit(main(token))