caellion-python-commons
version-check.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 import sys
3 from glob import glob # noqa
4 
5 version = "0.0.0"
6 commitid = "0000000"
7 branch = "unknown"
8 
9 is_initial = False
10 invalid_set_version = False
11 
12 set_version_term = "set-version"
13 bump_minor_term = "bump-minor"
14 bump_major_term = "bump-major"
15 
16 major = 0
17 minor = 0
18 patch = 0
19 
20 describe = sys.argv[1]
21 branch = sys.argv[2]
22 
23 if len(sys.argv) >= 4:
24  commits = sys.argv[3]
25 else:
26  commits = ""
27 
28 # check if we have a valid describe, if not we will assume this is a 0.0.0-version
29 describe = describe.replace("-" + str(branch), "")
30 if "-" in describe:
31  dsplit = describe.split("-")
32  if len(dsplit) > 0:
33  try:
34  splits = describe.split("-")
35  version = splits[0]
36  commitid = splits[len(dsplit) - 1].replace("g", "")
37  version_split = version.split(".")
38  major = int(version_split[0])
39  minor = int(version_split[1])
40  patch = int(version_split[2])
41  except: # noqa
42  is_initial = True
43  else:
44  is_initial = True
45 else:
46  is_initial = True
47 
48 # decide which part to bump based on special comments
49 # If there's a bump in major, we reset minor and patch, if there's a bump in minor, we reset patch
50 # If we build from a few commits, we apply largest version-bump to the build
51 # We will tag version regardless of build state
52 # And if this appears to be initial versioned commit, we just bail out at 0.0.0
53 bump_by = 1
54 if not is_initial:
55  # if it is not initial, we need to parse commits line-by-line
56  # if we encounter ONLY ONE [set-version valid-version] then we set the version to that
57  # if we encounter MORE THAN ONE [set-version valid-version] we use the greatest of the two
58  # [bump-minor] or [bump-major] after [set-version] will not be processed
59  # invalid [set-version] causes a bailout with incrementing patch instead (also returning error-status)
60 
61  major_new = major
62  minor_new = minor
63  patch_new = patch
64 
65  for commit in commits.split("\n"):
66  # if standard tags found, bump by these
67  if bump_by < 2 and bump_by > 0 and "[" + bump_minor_term + "]" in commit:
68  bump_by = 2
69  if bump_by < 3 and bump_by > 0 and "[" + bump_major_term + "]" in commit:
70  bump_by = 3
71 
72  if "[" + set_version_term in commit:
73  # set-version in commit, forfeit standard processing
74  bump_by = 0
75 
76  # find version substring
77  vsubs = commit.split("[" + set_version_term)
78  if len(vsubs) < 2:
79  # invalid tag, bailout
80  bump_by = 1
81  invalid_set_version = True
82  print("Invalid set-version tag [1] " + str(vsubs), file=sys.stderr)
83  break
84 
85  vsubs = vsubs[1].split("]")
86  if len(vsubs) < 2:
87  # invalid tag, bailout
88  bump_by = 1
89  invalid_set_version = True
90  print("Invalid set-version tag [2] " + str(vsubs), file=sys.stderr)
91  break
92 
93  vsubs = vsubs[0].split(".")
94  if len(vsubs) < 3:
95  # invalid tag, bailout
96  bump_by = 1
97  invalid_set_version = True
98  print("Invalid set-version tag [3] " + str(vsubs), file=sys.stderr)
99  break
100 
101  if vsubs[0] == "" or vsubs[1] == "" or vsubs[2] == "":
102  # invalid tag, bailout
103  bump_by = 1
104  invalid_set_version = True
105  print("Invalid set-version tag [4] " + str(vsubs), file=sys.stderr)
106  break
107 
108  major_ = int(vsubs[0])
109  minor_ = int(vsubs[1])
110  patch_ = int(vsubs[2])
111  if major_ < major or (major_ == major and minor_ < minor) or (major_ == major and minor_ == minor and patch_ < (patch + 0)): # minimally one patch later
112  # trying to set lesser version, bailout
113  bump_by = 1
114  invalid_set_version = True
115  print("Invalid set-version tag [5] " + str(vsubs), file=sys.stderr)
116  else:
117  # versions valid, update value
118  bump_by = 0
119  invalid_set_version = False
120  major_new = major_
121  minor_new = minor_
122  patch_new = patch_
123 
124  # all valid?
125  if not invalid_set_version:
126  major = major_new
127  minor = minor_new
128  patch = patch_new
129 
130 else:
131  bump_by = 0
132 
133 
134 if bump_by == 3:
135  major += 1
136  minor = 0
137  patch = 0
138 elif bump_by == 2:
139  minor += 1
140  patch = 0
141 elif bump_by == 1:
142  patch += 1
143 
144 
145 version_tag = str(major) + "." + str(minor) + "." + str(patch) + str("-") + branch
146 version_short = str(major) + "." + str(minor) + "." + str(patch)
147 final_version = str(major) + "." + str(minor) + "." + str(patch) + "-" + branch + "-" + commitid
148 
149 # PROJECT SPECIFIC CODE
150 # now apply final_version to assemblies
151 with open("setup.cfg", "r") as fp:
152  contents = fp.read()
153  contents = contents.replace("{VERSION_STRING}", version_short)
154 with open("setup.cfg", "w") as fp:
155  fp.write(contents)
156 # /PROJECT SPECIFIC CODE
157 
158 # We also print version_tag so that we can use it in jenkins to tag repo
159 print(version_tag)
160 if invalid_set_version:
161  print("Invalid set-version tag", file=sys.stderr)
162  exit(1)