@@ -72,12 +72,19 @@ def get_root_path(wc_path):
7272 )
7373
7474
75- def run_black (filepath ):
75+ def apply_styling (filepath ):
7676 """
7777 Run black on a given file
7878 Inputs:
7979 - filepath, the path to the file to run black on
8080 """
81+ result = run_command (f"isort --profile black { filepath } " )
82+ if result .returncode :
83+ raise Exception (
84+ "Running 'isort' as a subprocess failed. This may indicate a "
85+ "syntax error with your macro.\n The error message produced "
86+ f"was:\n \n { result .stderr } "
87+ )
8188 result = run_command (f"{ BLACK_COMMAND } { filepath } " )
8289 if result .returncode :
8390 raise Exception (
@@ -212,7 +219,7 @@ class ApplyMacros:
212219 Object to hold data + methods to apply upgrade macros in lfric_apps
213220 """
214221
215- def __init__ (self , tag , cname , apps , core , jules ):
222+ def __init__ (self , tag , cname , version , apps , core , jules ):
216223 self .tag = tag
217224 if cname :
218225 self .class_name = cname
@@ -228,7 +235,10 @@ def __init__(self, tag, cname, apps, core, jules):
228235 # system needs modifying to enable this
229236 # self.jules_source = self.get_dependency_paths(jules, "jules")
230237 self .set_rose_meta_path ()
231- self .version = re .search (r".*vn(\d+\.\d+)(_.*)?" , tag ).group (1 )
238+ if version is None :
239+ self .version = re .search (r".*vn(\d+\.\d+)(_.*)?" , tag ).group (1 )
240+ else :
241+ self .version = version
232242 self .ticket_number = None
233243 self .author = None
234244 self .parsed_macros = {}
@@ -473,7 +483,7 @@ def remove_macro(self, contents, meta_dir):
473483 if not in_new_macro :
474484 f .write (line )
475485
476- run_black (temppath )
486+ apply_styling (temppath )
477487
478488 if not os .path .getsize (temppath ) > 0 :
479489 raise Exception (
@@ -742,7 +752,7 @@ def write_new_macro(self, meta_dir, full_command):
742752 " return config, self.reports\n "
743753 )
744754
745- run_black (temppath )
755+ apply_styling (temppath )
746756
747757 os .rename (temppath , filepath )
748758
@@ -956,6 +966,20 @@ def check_tag(opt):
956966 return opt
957967
958968
969+ def version_number (opt ):
970+ """
971+ Check that the command line supplied version number is of a suitable format
972+ """
973+ if opt is None :
974+ return opt
975+ if not re .match (r"\d+.\d+" , opt ):
976+ raise argparse .ArgumentTypeError (
977+ f"The version number '{ opt } ' does not conform to the 'X.Y' format."
978+ "Please modify the command line argument and rerun."
979+ )
980+ return opt
981+
982+
959983def parse_args ():
960984 """
961985 Read command line args
@@ -977,6 +1001,13 @@ def parse_args():
9771001 help = "The class name of the upgrade macro. This should only be used at "
9781002 "a new release when the tag and classname differ." ,
9791003 )
1004+ parser .add_argument (
1005+ "-v" ,
1006+ "--version" ,
1007+ default = None ,
1008+ type = version_number ,
1009+ help = "The new version number we are updating to (format X.Y)" ,
1010+ )
9801011 parser .add_argument (
9811012 "-a" ,
9821013 "--apps" ,
@@ -1004,16 +1035,14 @@ def parse_args():
10041035 return parser .parse_args ()
10051036
10061037
1007- def main ():
1038+ def apply_macros_main (
1039+ tag , cname = None , version = None , apps = "." , core = None , jules = None
1040+ ):
10081041 """
10091042 Main function for this program
10101043 """
10111044
1012- args = parse_args ()
1013-
1014- macro_object = ApplyMacros (
1015- args .tag , args .cname , args .apps , args .core , args .jules
1016- )
1045+ macro_object = ApplyMacros (tag , cname , version , apps , core , jules )
10171046
10181047 # Pre-process macros
10191048 banner_print ("Pre-Processing Macros" )
@@ -1043,4 +1072,7 @@ def main():
10431072
10441073
10451074if __name__ == "__main__" :
1046- main ()
1075+ args = parse_args ()
1076+ apply_macros_main (
1077+ args .tag , args .cname , args .version , args .apps , args .core , args .jules
1078+ )
0 commit comments