diff --git a/src/octoprint/util/files.py b/src/octoprint/util/files.py index cd3a4b404..62c5cb820 100644 --- a/src/octoprint/util/files.py +++ b/src/octoprint/util/files.py @@ -2,6 +2,7 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agp __copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License" import itertools +import logging import os.path import re @@ -270,3 +271,47 @@ def silent_remove(file): os.remove(file) except OSError: pass + + +def search_through_file(path, term, regex=False): + if regex: + pattern = term + else: + pattern = re.escape(term) + compiled = re.compile(pattern) + + logger = logging.getLogger(__name__) + + try: + try: + # try native grep + import sarge + + result = sarge.capture_stderr(["grep", "-q", "-E", pattern, path]) + if result.stderr.text: + logger.warning( + "Error raised by native grep, falling back to python " + "implementation: {}".format(result.stderr.text.strip()) + ) + return search_through_file_python(path, term, compiled) + return result.returncode == 0 + except ValueError as exc: + if "Command not found" in str(exc): + return search_through_file_python(path, term, compiled) + else: + raise + except Exception: + logger.exception( + "Something unexpectedly went wrong while trying to " + "search for {} in {} via grep".format(term, path) + ) + + return False + + +def search_through_file_python(path, term, compiled): + with open(path, encoding="utf8", errors="replace") as f: + for line in f: + if term in line or compiled.match(term): + return True + return False diff --git a/tests/util/test_files.py b/tests/util/test_files.py index 5b2c09c77..272f39972 100644 --- a/tests/util/test_files.py +++ b/tests/util/test_files.py @@ -1,11 +1,17 @@ __license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html" __copyright__ = "Copyright (C) 2021 The OctoPrint Project - Released under terms of the AGPLv3 License" +import os +import re import unittest from ddt import data, ddt, unpack -from octoprint.util.files import sanitize_filename +from octoprint.util.files import ( + sanitize_filename, + search_through_file, + search_through_file_python, +) @ddt @@ -33,3 +39,30 @@ class FilesUtilTest(unittest.TestCase): self.fail("expected ValueError") except ValueError as ex: self.assertEqual(str(ex), "name must not contain / or \\") + + @data( + ("umlaut", False, True), + ("BOM", False, True), + (r"^[^#]*BOM", True, False), + ) + @unpack + def test_search_through_file(self, term, regex, expected): + path = os.path.join( + os.path.abspath(os.path.dirname(__file__)), "_files", "utf8_without_bom.txt" + ) + actual = search_through_file(path, term, regex=regex) + self.assertEqual(actual, expected) + + @data( + ("umlaut", True), + ("BOM", True), + (r"^[^#]*BOM", False), + ) + @unpack + def test_search_through_file_python(self, term, expected): + compiled = re.compile(term) + path = os.path.join( + os.path.abspath(os.path.dirname(__file__)), "_files", "utf8_without_bom.txt" + ) + actual = search_through_file_python(path, term, compiled) + self.assertEqual(actual, expected)