forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbump_versions.py
More file actions
126 lines (109 loc) · 4.18 KB
/
bump_versions.py
File metadata and controls
126 lines (109 loc) · 4.18 KB
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
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Use case: Bump to the next patch release version in preparation for a release
#
# python utilities/bump_versions.py next_release patch
#
# Use case: Bump to the next minor release version in preparation for a release
#
# python utilities/bump_versions.py next_release minor
#
# Use case: Bump to the next snapshot version in preparation for new development
#
# python utilities/bump_versions.py next_snapshot patch
#
# This script will update versions.txt in place.
# The script must be run at the root of google-cloud-java.
import argparse
import copy
import re
version_regex = re.compile(r'(\d+)\.(\d+)\.(\d+)(-\w+)?(-\w+)?')
class Version:
def __init__(self, version_str):
match = version_regex.match(version_str)
major = int(match.group(1))
minor = int(match.group(2))
patch = int(match.group(3))
qualifier1 = match.group(4)
qualifier2 = match.group(5)
variant = ''
snapshot = False
if qualifier1 and qualifier2:
if qualifier2 == '-SNAPSHOT':
variant = qualifier1
snapshot = True
else:
variant = qualifier1 + qualifier2
elif qualifier1:
if qualifier1 == '-SNAPSHOT':
snapshot = True
else:
variant = qualifier1
self.major = major
self.minor = minor
self.patch = patch
self.variant = variant
self.snapshot = snapshot
def bump(self, bump_type):
if bump_type == 'minor':
self.bump_minor()
elif bump_type == 'patch':
self.bump_patch()
else:
raise ValueError('invalid bump_type: {}'.format(bump_type))
def bump_minor(self):
self.minor += 1
self.patch = 0
def bump_patch(self):
self.patch += 1
def set_snapshot(self, snapshot):
self.snapshot = snapshot
def __str__(self):
mmp = '{}.{}.{}'.format(self.major, self.minor, self.patch)
postfix = self.variant
if self.snapshot:
postfix += '-SNAPSHOT'
return mmp + postfix
def bump_versions(next_version_type, bump_type):
newlines = []
with open('versions.txt') as f:
for line in f:
version_line = line.strip()
if not version_line or version_line.startswith('#'):
newlines.append(line)
continue
(module, released_version_str, current_version_str) = version_line.split(':')
released_version = Version(released_version_str)
if next_version_type == 'next_release':
released_version.bump(bump_type)
released_version.set_snapshot(False)
current_version = copy.deepcopy(released_version)
elif next_version_type == 'next_snapshot':
current_version = copy.deepcopy(released_version)
current_version.bump(bump_type)
current_version.set_snapshot(True)
else:
raise ValueError('invalid next_version_type: {}'.format(next_version_type))
newlines.append('{}:{}:{}\n'.format(module, released_version, current_version))
with open('versions.txt', 'w') as f:
for line in newlines:
f.write(line)
def main():
parser = argparse.ArgumentParser(description='Add snippets to Javadoc.')
parser.add_argument('next_version_type', help='Either next_snapshot or next_release')
parser.add_argument('bump_type', help='Either minor or patch')
args = parser.parse_args()
bump_versions(args.next_version_type, args.bump_type)
if __name__ == '__main__':
main()