diff --git a/gmsh_interop/runner.py b/gmsh_interop/runner.py index 1fdc6cdaf6db871a472583d22530b41a4a1a3ac5..9ef66e1647931670ebd8a258442bfccff4ac94f4 100644 --- a/gmsh_interop/runner.py +++ b/gmsh_interop/runner.py @@ -125,7 +125,8 @@ class GmshRunner(object): incomplete_elements=None, other_options=[], extension="geo", gmsh_executable="gmsh", output_file_name="output.msh", - target_unit=None): + target_unit=None, + save_tmp_files_in=None): if isinstance(source, str): from warnings import warn warn("passing a string as 'source' is deprecated--use " @@ -147,6 +148,7 @@ class GmshRunner(object): self.other_options = other_options self.gmsh_executable = gmsh_executable self.output_file_name = output_file_name + self.save_tmp_files_in = save_tmp_files_in self.target_unit = target_unit.upper() if self.dimensions not in [1, 2, 3, None]: @@ -167,7 +169,9 @@ class GmshRunner(object): from pytools.prefork import call_capture_output retcode, stdout, stderr = call_capture_output(cmdline) - version = stderr.decode().strip() + # stderr can contain irregular info + import re + version = re.search(r'[0-9]+.[0-9]+.[0-9]+', stderr.decode().strip()).group() return LooseVersion(version) def __enter__(self): @@ -276,6 +280,36 @@ class GmshRunner(object): self.output_file = open(output_file_name, "r") + if self.save_tmp_files_in: + import shutil, errno + try: + shutil.copytree(working_dir, self.save_tmp_files_in) + except FileExistsError as exc: + import select, sys + print("%s exists! Overwrite? (Y/N, will default to Y in 10sec)." + % self.save_tmp_files_in) + decision = None + while not decision: + i, o, e = select.select([sys.stdin], [], [], 10) + if i == "N" or i == "n": + decision = 0 + elif i == "Y" or i == "y" or not i: + decision = 1 + else: + print("Illegal input %s, please retry." % i) + if decision == 0: + pass + else: + assert decision == 1 + shutil.rmtree(self.save_tmp_files_in) + shutil.copytree(working_dir, self.save_tmp_files_in) + except OSError as exc: + if exc.errno == errno.ENOTDIR: + shutil.copy(output_file_name, + '/'.join([self.save_tmp_files_in, + self.output_file_name])) + else: raise + self.temp_dir_mgr = temp_dir_mgr return self except Exception: