From 9dc47619bedd560ff25ae86b4afa835586fd3a8f Mon Sep 17 00:00:00 2001 From: Anton Utz Date: Thu, 21 Nov 2024 10:45:10 +0100 Subject: [PATCH] moved check to extra file Signed-off-by: Anton Utz --- src/ros_license_toolkit/checks.py | 61 +------------- .../license_checks/schema_check.py | 80 +++++++++++++++++++ src/ros_license_toolkit/main.py | 3 +- 3 files changed, 83 insertions(+), 61 deletions(-) create mode 100644 src/ros_license_toolkit/license_checks/schema_check.py diff --git a/src/ros_license_toolkit/checks.py b/src/ros_license_toolkit/checks.py index 2d52012..cc5ed04 100644 --- a/src/ros_license_toolkit/checks.py +++ b/src/ros_license_toolkit/checks.py @@ -18,9 +18,6 @@ from enum import IntEnum -from typing import Tuple - -from lxml import etree from ros_license_toolkit.package import Package, PackageException from ros_license_toolkit.ui_elements import NO_REASON_STR, green, red, yellow @@ -110,60 +107,4 @@ def _check(self, package: Package): raise NotImplementedError("Overwrite this") -class SchemaCheck(Check): - """This checks the xml scheme and returns the version number.""" - def __init__(self): - super().__init__() - xml_schema_3_parsed = etree.parse('./schemas/package_format3.xsd') - self.xml_schema_3 = etree.XMLSchema(xml_schema_3_parsed) - xml_schema_2_parsed = etree.parse('./schemas/package_format3.xsd') - self.xml_schema_2 = etree.XMLSchema(xml_schema_2_parsed) - xml_schema_1_parsed = etree.parse('./schemas/package_format1.xsd') - self.xml_schema_1 = etree.XMLSchema(xml_schema_1_parsed) - - def _check(self, package): - version, status = self.validate(package.abspath + "/package.xml") - if status: - self._success(f"Detected package.xml version {version}, " - "validation of scheme successful.") - else: - if version == -1: - self._failed("package.xml does not contain correct package " - "format number. Please use a real version. " - "(e.g. )") - elif version == 1: - self._failed( - "package.xml contains errors: " - f"{self.xml_schema_1.error_log.last_error.message}") - elif version == 2: - self._failed( - "package.xml contains errors: " - f"{self.xml_schema_2.error_log.last_error.message}") - elif version == 3: - self._failed( - "package.xml contains errors: " - f"{self.xml_schema_3.error_log.last_error.message}") - - def validate(self, pck_xml_path: str) -> Tuple[int, bool]: - """This is validating package.xml file from given path. - Automatically detects version number and validates - it with corresponding scheme, e.g. format 3. - If everything is correct, returns format number, else -1.""" - xml_doc_parsed: etree._ElementTree = etree.parse(pck_xml_path) - for element in xml_doc_parsed.getiterator(): - if element.tag == 'package' and 'format' in element.attrib: - version = element.attrib['format'] - status_check = False - if version == '3': - version = 3 - status_check = self.xml_schema_3.validate(xml_doc_parsed) - elif version == '2': - version = 2 - status_check = self.xml_schema_2.validate(xml_doc_parsed) - elif version == '1': - version = 1 - status_check = self.xml_schema_1.validate(xml_doc_parsed) - else: - version = -1 - return (version, status_check) - return (-1, False) + diff --git a/src/ros_license_toolkit/license_checks/schema_check.py b/src/ros_license_toolkit/license_checks/schema_check.py new file mode 100644 index 0000000..e037bc0 --- /dev/null +++ b/src/ros_license_toolkit/license_checks/schema_check.py @@ -0,0 +1,80 @@ +# Copyright (c) 2024 - for information on the respective copyright owner +# see the NOTICE file and/or the repository +# https://github.com/boschresearch/ros_license_toolkit + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""This Module contains SchemaCheck, which implements Check.""" + +from lxml import etree +from typing import Tuple +from ros_license_toolkit.checks import Check + + +class SchemaCheck(Check): + """This checks the xml scheme and returns the version number.""" + def __init__(self): + super().__init__() + xml_schema_3_parsed = etree.parse('./schemas/package_format3.xsd') + self.xml_schema_3 = etree.XMLSchema(xml_schema_3_parsed) + xml_schema_2_parsed = etree.parse('./schemas/package_format3.xsd') + self.xml_schema_2 = etree.XMLSchema(xml_schema_2_parsed) + xml_schema_1_parsed = etree.parse('./schemas/package_format1.xsd') + self.xml_schema_1 = etree.XMLSchema(xml_schema_1_parsed) + + def _check(self, package): + version, status = self.validate(package.abspath + "/package.xml") + if status: + self._success(f"Detected package.xml version {version}, " + "validation of scheme successful.") + else: + if version == -1: + self._failed("package.xml does not contain correct package " + "format number. Please use a real version. " + "(e.g. )") + elif version == 1: + self._failed( + "package.xml contains errors: " + f"{self.xml_schema_1.error_log.last_error.message}") + elif version == 2: + self._failed( + "package.xml contains errors: " + f"{self.xml_schema_2.error_log.last_error.message}") + elif version == 3: + self._failed( + "package.xml contains errors: " + f"{self.xml_schema_3.error_log.last_error.message}") + + def validate(self, pck_xml_path: str) -> Tuple[int, bool]: + """This is validating package.xml file from given path. + Automatically detects version number and validates + it with corresponding scheme, e.g. format 3. + If everything is correct, returns format number, else -1.""" + xml_doc_parsed = etree.parse(pck_xml_path) + for element in xml_doc_parsed.getiterator(): + if element.tag == 'package' and 'format' in element.attrib: + version = element.attrib['format'] + status_check = False + if version == '3': + version = 3 + status_check = self.xml_schema_3.validate(xml_doc_parsed) + elif version == '2': + version = 2 + status_check = self.xml_schema_2.validate(xml_doc_parsed) + elif version == '1': + version = 1 + status_check = self.xml_schema_1.validate(xml_doc_parsed) + else: + version = -1 + return (version, status_check) + return (-1, False) diff --git a/src/ros_license_toolkit/main.py b/src/ros_license_toolkit/main.py index ae11d0a..ebe3d99 100644 --- a/src/ros_license_toolkit/main.py +++ b/src/ros_license_toolkit/main.py @@ -24,7 +24,8 @@ import timeit from typing import Optional, Sequence -from ros_license_toolkit.checks import SchemaCheck, Status +from ros_license_toolkit.checks import Status +from ros_license_toolkit.license_checks.schema_check import SchemaCheck from ros_license_toolkit.license_checks.license_file_referenced_check import \ LicenseFilesReferencedCheck from ros_license_toolkit.license_checks.license_in_code_check import \