|
| 1 | +#!/usr/bin/env python2 |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import os |
| 5 | +import os.path |
| 6 | +import requests |
| 7 | +import shutil |
| 8 | +import stat |
| 9 | +import sys |
| 10 | +from subprocess import call |
| 11 | + |
| 12 | +buck_version = open(".buckversion").readline().rstrip() |
| 13 | +buck_hash = open(".buckhash").readline().rstrip() |
| 14 | +buck_pex = os.path.join("buck-out", "crazy-fun", buck_version, "buck.pex") |
| 15 | + |
| 16 | +url = "https://github.com/SeleniumHQ/buck/releases/download/buck-release-%s/buck.pex" % buck_version |
| 17 | + |
| 18 | +def download(url, out, hash): |
| 19 | + base_name = os.path.dirname(out) |
| 20 | + if not os.path.exists(base_name): |
| 21 | + try: |
| 22 | + os.makedirs(base_name) |
| 23 | + except OSError as e: |
| 24 | + if e.errno != errno.EEXIST: |
| 25 | + raise |
| 26 | + |
| 27 | + print "Downloading Buck. This may take some time" |
| 28 | + response = requests.get(url, stream=True) |
| 29 | + if response.status_code != 200: |
| 30 | + raise IOError("Unable to download buck") |
| 31 | + |
| 32 | + total_length = float(response.headers.get('content-length')) |
| 33 | + count = 0 |
| 34 | + with open(out, "wb") as f: |
| 35 | + md5 = hashlib.md5() |
| 36 | + for chunk in response.iter_content(chunk_size=4096): |
| 37 | + count += len(chunk) |
| 38 | + done = int(50 * float(count) / total_length) |
| 39 | + sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) ) |
| 40 | + sys.stdout.flush() |
| 41 | + |
| 42 | + f.write(chunk) |
| 43 | + md5.update(chunk) |
| 44 | + |
| 45 | + if md5.hexdigest() != hash: |
| 46 | + raise IOError("Downloaded buck version doesn't match expected hash") |
| 47 | + |
| 48 | +if os.path.isfile(buck_pex): |
| 49 | + md5 = hashlib.md5() |
| 50 | + with open(buck_pex, "rb") as f: |
| 51 | + for chunk in iter(lambda: f.read(4096), b""): |
| 52 | + md5.update(chunk) |
| 53 | + if md5.hexdigest() != buck_hash: |
| 54 | + print "MD5 hashes don't match. Re-downloading" |
| 55 | + download(url, buck_pex, buck_hash) |
| 56 | +else: |
| 57 | + download(url, buck_pex, buck_hash) |
| 58 | + |
| 59 | +st = os.stat(buck_pex) |
| 60 | +os.chmod(buck_pex, st.st_mode | stat.S_IEXEC) |
| 61 | + |
| 62 | +sys.argv.pop(0) |
| 63 | +args = ["python2", buck_pex] |
| 64 | +args.extend(sys.argv) |
| 65 | +call(args) |
| 66 | + |
0 commit comments