-
Notifications
You must be signed in to change notification settings - Fork 2
/
make-release.py
executable file
·193 lines (157 loc) · 6.07 KB
/
make-release.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
#!/usr/bin/env python3
# coding=utf-8
#
# pylint: skip-file
"""
Copyright (c) 2019, Alexander Magola
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import sys
import os
import io
import re
import subprocess
import shutil
if sys.hexversion < 0x3060000:
raise ImportError('Python >= 3.6 is required')
here = os.path.dirname(os.path.abspath(__file__))
os.chdir(here)
import setup
#sys.path.append(os.path.join(here, setup.SRC_DIR))
from zenmake.zm import pyutils
version = setup.version
GIT_EXE = shutil.which('git')
PY_EXE = sys.executable
def _runInShell(cmd):
try:
output = subprocess.check_output(cmd, shell = True)
except subprocess.CalledProcessError as ex:
print(ex)
sys.exit(ex.returncode)
return output.decode(sys.stdout.encoding)
def _runPyScript(args, tryPy3 = True):
python = shutil.which('python3') if tryPy3 else PY_EXE
if not python:
python = PY_EXE
return _runInShell('%s %s' % (python, args))
def _runGitCmd(args):
return _runInShell('git %s' % args)
def _getAnswerYesNo(question):
while True:
answer = input(question + ' [y/n]')
if not answer or answer[0] == 'n':
return False
if answer != 'y' and answer != 'yes':
print('Invalid answer %r' % answer)
continue
return True
def _writeVersionFile(ver):
filePath = version.VERSION_FILE_PATH
with io.open(filePath, 'wt') as file:
file.write(pyutils.texttype(ver))
def _bumpVersion(ver):
output = _runGitCmd("tag")
existingVers = [x[1:] for x in output.split() if x and x[0] == 'v']
if ver in existingVers:
print("Version %r already exists in git tags. Stopped." % ver)
sys.exit(1)
_writeVersionFile(ver)
verFilePath = os.path.relpath(version.VERSION_FILE_PATH, here)
_runGitCmd("add %s" % verFilePath)
_runGitCmd("commit -m 'bump version'")
_runGitCmd("tag -a v%s -m 'version %s'" % (ver, ver))
#_runGitCmd("push")
#_runGitCmd("push --tags")
_runGitCmd("push --follow-tags")
def _writeNewDevVersion(baseVer):
parsed = version.parseVersion(baseVer)
gr = parsed.groups()
nextVer = '.'.join([gr[0], gr[1], str(int(gr[2])+1)])
nextVer += '-dev'
_writeVersionFile(nextVer)
return nextVer
def _checkChangeLog(newVer):
filePath = os.path.join(here, 'CHANGELOG.rst')
if not os.path.isfile(filePath):
print("File %r doesn't exist" % filePath)
sys.exit(1)
pattern = 'Version\s+%s\s+' % newVer
pattern = re.compile(pattern)
with io.open(filePath, 'rt') as file:
for line in file:
if pattern.search(line):
break
else:
return False
return True
def main():
""" do main work """
cmdArgs = sys.argv[1:]
if not cmdArgs:
msg = "There is no version in args. Current version: "
msg += version.current()
print(msg)
if GIT_EXE:
print("Result of 'git describe': ")
print(_runGitCmd('describe'))
msg = "\nUsage: " + sys.argv[0] + " x.y.z where x,y,z are numbers"
print(msg)
return 0
newVer = cmdArgs[0]
if not version.checkFormat(newVer):
print('Version %r has invalid format' % newVer)
return 1
if not GIT_EXE:
print("There is no 'git'. Install 'git' to use this script.")
return 2
if not _checkChangeLog(newVer):
print('There is no records for the version %r in changelog file' % newVer)
return 3
question = 'Bump version to %s?'
question += ' It will write the version to file,'
question += '\nadd it to git repo, commit it and add git tag with the version.'
answer = _getAnswerYesNo(question % newVer)
if not answer:
return 0
print("Bumping version to %r .." % newVer)
_bumpVersion(newVer)
print("Building distribution ..")
_runPyScript('setup.py clean sdist bdist_wheel')
answer = _getAnswerYesNo('Distribution was built successfully. Publish it to pypi?')
if answer:
print("Publishing distribution ..")
_runPyScript('setup.py publish')
print("Distribution was published.")
answer = _getAnswerYesNo('Publish release to github?')
if answer:
scriptPath = os.path.join('scripts', 'publish-github-release.py')
args = '%s %s' % (scriptPath, newVer)
print("Publishing release to github ..")
_runPyScript(args, tryPy3 = True)
print("Release was published on github.")
print("Writing new dev version to file %r .." % version.VERSION_FILE_PATH)
nextVer = _writeNewDevVersion(newVer)
print("New dev version %r was written to file." % nextVer)
return 0
if __name__ == '__main__':
sys.exit(main())