diff --git a/wetb/hawc2/htc_contents.py b/wetb/hawc2/htc_contents.py index e0384baf6ea0efa8ac73f6c5ac637af0490472ef..3da738a0f77d73acf68a6bd3b56c7644a08c307c 100644 --- a/wetb/hawc2/htc_contents.py +++ b/wetb/hawc2/htc_contents.py @@ -204,6 +204,8 @@ class HTCSection(HTCContents): break else: section._add_contents(section.line_from_line(lines)) + else: + raise Exception("Section '%s' has not end" % section.name_) return section def line_from_line(self, lines): diff --git a/wetb/hawc2/log_file.py b/wetb/hawc2/log_file.py index 1bf59af5319411ff9b532c1907bc1aad073ad2fd..ed5f39e86e3a70ef16d4fc0fd3acccf7fdd8db60 100644 --- a/wetb/hawc2/log_file.py +++ b/wetb/hawc2/log_file.py @@ -23,6 +23,7 @@ INITIALIZATION = 'Initializing simulation' SIMULATING = "Simulating" DONE = "Simulation succeded" + class LogInterpreter(object): def __init__(self, time_stop): self.time_stop = time_stop @@ -60,7 +61,7 @@ class LogInterpreter(object): try: return float(time_line[time_line.index('=') + 1:time_line.index('Iter')]) except: - print ("Cannot extract time from #" + time_line + "#") + print("Cannot extract time from #" + time_line + "#") pass def update_status(self, new_lines=""): @@ -97,14 +98,16 @@ class LogInterpreter(object): _3to2list1 = list(txt.split('Elapsed time')) simulation_txt, rest, = _3to2list1[:1] + [_3to2list1[1:]] if "*** ERROR ***" in simulation_txt: - self.errors.extend([l.strip() for l in simulation_txt.strip().split("\n") if "error" in l.lower()]) + self.errors.extend([l.strip() + for l in simulation_txt.strip().split("\n") if "error" in l.lower()]) i1 = simulation_txt.rfind("Global time") if i1 > -1: self.current_time = self.extract_time(simulation_txt[i1:]) if self.current_time is not None and self.time_stop > 0: self.pct = int(100 * self.current_time // self.time_stop) try: - self.remaining_time = (time.time() - self.start_time[1]) / (self.current_time - self.start_time[0]) * (self.time_stop - self.current_time) + self.remaining_time = ( + time.time() - self.start_time[1]) / (self.current_time - self.start_time[0]) * (self.time_stop - self.current_time) except: pass if rest: @@ -118,7 +121,6 @@ class LogInterpreter(object): error_dict[error] = error_dict.get(error, 0) + 1 return "\n".join([("%d x %s" % (v, k), k)[v == 1] for k, v in error_dict.items()]) - def remaining_time_str(self): if self.remaining_time: if self.remaining_time < 3600: @@ -138,7 +140,6 @@ class LogFile(LogInterpreter): self.filename = log_filename LogInterpreter.__init__(self, time_stop) - @staticmethod def from_htcfile(htcfile, modelpath=None): logfilename = htcfile.simulation.logfile[0] @@ -150,13 +151,14 @@ class LogFile(LogInterpreter): def clear(self): # exist_ok does not exist in Python27 - if not os.path.exists(os.path.dirname(self.filename)): - os.makedirs(os.path.dirname(self.filename)) #, exist_ok=True) - try: - with open(self.filename, 'w', encoding='utf-8'): - pass - except PermissionError as e: - raise PermissionError(str(e) + "\nLog file cannot be cleared. Check if it is open in another program") + # if not os.path.exists(os.path.dirname(self.filename)): + # os.makedirs(os.path.dirname(self.filename)) #, exist_ok=True) + if os.path.isfile(self.filename): + try: + with open(self.filename, 'w', encoding='utf-8'): + pass + except PermissionError as e: + raise PermissionError(str(e) + "\nLog file cannot be cleared. Check if it is open in another program") LogInterpreter.clear(self) def update_status(self): @@ -174,6 +176,7 @@ class LogFile(LogInterpreter): if txt != "": LogInterpreter.update_status(self, txt) + class LogInfo(LogFile): def __init__(self, status, pct, remaining_time, lastline): self.status = status diff --git a/wetb/hawc2/simulation.py b/wetb/hawc2/simulation.py index 94de7078a476ee9e278820db571d52108b7b1dda..cac456acfe5a2a57430a32c8532c0fad4125ef9a 100755 --- a/wetb/hawc2/simulation.py +++ b/wetb/hawc2/simulation.py @@ -18,6 +18,8 @@ from wetb.hawc2 import log_file from wetb.hawc2.htc_file import HTCFile, fmt_path from wetb.hawc2.log_file import LogFile import tempfile +import stat +import shutil standard_library.install_aliases() @@ -240,7 +242,8 @@ class Simulation(object): if self.status == ABORTED: return if "error" in self.host.stdout.lower(): - self.errors = (list(set([l for l in self.host.stdout.split("\n") if 'error' in l.lower()]))) + self.errors = (list(set([l for l in self.host.stdout.split( + "\n") if 'error' in l.lower() and not 'rms error' in l]))) self.status = ERROR if 'HAWC2MB version:' not in self.host.stdout: self.errors.append(self.host.stdout) @@ -331,23 +334,33 @@ class Simulation(object): def simulate_distributed(self, raise_simulation_errors=True): try: - with tempfile.TemporaryDirectory(prefix="h2launcher_") as tmpdirname: - self.tmp_modelpath = tmpdirname + "/" - self.tmp_exepath = os.path.join(self.tmp_modelpath, os.path.relpath(self.exepath, self.modelpath)) + "/" - self.prepare_simulation() + tmpdirname = tempfile.TemporaryDirectory(prefix="h2launcher_%s_" % os.path.basename(self.filename)).name + self.tmp_modelpath = tmpdirname + "/" + self.tmp_exepath = os.path.join(self.tmp_modelpath, os.path.relpath(self.exepath, self.modelpath)) + "/" + self.prepare_simulation() + try: + self.simulate() + except Warning as e: + print("simulation failed", str(self)) + print("Trying to finish") + raise + finally: try: - self.simulate() - except Warning as e: - print("simulation failed", str(self)) - print("Trying to finish") + if self.status != ABORTED: + self.finish_simulation() + except Exception: + print("finish_simulation failed", str(self)) raise finally: - try: - if self.status != ABORTED: - self.finish_simulation() - except Exception: - print("finish_simulation failed", str(self)) - raise + def remove_readonly(fn, path, excinfo): + try: + os.chmod(path, stat.S_IWRITE) + fn(path) + except Exception as exc: + print("Skipped:", path, "because:\n", exc) + + shutil.rmtree(tmpdirname, onerror=remove_readonly) + except Exception as e: self.status = ERROR self.errors.append(str(e))