def main():
    parser = argparse.ArgumentParser(
        description='Utility to fetch the latest Tor Browser and upload it \
                    to popular cloud services.'
    )

    # if no OS specified, download all
    parser.add_argument('-o', '--os', default=None,
                        help='filter by OS')

    # if no LC specified, download all
    parser.add_argument('-l', '--lc', default='',
                        help='filter by locale')

    args = parser.parse_args()

    # server from which to download Tor Browser
    dist_tpo = 'https://dist.torproject.org/torbrowser/'

    # find out the latest version
    url = 'https://www.torproject.org/projects/torbrowser/RecommendedTBBVersions'
    response = urllib2.urlopen(url)
    json_response = json.load(response)
    latest_version = json_response[0]

    # find out the current version delivered by GetTor
    config = ConfigParser.RawConfigParser()
    config.read('latest_torbrowser.cfg')
    current_version = config.get('version', 'current')

    if current_version != latest_version:
        mirror = '%s%s/' % (dist_tpo, latest_version)

        # what LC should we download?
        lc_re = args.lc

        # what OS should we download?
        if args.os == 'windows':
            os_re = OS_RE['windows'] % (lc_re, lc_re)

        elif args.os == 'osx':
            os_re = OS_RE['osx'] % (lc_re, lc_re)

        elif args.os == 'linux':
            os_re = OS_RE['linux'] % (lc_re, lc_re)

        else:
            os_re = '%s.exe,%s.exe.asc,%s.dmg,%s.dmg.asc,%s.tar.xz,%s.tar'\
                    '.xz.asc' % (lc_re, lc_re, lc_re, lc_re, lc_re, lc_re)

        params = "-nH --cut-dirs=1 -L 1 --accept %s" % os_re

        # in wget we trust
        cmd = 'wget %s --mirror %s' % (params, mirror)

        print "Going to execute %s" % cmd
        # make the mirror
        # a folder with the value of 'latest_version' will be created
        os.system(cmd)
        # everything inside upload will be uploaded by the provivers' scripts
        shutil.move('latest', 'latest_backup')
        shutil.move(latest_version, 'latest')
        shutil.rmtree('latest_backup')

        # latest version of Tor Browser has been syncronized
        # let's upload it
        for provider in UPLOAD_SCRIPTS:
            os.system('python2.7 %s' % UPLOAD_SCRIPTS[provider])

        # if everything is OK, update the current version delivered by GetTor
        config.set('version', 'current', latest_version)
        with open(r'latest_torbrowser.cfg', 'wb') as config_file:
            config.write(config_file)
Script to fetch the latest Tor Browser.
dataset/ETHPy150Open TheTorProject/gettor/upload/fetch_latest_torbrowser.py/main