/
upversion.py
executable file
·136 lines (97 loc) · 3.63 KB
/
upversion.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
#!/bin/env python
# -*- coding: utf-8 -*-
import re
import click
from versio.version import Version
VERSION_RE = r"""
(?P<name>\s*{name}\s*=\s*) # NAME =
([urb]?["']) # u'
(?P<version>[^"']+) # VERSION
(["']) # '
"""
def error(message, *args, **kwargs):
raise click.ClickException(message.format(*args, **kwargs))
def compile_re(var):
return re.compile(VERSION_RE.format(name=var), re.VERBOSE)
def extract_version(path, var):
"""Parse version from file
:path: Path to the version file
:var: var name to which is assigned
"""
with open(path) as stream:
content = stream.read()
match = compile_re(var).search(content)
if match is None:
error(u'Could not find \'{} = "x.x.x"\' in file "{}"', var, path)
return match.group('version')
def write_version(path, var, version):
r = compile_re(var)
with open(path, 'r') as stream:
content = stream.read()
with open(path, 'w') as stream:
stream.write(r.subn(r'\g<1>\g<2>{}\g<4>'.format(version), content)[0])
@click.group()
@click.version_option()
def cli():
"""Handle version numbers"""
def options(function):
opts = [
click.option(u'--path', default='./setup.py', envvar=u'UPVERSION_PATH',
type=click.Path(dir_okay=False, exists=True, resolve_path=True),
show_default=True, help="Path to the file containing the "
"version (env: UPVERSION_PATH)"),
click.option(u'--var', default='version', envvar=u'UPVERSION_VAR',
show_default=True, help=u"Name of the variable holding "
u"the version string (env: UPVERSION_VAR)"),
click.option(u'-M', u'--major', is_flag=True,
help="Increase version major number M+1.m.p"),
click.option(u'-m', u'--minor', is_flag=True,
help="Increase version minor number M.m+1.p"),
click.option(u'-p', u'--revision', is_flag=True,
help="Increase version revision number M.m.p+1"),
click.option(u'-d', u'--dev', is_flag=True,
help="Increase version dev number M.m.p.dev+1"),
click.option(u'-P', u'--post', is_flag=True,
help="Increase version post number M.m.p.post+1"),
click.option(u'-F', u'--final', is_flag=True,
help="Make the version final, removing dev or post")
]
for option in opts:
function = option(function)
return function
def change_version(version, **flags):
new_version = upversion(version, **flags)
click.echo(u'From {} to {}'.format(version, new_version))
return new_version
def upversion(version, major, minor, revision, dev, post, final):
v = Version(version)
if major:
v.bump('major')
if minor:
v.bump('minor')
if revision:
v.bump('tiny')
if post:
v.bump('post')
if dev:
v.bump('dev')
if final:
v.parts = [v.parts[0], None, None, None, None]
return str(v)
def check_number_arguments(**kwargs):
if not any(kwargs.values()):
options = u' '.join(u'--' + opt for opt in kwargs)
raise click.UsageError(
u'Should specify at least one number to increase, use ' + options)
@cli.command()
@options
def view(path, var, **flags):
check_number_arguments(**flags)
change_version(extract_version(path, var), **flags)
@cli.command()
@options
def up(path, var, **flags):
check_number_arguments(**flags)
new_version = change_version(extract_version(path, var), **flags)
click.secho(u'writing "{}"'.format(path), fg='yellow')
write_version(path, var, new_version)