From e187c8e7035f5d034fa1ad9ba6e980625a2f21d0 Mon Sep 17 00:00:00 2001 From: mzfr Date: Sun, 20 Sep 2020 12:18:50 +0200 Subject: [PATCH] Add check for print statement --- README.md | 2 ++ kodi_addon_checker/check_addon.py | 2 ++ kodi_addon_checker/check_files.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/README.md b/README.md index 31620ed7..432e2ab6 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ It can also be used locally for detecting problems in your addons. - Check if all PO files are valid +- Check if there exists any print statement in the addon. + All of the validation and checks are done according to the kodi [addon rules](https://kodi.wiki/view/Add-on_rules) ## Installation diff --git a/kodi_addon_checker/check_addon.py b/kodi_addon_checker/check_addon.py index 339cfdd2..bb67fe0e 100644 --- a/kodi_addon_checker/check_addon.py +++ b/kodi_addon_checker/check_addon.py @@ -78,6 +78,8 @@ def start(addon_path, args, all_repo_addons, config=None): check_py3_compatibility.check_py3_compatibility(addon_report, addon_path, KodiVersion(args.branch)) + check_files.check_print_statement(addon_report, file_index) + if config.is_enabled("check_license_file_exists"): # check if license file is existing handle_files.addon_file_exists(addon_report, addon_path, diff --git a/kodi_addon_checker/check_files.py b/kodi_addon_checker/check_files.py index 6aa0000d..4312fe72 100644 --- a/kodi_addon_checker/check_files.py +++ b/kodi_addon_checker/check_files.py @@ -6,6 +6,7 @@ See LICENSES/README.md for more information. """ +import ast import json import os import re @@ -148,3 +149,31 @@ def check_file_permission(report: Report, file_index: list): file = os.path.join(file["path"], file["name"]) if os.path.isfile(file) and os.access(str(file), os.X_OK): report.add(Record(PROBLEM, "%s is marked as stand-alone executable" % relative_path(str(file)))) + + +def check_print_statement(report: Report, file_index: list): + """Check whether any addon files have a print statement in them + or not + :file_index: list having names and path of all the files present in addon + """ + for file in file_index: + if os.path.splitext(file["name"])[1] == '.py': + file = os.path.join(file["path"], file["name"]) + try: + with open(file, 'r', encoding="utf-8") as f: + source = f.read() + + try: + for node in ast.walk(ast.parse(source)): + if (isinstance(node, ast.Expr) and + isinstance(node.value, ast.Call) and + isinstance(node.value.func, ast.Name)): + + if node.value.func.id == "print": + report.add(Record(WARNING, "%s has a print statement on line %s" % + (relative_path(str(file)), node.lineno))) + except SyntaxError as e: + report.add(Record(PROBLEM, "%s failed to parse with %s" % (relative_path(str(file)), e))) + + except UnicodeDecodeError as e: + report.add(Record(PROBLEM, "UnicodeDecodeError: {}".format(e)))