forked from numenta/htmresearch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetch_wheels.py
91 lines (66 loc) · 2.31 KB
/
fetch_wheels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import sys
import os
import requests
import boto
# This script assumes the following environment variables are set for boto:
# - AWS_ACCESS_KEY_ID
# - AWS_SECRET_ACCESS_KEY
REGION = "us-west-2"
BUCKET = "artifacts.numenta.org"
REPO = "numenta/nupic"
SHA_FILE = "nupic_sha.txt"
WHEEL_DIR = "wheelhouse"
def getScriptPath():
return os.path.dirname(os.path.realpath(sys.argv[0]))
def fetchWheel(url, localFilePath):
blockCount = 0
with open(localFilePath, "wb") as handle:
response = requests.get(url, stream=True)
if not response.ok:
raise Exception("Cannot fetch wheel from %s" % url)
for block in response.iter_content(1024):
if blockCount % 100 == 0:
sys.stdout.write('.')
sys.stdout.flush()
blockCount += 1
if not block:
break
handle.write(block)
print "\nDone."
def fetchWheels(sha):
conn = boto.connect_s3(is_secure=False)
artifactsBucket = conn.get_bucket(BUCKET)
wheelDir = os.path.join(getScriptPath(), WHEEL_DIR)
# Here's where we put the wheels!
if not os.path.exists(wheelDir):
os.makedirs(wheelDir)
wheels = artifactsBucket.list(prefix="%s/%s" % (REPO, sha))
wheelCount = sum(1 for _ in wheels)
if wheelCount is 0:
raise ValueError(
"Expected to find .whl files in %s/%s/%s, but found nothing!" % (BUCKET, REPO, sha)
)
print "Downloading %i wheels..." % wheelCount
downloaded = 0
for key in wheels:
# Fail fast.
if not key.name.endswith(".whl"):
raise ValueError("Expected .whl file, found %s" % key.name)
# Expects a key like:
# numenta/nupic/9e17ca7caef03a2b1aa925e010ee76c1a74ec3cc/setuptools-8.0.4-py2.py3-none-any.whl
print key.name
wheelName = key.name.split("/").pop()
url = "https://s3-%s.amazonaws.com/%s/%s" % (REGION, BUCKET, key.name)
# Looks like:
# https://s3-us-west-2.amazonaws.com/artifacts.numenta.org/numenta/nupic/9e17ca7caef03a2b1aa925e010ee76c1a74ec3cc/setuptools-8.0.4-py2.py3-none-any.whl
localFilePath = os.path.join(wheelDir, wheelName)
print "Fetching %s ..." % url
fetchWheel(url, localFilePath)
downloaded += 1
print "%i files to download..." % (wheelCount - downloaded)
def getSha():
with open(SHA_FILE, "r") as shaFile:
return shaFile.read().strip()
if __name__ == "__main__":
sha = getSha()
fetchWheels(sha)