remove redundant includes
[liba.git] / script / version.py
blobc1cdff3677b82ee2e01a26d655f68c76effe8ee9
1 #!/usr/bin/env python
2 import time, os, re
5 class version(object):
6 def __init__(self, release="0.0.0") -> None:
7 self.major, self.minor, self.patch = re.findall(r"(\d+)\.(\d+)\.(\d+)", release)[0]
8 self.version = self.major + "." + self.minor + "." + self.patch
9 self.tweak = time.strftime("%Y%m%d")
10 self.release = release
12 @staticmethod
13 def load(file):
14 if not os.path.exists(file):
15 print("missing {}".format(file))
16 return ""
17 with open(file, "r", encoding="UTF-8") as f:
18 return f.read()
20 @staticmethod
21 def save(file, text):
22 with open(file, "wb") as f:
23 f.write(text.encode("UTF-8"))
25 def update_version(self, file, feild="version"):
26 text = self.load(file)
27 res = re.findall(
28 r"{}([^0-9A-Za-z]+)([0-9]+)\.([0-9]+)\.([0-9]+)([.\w-]*)".format(feild), text
30 if res:
31 release = "{}.{}.{}{}".format(res[0][1], res[0][2], res[0][3], res[0][4])
32 if self.release != release:
33 line0 = "{}{}{}".format(feild, res[0][0], release)
34 line1 = "{}{}{}".format(feild, res[0][0], self.release)
35 self.save(file, text.replace(line0, line1))
37 def update_version_h(self, file):
38 text = text_ = self.load(file)
39 res = re.findall(r"#define A_VERSION_MAJOR (\d+)", text)
40 if res and self.major != res[0]:
41 line0 = "#define A_VERSION_MAJOR {}".format(res[0])
42 line1 = "#define A_VERSION_MAJOR {}".format(self.major)
43 text = text.replace(line0, line1)
44 res = re.findall(r"#define A_VERSION_MINOR (\d+)", text)
45 if res and self.minor != res[0]:
46 line0 = "#define A_VERSION_MINOR {}".format(res[0])
47 line1 = "#define A_VERSION_MINOR {}".format(self.minor)
48 text = text.replace(line0, line1)
49 res = re.findall(r"#define A_VERSION_PATCH (\d+)", text)
50 if res and self.patch != res[0]:
51 line0 = "#define A_VERSION_PATCH {}".format(res[0])
52 line1 = "#define A_VERSION_PATCH {}".format(self.patch)
53 text = text.replace(line0, line1)
54 res = re.findall(r"#define A_VERSION_TWEAK (\d+)", text)
55 if res and self.tweak != res[0]:
56 line0 = "#define A_VERSION_TWEAK {}".format(res[0])
57 line1 = "#define A_VERSION_TWEAK {}".format(self.tweak)
58 text = text.replace(line0, line1)
59 if text != text_:
60 self.save(file, text)
62 def update_version_ac(self, file):
63 text = text_ = self.load(file)
64 res = re.findall(r"\[version_major\], \[(\d+)\]", text)
65 if res and self.major != res[0]:
66 line0 = "[version_major], [{}]".format(res[0])
67 line1 = "[version_major], [{}]".format(self.major)
68 text = text.replace(line0, line1)
69 res = re.findall(r"\[version_minor\], \[(\d+)\]", text)
70 if res and self.minor != res[0]:
71 line0 = "[version_minor], [{}]".format(res[0])
72 line1 = "[version_minor], [{}]".format(self.minor)
73 text = text.replace(line0, line1)
74 res = re.findall(r"\[version_patch\], \[(\d+)\]", text)
75 if res and self.patch != res[0]:
76 line0 = "[version_patch], [{}]".format(res[0])
77 line1 = "[version_patch], [{}]".format(self.patch)
78 text = text.replace(line0, line1)
79 if text != text_:
80 self.save(file, text)
82 def update_autogen(self, file):
83 text = self.load(file)
84 age = str(int(self.minor) - int(self.major))
85 version = self.minor + ":" + self.patch + ":" + age
86 res = re.findall(r"version-info ([0-9:]+)", text)
87 if res and version != res[0]:
88 line0 = "version-info {}".format(res[0])
89 line1 = "version-info {}".format(version)
90 self.save(file, text.replace(line0, line1))
93 if __name__ == "__main__":
94 import argparse
96 parser = argparse.ArgumentParser()
97 parser.add_argument("version")
98 args = parser.parse_known_args()
99 v = version(args[0].version)
100 v.update_version("doc/Doxyfile", "PROJECT_NUMBER")
101 v.update_version("cmake/Project.cmake", "VERSION")
102 v.update_version("java/src/liba.java", "VERSION")
103 v.update_version("lua/liba.lua", "VERSION")
104 v.update_version_h("include/a/version.h")
105 v.update_version("javascript/package.json")
106 v.update_version_ac("script/configure.ac")
107 v.update_autogen("script/autogen")
108 v.update_version("Cargo.toml")
109 v.update_version("conanfile.py")
110 v.update_version("meson.build")
111 v.update_version("setup.cfg")
112 v.update_version("xmake.lua")