-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathrelease_sgfmill.py
214 lines (178 loc) · 6.58 KB
/
release_sgfmill.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""Package a sgfmill release.
Requires a release_sgfmill.conf file, eg:
«
# All paths are relative to this config file
repo_dir = "."
working_dir = "./tmp/release"
log_pathname = "./tmp/release/release.log"
target_dir = "./tmp/release"
html_files_to_remove = [
".buildinfo",
]
»
"""
import os
import re
import shutil
import sys
from optparse import OptionParser
from subprocess import check_call, check_output, CalledProcessError, Popen, PIPE
class Failure(Exception):
pass
def read_python_file(pathname):
dummy = {}
result = {}
with open(pathname, 'rb') as f:
bb = f.read()
exec(bb, dummy, result)
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 prepare_dir():
"""Make any required changes in the exported distribution directory.
cwd must be the distribution directory (the project root).
"""
os.rename("README.sdist.txt", "README.txt")
os.remove("README.rst")
def get_version():
"""Obtain the sgfmill version from setup.py."""
try:
output = check_output("python setup.py --version".split(),
universal_newlines=True)
except CalledProcessError:
raise Failure("'setup.py --version' 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 (the project root).
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", "sgfmill-%s.tar.gz" % version)
if not os.path.exists(result):
raise Failure("'setup.py sdist' did not create %s" % result)
return result
def make_wheel(version, logfile):
"""Run 'setup.py bdist_wheel'.
cwd must be the distribution directory (the project root).
Returns the pathname of the sdist tar.gz, relative to the distribution
directory.
"""
try:
check_call("python setup.py bdist_wheel".split(), stdout=logfile)
except CalledProcessError:
raise Failure("'setup.py bdist_wheel' failed")
result = os.path.join("dist", "sgfmill-%s-py3-none-any.whl" % version)
if not os.path.exists(result):
raise Failure("'setup.py bdist_wheel' did not create %s" % result)
return result
def make_sphinx(version, logfile, html_files_to_remove):
"""Run build_docs and make a tarball.
cwd must be the distribution directory (the project root).
Returns the pathname of the docs tar.gz, relative to the distribution
directory.
"""
try:
check_call(["./build_docs"], stdout=logfile)
except CalledProcessError:
raise Failure("'build_docs' failed")
htmlpath = os.path.join("doc", "_build", "html")
for filename in html_files_to_remove:
os.remove(os.path.join(htmlpath, filename))
os.rename(htmlpath, "sgfmill-doc-%s" % version)
try:
check_call(("tar -czf sgfmill-doc-%s.tar.gz sgfmill-doc-%s" %
(version, version)).split())
except CalledProcessError:
raise Failure("tarring up sgfmill-doc failed")
return "sgfmill-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 Exception as e:
raise Failure("error reading config file:\n%s" % e)
export_dir = os.path.join(config['working_dir'], tag)
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(export_dir)
prepare_dir()
version = get_version()
sdist_pathname = make_sdist(version, logfile)
wheel_pathname = make_wheel(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))
wheel_dst = os.path.join(config['target_dir'],
os.path.basename(wheel_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(wheel_dst):
os.remove(wheel_dst)
if os.path.exists(docs_dst):
os.remove(docs_dst)
shutil.move(os.path.join(export_dir, sdist_pathname), sdist_dst)
shutil.move(os.path.join(export_dir, wheel_pathname), wheel_dst)
shutil.move(os.path.join(export_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_sgfmill.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 (OSError, Failure) as e:
print("release_sgfmill.py: %s" % e, file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main(sys.argv[1:])