Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Add wrapper for buck so it can be executed easily
This downloads the same version of buck as "./go" does so the two will play nicely together and share the same cache (and only download buck once) The buckw script takes the same arguments as buck itself.
- Loading branch information
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python2 | ||
|
||
import hashlib | ||
import os | ||
import os.path | ||
import requests | ||
import shutil | ||
import stat | ||
import sys | ||
from subprocess import call | ||
|
||
buck_version = open(".buckversion").readline().rstrip() | ||
buck_hash = open(".buckhash").readline().rstrip() | ||
buck_pex = os.path.join("buck-out", "crazy-fun", buck_version, "buck.pex") | ||
|
||
url = "https://github.com/SeleniumHQ/buck/releases/download/buck-release-%s/buck.pex" % buck_version | ||
|
||
def download(url, out, hash): | ||
base_name = os.path.dirname(out) | ||
if not os.path.exists(base_name): | ||
try: | ||
os.makedirs(base_name) | ||
except OSError as e: | ||
if e.errno != errno.EEXIST: | ||
raise | ||
|
||
print "Downloading Buck. This may take some time" | ||
response = requests.get(url, stream=True) | ||
if response.status_code != 200: | ||
raise IOError("Unable to download buck") | ||
|
||
total_length = float(response.headers.get('content-length')) | ||
count = 0 | ||
with open(out, "wb") as f: | ||
md5 = hashlib.md5() | ||
for chunk in response.iter_content(chunk_size=4096): | ||
count += len(chunk) | ||
done = int(50 * float(count) / total_length) | ||
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) ) | ||
sys.stdout.flush() | ||
|
||
f.write(chunk) | ||
md5.update(chunk) | ||
|
||
if md5.hexdigest() != hash: | ||
raise IOError("Downloaded buck version doesn't match expected hash") | ||
|
||
if os.path.isfile(buck_pex): | ||
md5 = hashlib.md5() | ||
with open(buck_pex, "rb") as f: | ||
for chunk in iter(lambda: f.read(4096), b""): | ||
md5.update(chunk) | ||
if md5.hexdigest() != buck_hash: | ||
print "MD5 hashes don't match. Re-downloading" | ||
download(url, buck_pex, buck_hash) | ||
else: | ||
download(url, buck_pex, buck_hash) | ||
|
||
st = os.stat(buck_pex) | ||
os.chmod(buck_pex, st.st_mode | stat.S_IEXEC) | ||
|
||
sys.argv.pop(0) | ||
args = ["python2", buck_pex] | ||
args.extend(sys.argv) | ||
call(args) | ||
|