-
Notifications
You must be signed in to change notification settings - Fork 147
Issue 381 add tag value writer #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
meretp
wants to merge
3
commits into
spdx:refactor-python-tools
from
meretp:issue-381-add-tag-value-writer
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,31 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
| from datetime import datetime | ||
|
|
||
| from src.parser.error import SPDXParsingError | ||
|
|
||
|
|
||
| def datetime_from_str(date_str: str) -> datetime: | ||
| if not isinstance(date_str, str): | ||
| raise SPDXParsingError([f"Could not convert str to datetime, invalid type: {type(date_str).__name__}"]) | ||
| try: | ||
| date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ") | ||
| except ValueError: | ||
| raise SPDXParsingError( | ||
| [f'Could not convert str to datetime, format of {date_str} does not match "%Y-%m-%dT%H:%M:%SZ"']) | ||
| return date | ||
|
|
||
| def datetime_to_iso_string(date: datetime) -> str: | ||
| """ | ||
| Return an ISO-8601 representation of a datetime object. | ||
| """ | ||
| return date.isoformat() + "Z" | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,11 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
|
|
||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,26 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
| from typing import TextIO | ||
|
|
||
| from src.datetime_conversions import datetime_to_iso_string | ||
| from src.model.annotation import Annotation | ||
| from src.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value | ||
|
|
||
|
|
||
| def write_annotation(annotation: Annotation, text_output: TextIO): | ||
| """ | ||
| Write the fields of a single annotation to text_output. | ||
| """ | ||
| write_value("Annotator", annotation.annotator.to_serialized_string(), text_output) | ||
| write_value("AnnotationDate", datetime_to_iso_string(annotation.annotation_date), text_output) | ||
| write_text_value("AnnotationComment", annotation.annotation_comment, text_output, True) | ||
| write_value("AnnotationType", annotation.annotation_type.name, text_output) | ||
| write_value("SPDXREF", annotation.spdx_id, text_output, True) | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,31 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
| from src.model.checksum import Checksum, ChecksumAlgorithm | ||
|
|
||
|
|
||
| def write_checksum_to_tag_value(checksum: Checksum) -> str: | ||
| algorithm_name: str = checksum.algorithm.name | ||
| # Convert underscores to dashes, and other Blake2b-specific casing rules | ||
| if "_" in algorithm_name: | ||
| algorithm_name = CHECKSUM_ALGORITHM_TO_TV.get(algorithm_name) | ||
| if algorithm_name is None: | ||
| raise ValueError(f"Missing conversion rule for converting {checksum.algorithm.name} to tag-value string") | ||
|
Comment on lines
+17
to
+20
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a method in the json writer which we should extract and reuse here. |
||
| return "{0}: {1}".format(algorithm_name, checksum.value) | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| CHECKSUM_ALGORITHM_TO_TV = { | ||
| ChecksumAlgorithm.BLAKE2B_256.name: "BLAKE2b-256", | ||
| ChecksumAlgorithm.BLAKE2B_384.name: "BLAKE2b-384", | ||
| ChecksumAlgorithm.BLAKE2B_512.name: "BLAKE2b-512", | ||
| ChecksumAlgorithm.SHA3_256.name: "SHA3-256", | ||
| ChecksumAlgorithm.SHA3_384.name: "SHA3-384", | ||
| ChecksumAlgorithm.SHA3_512.name: "SHA3-512" | ||
| } | ||
This file contains hidden or 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,46 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
| from typing import TextIO | ||
|
|
||
| from src.datetime_conversions import datetime_to_iso_string | ||
| from src.model.document import CreationInfo | ||
| from src.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, write_optional_heading, \ | ||
| write_separator | ||
|
|
||
|
|
||
| def write_creation_info(creation_info: CreationInfo, text_output: TextIO): | ||
| """ | ||
| Write the creation info to text_output. | ||
| """ | ||
| write_value("SPDXVersion", creation_info.spdx_version, text_output) | ||
| write_value("DataLicense", creation_info.data_license, text_output) | ||
| write_value("DocumentNamespace", creation_info.document_namespace, text_output, True) | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| write_value("DocumentName", creation_info.name, text_output, True) | ||
| write_value("LicenseListVersion", str(creation_info.spdx_version), text_output, True) | ||
| write_value("SPDXID", creation_info.spdx_id, text_output) | ||
| write_text_value("DocumentComment", creation_info.document_comment, text_output, True) | ||
|
|
||
| write_optional_heading(creation_info.external_document_refs, "\n## External Document References\n", text_output) | ||
| for external_document_ref in creation_info.external_document_refs: | ||
| external_document_ref_str = " ".join([external_document_ref.document_ref_id, external_document_ref.document_uri, | ||
| external_document_ref.checksum.algorithm.name + ": " + external_document_ref.checksum.value]) | ||
| write_value("ExternalDocumentRef", external_document_ref_str, text_output) | ||
| write_separator(text_output) | ||
|
|
||
| text_output.write("## Creation Information\n") | ||
| # Write sorted creators | ||
| for creator in creation_info.creators: | ||
| write_value("Creator", creator.to_serialized_string(), text_output) | ||
|
|
||
| # write created | ||
| write_value("Created", datetime_to_iso_string(creation_info.created), text_output) | ||
| # possible comment | ||
| write_text_value("CreatorComment", creation_info.creator_comment, text_output, True) | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,28 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
| from typing import TextIO | ||
|
|
||
| from src.model.extracted_licensing_info import ExtractedLicensingInfo | ||
| from src.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value | ||
|
|
||
|
|
||
| def write_extracted_licensing_info(extracted_licensing_info: ExtractedLicensingInfo, text_output: TextIO): | ||
| """ | ||
| Write extracted licenses fields to out. | ||
| """ | ||
| write_value("LicenseID", extracted_licensing_info.license_id, text_output) | ||
| write_value("LicenseName", extracted_licensing_info.license_name, text_output, True) | ||
| write_text_value("LicenseComment", extracted_licensing_info.comment, text_output, True) | ||
|
|
||
| for cross_reference in sorted(extracted_licensing_info.cross_references): | ||
| write_value("LicenseCrossReference", cross_reference, text_output) | ||
|
|
||
| write_text_value("ExtractedText", extracted_licensing_info.extracted_text, text_output) |
This file contains hidden or 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,42 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
| from typing import TextIO | ||
|
|
||
| from src.model.file import File | ||
| from src.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, \ | ||
| write_field_or_none_or_no_assertion | ||
| from src.writer.tagvalue.checksum_writer import write_checksum_to_tag_value | ||
|
|
||
|
|
||
| def write_file(file: File, text_output: TextIO): | ||
| """ | ||
| Write all file information to output_text. | ||
| """ | ||
| text_output.write("## File Information\n") | ||
| write_value("FileName", file.name, text_output) | ||
| write_value("SPDXID", file.spdx_id, text_output, True) | ||
| for file_type in file.file_type: | ||
| write_value("FileType", file_type.name, text_output) | ||
| for file_checksum in file.checksums: | ||
| write_value("FileChecksum", write_checksum_to_tag_value(file_checksum), text_output) | ||
| write_field_or_none_or_no_assertion("LicenseConcluded", file.concluded_license, text_output, True) | ||
| write_field_or_none_or_no_assertion("LicenseInfoInFile", file.license_info_in_file, text_output, True) | ||
| write_field_or_none_or_no_assertion("FileCopyrightText", file.copyright_text, text_output, True) | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| write_text_value("LicenseComments", file.license_comment, text_output, True) | ||
|
|
||
| for attribution_text in file.attribution_texts: | ||
| write_text_value("FileAttributionText", attribution_text, text_output) | ||
|
|
||
| write_text_value("FileComment", file.comment, text_output, True) | ||
| write_text_value("FileNotice", file.notice, text_output, True) | ||
|
|
||
| for contributor in sorted(file.contributors): | ||
| write_value("FileContributor", contributor, text_output, True) | ||
This file contains hidden or 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,86 @@ | ||
| # Copyright (c) 2022 spdx contributors | ||
| # 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. | ||
| from typing import TextIO | ||
|
|
||
| from src.datetime_conversions import datetime_to_iso_string | ||
| from src.model.package import Package, PackageVerificationCode | ||
| from src.writer.tagvalue.tagvalue_writer_helper_functions import write_value, write_text_value, \ | ||
| write_field_or_none_or_no_assertion, transform_enum_name_to_tv, write_actor_or_no_assertion | ||
| from src.writer.tagvalue.checksum_writer import write_checksum_to_tag_value | ||
|
|
||
|
|
||
| def write_package(package: Package, text_output: TextIO): | ||
| """ | ||
| Write all package information to text_output. | ||
| """ | ||
| text_output.write("## Package Information\n") | ||
|
|
||
| write_value("PackageName", package.name, text_output, True) | ||
| write_value("SPDXID", package.spdx_id, text_output, True) | ||
| write_value("PackageVersion", package.version, text_output, True) | ||
| write_value("PackageDownloadLocation", package.download_location, text_output) | ||
| write_value("FilesAnalyzed", package.files_analyzed, text_output, True) | ||
| write_text_value("PackageSummary", package.summary, text_output, True) | ||
| for attribution_text in package.attribution_texts: | ||
| write_text_value("PackageAttributionText", attribution_text, text_output) | ||
|
|
||
| write_text_value("PackageSourceInfo", package.source_info, text_output, True) | ||
| write_value("PackageFileName", package.file_name, text_output, True) | ||
| write_actor_or_no_assertion("PackageSupplier", package.supplier, text_output, True) | ||
| write_actor_or_no_assertion("PackageOriginator", package.originator, text_output, True) | ||
|
|
||
| for package_checksum in package.checksums: | ||
| write_value("PackageChecksum", write_checksum_to_tag_value(package_checksum), text_output, True) | ||
|
|
||
| if package.verification_code: | ||
| package_verification_code = write_package_verification_code(package.verification_code) | ||
| write_value("PackageVerificationCode", package_verification_code, text_output, True) | ||
|
|
||
| write_text_value("PackageDescription", package.description, text_output, True) | ||
| write_text_value("PackageComment", package.comment, text_output, True) | ||
|
|
||
| write_field_or_none_or_no_assertion("PackageLicenseDeclared", package.license_declared, text_output, True) | ||
| write_field_or_none_or_no_assertion("PackageLicenseConcluded", package.license_concluded, text_output, True) | ||
| write_field_or_none_or_no_assertion("PackageLicenseInfoFromFiles", package.license_info_from_files, text_output, | ||
| True) | ||
|
|
||
| write_text_value("PackageLicenseComments", package.license_comment, text_output, True) | ||
| write_field_or_none_or_no_assertion("PackageCopyrightText", package.copyright_text, text_output, True) | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| write_value("PackageHomePage", package.homepage, text_output, True) | ||
|
|
||
| for external_reference in package.external_references: | ||
| external_reference_str = " ".join( | ||
| [transform_enum_name_to_tv(external_reference.category.name), external_reference.reference_type, | ||
| external_reference.locator] | ||
| ) | ||
| write_value("ExternalRef", external_reference_str, text_output, True) | ||
| if external_reference.comment: | ||
| write_text_value("ExternalRefComment", external_reference.comment, text_output) | ||
|
|
||
| if package.primary_package_purpose: | ||
| write_value("PrimaryPackagePurpose", transform_enum_name_to_tv(package.primary_package_purpose.name), | ||
| text_output) | ||
|
|
||
| if package.built_date: | ||
| write_value("BuiltDate", datetime_to_iso_string(package.built_date), text_output) | ||
| if package.release_date: | ||
| write_value("ReleaseDate", datetime_to_iso_string(package.release_date), text_output) | ||
| if package.valid_until_date: | ||
| write_value("ValidUntilDate", datetime_to_iso_string(package.valid_until_date), text_output) | ||
|
|
||
|
|
||
| def write_package_verification_code(verification_code: PackageVerificationCode): | ||
armintaenzertng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if not verification_code.excluded_files: | ||
| return verification_code.value | ||
|
|
||
| excluded_files_str = " (excludes: " + " ".join(verification_code.excluded_files) + ")" | ||
| return verification_code.value + excluded_files_str | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.