-
Notifications
You must be signed in to change notification settings - Fork 15
/
version
executable file
·239 lines (183 loc) · 7.04 KB
/
version
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python
"""
Script for creating new releases
"""
import ast
import re
from subprocess import call as _call
from functools import reduce
import datetime
import click
TESTING = False
PACKAGE = 'src/yass'
PACKAGE_NAME = 'YASS'
def replace_in_file(path_to_file, original, replacement):
"""Replace string in file
"""
with open(path_to_file, 'r+') as f:
content = f.read()
updated = content.replace(original, replacement)
f.seek(0)
f.write(updated)
f.truncate()
def read_file(path_to_file):
with open(path_to_file, 'r') as f:
content = f.read()
return content
def call(*args, **kwargs):
"""Mocks call function for testing
"""
if TESTING:
print(args, kwargs)
return 0
else:
return _call(*args, **kwargs)
class Versioner(object):
"""Utility functions to manage versions
"""
@classmethod
def current_version(cls):
"""Returns the current version in __init__.py
"""
_version_re = re.compile(r'__version__\s+=\s+(.*)')
with open('{package}/__init__.py'.format(package=PACKAGE), 'rb') as f:
VERSION = str(ast.literal_eval(_version_re.search(
f.read().decode('utf-8')).group(1)))
return VERSION
@classmethod
def release_version(cls):
"""
Returns a release version number
e.g. 2.4.4dev -> v.2.2.4
"""
current = cls.current_version()
if 'dev' not in current:
raise ValueError('Current version is not a dev version')
return current.replace('dev', '')
@classmethod
def bump_up_version(cls):
"""
Gets gets a release version and returns a the next value value.
e.g. 1.2.5 -> 1.2.6dev
"""
# Get current version
current = cls.current_version()
if 'dev' in current:
raise ValueError('Current version is dev version, new dev '
'versions can only be made from release versions')
# Get Z from X.Y.Z and sum 1
new_subversion = int(current.split('.')[-1]) + 1
# Replace new_subversion in current version
elements = current.split('.')
elements[-1] = new_subversion
new_version = reduce(lambda x, y: str(x)+'.'+str(y), elements)+'dev'
return new_version
@classmethod
def commit_version(cls, new_version, tag=False):
"""
Replaces version in __init__ and optionally creates a tag in the git
repository (also saves a commit)
"""
current = cls.current_version()
# replace new version in __init__.py
replace_in_file('{package}/__init__.py'.format(package=PACKAGE),
current, new_version)
# Create tag
if tag:
# Run git add and git status
click.echo('Adding new changes to the repository...')
call(['git', 'add', '--all'])
call(['git', 'status'])
# Commit repo with updated dev version
click.echo('Creating new commit release version...')
msg = 'Release {}'.format(new_version)
call(['git', 'commit', '-m', msg])
click.echo('Creating tag {}...'.format(new_version))
message = '{} release {}'.format(PACKAGE_NAME, new_version)
call(['git', 'tag', '-a', new_version, '-m', message])
click.echo('Pushing tags...')
call(['git', 'push', 'origin', new_version])
@classmethod
def update_changelog_release(cls, new_version):
current = cls.current_version()
# update CHANGELOG header
header_current = '{ver}\n'.format(ver=current)+'-'*len(current)
today = datetime.datetime.now().strftime('%Y-%m-%d')
header_new = '{ver} ({today})\n'.format(ver=new_version, today=today)
header_new = header_new+'-'*len(header_new)
replace_in_file('CHANGELOG.rst', header_current, header_new)
@classmethod
def add_changelog_dev_section(cls, dev_version):
# add new CHANGELOG section
start_current = 'Changelog\n========='
start_new = (('Changelog\n=========\n\n{dev_version}\n'
.format(dev_version=dev_version)
+ '-' * len(dev_version)) + '\n')
replace_in_file('CHANGELOG.rst', start_current, start_new)
@click.group()
def cli():
pass
@cli.command(help='Sets a new version for the project')
def new():
"""
Create a new version for the project: updates __init__.py, CHANGELOG,
creates new commit for released version (creating a tag) and commits
to a new dev version
"""
current = Versioner.current_version()
release = Versioner.release_version()
release = click.prompt('Current version in app.yaml is {current}. Enter'
' release version'.format(current=current,
release=release),
default=release, type=str)
Versioner.update_changelog_release(release)
changelog = read_file('CHANGELOG.rst')
click.confirm('\nCHANGELOG.rst:\n\n{}\n Continue?'.format(changelog),
'done', abort=True)
# Replace version number and create tag
click.echo('Commiting release version: {}'.format(release))
Versioner.commit_version(release, tag=True)
# Create a new dev version and save it
bumped_version = Versioner.bump_up_version()
click.echo('Creating new section in CHANGELOG...')
Versioner.add_changelog_dev_section(bumped_version)
click.echo('Commiting dev version: {}'.format(bumped_version))
Versioner.commit_version(bumped_version)
# Run git add and git status
click.echo('Adding new changes to the repository...')
call(['git', 'add', '--all'])
call(['git', 'status'])
# Commit repo with updated dev version
click.echo('Creating new commit with new dev version...')
msg = 'Bumps up project to version {}'.format(bumped_version)
call(['git', 'commit', '-m', msg])
click.echo('Version {} was created, you are now in {}'
.format(release, bumped_version))
@cli.command(help='Merges changes in dev with master')
def tomaster():
"""
Merges dev with master and pushes
"""
click.echo('Checking out master...')
call(['git', 'checkout', 'master'])
click.echo('Merging master with dev...')
call(['git', 'merge', 'dev'])
click.echo('Pushing changes...')
call(['git', 'push'])
@cli.command(help='Publishes to PyPI')
@click.argument('tag')
@click.option('--production', is_flag=True)
def release(tag, production):
"""
Merges dev with master and pushes
"""
click.echo('Checking out tag {}'.format(tag))
call(['git', 'checkout', tag])
current = Versioner.current_version()
click.confirm('Version in {} tag is {}. Do you want to continue?'
.format(tag, current))
click.echo('Publishing to PyPI...')
where = 'pypitest' if not production else 'pypi'
call(['python', 'setup.py', 'sdist', 'upload', '-r', where])
if __name__ == '__main__':
cli()