From 4531f44ed5a93684ebaab7cef92887d592583f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 25 Jan 2021 15:43:05 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20analysis:=20detect=20invalid=20a?= =?UTF-8?q?nalysis=20and=20produce=20error=20log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See #3930 --- src/octoprint/cli/analysis.py | 46 +++++++++++++++++++++++++++ src/octoprint/filemanager/analysis.py | 9 +++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/octoprint/cli/analysis.py b/src/octoprint/cli/analysis.py index 3ddcd4ae8..b2bcd4d4c 100644 --- a/src/octoprint/cli/analysis.py +++ b/src/octoprint/cli/analysis.py @@ -4,6 +4,8 @@ from __future__ import absolute_import, division, print_function, unicode_litera __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms of the AGPLv3 License" +import sys + import click click.disable_unicode_literals_warning = True @@ -11,6 +13,40 @@ click.disable_unicode_literals_warning = True # ~~ "octoprint util" commands +def validate_result(result): + dimensions = ("depth", "height", "width") + printing_area = ("maxX", "maxY", "maxZ", "minX", "minY", "minZ") + + def validate_list(data): + return not any(map(invalid_float, data)) + + def validate_dict(data, keys): + for k in keys: + if k not in data or invalid_float(data[k]): + return False + return True + + def invalid_float(value): + return value is None or value == float("inf") or value == float("-inf") + + if "dimensions" not in result or not validate_dict(result["dimensions"], dimensions): + return False + + if "extrusion_length" not in result or not validate_list(result["extrusion_length"]): + return False + + if "extrusion_volume" not in result or not validate_list(result["extrusion_volume"]): + return False + + if "printing_area" not in result or not validate_dict( + result["printing_area"], printing_area + ): + return False + + if "total_time" not in result or invalid_float(result["total_time"]): + return False + + @click.group() def analysis_commands(): pass @@ -92,6 +128,16 @@ def gcode_command( ) click.echo("DONE:{}s".format(monotonic_time() - start_time)) + + result = interpreter.get_result() + if not validate_result(result): + click.echo( + "ERROR:Invalid analysis result, please create a bug report in OctoPrint's " + "issue tracker and be sure to also include the GCODE file with which this " + "happened" + ) + sys.exit(-1) + click.echo("RESULTS:") click.echo( yaml.safe_dump( diff --git a/src/octoprint/filemanager/analysis.py b/src/octoprint/filemanager/analysis.py index 67a2761ff..5b62230ca 100644 --- a/src/octoprint/filemanager/analysis.py +++ b/src/octoprint/filemanager/analysis.py @@ -319,6 +319,10 @@ class AbstractAnalysisQueue(object): ) ) self._finished_callback(self._current, result) + except RuntimeError as exc: + self._logger.error( + "Analysis for {} ran into error: {}".format(self._current, exc) + ) finally: self._current = None self._current_progress = None @@ -477,7 +481,10 @@ class GcodeAnalysisQueue(AbstractAnalysisQueue): output = p.stdout.text self._logger.debug("Got output: {!r}".format(output)) - if "RESULTS:" not in output: + if "ERROR:" in output: + _, error = output.split("ERROR:") + raise RuntimeError(error.strip()) + elif "RESULTS:" not in output: raise RuntimeError("No analysis result found") _, output = output.split("RESULTS:")