This repository was archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbump_version.py
More file actions
179 lines (139 loc) · 5.73 KB
/
Copy pathbump_version.py
File metadata and controls
179 lines (139 loc) · 5.73 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
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
# -*- coding: utf-8 -*-
"""
Set new version tag, using git command line interface.
Git command 'git describe --tag' is used to get the current version (before setting new). The tags are like '4.1.0' or
'4.1.0-1-g82869e0'.
"""
import argparse
import os
import sys
import textwrap
from pkg_resources import get_distribution
def get_version_setuptools(package="omnia_timeseries_sdk", return_dev=False):
# get version (will raise DistributionNotFound error if package is not found/installed)
version_string = get_distribution(package).version
version = version_string.split(".", maxsplit=2)
assert len(version) == 3, f"Not able to interpret version string: {version_string}"
# extract major, minor, micro
major, minor, micro = version
# interpret/correct micro
if "-" in micro:
# dev info included in micro
micro, dev = micro.split(".", maxsplit=1)
else:
# pure major.minor.micro version, no dev part of tag
dev = ""
if return_dev:
return major, minor, micro, dev
else:
return major, minor, micro
def get_version_git(return_dev=False):
# get version (will raise DistributionNotFound error if package is not found/installed)
version_string = os.popen("git describe --tag").read().strip()
if version_string.startswith("fatal"):
# git failed, probably because not invoked at root of a git repo (.git not found)
raise Exception("Not able to extract version using git")
version = version_string.split(".", maxsplit=2)
assert len(version) == 3, f"Not able to interpret version string: {version_string}"
# extract major, minor, micro
major, minor, micro = version
# interpret/correct micro
if "-" in micro:
# dev info included in micro
micro, dev = micro.split("-", maxsplit=1)
else:
# pure major.minor.micro version, no dev part of tag
dev = ""
if return_dev:
return major, minor, micro, dev
else:
return major, minor, micro
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
This function is an adjusted copy of: https://stackoverflow.com/a/3041990
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
assert default is None or default in valid, f"Invalid default option: {default}"
if default is None:
prompt = " (y/n) "
elif default == "yes":
prompt = " ([y]/n) "
elif default == "no":
prompt = " (y/[n]) "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def construct_version_string(major, minor, micro, dev=None):
"""
Construct version tag: "major.minor.micro" (or if 'dev' is specified: "major.minor.micro-dev").
"""
version_tag = f"{major}.{minor}.{micro}"
if dev is not None:
version_tag += f"-{dev}"
return version_tag
def main():
parser = argparse.ArgumentParser(
description="Set new version tag using git command line interface. The tag is set by augmenting either "
"'major', 'minor' or 'micro' (specified by user) by 1.",
)
parser.add_argument("type", choices=("major", "minor", "micro"),
help="Which part of version tag to augment by one.")
parser.add_argument("-m", "--message", default="", help="Commit message to include. Default is empty string")
args = parser.parse_args()
# extract current version tag
major, minor, micro, dev = get_version_git(return_dev=True)
current_version = construct_version_string(major, minor, micro, dev=dev)
# determine new version
if args.type == "major":
# augment major by 1, reset minor and micro
major = str(int(major) + 1)
minor = "0"
micro = "0"
elif args.type == "minor":
# augment minor by 1, reset minor
minor = str(int(minor) + 1)
micro = "0"
else:
# augment micro by 1
micro = str(int(micro) + 1)
# finally, reset dev in any case
dev = None
# construct new version tag
new_version = construct_version_string(major, minor, micro, dev=dev)
# ask user whether to conduct version tag update
info_string = textwrap.dedent(f'''
Current version tag : {current_version}
New version tag : {new_version}
Commit message : {args.message}
Set to new version tag? ''')
set_new_tag = query_yes_no(info_string, default="no") # bool
# act according to answer from user
if set_new_tag:
# ref: https://git-scm.com/book/en/v2/Git-Basics-Tagging
_ = os.popen(f'git tag -a {new_version} -m "{args.message}"').read()
sys.stdout.write(textwrap.dedent(f'''
New version tag ({new_version}) has been set.
Verify the new tag by the following git command:
git describe --tag
To reverse (delete) the new tag, use the following git command:
git tag -d {new_version}
'''))
else:
sys.stdout.write("\nNew tag has NOT been set.")
if __name__ == "__main__":
main()