-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectVersion.py
More file actions
192 lines (158 loc) · 5.15 KB
/
Copy pathProjectVersion.py
File metadata and controls
192 lines (158 loc) · 5.15 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/env python
# --------------------------------------------------------------------------------------------------
#
# Imports.
#
# --------------------------------------------------------------------------------------------------
import argparse
import os
import sys
from dataclasses import dataclass
from typing import List
# --------------------------------------------------------------------------------------------------
#
# Global variables.
#
# --------------------------------------------------------------------------------------------------
@dataclass
class Version:
"""
Class to represent a version.
"""
def __init__(self, version: str):
version_parts = version.split(".")
if len(version_parts) == 0:
raise ValueError("Version string is empty!")
self.major = 0
self.minor = 0
self.patch = 0
self.dev = 0
if len(version_parts) >= 1:
self.major = int(version_parts[0])
if len(version_parts) >= 2:
self.minor = int(version_parts[1])
if len(version_parts) >= 3:
self.patch = int(version_parts[2])
if len(version_parts) == 4:
dev_part = version_parts[3]
if dev_part.startswith("dev"):
self.dev = int(dev_part.replace("dev", ""))
else:
raise ValueError("Development version must be in the format 'devN'")
def __str__(self) -> str:
version = f"{self.major}.{self.minor}.{self.patch}"
if self.dev > 0:
version += f".dev{self.dev}"
return version
def increment(self, version_part: str):
"""
Increment the specified version part.
"""
if version_part == "MAJOR":
self.major += 1
self.minor = 0
self.patch = 0
self.dev = 0
elif version_part == "MINOR":
self.minor += 1
self.patch = 0
self.dev = 0
elif version_part == "PATCH":
self.patch += 1
self.dev = 0
elif version_part == "DEV":
self.dev += 1
else:
raise ValueError(f"Unknown version part: {version_part}!")
# --------------------------------------------------------------------------------------------------
#
# Class definition.
#
# --------------------------------------------------------------------------------------------------
class VersionFileIO:
"""
Class to manage the project version.
"""
def __init__(self):
"""
Initialize the ProjectVersion class.
"""
self.__current_working_directory = os.getcwd()
self.__version_filepath = os.path.join(
self.__current_working_directory, "setup.cfg"
)
def increment_and_write(self, version_part: str):
"""
Increment the specified version part.
"""
if version_part == "NONE":
sys.exit(0)
version = Version(self.read())
version.increment(version_part)
version_file_lines: List[str] = []
with open(
file=self.__version_filepath, mode="r", encoding="utf-8"
) as version_file:
version_file_lines = version_file.readlines()
for line_index, line in enumerate(version_file_lines):
if line.startswith("version = "):
version_file_lines[line_index] = f"version = {str(version)}\n"
with open(
file=self.__version_filepath, mode="w", encoding="utf-8"
) as version_file:
version_file.writelines(version_file_lines)
def read(self) -> str:
"""
Get the current project version.
"""
version_file_lines: List[str] = []
with open(
file=self.__version_filepath, mode="r", encoding="utf-8"
) as version_file:
version_file_lines = version_file.readlines()
version = ""
for _, line in enumerate(version_file_lines):
if line.startswith("version = "):
version = line.replace("version = ", "").strip()
break
return version
# --------------------------------------------------------------------------------------------------
#
# Entry point.
#
# --------------------------------------------------------------------------------------------------
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
dest="action",
type=str,
nargs="?",
choices=[
"NONE",
"INCREMENT_VERSION",
"GET_VERSION",
],
default="NONE",
const="NONE",
)
parser.add_argument(
dest="version_part",
type=str,
nargs="?",
choices=[
"NONE",
"MAJOR",
"MINOR",
"PATCH",
"DEV",
],
default="NONE",
const="NONE",
)
args = parser.parse_args()
if args.action == "NONE":
sys.exit(0)
elif args.action == "INCREMENT_VERSION":
VersionFileIO().increment_and_write(args.version_part)
elif args.action == "GET_VERSION":
print(VersionFileIO().read())