Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for print statement #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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
Expand Down
2 changes: 2 additions & 0 deletions kodi_addon_checker/check_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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,
Expand Down
29 changes: 29 additions & 0 deletions kodi_addon_checker/check_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
See LICENSES/README.md for more information.
"""

import ast
import json
import os
import re
Expand Down Expand Up @@ -146,3 +147,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, f"{relative_path(str(file))} is marked as stand-alone executable"))


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,
f"{relative_path(str(file))} has a print statement on line {node.lineno}"))
except SyntaxError as e:
report.add(Record(PROBLEM, f"{relative_path(str(file))} failed to parse with {e}"))

except UnicodeDecodeError as e:
report.add(Record(PROBLEM, f"UnicodeDecodeError: {e}"))