1111import typing
1212from concurrent import futures
1313from typing import List , Union
14-
14+ from pathlib import Path
1515import psutil
1616
1717from ..logging import str_error , str_info , str_running , str_success , str_warn
@@ -236,10 +236,10 @@ def __init__(
236236
237237 # logdir
238238 if logfile_dir is not None :
239- self .logfile_dir = logfile_dir
239+ self .logfile_dir = Path ( logfile_dir )
240240 else :
241- self .logfile_dir = os . path . join (project_dir , "Log" )
242- self .logfile_filepath = os . path . join ( self .logfile_dir , "DiffcrashRun.log" )
241+ self .logfile_dir = Path (project_dir ) / "Log"
242+ self .logfile_filepath = self .logfile_dir / "DiffcrashRun.log"
243243
244244 # logger
245245 self .logger = self ._setup_logger ()
@@ -248,18 +248,14 @@ def __init__(
248248 self .logger .info (get_application_header ())
249249
250250 # diffcrash home
251- self .diffcrash_home = self ._parse_diffcrash_home (diffcrash_home )
252- self .diffcrash_home = os . path . join ( self .diffcrash_home , "bin" )
253- self .diffcrash_lib = os . path . join ( os . path . dirname ( self .diffcrash_home ), "lib" )
251+ self .diffcrash_home = Path ( self ._parse_diffcrash_home (diffcrash_home ) )
252+ self .diffcrash_home = self .diffcrash_home / "bin"
253+ self .diffcrash_lib = self .diffcrash_home . parent / "lib"
254254
255255 if platform .system () == "Linux" :
256- os .environ ["PATH" ] = (
257- os .environ ["PATH" ] + ":" + self .diffcrash_home + ":" + self .diffcrash_lib
258- )
256+ os .environ ["PATH" ] += f":{ self .diffcrash_home } :{ self .diffcrash_lib } "
259257 if platform .system () == "Windows" :
260- os .environ ["PATH" ] = (
261- os .environ ["PATH" ] + ";" + self .diffcrash_home + ";" + self .diffcrash_lib
262- )
258+ os .environ ["PATH" ] += f";{ self .diffcrash_home } ;{ self .diffcrash_lib } "
263259
264260 # project dir
265261 self .project_dir = self ._parse_project_dir (project_dir )
@@ -354,7 +350,7 @@ def _parse_crash_code(self, crash_code) -> str:
354350
355351 def _parse_reference_run (self , reference_run ) -> str :
356352
357- reference_run_ok = os . path . isfile (reference_run )
353+ reference_run_ok = Path (reference_run ). is_file ( )
358354
359355 msg = self ._msg_option .format ("reference-run" , reference_run )
360356 print (str_info (msg ))
@@ -376,7 +372,7 @@ def _parse_use_id_mapping(self, use_id_mapping) -> bool:
376372 return use_id_mapping
377373
378374 def _parse_project_dir (self , project_dir ):
379- project_dir = os . path . abspath (project_dir )
375+ project_dir = Path (project_dir ). resolve ( )
380376
381377 msg = self ._msg_option .format ("project-dir" , project_dir )
382378 print (str_info (msg ))
@@ -395,13 +391,13 @@ def _parse_simulation_runs(
395391 simulation_runs = []
396392 for pattern in simulation_run_patterns :
397393 simulation_runs += glob .glob (pattern )
398- simulation_runs = [filepath for filepath in simulation_runs if os . path . isfile (filepath )]
394+ simulation_runs = [filepath for filepath in simulation_runs if Path (filepath ). is_file ( )]
399395
400396 # search all excluded runs
401397 runs_to_exclude = []
402398 for pattern in exclude_runs :
403399 runs_to_exclude += glob .glob (pattern )
404- runs_to_exclude = [filepath for filepath in runs_to_exclude if os . path . isfile (filepath )]
400+ runs_to_exclude = [filepath for filepath in runs_to_exclude if Path (filepath ). is_file ( )]
405401
406402 n_runs_before_filtering = len (simulation_runs )
407403 simulation_runs = [
@@ -448,7 +444,7 @@ def natural_keys(text):
448444 def _parse_config_file (self , config_file ) -> Union [str , None ]:
449445
450446 _msg_config_file = ""
451- if len (config_file ) > 0 and not os . path . isfile (config_file ):
447+ if len (config_file ) > 0 and not Path (config_file ). is_file ( ):
452448 config_file = None
453449 _msg_config_file = f"Can not find config file '{ config_file } '"
454450
@@ -474,7 +470,7 @@ def _parse_config_file(self, config_file) -> Union[str, None]:
474470 def _parse_parameter_file (self , parameter_file ) -> Union [None , str ]:
475471
476472 _msg_parameter_file = ""
477- if len (parameter_file ) > 0 and not os . path . isfile (parameter_file ):
473+ if len (parameter_file ) > 0 and not Path (parameter_file ). is_file ( ):
478474 parameter_file = None
479475 _msg_parameter_file = f"Can not find parameter file '{ parameter_file } '"
480476 # missing parameter file
@@ -535,13 +531,13 @@ def run_setup(self, pool: futures.ThreadPoolExecutor):
535531 args = []
536532 if self .config_file is None and self .parameter_file is None :
537533 args = [
538- os . path . join ( self .diffcrash_home , "DFC_Setup_" + self .crash_code + " _fem") ,
534+ self .diffcrash_home / f "DFC_Setup_{ self .crash_code } _fem" ,
539535 self .reference_run ,
540536 self .project_dir ,
541537 ]
542538 elif self .config_file is not None and self .parameter_file is None :
543539 args = [
544- os . path . join ( self .diffcrash_home , "DFC_Setup_" + self .crash_code + " _fem") ,
540+ self .diffcrash_home / f "DFC_Setup_{ self .crash_code } _fem" ,
545541 self .reference_run ,
546542 self .project_dir ,
547543 "-C" ,
@@ -550,15 +546,15 @@ def run_setup(self, pool: futures.ThreadPoolExecutor):
550546 elif self .config_file is None and self .parameter_file is not None :
551547 if ".fz" in self .reference_run :
552548 args = [
553- os . path . join ( self .diffcrash_home , "DFC_Setup_" + self .crash_code + " _fem") ,
549+ self .diffcrash_home / f "DFC_Setup_{ self .crash_code } _fem" ,
554550 self .reference_run ,
555551 self .project_dir ,
556552 "-P" ,
557553 self .parameter_file ,
558554 ]
559555 else :
560556 args = [
561- os . path . join ( self .diffcrash_home , "DFC_Setup_" + self .crash_code ) ,
557+ self .diffcrash_home / f "DFC_Setup_{ self .crash_code } " ,
562558 self .reference_run ,
563559 self .project_dir ,
564560 "-P" ,
@@ -567,7 +563,7 @@ def run_setup(self, pool: futures.ThreadPoolExecutor):
567563 elif self .config_file is not None and self .parameter_file is not None :
568564 if ".fz" in self .reference_run :
569565 args = [
570- os . path . join ( self .diffcrash_home , "DFC_Setup_" + self .crash_code + " _fem") ,
566+ self .diffcrash_home / f "DFC_Setup_{ self .crash_code } _fem" ,
571567 self .reference_run ,
572568 self .project_dir ,
573569 "-C" ,
@@ -577,7 +573,7 @@ def run_setup(self, pool: futures.ThreadPoolExecutor):
577573 ]
578574 else :
579575 args = [
580- os . path . join ( self .diffcrash_home , "DFC_Setup_" + self .crash_code ) ,
576+ self .diffcrash_home / f "DFC_Setup_{ self .crash_code } " ,
581577 self .reference_run ,
582578 self .project_dir ,
583579 "-C" ,
@@ -649,15 +645,15 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
649645 if self .parameter_file is None :
650646 if self .use_id_mapping :
651647 args = [
652- os . path . join ( self .diffcrash_home , "DFC_Import_" + self .crash_code + " _fem") ,
648+ self .diffcrash_home / f "DFC_Import_{ self .crash_code } _fem" ,
653649 "-id" ,
654650 self .simulation_runs [i_filepath ],
655651 self .project_dir ,
656652 str (i_filepath + counter_offset ),
657653 ]
658654 else :
659655 args = [
660- os . path . join ( self .diffcrash_home , "DFC_Import_" + self .crash_code + " _fem") ,
656+ self .diffcrash_home / f "DFC_Import_{ self .crash_code } _fem" ,
661657 self .simulation_runs [i_filepath ],
662658 self .project_dir ,
663659 str (i_filepath + counter_offset ),
@@ -666,15 +662,15 @@ def run_import(self, pool: futures.ThreadPoolExecutor):
666662 else :
667663 if self .use_id_mapping :
668664 args = [
669- os . path . join ( self .diffcrash_home , "DFC_Import_" + self .crash_code ) ,
665+ self .diffcrash_home / f "DFC_Import_{ self .crash_code } " ,
670666 "-ID" ,
671667 self .simulation_runs [i_filepath ],
672668 self .project_dir ,
673669 str (i_filepath + counter_offset ),
674670 ]
675671 else :
676672 args = [
677- os . path . join ( self .diffcrash_home , "DFC_Import_" + self .crash_code ) ,
673+ self .diffcrash_home / f "DFC_Import_{ self .crash_code } " ,
678674 self .simulation_runs [i_filepath ],
679675 self .project_dir ,
680676 str (i_filepath + counter_offset ),
@@ -779,7 +775,7 @@ def run_math(self, pool: futures.ThreadPoolExecutor):
779775 start_time = time .time ()
780776 return_code_future = pool .submit (
781777 run_subprocess ,
782- [os . path . join ( self .diffcrash_home , "DFC_Math_" + self .crash_code ) , self .project_dir ],
778+ [self .diffcrash_home / f "DFC_Math_{ self .crash_code } " , self .project_dir ],
783779 )
784780 return_code = return_code_future .result ()
785781
@@ -837,31 +833,31 @@ def run_export(self, pool: futures.ThreadPoolExecutor):
837833 export_item_list = []
838834
839835 # check for pdmx
840- pdmx_filepath_list = glob . glob ( os . path . join ( self .project_dir , "*_pdmx" ))
836+ pdmx_filepath_list = list ( self .project_dir . glob ( "*_pdmx" ))
841837 if pdmx_filepath_list :
842- export_item_list .append (os . path . basename ( pdmx_filepath_list [0 ]) )
838+ export_item_list .append (pdmx_filepath_list [0 ]. name )
843839
844840 # check for pdij
845- pdij_filepath_list = glob . glob ( os . path . join ( self .project_dir , "*_pdij" ))
841+ pdij_filepath_list = list ( self .project_dir . glob ( "*_pdij" ))
846842 if pdij_filepath_list :
847- export_item_list .append (os . path . basename ( pdij_filepath_list [0 ]) )
843+ export_item_list .append (pdij_filepath_list [0 ]. name )
848844
849845 else :
850846 export_item_list = self .read_config_file (self .config_file )
851847
852848 # remove previous existing exports
853849 for export_item in export_item_list :
854- export_item_filepath = os . path . join ( self .project_dir , export_item + " .d3plot.fz")
855- if os . path . isfile ( export_item_filepath ):
856- os . remove ( export_item_filepath )
850+ export_item_filepath = self .project_dir / f" { export_item } .d3plot.fz"
851+ if export_item_filepath . is_file ( ):
852+ export_item_filepath . unlink ( )
857853
858854 # do the thing
859855 start_time = time .time ()
860856 return_code_futures = [
861857 pool .submit (
862858 run_subprocess ,
863859 [
864- os . path . join ( self .diffcrash_home , "DFC_Export_" + self .crash_code ) ,
860+ self .diffcrash_home / f "DFC_Export_{ self .crash_code } " ,
865861 self .project_dir ,
866862 export_item ,
867863 ],
@@ -938,7 +934,7 @@ def run_matrix(self, pool: futures.ThreadPoolExecutor):
938934 return_code_future = pool .submit (
939935 run_subprocess ,
940936 [
941- os . path . join ( self .diffcrash_home , "DFC_Matrix_" + self .crash_code ) ,
937+ self .diffcrash_home / f "DFC_Matrix_{ self .crash_code } " ,
942938 self .project_dir ,
943939 matrix_inputfile ,
944940 ],
@@ -1003,7 +999,7 @@ def run_eigen(self, pool: futures.ThreadPoolExecutor):
1003999 return_code_future = pool .submit (
10041000 run_subprocess ,
10051001 [
1006- os . path . join ( self .diffcrash_home , "DFC_Eigen_" + self .crash_code ) ,
1002+ self .diffcrash_home / f "DFC_Eigen_{ self .crash_code } " ,
10071003 self .project_dir ,
10081004 eigen_inputfile ,
10091005 ],
@@ -1062,16 +1058,16 @@ def run_merge(self, pool: futures.ThreadPoolExecutor):
10621058 merge_inputfile = self ._create_merge_input_file (self .project_dir )
10631059
10641060 # clear previous merges
1065- for filepath in glob . glob ( os . path . join ( self .project_dir , "mode_*" ) ):
1066- if os . path . isfile ( filepath ):
1067- os . remove ( filepath )
1061+ for filepath in self .project_dir . glob ( "mode_*" ):
1062+ if filepath . is_file ( ):
1063+ filepath . unlink ( )
10681064
10691065 # run the thing
10701066 start_time = time .time ()
10711067 return_code_future = pool .submit (
10721068 run_subprocess ,
10731069 [
1074- os . path . join ( self .diffcrash_home , "DFC_Merge_All_" + self .crash_code ) ,
1070+ self .diffcrash_home / f "DFC_Merge_All_{ self .crash_code } " ,
10751071 self .project_dir ,
10761072 merge_inputfile ,
10771073 ],
@@ -1108,7 +1104,7 @@ def run_merge(self, pool: futures.ThreadPoolExecutor):
11081104 print (str_success (msg ))
11091105 self .logger .info (msg )
11101106
1111- def is_logfile_successful (self , logfile : str ) -> bool :
1107+ def is_logfile_successful (self , logfile : Path ) -> bool :
11121108 """Checks if a logfile indicates a success
11131109
11141110 Parameters
@@ -1127,7 +1123,7 @@ def is_logfile_successful(self, logfile: str) -> bool:
11271123 return True
11281124 return False
11291125
1130- def _create_merge_input_file (self , directory : str ) -> str :
1126+ def _create_merge_input_file (self , directory : Path ) -> Path :
11311127 """Create an input file for the merge executable
11321128
11331129 Notes
@@ -1136,7 +1132,7 @@ def _create_merge_input_file(self, directory: str) -> str:
11361132 """
11371133
11381134 # creates default inputfile for DFC_Merge
1139- filepath = os . path . join ( directory , "merge_all.txt" )
1135+ filepath = directory / "merge_all.txt"
11401136 with open (filepath , "w" , encoding = "utf-8" ) as merge_input_file :
11411137 merge_input_file .write ("eigen_all ! Name of eigen input file\n " )
11421138 merge_input_file .write (
@@ -1156,7 +1152,7 @@ def _create_merge_input_file(self, directory: str) -> str:
11561152
11571153 return filepath
11581154
1159- def _create_eigen_input_file (self , directory : str ) -> str :
1155+ def _create_eigen_input_file (self , directory : Path ) -> Path :
11601156 """Create an input file for the eigen executable
11611157
11621158 Notes
@@ -1165,7 +1161,7 @@ def _create_eigen_input_file(self, directory: str) -> str:
11651161 """
11661162
11671163 # creates default inputfile for DFC_Eigen
1168- filepath = os . path . join ( directory , "eigen_all.txt" )
1164+ filepath = directory / "eigen_all.txt"
11691165 with open (filepath , "w" , encoding = "utf-8" ) as eigen_input_file :
11701166 eigen_input_file .write ("matrix_all\n " )
11711167 eigen_input_file .write ('""\n ' )
@@ -1179,16 +1175,15 @@ def _create_eigen_input_file(self, directory: str) -> str:
11791175
11801176 return filepath
11811177
1182- def _create_matrix_input_file (self , directory : str ) -> str :
1178+ def _create_matrix_input_file (self , directory : Path ) -> Path :
11831179 """Create an input file for the matrix executable
11841180
11851181 Notes
11861182 -----
11871183 From the official diffcrash docs.
11881184 """
1189-
1185+ filepath = directory / "matrix.txt"
11901186 # creates default inputfile for DFC_Matrix
1191- filepath = os .path .join (directory , "matrix.txt" )
11921187 with open (filepath , "w" , encoding = "utf-8" ) as matrix_input_file :
11931188 matrix_input_file .write ("0 1000 ! Initial and final time stept to consider\n " )
11941189 matrix_input_file .write ('"" ! not used\n ' )
@@ -1206,7 +1201,7 @@ def clear_project_dir(self):
12061201 self .logger .removeHandler (handler )
12071202
12081203 # delete folder
1209- if os . path . exists ( self .project_dir ):
1204+ if self .project_dir . is_dir ( ):
12101205 shutil .rmtree (self .project_dir )
12111206
12121207 # reinit logger
@@ -1351,8 +1346,7 @@ def check_if_logfiles_show_success(self, pattern: str) -> List[str]:
13511346 _msg_logfile_nok = str_error ("Logfile '{0}' reports no success." )
13521347 messages = []
13531348
1354- logfiles = glob .glob (os .path .join (self .logfile_dir , pattern ))
1355- for filepath in logfiles :
1349+ for filepath in self .logfile_dir .glob (pattern ):
13561350 if not self .is_logfile_successful (filepath ):
13571351 messages .append (_msg_logfile_nok .format (filepath ))
13581352
0 commit comments