forked from mattheww/gomill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_gomill.py
174 lines (147 loc) · 5.5 KB
/
release_gomill.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
"""Package a gomill release."""
import os
import re
import shutil
import sys
from optparse import OptionParser
from subprocess import check_call, CalledProcessError, Popen, PIPE
def check_output(*args, **kwargs):
"""Version of Python 2.7's subprocess.check_output."""
process = Popen(stdout=PIPE, *args, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
raise CalledProcessError(retcode, args[0], output=output)
return output
class Failure(StandardError):
pass
def read_python_file(pathname):
dummy = {}
result = {}
f = open(pathname)
exec f in dummy, result
f.close()
return result
def is_safe_tag(s):
if s in (".", ".."):
return False
return bool(re.search(r"\A[-_.a-zA-Z0-9]+\Z", s))
def is_acceptable_version(s):
if s in (".", ".."):
return False
if len(s) > 12:
return False
return bool(re.search(r"\A[-_.a-zA-Z0-9]+\Z", s))
def export_tag(dst, repo_dir, tag):
"""Export from the git tag.
repo_dir -- git repository to export from (must contain .git)
tag -- tag to export
dst -- directory to export into
The exported tree will be placed in a directory named <tag> inside <dst>.
All files have the timestamp of the commit referred to by the tag.
"""
if not os.path.isdir(os.path.join(repo_dir, ".git")):
raise Failure("No .git repo in %s" % repo_dir)
try:
check_call("git archive --remote=%s --prefix=%s/ %s | tar -C %s -xf -" %
(repo_dir, tag, tag, dst),
shell=True)
except CalledProcessError:
raise Failure("export failed")
def get_version():
"""Obtain the gomill version from setup.py."""
try:
output = check_output("python setup.py --version".split())
except CalledProcessError:
raise Failure("'setup.py sdist' failed")
version = output.strip()
if not is_acceptable_version(version):
raise Failure("bad version: %s" % repr(version))
return version
def make_sdist(version, logfile):
"""Run 'setup.py sdist'.
cwd must be the distribution directory (gomill_setup).
Returns the pathname of the sdist tar.gz, relative to the distribution
directory.
"""
try:
check_call("python setup.py sdist".split(), stdout=logfile)
except CalledProcessError:
raise Failure("'setup.py sdist' failed")
result = os.path.join("dist", "gomill-%s.tar.gz" % version)
if not os.path.exists(result):
raise Failure("'setup.py sdist' did not create %s" % result)
return result
def make_sphinx(version, logfile, html_files_to_remove):
"""Run 'setup.py build_sphinx' and make a tarball.
cwd must be the distribution directory (gomill_setup).
Returns the pathname of the docs tar.gz, relative to the distribution
directory.
"""
try:
check_call("python setup.py build_sphinx".split(), stdout=logfile)
except CalledProcessError:
raise Failure("'setup.py build_sphinx' failed")
htmlpath = os.path.join("build", "sphinx", "html")
for filename in html_files_to_remove:
os.remove(os.path.join(htmlpath, filename))
os.rename(htmlpath, "gomill-doc-%s" % version)
try:
check_call(("tar -czf gomill-doc-%s.tar.gz gomill-doc-%s" %
(version, version)).split())
except CalledProcessError:
raise Failure("tarring up gomill-doc failed")
return "gomill-doc-%s.tar.gz" % version
def do_release(tag, config_pathname):
config_dir = os.path.abspath(os.path.dirname(config_pathname))
os.chdir(config_dir)
try:
config = read_python_file(config_pathname)
except StandardError, e:
raise Failure("error reading config file:\n%s" % e)
export_dir = os.path.join(config['working_dir'], tag)
dist_dir = os.path.join(export_dir, "gomill_setup")
if os.path.exists(export_dir):
shutil.rmtree(export_dir)
export_tag(config['working_dir'], config['repo_dir'], tag)
logfile = open(config['log_pathname'], "w")
os.chdir(dist_dir)
version = get_version()
sdist_pathname = make_sdist(version, logfile)
docs_pathname = make_sphinx(version, logfile,
config['html_files_to_remove'])
os.chdir(config_dir)
logfile.close()
sdist_dst = os.path.join(config['target_dir'],
os.path.basename(sdist_pathname))
docs_dst = os.path.join(config['target_dir'],
os.path.basename(docs_pathname))
if os.path.exists(sdist_dst):
os.remove(sdist_dst)
if os.path.exists(docs_dst):
os.remove(docs_dst)
shutil.move(os.path.join(dist_dir, sdist_pathname), sdist_dst)
shutil.move(os.path.join(dist_dir, docs_pathname), docs_dst)
shutil.rmtree(export_dir)
USAGE = """\
%(prog)s <tag>\
"""
def main(argv):
parser = OptionParser(usage=USAGE)
opts, args = parser.parse_args(argv)
if len(args) != 1:
parser.error("wrong number of arguments")
tag = args[0]
if not is_safe_tag(tag):
parser.error("ill-formed tag")
config_pathname = os.path.join(
os.path.abspath(os.path.dirname(__file__)), "release_gomill.conf")
try:
if not os.path.exists(config_pathname):
raise Failure("config file %s does not exist" % config_pathname)
do_release(tag, config_pathname)
except (EnvironmentError, Failure), e:
print >>sys.stderr, "release_gomill.py: %s" % e
sys.exit(1)
if __name__ == "__main__":
main(sys.argv[1:])