1+ #!/bin/env python
2+ import argparse
3+ from dataclasses import dataclass
4+
5+ from packaging .version import Version
6+
7+ PYPROJECT_PATH = "pyproject.toml"
8+ DEFAULT_CHANGELOG_PATH = "CHANGELOG.md"
9+ DEFAULT_YDB_VERSION_FILE = "ydb_mcp/version.py"
10+ MARKER = "# AUTOVERSION"
11+
12+
13+ @dataclass (init = False )
14+ class VersionLine :
15+ old_line : str
16+ major : int
17+ minor : int
18+ patch : int
19+ pre : int
20+
21+ def __init__ (self , old_line : str , version_str : str ):
22+ self .old_line = old_line
23+
24+ version = Version (version_str )
25+ self .major = version .major
26+ self .minor = version .minor
27+ self .micro = version .micro
28+
29+ if version .pre is None :
30+ self .pre = 0
31+ else :
32+ self .pre = version .pre [1 ]
33+
34+ def increment (self , part_name : str , with_beta : bool ):
35+ if part_name == "minor" :
36+ self .increment_minor (with_beta )
37+ elif part_name == "patch" or part_name == "micro" :
38+ self .increment_micro (with_beta )
39+ else :
40+ raise Exception ("unexpected increment type: '%s'" % part_name )
41+
42+ def increment_minor (self , with_beta : bool ):
43+ if with_beta :
44+ if self .pre == 0 or self .micro != 0 :
45+ self .increment_minor (False )
46+ self .pre += 1
47+ return
48+
49+ if self .micro == 0 and self .pre > 0 :
50+ self .pre = 0
51+ return
52+
53+ self .minor += 1
54+ self .micro = 0
55+ self .pre = 0
56+
57+ def increment_micro (self , with_beta : bool ):
58+ if with_beta :
59+ if self .pre == 0 :
60+ self .increment_micro (False )
61+ self .pre += 1
62+ return
63+
64+ if self .pre > 0 :
65+ self .pre = 0
66+ return
67+
68+ self .micro += 1
69+
70+ def __str__ (self ):
71+ if self .pre > 0 :
72+ pre = "b%s" % self .pre
73+ else :
74+ pre = ""
75+
76+ return "%s.%s.%s%s" % (self .major , self .minor , self .micro , pre )
77+
78+ def version_line_with_mark (self ):
79+ return 'version = "%s" %s' % (str (self ), MARKER )
80+
81+
82+ def extract_version (pyproject_content : str ):
83+ version_line = ""
84+ for line in pyproject_content .splitlines ():
85+ if MARKER in line :
86+ version_line = line
87+ break
88+
89+ if version_line == "" :
90+ raise Exception ("Not found version line" )
91+
92+ version_line = version_line .strip ()
93+
94+ parts = version_line .split ('"' )
95+ version_part = parts [1 ]
96+
97+ return VersionLine (old_line = version_line , version_str = version_part )
98+
99+
100+ def increment_version_at_pyproject (
101+ pyproject_path : str , inc_type : str , with_beta : bool
102+ ) -> str :
103+ with open (pyproject_path , "rt" ) as f :
104+ setup_content = f .read ()
105+
106+ version = extract_version (setup_content )
107+ version .increment (inc_type , with_beta )
108+ setup_content = setup_content .replace (
109+ version .old_line , version .version_line_with_mark ()
110+ )
111+
112+ with open (pyproject_path , "w" ) as f :
113+ f .write (setup_content )
114+
115+ return str (version )
116+
117+
118+ def add_changelog_version (changelog_path , version : str ):
119+ with open (changelog_path , "rt" ) as f :
120+ content = f .read ()
121+ content = content .strip ()
122+
123+ if content .startswith ("##" ):
124+ return
125+
126+ content = """## %s ##
127+ %s
128+ """ % (version , content )
129+ with open (changelog_path , "w" ) as f :
130+ f .write (content )
131+
132+
133+ def set_version_in_version_file (file_path : str , version : str ):
134+ with open (file_path , "w" ) as f :
135+ f .write ('VERSION = "%s"\n ' % version )
136+
137+
138+ def main ():
139+ parser = argparse .ArgumentParser ()
140+ parser .add_argument (
141+ "--inc-type" ,
142+ default = "minor" ,
143+ help = "increment version type: patch or minor" ,
144+ choices = ["minor" , "patch" ],
145+ )
146+ parser .add_argument (
147+ "--beta" , choices = ["true" , "false" ], help = "is beta version"
148+ )
149+ parser .add_argument (
150+ "--changelog-path" ,
151+ default = DEFAULT_CHANGELOG_PATH ,
152+ help = "path to changelog" ,
153+ type = str ,
154+ )
155+ parser .add_argument ("--pyproject-path" , default = PYPROJECT_PATH )
156+
157+ args = parser .parse_args ()
158+
159+ is_beta = args .beta == "true"
160+
161+ new_version = increment_version_at_pyproject (
162+ args .pyproject_path , args .inc_type , is_beta
163+ )
164+ add_changelog_version (args .changelog_path , new_version )
165+ set_version_in_version_file (DEFAULT_YDB_VERSION_FILE , new_version )
166+ print (new_version )
167+
168+
169+ if __name__ == "__main__" :
170+ main ()
0 commit comments