/
dev_release_builder.py
193 lines (153 loc) · 5.52 KB
/
dev_release_builder.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import os
import tarfile
import shutil
import sys
import urllib2
try:
import simplejson as json
except ImportError:
import json
import pip
# @@@ fix me
try:
from pip.log import Logger
except ImportError:
from pip import Logger
from pip import call_subprocess
logger = Logger([(1, sys.stdout)])
pip.logger = logger
REPOSITORIES_FILE = os.path.join(os.path.dirname(__file__), "repositories.txt")
COMPLETED_FILE = os.path.join(os.path.dirname(__file__), "completed")
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
WORK_DIR = os.path.join(DATA_DIR, "work")
DIST_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "dist"))
def format_size(bytes):
if bytes > 1000*1000:
return "%.1fMb" % (bytes/1000.0/1000)
elif bytes > 10*1000:
return "%iKb" % (bytes/1000)
elif bytes > 1000:
return "%.1fKb" % (bytes/1000.0)
else:
return "%ibytes" % bytes
def build_basename(user, repository, commit):
return "%s-%s-%s" % (user, repository, commit[:7])
def read_repositories(filename):
repos = []
for line in open(filename, "rb"):
repos.append(line.split())
return repos
def read_json_file(filename):
if not os.path.exists(filename):
return []
items = []
for line in open(filename, "rb"):
items.append(json.loads(line))
return items
def dump_json_items(filename, items):
fp = open(filename, "wb")
for item in items:
fp.write("%s\n" % json.dumps(item))
fp.close()
def find_head_github(user, repository):
url = "http://github.com/api/v2/json/repos/show/%s/%s/branches" % (user, repository)
response = urllib2.urlopen(url)
info = json.loads(response.read())
return info["branches"]["master"]
def download_tarball(user, repository, commit, show_progress=True):
"""
Given a repository name and SHA1 download and store the tarball
"""
basename = build_basename(user, repository, commit)
filename = os.path.join(DATA_DIR, "%s.tar.gz" % basename)
if not os.path.exists(filename):
url = "http://github.com/%s/%s/tarball/%s" % (user, repository, commit)
response = urllib2.urlopen(url)
try:
total_length = int(response.info()["content-length"])
except (ValueError, KeyError):
total_length = 0
logger.info("Fetching %s" % url)
logger.indent += 2
downloaded = 0
fp = open(filename, "wb")
try:
if show_progress:
if total_length:
logger.start_progress("Downloading tarball (%s): " % format_size(total_length))
else:
logger.start_progress("Downloading tarball (unknown size): ")
else:
logger.info("Downloading")
while 1:
chunk = response.read(4096)
if not chunk:
break
downloaded += len(chunk)
if show_progress:
if not total_length:
logger.show_progress("%s" % format_size(downloaded))
else:
logger.show_progress("%3i%% %s" % (100*downloaded/total_length, format_size(downloaded)))
fp.write(chunk)
fp.close()
finally:
if show_progress:
logger.end_progress("%s downloaded" % format_size(downloaded))
logger.indent -= 2
logger.info("Extracting %s" % os.path.basename(filename))
tar = tarfile.open(filename)
tar.extractall(path=WORK_DIR)
def build_release(dist_dir, user, repository, commit):
basename = build_basename(user, repository, commit)
source_dir = os.path.join(WORK_DIR, basename)
cmd = [
sys.executable,
"setup.py", "sdist",
"-d", dist_dir,
]
call_subprocess(cmd,
cwd = source_dir,
show_stdout = False,
command_desc = "python setup.py sdist",
)
def run(data_dir, work_dir, repositories_file, completed_file, dist_dir):
if not os.path.exists(data_dir):
os.makedirs(data_dir)
if os.path.exists(work_dir):
shutil.rmtree(work_dir)
os.makedirs(work_dir)
repositories = read_repositories(repositories_file)
commits = []
for kind, user, repository in repositories:
if kind == "github":
head = find_head_github(user, repository)
commits.append((user, repository, head))
completed = read_json_file(completed_file)
completed_cache = dict([(c, True) for u, r, c in completed])
try:
for user, repository, commit in commits:
current = "%s/%s (%s)" % (user, repository, commit[:7])
if commit in completed_cache:
logger.info("Skipping %s" % current)
continue
logger.info("Handling %s" % current)
logger.indent += 2
download_tarball(user, repository, commit)
build_release(dist_dir, user, repository, commit)
completed.append((user, repository, commit))
logger.indent -= 2
finally:
if os.path.exists(work_dir):
shutil.rmtree(work_dir)
dump_json_items(completed_file, completed)
def main():
# @@@ optparse
data_dir = DATA_DIR
work_dir = WORK_DIR
repositories_file = REPOSITORIES_FILE
completed_file = COMPLETED_FILE
dist_dir = DIST_DIR
run(data_dir, work_dir, repositories_file, completed_file, dist_dir)
if __name__ == "__main__":
main()