-
Notifications
You must be signed in to change notification settings - Fork 86
/
setversion.py
executable file
·143 lines (122 loc) · 4.84 KB
/
setversion.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
#!/usr/bin/env python
# CbAPI Project Version Number Setting Script
# AGRB 11/7/2019
import sys
import os
import re
import argparse
from datetime import date
def readme_rewriter(line, ctxt):
expr = ctxt.get("readme_expr", None)
if not expr:
expr = re.compile(r"^\*\*Latest Version:")
ctxt["readme_expr"] = expr
if expr.match(line):
return "**Latest Version: {0}**\n".format(ctxt["version"])
return None
def changelog_rewriter(line, ctxt):
expr = ctxt.get("changelog_expr", None)
if not expr:
expr = re.compile(r"^\.\. top-of-changelog")
ctxt["changelog_expr"] = expr
if expr.match(line):
datestr = date.today().strftime("%B %d, %Y")
cl1 = "CbAPI {0} - Released {1}".format(ctxt["version"], datestr)
cl2 = "".ljust(len(cl1), "-")
updates = "\n\nUpdates\n\n.. (add your updates here)\n"
return line + "\n" + cl1 + "\n" + cl2 + updates
return None
def doc_conf_rewriter(line, ctxt):
vexpr = ctxt.get("doc_version_expr", None)
if not vexpr:
vexpr = re.compile(r"^version = ")
ctxt["doc_version_expr"] = vexpr
rexpr = ctxt.get("doc_release_expr", None)
if not rexpr:
rexpr = re.compile(r"^release = ")
ctxt["doc_release_expr"] = rexpr
if vexpr.match(line):
t = re.match(r"^(\d+\.\d+)\.", ctxt["version"])
vn = ctxt["version"]
if t:
vn = t.group(1)
return "version = u'{0}'\n".format(vn)
if rexpr.match(line):
return "release = u'{0}'\n".format(ctxt["version"])
return None
def setup_rewriter(line, ctxt):
expr = ctxt.get("setup_expr", None)
if not expr:
expr = re.compile(r"^(\s*)version=")
ctxt["setup_expr"] = expr
m = expr.match(line)
if m:
return "{0}version='{1}',\n".format(m.group(1), ctxt["version"])
return None
def init_rewriter(line, ctxt):
expr = ctxt.get("init_expr", None)
if not expr:
expr = re.compile(r"^__version__ = ")
ctxt["init_expr"] = expr
if expr.match(line):
return "__version__ = '{0}'\n".format(ctxt["version"])
return None
def rewrite_file(infilename, rewritefunc, ctxt):
outfilename = infilename + ".new"
if not ctxt["accept"]:
infile = open(infilename, "r")
outfile = open(outfilename, "w")
try:
s = infile.readline()
while s:
s2 = rewritefunc(s, ctxt)
if s2:
outfile.write(s2)
else:
outfile.write(s)
s = infile.readline()
finally:
infile.close()
outfile.close()
else:
if not os.access(outfilename, os.F_OK):
print("warning: new file {0} does not exist to be renamed".format(outfilename))
return
if not ctxt["nodelete"]:
if ctxt["backup"]:
os.rename(infilename, infilename + ".bak")
else:
os.remove(infilename)
os.rename(outfilename, infilename)
def main():
parser = argparse.ArgumentParser(description="Set the version number in CbAPI source and documentation.\n"
"Execute this on a release or hotfix branch to update "
"the version numbers in the source.",
epilog="After running, edit docs/changelog.rst and add the new changelog "
"information under the new heading.")
parser.add_argument("version", help="New version number to add, or \"accept\" to accept changes to .new files")
parser.add_argument("-n", "--nodelete", action="store_true",
help="Do not delete existing files, leave new files with .new extension")
parser.add_argument("-b", "--backup", action="store_true",
help="Keep old versions of files around with a .bak extension")
args = parser.parse_args()
is_accept = False
if args.version == "accept":
if args.nodelete:
print("cannot specify --nodelete with accept command")
return 1
is_accept = True
else:
vnexpr = re.compile(r"^[1-9]\d*\.\d+\.\d+$")
if not vnexpr.match(args.version):
print("Invalid version number {0}: must be three numeric values separated by dots\n".format(args.version))
return 1
ctxt = {"version": args.version, "nodelete": args.nodelete, "backup": args.backup, "accept": is_accept}
rewrite_file("README.md", readme_rewriter, ctxt)
rewrite_file("docs/changelog.rst", changelog_rewriter, ctxt)
rewrite_file("docs/conf.py", doc_conf_rewriter, ctxt)
rewrite_file("setup.py", setup_rewriter, ctxt)
rewrite_file("src/cbapi/__init__.py", init_rewriter, ctxt)
return 0
if __name__ == "__main__":
sys.exit(main())