diff --git a/scripts/twist_check.py b/scripts/twist_check.py index ecf5b08..ffe6edd 100644 --- a/scripts/twist_check.py +++ b/scripts/twist_check.py @@ -91,7 +91,10 @@ class Stopped(Exception): def log(text): line = "%s %s" % (time.strftime("%H:%M:%S"), text) - print(line, flush=True) + try: + print(line, flush=True) + except OSError: + pass # SSH window closed: the run continues, the log file stays if LOG: LOG.write(line + "\n") LOG.flush() @@ -635,16 +638,25 @@ def run(mode, host): failure = ("Twist %s stopped" % mode, [ str(e), "Nothing changed in the configuration. " "Fix this, then start again with TWIST_CALIBRATE."]) + except Exception as e: + # Whatever went wrong, the nozzle must still be lifted and switched off + failure = ("Twist %s stopped" % mode, [ + "Unexpected error: %r" % e, "Nothing changed in the configuration." + " See twist_data/twist_run.log."]) signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGINT, signal.SIG_IGN) if started: cleanup(p) if res: - report_console(res) - report_html(res, report) - if res["block"]: - with open(os.path.join(DATA_DIR, "twist_suggested.cfg"), "w") as f: - f.write(res["block"]) + try: + if res["block"]: + with open(os.path.join(DATA_DIR, "twist_suggested.cfg"), + "w") as f: + f.write(res["block"]) + report_html(res, report) + report_console(res) + except Exception as e: + log("Could not write the report: %r" % e) log(res["title"]) return finish(p, res, failure, os.path.basename(report), started)