-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add no_use_of_undefined_import_prefixes check
Signed-off-by: romanodanilo <[email protected]>
- Loading branch information
1 parent
1102f21
commit cbbe8de
Showing
11 changed files
with
302 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
qc_otx/checks/core_checker/no_use_of_undefined_import_prefixes.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import logging, os | ||
|
||
from typing import List | ||
|
||
from lxml import etree | ||
|
||
from qc_baselib import Result, IssueSeverity | ||
|
||
from qc_otx import constants | ||
from qc_otx.checks import models, utils | ||
|
||
from qc_otx.checks.core_checker import core_constants | ||
|
||
logging.basicConfig(level=logging.DEBUG) | ||
|
||
RULE_SEVERITY = IssueSeverity.ERROR | ||
|
||
OTX_LINK_ATTRIBUTES = set() | ||
OTX_LINK_ATTRIBUTES.add("implements") | ||
OTX_LINK_ATTRIBUTES.add("validFor") | ||
OTX_LINK_ATTRIBUTES.add("procedure") | ||
OTX_LINK_ATTRIBUTES.add("valueOf") | ||
OTX_LINK_ATTRIBUTES.add("mutexLock") | ||
|
||
|
||
def check_rule(checker_data: models.CheckerData) -> None: | ||
""" | ||
Implements core checker rule Core_Chk005 | ||
Criterion: If an imported name is accessed by prefix in an OtxLink type attribute, | ||
the corresponding prefix definition shall exist in an <import> element. | ||
Severity: Critical | ||
""" | ||
logging.info("Executing no_use_of_undefined_import_prefixes check") | ||
|
||
rule_uid = checker_data.result.register_rule( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
emanating_entity="asam.net", | ||
standard="otx", | ||
definition_setting="1.0.0", | ||
rule_full_name="core.chk_005.no_use_of_undefined_import_prefixes", | ||
) | ||
|
||
tree = checker_data.input_file_xml_root | ||
root = tree.getroot() | ||
|
||
import_nodes = root.findall(".//import", namespaces=root.nsmap) | ||
|
||
if import_nodes is None: | ||
logging.error("No import nodes found. Skipping check..") | ||
return | ||
|
||
import_prefixes = [x.get("prefix") for x in import_nodes] | ||
|
||
attributes = utils.get_all_attributes(tree, root) | ||
otx_link_attributes = [x for x in attributes if x.name in OTX_LINK_ATTRIBUTES] | ||
|
||
logging.debug(f"attributes: {attributes}") | ||
logging.debug(f"import_prefixes: {import_prefixes}") | ||
logging.debug(f"otx_link_attributes: {otx_link_attributes}") | ||
|
||
for otx_link in otx_link_attributes: | ||
if ":" not in otx_link.value: | ||
continue | ||
current_value_split = otx_link.value.split(":") | ||
if len(current_value_split) == 0: | ||
continue | ||
current_prefix = current_value_split[0] | ||
|
||
has_issue = current_prefix not in import_prefixes | ||
|
||
if has_issue: | ||
issue_id = checker_data.result.register_issue( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
description="Issue flagging when prefix definition does not exists in an import element", | ||
level=RULE_SEVERITY, | ||
rule_uid=rule_uid, | ||
) | ||
|
||
checker_data.result.add_xml_location( | ||
checker_bundle_name=constants.BUNDLE_NAME, | ||
checker_id=core_constants.CHECKER_ID, | ||
issue_id=issue_id, | ||
xpath=otx_link.xpath, | ||
description=f"Imported prefix {current_prefix} not found across import elements", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk005_negative" | ||
package="Core_Chk005" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<imports> | ||
<import package="Core_Chk005" document="contexts" prefix="context" /> | ||
</imports> | ||
|
||
<declarations> | ||
<constant name="PI" visibility="PUBLIC" id="3-d1"> | ||
<specification>This defines global constant</specification> | ||
<realisation> | ||
<dataType xsi:type="Float"> | ||
<init value="3.14159265" /> | ||
</dataType> | ||
</realisation> | ||
</constant> | ||
</declarations> | ||
|
||
<validities> | ||
<validity name="Workshop" id="4-v1"> | ||
<specification>Valid if executed in a workshop environment</specification> | ||
<realisation xsi:type="IsEqual"> | ||
<!-- Inserted rule violation here - prefix not present among import nodes above--> | ||
<term xsi:type="StringValue" valueOf="foo:LOCATION" /> | ||
<term xsi:type="StringLiteral" value="DealershopWorkshop" /> | ||
</realisation> | ||
</validity> | ||
<validity name="Assembly" id="4-v2"> | ||
<specification>Valid if executed at an assembly line</specification> | ||
<realisation xsi:type="IsEqual"> | ||
<term xsi:type="StringValue" valueOf="context:LOCATION" /> | ||
<term xsi:type="StringLiteral" value="AssemblyLine" /> | ||
</realisation> | ||
</validity> | ||
</validities> | ||
|
||
<action id="a1"> | ||
<realisation xsi:type="Assignment"> | ||
<result xsi:type="FloatVariable" name="foo" /> | ||
<term xsi:type="FloatValue" valueOf="PI" /> | ||
</realisation> | ||
</action> | ||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx id="2" | ||
name="Core_Chk005_positive" | ||
package="Core_Chk005" | ||
version="1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
timestamp="2010-11-11T14:40:10"> | ||
|
||
<imports> | ||
<import package="Core_Chk005" document="contexts" prefix="context" /> | ||
</imports> | ||
|
||
<declarations> | ||
<constant name="PI" visibility="PUBLIC" id="3-d1"> | ||
<specification>This defines global constant</specification> | ||
<realisation> | ||
<dataType xsi:type="Float"> | ||
<init value="3.14159265" /> | ||
</dataType> | ||
</realisation> | ||
</constant> | ||
</declarations> | ||
|
||
<validities> | ||
<validity name="Workshop" id="4-v1"> | ||
<specification>Valid if executed in a workshop environment</specification> | ||
<realisation xsi:type="IsEqual"> | ||
<term xsi:type="StringValue" valueOf="context:LOCATION" /> | ||
<term xsi:type="StringLiteral" value="DealershopWorkshop" /> | ||
</realisation> | ||
</validity> | ||
<validity name="Assembly" id="4-v2"> | ||
<specification>Valid if executed at an assembly line</specification> | ||
<realisation xsi:type="IsEqual"> | ||
<term xsi:type="StringValue" valueOf="context:LOCATION" /> | ||
<term xsi:type="StringLiteral" value="AssemblyLine" /> | ||
</realisation> | ||
</validity> | ||
</validities> | ||
|
||
<action id="a1"> | ||
<realisation xsi:type="Assignment"> | ||
<result xsi:type="FloatVariable" name="foo" /> | ||
<term xsi:type="FloatValue" valueOf="PI" /> | ||
</realisation> | ||
</action> | ||
|
||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<otx xmlns="http://iso.org/OTX/1.0.0" id="1" | ||
name="ImportExample" | ||
package="Core_Chk005" | ||
version="1.0" | ||
timestamp="2010-03-18T14:40:10"> | ||
|
||
<specification> Example for showing the OTX document root structure </specification> | ||
</otx> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters