diff --git a/README.md b/README.md
index ea362788..c8fc806b 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,8 @@ It can also be used locally for detecting problems in your addons.
- Check if all PO files are valid
+- Check for circular dependencies
+
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/addons/Repository.py b/kodi_addon_checker/addons/Repository.py
index af28d2c7..7d7e5d54 100644
--- a/kodi_addon_checker/addons/Repository.py
+++ b/kodi_addon_checker/addons/Repository.py
@@ -16,15 +16,25 @@
class Repository():
- def __init__(self, version, path):
+ def __init__(self, version, path, local_xml=False):
+ """Get information of all the addons
+ :version: Kodi version name for the repository
+ :path: path to the Kodi repository file
+ :local_xml: use a local xml file instead of gzip, this is for tests only
+ """
super(Repository, self).__init__()
self.version = version
self.path = path
- content = requests.get(path, timeout=(10, 10)).content
- if path.endswith('.gz'):
- with gzip.open(BytesIO(content), 'rb') as xml_file:
+ if local_xml:
+ with open(path, 'rb') as xml_file:
content = xml_file.read()
+ else:
+ content = requests.get(path, timeout=(10, 10)).content
+
+ if path.endswith('.gz'):
+ with gzip.open(BytesIO(content), 'rb') as xml_file:
+ content = xml_file.read()
tree = ET.fromstring(content)
self.addons = []
diff --git a/kodi_addon_checker/check_addon.py b/kodi_addon_checker/check_addon.py
index 7078f171..ade966f2 100644
--- a/kodi_addon_checker/check_addon.py
+++ b/kodi_addon_checker/check_addon.py
@@ -59,6 +59,8 @@ def start(addon_path, args, all_repo_addons, config=None):
check_dependencies.check_reverse_dependencies(addon_report, addon_id, args.branch, all_repo_addons)
+ check_dependencies.check_circular_dependencies(addon_report, all_repo_addons, parsed_xml, args.branch)
+
check_files.check_file_permission(addon_report, file_index)
check_files.check_for_invalid_xml_files(addon_report, file_index)
diff --git a/kodi_addon_checker/check_dependencies.py b/kodi_addon_checker/check_dependencies.py
index ec1865af..d187b298 100644
--- a/kodi_addon_checker/check_dependencies.py
+++ b/kodi_addon_checker/check_dependencies.py
@@ -139,6 +139,58 @@ def check_reverse_dependencies(report: Report, addon: str, branch_name: str, all
.format(", ".join(sorted([r.id for r in rdependsLowerBranch])), len(rdependsLowerBranch))))
+def check_circular_dependencies(report: Report, all_repo_addons: dict, parsed_xml, branch_name: str):
+ """Check for any circular dependencies in addon.xml file and reports them
+ :report: current report for adding result records
+ :all_repo_addons: dictionary return by all_repo_addon() function
+ :parsed_xml: parsed addon.xml file
+ :branch_name: name of the kodi branch/version
+ """
+ root_addon = Addon(parsed_xml)
+ ignore_list = _get_ignore_list(branch_name)
+
+ dependencies = []
+ dependency_tree = {}
+
+ # add dependencies of `addon` to dependencies list
+ def add_dependencies(addon):
+ new_depends = []
+ for dependency in addon.dependencies:
+ # don't check ignored for dependencies
+ if dependency.id in ignore_list:
+ continue
+ new_depends.append(dependency)
+
+ if addon.id not in dependency_tree:
+ dependency_tree[addon.id] = []
+ if new_depends:
+ dependency_tree[addon.id] = [d.id for d in new_depends]
+ dependencies.extend(new_depends)
+
+ add_dependencies(root_addon)
+
+ i = 0
+ # add all dependencies and their dependencies
+ while i < len(dependencies):
+ if dependencies[i].id not in dependency_tree:
+ for _, repo in sorted(all_repo_addons.items()):
+ found_addon = repo.find(dependencies[i].id)
+ if found_addon:
+ add_dependencies(found_addon)
+ i += 1
+
+ if dependency_tree:
+ circular_dependencies = []
+
+ for depend in list(dependency_tree.keys()):
+ if root_addon.id in dependency_tree.get(depend, []):
+ circular_dependencies.append(depend)
+
+ if circular_dependencies:
+ report.add(Record(PROBLEM, "Circular dependencies: {}"
+ .format(", ".join(sorted(set(circular_dependencies))))))
+
+
def _get_ignore_list(branch_name):
"""Generate an dependency ignore list based
on the branch name
diff --git a/tests/test_check_dependencies.py b/tests/test_check_dependencies.py
new file mode 100644
index 00000000..d0c62510
--- /dev/null
+++ b/tests/test_check_dependencies.py
@@ -0,0 +1,44 @@
+import unittest
+
+from os.path import abspath, dirname, join
+import xml.etree.ElementTree as ET
+
+from kodi_addon_checker.addons.Repository import Repository
+from kodi_addon_checker.check_dependencies import check_circular_dependencies
+from kodi_addon_checker.common import load_plugins
+from kodi_addon_checker.record import Record
+from kodi_addon_checker.reporter import ReportManager
+from kodi_addon_checker.report import Report
+
+HERE = abspath(dirname(__file__))
+
+
+class TestCheckDependencies(unittest.TestCase):
+ """Test dependency checks
+ """
+
+ def setUp(self):
+ """Test setup
+ """
+ load_plugins()
+ ReportManager.enable(["array"])
+ self.report = Report("")
+ self.branch = 'krypton'
+ self.path = join(HERE, 'test_data', 'Circular_depend')
+
+ def test_check_circular_dependency(self):
+ """Test circular dependency check
+ """
+ addon_xml = join(self.path, "addon.xml")
+ addons_xml = join(self.path, "addons.xml")
+
+ parsed_xml = ET.parse(addon_xml).getroot()
+ all_repo_addons = {self.branch: Repository(self.branch, addons_xml, local_xml=True)}
+
+ check_circular_dependencies(self.report, all_repo_addons, parsed_xml, self.branch)
+
+ records = [Record.__str__(r) for r in ReportManager.getEnabledReporters()[0].reports]
+ output = [record for record in records if record.startswith("ERROR: Circular")]
+ expected = ["ERROR: Circular dependencies: plugin.test.one"]
+
+ self.assertListEqual(expected, output)
diff --git a/tests/test_data/Circular_depend/addon.xml b/tests/test_data/Circular_depend/addon.xml
new file mode 100644
index 00000000..921380cb
--- /dev/null
+++ b/tests/test_data/Circular_depend/addon.xml
@@ -0,0 +1,23 @@
+
+
+
+
+
+
+
+ video
+
+
+ Testing
+ Testing
+ en
+ This is just a test
+ all
+ GPL-3.0-only
+
+ icon.png
+ fanart.jpg
+
+ Testing 123
+
+
diff --git a/tests/test_data/Circular_depend/addons.xml b/tests/test_data/Circular_depend/addons.xml
new file mode 100644
index 00000000..bfb3ce0a
--- /dev/null
+++ b/tests/test_data/Circular_depend/addons.xml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+ video
+
+
+ Testing
+ Testing
+ en
+ This is just a test
+ all
+ GPL-3.0-only
+
+ icon.png
+ fanart.jpg
+
+ Testing 123
+
+
+