Skip to content

Commit

Permalink
moved check to extra file
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Utz <[email protected]>
  • Loading branch information
ant-u committed Nov 21, 2024
1 parent 03279cb commit 9dc4761
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 61 deletions.
61 changes: 1 addition & 60 deletions src/ros_license_toolkit/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. <package format=\"3\">)")
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)

80 changes: 80 additions & 0 deletions src/ros_license_toolkit/license_checks/schema_check.py
Original file line number Diff line number Diff line change
@@ -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. <package format=\"3\">)")
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)
3 changes: 2 additions & 1 deletion src/ros_license_toolkit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 9dc4761

Please sign in to comment.