From 8212b8e127bd0ff1e94010c4caed4ff679d8c7e7 Mon Sep 17 00:00:00 2001 From: Thomas Graf Date: Tue, 7 May 2024 17:33:44 +0200 Subject: [PATCH 1/2] feat: do not add empty acknowledgments, files and filehashes --- ChangeLog.md | 6 +++++- cli_support/CLI.py | 7 ++++--- cli_support/cli_assessment_summary.py | 13 ++++++++----- cli_support/cli_file_item_base.py | 18 ++++++++++-------- cli_support/cli_general_information.py | 12 +++++++----- cli_support/cli_license.py | 9 +++++---- tests/test_cli_clifile.py | 24 +++++++----------------- 7 files changed, 46 insertions(+), 43 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 862eebd..be3e5d9 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,8 +1,12 @@ # Python library to read Component License Information (CLI) files +## NEXT + +* do not add empty acknowledgments, files and filehashes. + ## V2.0.1 -* remove requests dependency (has never been used...) +* remove requests dependency (has never been used...). ## V2.0.0 diff --git a/cli_support/CLI.py b/cli_support/CLI.py index 5a074cc..4bfde83 100644 --- a/cli_support/CLI.py +++ b/cli_support/CLI.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# (c) 2019-2023 Siemens AG +# (c) 2019-2024 Siemens AG # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -197,8 +197,9 @@ def write_to_file(self, filename: str) -> None: tags.text = ",".join(str(x) for x in self.tags) comment = ET.SubElement(root, "Comment") - cdata = self.CDATA(self.comment) - comment.append(cdata) + if self.comment: + cdata = self.CDATA(self.comment) + comment.append(cdata) tree = ET.ElementTree(root) if not sys.version_info < (3, 9): diff --git a/cli_support/cli_assessment_summary.py b/cli_support/cli_assessment_summary.py index a7a4b68..855bcf7 100644 --- a/cli_support/cli_assessment_summary.py +++ b/cli_support/cli_assessment_summary.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# (c) 2023 Siemens AG +# (c) 2023-2024 Siemens AG # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -59,9 +59,11 @@ def _read_from_element(self, element: ET.Element) -> None: def _append_to_xml(self, parent: ET.Element) -> None: """Write assessment summary to XML element.""" gi = ET.SubElement(parent, "AssessmentSummary") + node = ET.SubElement(gi, "GeneralAssessment") - cdata = self.CDATA(self.general_assessment) - node.append(cdata) + if self.general_assessment: + cdata = self.CDATA(self.general_assessment) + node.append(cdata) node = ET.SubElement(gi, "CriticalFilesFound") node.text = self.critical_files_found @@ -76,5 +78,6 @@ def _append_to_xml(self, parent: ET.Element) -> None: node.text = self.usage_restrictions_found node = ET.SubElement(gi, "AdditionalNotes") - cdata = self.CDATA(self.additional_notes) - node.append(cdata) + if self.additional_notes: + cdata = self.CDATA(self.additional_notes) + node.append(cdata) diff --git a/cli_support/cli_file_item_base.py b/cli_support/cli_file_item_base.py index 80b889f..19fdbcf 100644 --- a/cli_support/cli_file_item_base.py +++ b/cli_support/cli_file_item_base.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# (c) 2019-2023 Siemens AG +# (c) 2019-2024 Siemens AG # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -40,10 +40,12 @@ def _read_files_from_element(self, element: ET.Element) -> None: def _append_to_xml(self, parent: ET.Element) -> None: """Write files and hashes to XML element.""" - file_data = ET.SubElement(parent, "Files") - cdata = self.CDATA("\n".join(str(x) for x in self.files)) - file_data.append(cdata) - - hash_data = ET.SubElement(parent, "FileHash") - cdata = self.CDATA("\n".join(str(x) for x in self.hashes)) - hash_data.append(cdata) + if len(self.files) > 0: + file_data = ET.SubElement(parent, "Files") + cdata = self.CDATA("\n".join(str(x) for x in self.files)) + file_data.append(cdata) + + if len(self.hashes) > 0: + hash_data = ET.SubElement(parent, "FileHash") + cdata = self.CDATA("\n".join(str(x) for x in self.hashes)) + hash_data.append(cdata) diff --git a/cli_support/cli_general_information.py b/cli_support/cli_general_information.py index 70f6e2c..83c99dd 100644 --- a/cli_support/cli_general_information.py +++ b/cli_support/cli_general_information.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# (c) 2023 Siemens AG +# (c) 2023-2024 Siemens AG # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -115,12 +115,14 @@ def _append_to_xml(self, parent: ET.Element) -> None: node.text = self.component_release_date hash_data = ET.SubElement(gi, "LinkComponentManagement") - cdata = self.CDATA(self.link_component_management) - hash_data.append(cdata) + if self.link_component_management: + cdata = self.CDATA(self.link_component_management) + hash_data.append(cdata) hash_data = ET.SubElement(gi, "LinkScanTool") - cdata = self.CDATA(self.link_scan_tool) - hash_data.append(cdata) + if self.link_scan_tool: + cdata = self.CDATA(self.link_scan_tool) + hash_data.append(cdata) ci = ET.SubElement(gi, "ComponentId") diff --git a/cli_support/cli_license.py b/cli_support/cli_license.py index efa1176..4fcd79e 100644 --- a/cli_support/cli_license.py +++ b/cli_support/cli_license.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# (c) 2019-2023 Siemens AG +# (c) 2019-2024 Siemens AG # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -77,9 +77,10 @@ def _append_to_xml(self, parent: ET.Element) -> None: CliFileItemBase._append_to_xml(self, lic) - ack = ET.SubElement(lic, "Acknowledgements") - cdata = self.CDATA("\n".join(str(x) for x in self.acknowledgements)) - ack.append(cdata) + if len(self.acknowledgements) > 0: + ack = ET.SubElement(lic, "Acknowledgements") + cdata = self.CDATA("\n".join(str(x) for x in self.acknowledgements)) + ack.append(cdata) tags = ET.SubElement(lic, "Tags") tags.text = ",".join(str(x) for x in self.tags) diff --git a/tests/test_cli_clifile.py b/tests/test_cli_clifile.py index 808791b..baf02ec 100644 --- a/tests/test_cli_clifile.py +++ b/tests/test_cli_clifile.py @@ -1,5 +1,5 @@ # ------------------------------------------------------------------------------- -# (c) 2022-2023 Siemens AG +# (c) 2022-2024 Siemens AG # All Rights Reserved. # Author: thomas.graf@siemens.com # @@ -179,35 +179,25 @@ def test_write_file_minimal(self) -> None: - - - - + + - - + None None None None - - + - - - - - - + - - + ''' self.delete_file(self.TESTFILE4) From d08ce0ea0559f1cee1f2eb2452a241c92bc6da06 Mon Sep 17 00:00:00 2001 From: Thomas Graf Date: Tue, 7 May 2024 17:34:19 +0200 Subject: [PATCH 2/2] chores: be more verbose while running checks --- RunChecks.ps1 | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/RunChecks.ps1 b/RunChecks.ps1 index 188d738..e667f3e 100644 --- a/RunChecks.ps1 +++ b/RunChecks.ps1 @@ -2,13 +2,21 @@ # Run quality checks # ------------------ -# 2023-12-24, T. Graf +# 2024-05-07, T. Graf +Write-Host "flake8 ..." poetry run flake8 + +Write-Host "markdownlint ..." npx -q markdownlint-cli *.md + +Write-Host "isort ..." poetry run isort . + +Write-Host "mypy ..." poetry run mypy . +Write-Host "Done." # ----------------------------------- # -----------------------------------