-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion_tag
More file actions
executable file
·96 lines (78 loc) · 3.3 KB
/
version_tag
File metadata and controls
executable file
·96 lines (78 loc) · 3.3 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
#!/usr/bin/env python
import os, subprocess, argparse, sys
def parseVersion(w):
ns = [int(v) for v in w.split(".")]
return (ns[0], ns[1], ns[2])
def mkVersion(major, feature, fix):
return ".".join([str(n) for n in [major, feature, fix]])
parser = argparse.ArgumentParser(description="version_tag - Script to git tag python package versions and automatically update setup.py/pyproject.toml")
parser.add_argument("command", type=str, default="", help="What version number to increase. pMay be 'fix', 'feature', or 'major'. This adheres to the semantic versioning scheme of major.feature.fix.")
parser.add_argument("-m", "--message", type=str, default="", help="Git message to add to the created tag.")
parser.add_argument("-f", "--file", type=str, default="", help="setup.py file to look for an replace version string. Default is to try first pyrpoject.toml, then setup.py, then fail.")
parser.add_argument("-n", "--needle", type=str, default="version", help="String to look for in the setup.py Expects a '=0.3.1' or similar to follow it.")
args = parser.parse_args()
if args.file == "":
candidates = "pyproject.toml setup.py".split(" ")
for f in candidates:
if os.path.isfile(os.getcwd() + "/" + f):
args.file = f
break
if not(os.path.isfile(args.file)):
print("Could not open " + args.file)
quit()
lines = open(args.file, "r").read().split("\n")
for i in range(0, len(lines)):
line = lines[i]
if line.startswith("name"):
ws = line.split("=")
packagename = eval(ws[1].strip())
if args.needle in line:
ws = line.split("=")
versionstring = eval(ws[1].strip())
# breaking here is important as we use the i later
break
if i == len(lines)-1:
print("error: Couldn't find needle.")
quit()
(major, feature, fix) = parseVersion(versionstring)
if args.command == "major":
newversion = mkVersion(major+1, 0, 0)
elif args.command == "feature":
newversion = mkVersion(major, feature+1, 0)
elif args.command == "fix":
newversion=mkVersion(major, feature, fix+1)
else:
print("error: No command. Please supply one of 'major', 'feature', or 'fix'")
quit()
# do the pyproject.toml/setup.py
if args.file.endswith("toml"):
version_line = f"version = '{newversion}'"
else:
version_line = f"{packagename}='{newversion}'"
lines[i] = version_line
fh = open(args.file, "w")
fh.write("\n".join(lines))
fh.flush()
# __VERSION__
# place a file with version string into package directory
# the attempt to find the package dir is a heuristic
dirname = os.path.join(os.getcwd(), packagename.replace(args.needle, "").lower())
versionfile = os.path.join(dirname, "__VERSION__")
if os.path.isdir(dirname):
vf = open(versionfile, "w")
vf.write(newversion)
vf.close()
else:
print("warning: Could not write version to " + versionfile, file=sys.stderr)
# msg
if args.message != "":
msg = args.message
else:
msg = input(f"Message for tag v{newversion}: ")
# git tag
gitcmd = f"git tag -a v{newversion} -m \"{msg}\""
subprocess.run("git add " + args.file, text=True, shell=True)
if os.path.isfile(versionfile):
subprocess.run("git add " + versionfile, text=True, shell=True)
subprocess.run("git commit -m \"" + msg + "\"", text=True, shell=True)
subprocess.run(gitcmd, text=True, shell=True)