verify-tor.py 2.42 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python3
import os
import sys
from subprocess import check_call, CalledProcessError

Torsten Grote's avatar
Torsten Grote committed
6 7
from utils import REPO_DIR, get_sha256, fail, get_build_versions, get_final_file_name, \
    get_version, get_tor_version
8 9 10 11 12 13


def main():
    # get Tor version from command or show usage information
    version = get_version()

14 15 16 17 18 19 20
    if verify(version, for_android=False) and verify(version, for_android=True):
        sys.exit(0)
    else:
        sys.exit(1)


def verify(version, for_android):
21 22 23 24
    # get Tor version and versions of its dependencies
    versions = get_build_versions(version)

    # download reference binary
25
    file_name = get_final_file_name(versions, for_android)
26 27
    try:
        # try downloading from jcenter
28
        check_call(['wget', '--no-verbose', get_url(versions, for_android), '-O', file_name])
29 30 31
    except CalledProcessError:
        # try fallback to bintray
        print("Warning: Download from jcenter failed. Trying bintray directly...")
32 33
        check_call(['wget', '--no-verbose', get_url(versions, for_android, fallback=True), '-O',
                    file_name])
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

    # check if Tor was already build
    build_file_name = os.path.join(REPO_DIR, file_name)
    if not os.path.isfile(build_file_name):
        # build Tor
        if version is None:
            check_call(['./build-tor.py'])
        else:
            check_call(['./build-tor.py', version])

    # calculate hashes for both files
    reference_hash = get_sha256(file_name)
    build_hash = get_sha256(build_file_name)
    print("Reference sha256: %s" % reference_hash)
    print("Build sha256:     %s" % build_hash)

    # compare hashes
51
    suffix = " for Android" if for_android else ""
52
    if reference_hash == build_hash:
53 54
        print("Tor%s version %s was successfully verified! \o/" % (suffix, versions['tor']))
        return True
55
    else:
56 57
        print("Hashes for Tor%s version %s do not match! :(" % (suffix, versions['tor']))
        return False
58 59


60
def get_url(versions, for_android, fallback=False):
61
    version = get_tor_version(versions)
62 63
    directory = "tor-android" if for_android else "tor"
    file = get_final_file_name(versions, for_android)
64
    if not fallback:
65
        return "https://jcenter.bintray.com/org/briarproject/%s/%s/%s" % (directory, version, file)
66
    else:
67 68
        return "https://dl.bintray.com/briarproject/org.briarproject/org/briarproject/%s/%s/%s" % \
               (directory, version, file)
69 70 71 72


if __name__ == "__main__":
    main()