1#!/usr/bin/env python 2 3import urllib, os, os.path, sys, json, tarfile, zipfile, tempfile 4 5def download(snapshot): 6 if snapshot == 'stable': 7 # TODO: Default version/filename if not available? 8 downloads = [ 9 "https://sourceforge.net/projects/boost/files/boost/%s/%s.tar.bz2/download" % 10 (os.environ['BOOST_VERSION'], os.environ['BOOST_FILENAME'])] 11 else: 12 json_response = urllib.urlopen('https://api.bintray.com/packages/boostorg/%s/snapshot/files' % (snapshot)) 13 x = json.load(json_response) 14 15 extension_priorities = { '.bz2': 2, '.gz': 1, '.zip': 0 } 16 file_list = [] 17 version_dates = {} 18 for file in x: 19 file_extension = os.path.splitext(file['path'])[1] 20 if (file_extension in extension_priorities): 21 file['priority'] = extension_priorities[file_extension] 22 file_list.append(file) 23 if not file['version'] in version_dates or file['created'] < version_dates[file['version']]: 24 version_dates[file['version']] = file['created'] 25 file_list.sort(key=lambda x: (version_dates[x['version']], x['priority']), reverse=True) 26 downloads = ['http://dl.bintray.com/boostorg/%s/%s' % (snapshot, file['path']) for file in file_list] 27 28 filename = '' 29 for download_url in downloads: 30 try: 31 print "Downloading: " + download_url 32 (filename, headers) = urllib.urlretrieve(download_url) 33 34 print "Extracting: " + filename 35 dir = tempfile.mkdtemp() 36 extract(filename, dir) 37 os.remove(filename) 38 files = os.listdir(dir) 39 assert(len(files) == 1) 40 os.rename(os.path.join(dir, files[0]), 'boost') 41 return 42 except IOError: 43 print "Error opening URL: " + download_url 44 45def extract(filename, path = '.'): 46 if (filename.endswith(".gz")): 47 tar = tarfile.open(filename, "r:gz") 48 tar.extractall(path) 49 tar.close 50 elif (filename.endswith(".bz2")): 51 tar = tarfile.open(filename, "r:bz2") 52 tar.extractall(path) 53 tar.close 54 elif (filename.endswith(".zip")): 55 zip = zipfile.ZipFile(filename, "r") 56 zip.extractall(path) 57 zip.close 58 else: 59 assert False 60 61if len(sys.argv) == 1: 62 download('stable') 63elif len(sys.argv) == 2: 64 download(sys.argv[1]) 65else: 66 print "Usage: %s [stable|branch-name]" % (sys.argv[0]) 67