Skip to content
Snippets Groups Projects
Commit 574c6214 authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

Div

parent c23df80f
No related branches found
No related tags found
No related merge requests found
......@@ -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):
......
......@@ -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
......
......@@ -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))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment