1+ import argparse
2+ import re
3+ import os
4+ from datetime import datetime
5+
6+ def create_new_tag (tag , update_major ):
7+ if not (re .match (r'^v\d+\.\d+$' , tag )):
8+ raise Exception (f'tag: { tag } is not giving in the correct format e.i v0.0' )
9+
10+ # Notice that this could make a tag version of three digits become two digits
11+ # e.i 3.2.1 -> 3.3
12+ digits_tags = (re .match (r'^v\d+\.\d+' , tag )).group ()[1 ::].split ('.' )
13+ if len (digits_tags ) != 2 :
14+ raise Exception (f'tag: { tag } must contain two version digits' )
15+
16+ major_num = int (digits_tags [0 ])
17+ minor_num = int (digits_tags [1 ])
18+ if update_major :
19+ print (f"Label detected to update major version" )
20+ major_num += 1
21+ minor_num = 0
22+ else :
23+ minor_num += 1
24+ return f"v{ major_num } .{ minor_num } "
25+
26+ def store_tag (tag ):
27+ with open (os .environ ['GITHUB_OUTPUT' ], 'a' ) as fh :
28+ print (f'new_tag={ tag } ' , file = fh )
29+
30+ def update_file_with_tag (f_name , old_tag , new_tag , replace_dates = True ):
31+ if os .path .isfile (f_name ):
32+ try :
33+ with open (f_name , "r" ,encoding = "utf-8" ) as f :
34+ data = f .read ()
35+ data = data .replace (old_tag , new_tag )
36+ if replace_dates :
37+ date_re = r"\d{4}-\d{2}-\d{2}"
38+ today = datetime .today ().strftime ('%Y-%m-%d' )
39+ data = re .sub (date_re , today , data )
40+ with open (f_name , "w" ,encoding = "utf-8" ) as f :
41+ f .write (data )
42+ except Exception as e :
43+ print (e )
44+ else :
45+ print (f"Warning: { f_name } doest exist at the current path { os .getcwd ()} " )
46+
47+ def main (args ):
48+ tag = args .tag
49+ new_tag = "v2.0"
50+ if not tag :
51+ print (f"Warning: a latest release with a tag does not exist in current repository, starting from { new_tag } " )
52+ else :
53+ new_tag = create_new_tag (tag ,args .update_major_ver )
54+ print (f"Repository with tag: { tag } , creating a new tag with: { new_tag } " )
55+ update_file_with_tag (".zenodo.json" , tag , new_tag )
56+ update_file_with_tag ("CITATION.cff" , tag , new_tag )
57+ update_file_with_tag ("README.md" , tag , new_tag , replace_dates = False )
58+ store_tag (new_tag )
59+
60+ def run ():
61+ args = parser .parse_args ()
62+ main (args )
63+
64+
65+ def str_to_bool (value ):
66+ if value .lower () == "true" :
67+ return True
68+ elif value .lower () == "false" :
69+ return False
70+ else :
71+ raise Exception (
72+ f"Error: value { value } as argument is not accepted\n "
73+ f"retry with true or false"
74+ )
75+
76+ if __name__ == "__main__" :
77+ parser = argparse .ArgumentParser ()
78+ parser .add_argument (
79+ "--tag" , type = str ,
80+ help = "Require: latest tag" ,
81+ required = True
82+ )
83+ parser .add_argument (
84+ "--update_major_ver" , type = str_to_bool ,
85+ help = "Require: boolean to update the major tag number" ,
86+ required = True
87+ )
88+ run ()
0 commit comments