diff options
author | Nino van Hooff <ninovanhooff@gmail.com> | 2020-01-20 18:22:02 +0300 |
---|---|---|
committer | Nino van Hooff <ninovanhooff@gmail.com> | 2020-01-20 18:24:26 +0300 |
commit | b830a6faa3f83814b795fdca41bb73b90e48ac68 (patch) | |
tree | 41d5744cfe4383af733bb10c4d07e4c7460ed337 /scripts | |
parent | 66105e8a3a8cfc54410d57a48eb0bfc81715417f (diff) |
Rewrite invalid imports checker to Python
Makes it consistent with other checkers we already have
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/check_invalid_imports.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/scripts/check_invalid_imports.py b/scripts/check_invalid_imports.py new file mode 100644 index 0000000000..121184e739 --- /dev/null +++ b/scripts/check_invalid_imports.py @@ -0,0 +1,60 @@ +import os +import re +import sys +from pathlib import Path + +""" +Run this file with the Cura project root as the working directory +""" + +class InvalidImportsChecker: + # compile regex + REGEX = re.compile(r"^\s*(from plugins|import plugins)") + + def check(self): + """ Checks for invalid imports + + :return: True if checks passed, False when the test fails + """ + cwd = os.getcwd() + cura_result = checker.check_dir(os.path.join(cwd, "cura")) + plugins_result = checker.check_dir(os.path.join(cwd, "plugins")) + result = cura_result and plugins_result + if not result: + print("error: sources contain invalid imports. Use relative imports when referencing plugin source files") + + return result + + def check_dir(self, root_dir: str) -> bool: + """ Checks a directory for invalid imports + + :return: True if checks passed, False when the test fails + """ + passed = True + for path_like in Path(root_dir).rglob('*.py'): + if not self.check_file(str(path_like)): + passed = False + + return passed + + def check_file(self, file_path): + """ Checks a file for invalid imports + + :return: True if checks passed, False when the test fails + """ + passed = True + with open(file_path, 'r', encoding = "utf-8") as inputFile: + # loop through each line in file + for line_i, line in enumerate(inputFile, 1): + # check if we have a regex match + match = self.REGEX.search(line) + if match: + path = os.path.relpath(file_path) + print("{path}:{line_i}:{match}".format(path=path, line_i=line_i, match=match.group(1))) + passed = False + return passed + + +if __name__ == "__main__": + checker = InvalidImportsChecker() + sys.exit(0 if checker.check() else 1) |