From 4683fa3d25bdfad9f6368424583b41da3c179f87 Mon Sep 17 00:00:00 2001 From: Dan Lipsitt Date: Fri, 16 Jan 2015 16:22:34 -0800 Subject: [PATCH 1/7] Inherit clean command so that it supports the usual flags. This changes the semantics of setup.py slightly. '--all' needs to be passed in order to delete everything that is deleted by previous versions. Some additional build files may also be deleted. --- setup.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/setup.py b/setup.py index 471e546f3..2d87e9c0a 100644 --- a/setup.py +++ b/setup.py @@ -9,6 +9,7 @@ versioneer.tag_prefix = '' versioneer.parentdir_prefix = '' from setuptools import setup, find_packages, Command +from distutils.command.clean import clean as Clean import os import shutil import glob @@ -26,21 +27,10 @@ def package_data_dirs(source, sub_folders): return dirs -class CleanCommand(Command): - description = "clean build artifacts" - user_options = [] - boolean_options = [] - - def initialize_options(self): - pass - - def finalize_options(self): - pass +class CleanCommand(Clean): def run(self): - if os.path.exists('build'): - print "Deleting build directory" - shutil.rmtree('build') + Clean.run(self) eggs = glob.glob('OctoPrint*.egg-info') for egg in eggs: print "Deleting %s directory" % egg From 136736d307459476840d7b6fc3211d0febcfb049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 9 Mar 2017 14:35:07 +0100 Subject: [PATCH 2/7] Reset line numbers if printer sends "start" when operational Might have been an external reset of the printer we didn't otherwise detect, and unless we reset the line numbers we'll now be off and run into resend requests. Solves #1676 --- src/octoprint/plugins/virtual_printer/virtual.py | 11 ++++++++++- src/octoprint/util/comm.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/octoprint/plugins/virtual_printer/virtual.py b/src/octoprint/plugins/virtual_printer/virtual.py index e231fb7a5..456456535 100644 --- a/src/octoprint/plugins/virtual_printer/virtual.py +++ b/src/octoprint/plugins/virtual_printer/virtual.py @@ -25,7 +25,8 @@ class VirtualPrinter(object): sleep_after_regex = re.compile("sleep_after ([GMTF]\d+) (\d+)") sleep_after_next_regex = re.compile("sleep_after_next ([GMTF]\d+) (\d+)") custom_action_regex = re.compile("action_custom ([a-zA-Z0-9_]+)(\s+.*)?") - prepare_ok_regex = re.compile("prepare_ok (.*)?") + prepare_ok_regex = re.compile("prepare_ok (.*)") + send_regex = re.compile("send (.*)") def __init__(self, seriallog_handler=None, read_timeout=5.0, write_timeout=10.0): import logging @@ -560,6 +561,11 @@ class VirtualPrinter(object): | Sleeps s after each execution of sleep_after_next | Sleeps s after execution of next + + # Misc + + send + | Sends back """ for line in usage.split("\n"): self._send("echo: {}".format(line.strip())) @@ -589,6 +595,7 @@ class VirtualPrinter(object): sleep_after_next_match = VirtualPrinter.sleep_after_next_regex.match(data) custom_action_match = VirtualPrinter.custom_action_regex.match(data) prepare_ok_match = VirtualPrinter.prepare_ok_regex.match(data) + send_match = VirtualPrinter.send_regex.match(data) if sleep_match is not None: interval = int(sleep_match.group(1)) @@ -612,6 +619,8 @@ class VirtualPrinter(object): elif prepare_ok_match is not None: ok = prepare_ok_match.group(1) self._prepared_oks.append(ok) + elif send_match is not None: + self._send(send_match.group(1)) except: pass diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 562a3b3af..15bbf5ebf 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -1392,6 +1392,18 @@ class MachineCom(object): self._log("There was a timeout while trying to connect to the printer") self.close(wait=False) + ### Operational (idle or busy) + elif self._state in (self.STATE_OPERATIONAL, + self.STATE_PRINTING, + self.STATE_PAUSED, + self.STATE_TRANSFERING_FILE): + if line == "start": # exact match, to be on the safe side + message = "Printer sent 'start' while already operational. External reset? " \ + "Resetting line numbers to be on the safe side" + self._log(message) + self._logger.warn(message) + self.resetLineNumbers() + except: self._logger.exception("Something crashed inside the serial connection loop, please report this in OctoPrint's bug tracker:") From d7b29a87256399b80579b244c417394a7e2a557e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 9 Mar 2017 15:36:26 +0100 Subject: [PATCH 3/7] Added @kantlivelong to AUTHORS.md --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index a91f18e6f..4279200cf 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -78,6 +78,7 @@ date of first contribution): * [Eyal Soha](https://github.com/eyal0) * [Greg Hulands](https://github.com/ghulands) * [Andreas Werner](https://github.com/gallore) + * [Shawn Bruce](https://github.com/kantlivelong) OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by [Daid Braam](https://github.com/daid). Parts of its communication layer and From d298c3c6727ac4c39267ccebe6e733c1dcfafc9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 9 Mar 2017 15:38:52 +0100 Subject: [PATCH 4/7] Apply analysis throttle only every n lines Default n = 100, configurable via config.yaml. Should make analysis a bit faster while still giving the Pi air to breathe. --- src/octoprint/filemanager/analysis.py | 7 +++++-- src/octoprint/settings.py | 3 ++- src/octoprint/util/gcodeInterpreter.py | 10 +++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/octoprint/filemanager/analysis.py b/src/octoprint/filemanager/analysis.py index f0c515b2e..a774cc378 100644 --- a/src/octoprint/filemanager/analysis.py +++ b/src/octoprint/filemanager/analysis.py @@ -308,9 +308,12 @@ class GcodeAnalysisQueue(AbstractAnalysisQueue): def _do_analysis(self, high_priority=False): try: throttle = settings().getFloat(["gcodeAnalysis", "throttle_highprio"]) if high_priority else settings().getFloat(["gcodeAnalysis", "throttle_normalprio"]) + throttle_lines = settings().getInt(["gcodeAnalysis", "throttle_lines"]) if throttle > 0: - def throttle_callback(): - time.sleep(throttle) + def throttle_callback(filePos, readBytes): + if filePos % throttle_lines == 0: + # only apply throttle every 100 lines + time.sleep(throttle) else: throttle_callback = None diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 6640bbc18..57c3f49d8 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -179,7 +179,8 @@ default_settings = { "gcodeAnalysis": { "maxExtruders": 10, "throttle_normalprio": 0.01, - "throttle_highprio": 0.0 + "throttle_highprio": 0.0, + "throttle_lines": 100 }, "feature": { "temperatureGraph": True, diff --git a/src/octoprint/util/gcodeInterpreter.py b/src/octoprint/util/gcodeInterpreter.py index f05481594..ac10954db 100644 --- a/src/octoprint/util/gcodeInterpreter.py +++ b/src/octoprint/util/gcodeInterpreter.py @@ -221,7 +221,7 @@ class gcode(object): self._reenqueue = reenqueue def _load(self, gcodeFile, printer_profile, throttle=None): - filePos = 0 + lineNo = 0 readBytes = 0 pos = Vector3D(0.0, 0.0, 0.0) posOffset = Vector3D(0.0, 0.0, 0.0) @@ -245,18 +245,18 @@ class gcode(object): for line in gcodeFile: if self._abort: raise AnalysisAborted(reenqueue=self._reenqueue) - filePos += 1 + lineNo += 1 readBytes += len(line) if isinstance(gcodeFile, (file)): percentage = float(readBytes) / float(self._fileSize) elif isinstance(gcodeFile, (list)): - percentage = float(filePos) / float(len(gcodeFile)) + percentage = float(lineNo) / float(len(gcodeFile)) else: percentage = None try: - if self.progressCallback is not None and (filePos % 1000 == 0) and percentage is not None: + if self.progressCallback is not None and (lineNo % 1000 == 0) and percentage is not None: self.progressCallback(percentage) except: pass @@ -444,7 +444,7 @@ class gcode(object): totalExtrusion.append(0.0) if throttle is not None: - throttle() + throttle(lineNo, readBytes) if self.progressCallback is not None: self.progressCallback(100.0) From fa16037e9a2445c701d7aa2f8d5e121d46e077f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 9 Mar 2017 16:39:16 +0100 Subject: [PATCH 5/7] Unit test for M115 response parsing --- tests/util/test_comm_helpers.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index a8870c288..762ac7281 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -237,3 +237,12 @@ class TestCommHelpers(unittest.TestCase): result = canonicalize_temperatures(parsed, current) self.assertDictEqual(expected, result) + @data( + ("KEY1:Value 1 FIRMWARE_NAME:Some Firmware With Spaces KEY2:Value 2", + dict(KEY1="Value 1", KEY2="Value 2", FIRMWARE_NAME="Some Firmware With Spaces")) + ) + @unpack + def test_parse_firmware_line(self, line, expected): + from octoprint.util.comm import parse_firmware_line + result = parse_firmware_line(line) + self.assertDictEqual(expected, result) From 559238ca3c30a5932c4317801fe599bf113f8e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 9 Mar 2017 17:07:29 +0100 Subject: [PATCH 6/7] More solid parsing of line number from resends Matches: * Resend:23 * Resend:N23 * Resend:N:23 * rs 23 * rs N23 * rs N:23 * rs N23 Expected checksum 109 Based on issue reported via PR #300 --- src/octoprint/util/comm.py | 29 ++++++++++++++++++++++------- tests/util/test_comm_helpers.py | 15 +++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 15bbf5ebf..749eb0a92 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -127,6 +127,9 @@ Groups will be as follows: regex_firmware_splitter = re.compile("\s*([A-Z0-9_]+):") """Regex to use for splitting M115 responses.""" +regex_resend_linenumber = re.compile("(N|N:)?(?P%s)" % regex_int_pattern) +"""Regex to use for request line numbers in resend requests""" + def serialList(): baselist=[] if os.name=="nt": @@ -1862,13 +1865,7 @@ class MachineCom(object): def _handleResendRequest(self, line): try: - lineToResend = None - try: - lineToResend = int(line.replace("N:", " ").replace("N", " ").replace(":", " ").split()[-1]) - except: - if "rs" in line: - lineToResend = int(line.split()[1]) - + lineToResend = parse_resend_line(line) if lineToResend is None: return False @@ -3014,6 +3011,24 @@ def parse_firmware_line(line): result[key] = value return result +def parse_resend_line(line): + """ + Parses the provided resend line and returns requested line number. + + Args: + line (str): the line to parse + + Returns: + int or None: the extracted line number to resend, or None if no number could be extracted + """ + + match = regex_resend_linenumber.search(line) + if match is not None: + return int(match.group("n")) + + return None + + def gcode_command_for_cmd(cmd): """ Tries to parse the provided ``cmd`` and extract the GCODE command identifier from it (e.g. "G0" for "G0 X10.0"). diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 762ac7281..b3eb49b6e 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -246,3 +246,18 @@ class TestCommHelpers(unittest.TestCase): from octoprint.util.comm import parse_firmware_line result = parse_firmware_line(line) self.assertDictEqual(expected, result) + + @data( + ("Resend:23", 23), + ("Resend: N23", 23), + ("Resend: N:23", 23), + ("rs 23", 23), + ("rs N23", 23), + ("rs N:23", 23), + ("rs N23 expected checksum 109", 23) # teacup, see #300 + ) + @unpack + def test_parse_resend_line(self, line, expected): + from octoprint.util.comm import parse_resend_line + result = parse_resend_line(line) + self.assertEqual(expected, result) From a49772fcf36c411bae7e5be516c0ee5133c7a490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 9 Mar 2017 18:34:28 +0100 Subject: [PATCH 7/7] Removed bits from clean command made redundant by #732 Force --all on stock clean command unless --orig is specified, in which case "python setup.py clean" behaves like the stock clean command. Additionally there are now --noeggs to skip egg deletion and --nopyc to skip pyc deletion. --- src/octoprint_setuptools/__init__.py | 81 +++++++++++++++------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/src/octoprint_setuptools/__init__.py b/src/octoprint_setuptools/__init__.py index e441ae507..9592c163e 100644 --- a/src/octoprint_setuptools/__init__.py +++ b/src/octoprint_setuptools/__init__.py @@ -52,65 +52,74 @@ def recursively_handle_files(directory, file_matcher, folder_matcher=None, folde class CleanCommand(_clean): - description = "clean build artifacts" - user_options = [] - boolean_options = [] + user_options = _clean.user_options + [("orig", None, "behave like original clean command"), + ("noeggs", None, "don't clean up eggs"), + ("nopyc", None, "don't clean up pyc files")] + boolean_options = _clean.boolean_options + ["orig", "noeggs", "nopyc"] - build_folder = "build" source_folder = "src" - eggs = [] + eggs =None @classmethod - def for_options(cls, build_folder="build", source_folder="src", eggs=None): + def for_options(cls, source_folder="src", eggs=None): if eggs is None: eggs = [] return type(cls)(cls.__name__, (cls,), dict( - build_folder=build_folder, source_folder=source_folder, eggs=eggs )) def initialize_options(self): - pass + _clean.initialize_options(self) + + self.orig = None + self.noeggs = None + self.nopyc = None def finalize_options(self): - pass + _clean.finalize_options(self) + + if not self.orig: + self.all = True def run(self): _clean.run(self) - - # build folder - if os.path.exists(self.__class__.build_folder): - print("Deleting build directory") - shutil.rmtree(self.__class__.build_folder) + if self.orig: + return # eggs - for egg in self.__class__.eggs: - globbed_eggs = glob.glob(egg) - for globbed_egg in globbed_eggs: - print("Deleting %s directory" % globbed_egg) - shutil.rmtree(globbed_egg) + if not self.noeggs: + for egg in self.eggs: + globbed_eggs = glob.glob(egg) + for globbed_egg in globbed_eggs: + print("deleting '%s' egg" % globbed_egg) + if not self.dry_run: + shutil.rmtree(globbed_egg) # pyc files - def delete_folder_if_empty(path, applied_handler): - if not applied_handler: - return - if len(os.listdir(path)) == 0: - shutil.rmtree(path) - print("Deleted %s since it was empty" % path) + if not self.nopyc: + def delete_folder_if_empty(path, applied_handler): + if not applied_handler: + return + if len(os.listdir(path)) == 0: + if not self.dry_run: + shutil.rmtree(path) + print("removed %s since it was empty" % path[len(self.source_folder):]) - def delete_file(path): - os.remove(path) - print("Deleted %s" % path) + def delete_file(path): + print("removing '%s'" % path[len(self.source_folder):]) + if not self.dry_run: + os.remove(path) - import fnmatch - recursively_handle_files( - os.path.abspath(self.__class__.source_folder), - lambda name: fnmatch.fnmatch(name.lower(), "*.pyc"), - folder_matcher=lambda dir, name, path: name != ".git", - folder_handler=delete_folder_if_empty, - file_handler=delete_file - ) + import fnmatch + print("recursively removing *.pyc from '%s'" % self.source_folder) + recursively_handle_files( + os.path.abspath(self.source_folder), + lambda name: fnmatch.fnmatch(name.lower(), "*.pyc"), + folder_matcher=lambda dir, name, path: name != ".git", + folder_handler=delete_folder_if_empty, + file_handler=delete_file + ) class NewTranslation(Command):