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

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

5
6
7
# Before running, place the authentication token for a user with push access to the repository
# in an environment variable 'GITHUB_AUTH'

8
9
10
11
12
13
from github import Github

import sys
import json
import urllib
import subprocess
14
import os
15
16
17

REPO_NAME = "TheTorProject/gettorbrowser"

18
19
failure = False

Cecylia Bocovich's avatar
Cecylia Bocovich committed
20
21
failed_uploads = []

22
23
def delete_old_releases(repo):
    for release in repo.get_releases():
Cecylia Bocovich's avatar
Cecylia Bocovich committed
24
25
        for asset in release.get_assets():
            asset.delete_asset()
26
27
28
29
30
31
32
33
34
35
36
        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]
37
38
39
                try:
                    subprocess.check_call(["/usr/bin/wget", "--quiet", url])
                    release.upload_asset(filename)
40
                    os.remove(filename)
41
                except:
Cecylia Bocovich's avatar
Cecylia Bocovich committed
42
43
44
45
46
47
48
49
                    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)
50
            os.remove(filename)
Cecylia Bocovich's avatar
Cecylia Bocovich committed
51
52
53
        except:
            print("Error: failed to download "+url+". Please upload this file manually.")
            failure = True
54
55
56
57
58
59
60
61
62
63
64
65
66
67


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)

68
69
70
    if failure:
        sys.exit(1)

71
72

if __name__ == "__main__":
73
74
75
76
    if 'GITHUB_AUTH' not in os.environ:
        print("Usage: {}".format(sys.argv[0]), file=sys.stderr)
        print("\nThe authentication token for github should be placed in the environment"
                "variable 'GITHUB_AUTH'", file=sys.stderr)
77
        sys.exit(1)
78
    token = os.environ['GITHUB_AUTH']
79
    sys.exit(main(token))