@@ -72,12 +72,19 @@ def get_root_path(wc_path):
72
72
)
73
73
74
74
75
- def run_black (filepath ):
75
+ def apply_styling (filepath ):
76
76
"""
77
77
Run black on a given file
78
78
Inputs:
79
79
- filepath, the path to the file to run black on
80
80
"""
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
+ )
81
88
result = run_command (f"{ BLACK_COMMAND } { filepath } " )
82
89
if result .returncode :
83
90
raise Exception (
@@ -212,7 +219,7 @@ class ApplyMacros:
212
219
Object to hold data + methods to apply upgrade macros in lfric_apps
213
220
"""
214
221
215
- def __init__ (self , tag , cname , apps , core , jules ):
222
+ def __init__ (self , tag , cname , version , apps , core , jules ):
216
223
self .tag = tag
217
224
if cname :
218
225
self .class_name = cname
@@ -228,7 +235,10 @@ def __init__(self, tag, cname, apps, core, jules):
228
235
# system needs modifying to enable this
229
236
# self.jules_source = self.get_dependency_paths(jules, "jules")
230
237
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
232
242
self .ticket_number = None
233
243
self .author = None
234
244
self .parsed_macros = {}
@@ -473,7 +483,7 @@ def remove_macro(self, contents, meta_dir):
473
483
if not in_new_macro :
474
484
f .write (line )
475
485
476
- run_black (temppath )
486
+ apply_styling (temppath )
477
487
478
488
if not os .path .getsize (temppath ) > 0 :
479
489
raise Exception (
@@ -742,7 +752,7 @@ def write_new_macro(self, meta_dir, full_command):
742
752
" return config, self.reports\n "
743
753
)
744
754
745
- run_black (temppath )
755
+ apply_styling (temppath )
746
756
747
757
os .rename (temppath , filepath )
748
758
@@ -956,6 +966,20 @@ def check_tag(opt):
956
966
return opt
957
967
958
968
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
+
959
983
def parse_args ():
960
984
"""
961
985
Read command line args
@@ -977,6 +1001,13 @@ def parse_args():
977
1001
help = "The class name of the upgrade macro. This should only be used at "
978
1002
"a new release when the tag and classname differ." ,
979
1003
)
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
+ )
980
1011
parser .add_argument (
981
1012
"-a" ,
982
1013
"--apps" ,
@@ -1004,16 +1035,14 @@ def parse_args():
1004
1035
return parser .parse_args ()
1005
1036
1006
1037
1007
- def main ():
1038
+ def apply_macros_main (
1039
+ tag , cname = None , version = None , apps = "." , core = None , jules = None
1040
+ ):
1008
1041
"""
1009
1042
Main function for this program
1010
1043
"""
1011
1044
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 )
1017
1046
1018
1047
# Pre-process macros
1019
1048
banner_print ("Pre-Processing Macros" )
@@ -1043,4 +1072,7 @@ def main():
1043
1072
1044
1073
1045
1074
if __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