| 1 |
#!/usr/bin/env python3 |
| 2 |
# Update current package semantic version. |
| 3 |
# 2021 David Anderson |
| 4 |
# This python3 script is hereby placed in the Public Domain |
| 5 |
# for anyone to use in any way. |
| 6 |
|
| 7 |
import os |
| 8 |
import sys |
| 9 |
|
| 10 |
|
| 11 |
def usage(): |
| 12 |
print("Example of use:") |
| 13 |
print(" python3 tools/updatesemanticversion.py 0.2.0") |
| 14 |
print("Then git push as appropriate") |
| 15 |
sys.exit(0) |
| 16 |
|
| 17 |
|
| 18 |
def deriveversions(sver): |
| 19 |
nums = sver.split(".") |
| 20 |
if len(nums) != 3: |
| 21 |
return (False, 0, 0, 0) |
| 22 |
ma = nums[0] |
| 23 |
min = nums[1] |
| 24 |
mic = nums[2] |
| 25 |
if not ma.isdecimal(): |
| 26 |
return (False, 0, 0, 0) |
| 27 |
if not min.isdecimal(): |
| 28 |
return (False, 0, 0, 0) |
| 29 |
if not mic.isdecimal(): |
| 30 |
return (False, 0, 0, 0) |
| 31 |
return (True, ma, min, mic) |
| 32 |
|
| 33 |
|
| 34 |
hmeson=" version: '1.99.0'" |
| 35 |
hmmes =" version: '" |
| 36 |
hmmee = "'" |
| 37 |
def updatemesonversion(lcount, l, sver, maj, min, mic): |
| 38 |
if l.startswith(hmmes): |
| 39 |
s = ''.join([hmmes,sver,hmmee,",\n"]) |
| 40 |
return s, int(lcount)+1 |
| 41 |
return l,lcount |
| 42 |
|
| 43 |
ha = """#define DW_LIBDWARF_VERSION_MAJOR """ |
| 44 |
hb = """#define DW_LIBDWARF_VERSION_MINOR """ |
| 45 |
hc = """#define DW_LIBDWARF_VERSION_MICRO """ |
| 46 |
hv = """#define DW_LIBDWARF_VERSION """ |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def updatelhstring(lcount, l, sver, maj, min, mic): |
| 52 |
if l.startswith(ha): |
| 53 |
l2 = ha + maj + "\n" |
| 54 |
return l2, int(lcount) + 1 |
| 55 |
if l.startswith(hb): |
| 56 |
l2 = hb + min + "\n" |
| 57 |
return l2, int(lcount) + 1 |
| 58 |
if l.startswith(hc): |
| 59 |
l2 = hc + mic + "\n" |
| 60 |
return l2, int(lcount) + 1 |
| 61 |
if l.startswith(hv): |
| 62 |
l2 = "".join([hv, '"', sver, '"\n']) |
| 63 |
return l2, int(lcount) + 1 |
| 64 |
return l, lcount |
| 65 |
|
| 66 |
|
| 67 |
# set(VERSION 0.2.0) |
| 68 |
begincm = "project(libdwarf VERSION " |
| 69 |
|
| 70 |
|
| 71 |
# As of 10.0.0 looks like (but all one line) |
| 72 |
#project(libdwarf VERSION 0.9.3 DESCRIPTION "" |
| 73 |
# HOMEPAGE_URL "https://github.com/davea42/libdwarf-code.git" |
| 74 |
# LANGUAGES C CXX) |
| 75 |
|
| 76 |
def updatecmstring(cmcount, l, sver, maj, min, mic): |
| 77 |
if l.startswith(begincm): |
| 78 |
pa = begincm |
| 79 |
wds = l.split(begincm) |
| 80 |
postpl = wds[1] |
| 81 |
p3 = postpl.split() |
| 82 |
over = p3[0] |
| 83 |
partc = ' '.join(p3[1:]) |
| 84 |
l2 = "".join([begincm, sver," ", partc,"\n"]) |
| 85 |
return l2, int(cmcount) + 1 |
| 86 |
return l, cmcount |
| 87 |
|
| 88 |
|
| 89 |
mm = ".ds vE Rev " |
| 90 |
|
| 91 |
#libdwarf.dox |
| 92 |
# @date 2022-02-23 v0.3.4 |
| 93 |
doxdate = " @date " |
| 94 |
doxd2 = " @date" |
| 95 |
def updatedoxversion(doxcount, l, sver): |
| 96 |
if l.startswith(doxdate): |
| 97 |
wds = l.split() |
| 98 |
if len(wds) < 3: |
| 99 |
return l, doxcount |
| 100 |
if not wds[0] == "@date": |
| 101 |
return l, doxcount |
| 102 |
vnow = ''.join(["v",sver,"\n"]) |
| 103 |
l2 = " ".join([doxd2,wds[1],vnow]) |
| 104 |
return l2, int(doxcount + 1) |
| 105 |
return l, doxcount |
| 106 |
|
| 107 |
"d=0.10.2" |
| 108 |
def updatemakerelease(mrcount, l, sver): |
| 109 |
if l.startswith("d="): |
| 110 |
wds= l.split("="); |
| 111 |
l2= ''.join(["d=",sver]) |
| 112 |
return l2, int(mrcount + 1) |
| 113 |
return l,mrcount; |
| 114 |
|
| 115 |
def updatemmversion(mmcount, l, sver): |
| 116 |
if l.startswith(mm): |
| 117 |
wds = l.split() |
| 118 |
if len(wds) != 8: |
| 119 |
return l, mmcount |
| 120 |
wb = " ".join(wds[0:7]) |
| 121 |
l2 = " ".join([wb, sver, "\n"]) |
| 122 |
return l2, int(mmcount + 1) |
| 123 |
return l, mmcount |
| 124 |
|
| 125 |
|
| 126 |
# m4_define([v_maj], [0]) |
| 127 |
# m4_define([v_min], [2]) |
| 128 |
# m4_define([v_mic], [0]) |
| 129 |
ma = "m4_define([v_maj], [" |
| 130 |
mn = "m4_define([v_min], [" |
| 131 |
mc = "m4_define([v_mic], [" |
| 132 |
|
| 133 |
|
| 134 |
def updateacversion(account, l, sver, maj, min, mic): |
| 135 |
if l.startswith(ma): |
| 136 |
l2 = "".join([ma, maj, "])\n"]) |
| 137 |
return l2, int(account) + 1 |
| 138 |
if l.startswith(mn): |
| 139 |
l2 = "".join([mn, min, "])\n"]) |
| 140 |
return l2, int(account) + 1 |
| 141 |
if l.startswith(mc): |
| 142 |
l2 = "".join([mc, mic, "])\n"]) |
| 143 |
return l2, int(account) + 1 |
| 144 |
return l, int(account) |
| 145 |
|
| 146 |
|
| 147 |
# type is "ac" or "cm" |
| 148 |
def updatefile(fname, type, sver, maj, min, mic): |
| 149 |
foundcm = 0 |
| 150 |
foundac = 0 |
| 151 |
foundlh = 0 |
| 152 |
foundmm = 0 |
| 153 |
founddox = 0 |
| 154 |
foundmmeson = 0 |
| 155 |
foundmr = 0 |
| 156 |
print("Processing", fname, " type", type, " newver", sver) |
| 157 |
try: |
| 158 |
fin = open(fname, "r") |
| 159 |
except IOError as message: |
| 160 |
print("File could not be opened read: ", message, file=sys.stderr) |
| 161 |
sys.exit(1) |
| 162 |
content = fin.readlines() |
| 163 |
fin.close() |
| 164 |
outdata = [] |
| 165 |
for i, l in enumerate(content): |
| 166 |
if type == "cm": |
| 167 |
lx, foundcm = updatecmstring(foundcm, l, sver, maj, min, mic) |
| 168 |
outdata += [lx] |
| 169 |
continue |
| 170 |
elif type == "lh": |
| 171 |
lx, foundlh = updatelhstring(foundlh, l, sver, maj, min, mic) |
| 172 |
outdata += [lx] |
| 173 |
continue |
| 174 |
elif type == "ac": |
| 175 |
lx, foundac = updateacversion(foundac, l, sver, maj, min, mic) |
| 176 |
outdata += [lx] |
| 177 |
continue |
| 178 |
elif type == "mmeson": |
| 179 |
lx, foundac = updatemesonversion(foundmmeson, l, sver,\ |
| 180 |
maj, min, mic) |
| 181 |
outdata += [lx] |
| 182 |
continue |
| 183 |
elif type == "mr": |
| 184 |
lx, foundmr = updatemakerelease(foundmr, l, sver) |
| 185 |
outdata += [lx] |
| 186 |
continue |
| 187 |
elif type == "dox": |
| 188 |
lx, founddox = updatedoxversion(founddox, l, sver) |
| 189 |
outdata += [lx] |
| 190 |
continue |
| 191 |
elif type == "mm": |
| 192 |
lx, foundmm = updatemmversion(foundmm, l, sver) |
| 193 |
outdata += [lx] |
| 194 |
continue |
| 195 |
print("Unknown type of file! Give up!", type) |
| 196 |
sys.exit(1) |
| 197 |
if type == "mm" and not foundmm == 1: |
| 198 |
print("Something wrong, did not find", fname, " version line in mm") |
| 199 |
sys.exit(1) |
| 200 |
if type == "cm" and not foundcm == 1: |
| 201 |
print( |
| 202 |
"Something wrong, did not find", fname, " version line in CMakeLists.txt" |
| 203 |
) |
| 204 |
sys.exit(1) |
| 205 |
if type == "ac" and not foundac == 3: |
| 206 |
print("Something wrong, did not find configure.ac", "version lines") |
| 207 |
sys.exit(1) |
| 208 |
if type == "mr" and not foundmr == 1: |
| 209 |
print("Something wrong, did not find makerelease.sh", "version lines") |
| 210 |
sys.exit(1) |
| 211 |
if type == "dox" and not founddox == 1: |
| 212 |
print("Something wrong, did not find libdwarf.dox", "version lines") |
| 213 |
sys.exit(1) |
| 214 |
if type == "lh" and not foundlh == 4: |
| 215 |
print("Something wrong, did not find libdwarf.h", "version lines") |
| 216 |
sys.exit(1) |
| 217 |
try: |
| 218 |
fo = open(fname, "w") |
| 219 |
except IOError as message: |
| 220 |
print("File could not be opened write: ", message, file=sys.stderr) |
| 221 |
sys.exit(1) |
| 222 |
for i, l in enumerate(outdata): |
| 223 |
print(l, end="", file=fo) |
| 224 |
fo.close() |
| 225 |
|
| 226 |
|
| 227 |
expecteddirs = ["bugxml", "cmake", "doc", "m4", "scripts", "src", "test","tools"] |
| 228 |
|
| 229 |
|
| 230 |
if __name__ == "__main__": |
| 231 |
if len(sys.argv) < 2: |
| 232 |
usage() |
| 233 |
if len(sys.argv) > 2: |
| 234 |
usage() |
| 235 |
sver = sys.argv[1] |
| 236 |
|
| 237 |
ok, maj, min, mic = deriveversions(sver) |
| 238 |
if not ok: |
| 239 |
print("the new semantic version", sver, "is not in the properformat") |
| 240 |
sys.exit(1) |
| 241 |
dirnames = os.listdir(".") |
| 242 |
for d in expecteddirs: |
| 243 |
if d not in dirnames: |
| 244 |
print( |
| 245 |
"Run in base code directory.", |
| 246 |
d, |
| 247 |
"is missing so we are in the wrong directory", |
| 248 |
) |
| 249 |
sys.exit(1) |
| 250 |
updatefile("configure.ac", "ac", sver, maj, min, mic) |
| 251 |
updatefile("CMakeLists.txt", "cm", sver, maj, min, mic) |
| 252 |
updatefile("src/lib/libdwarf/libdwarf.h", "lh", sver, maj, min, mic) |
| 253 |
updatefile("doc/libdwarf.dox", "dox", sver, maj, min, mic) |
| 254 |
updatefile("doc/libdwarfp.mm", "mm", sver, maj, min, mic) |
| 255 |
updatefile("meson.build", "mmeson", sver, maj, min, mic) |
| 256 |
updatefile("tools/makerelease.sh", "mr", sver, maj, min, mic) |