Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/datetime_conversions.py
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"

7 changes: 7 additions & 0 deletions src/model/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ class Actor:

def __init__(self, actor_type: ActorType, name: str, email: Optional[str] = None):
check_types_and_set_values(self, locals())

def to_serialized_string(self) -> str:
"""
All serialization formats use the same representation of an actor, so this method is included in the data model
"""
optional_email = f" ({self.email})" if self.email else ""
return "".join([f"{self.actor_type.name.title()}:", f" {self.name}", optional_email])
3 changes: 2 additions & 1 deletion src/parser/json/annotation_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from src.model.annotation import Annotation, AnnotationType
from src.parser.error import SPDXParsingError
from src.parser.json.actor_parser import ActorParser
from src.parser.json.dict_parsing_functions import datetime_from_str, construct_or_raise_parsing_error, \
from src.parser.json.dict_parsing_functions import construct_or_raise_parsing_error, \
parse_field_or_log_error, append_parsed_field_or_log_error, raise_parsing_error_if_logger_has_messages
from src.datetime_conversions import datetime_from_str
from src.parser.logger import Logger


Expand Down
3 changes: 2 additions & 1 deletion src/parser/json/creation_info_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
from src.parser.error import SPDXParsingError
from src.parser.json.actor_parser import ActorParser
from src.parser.json.checksum_parser import ChecksumParser
from src.parser.json.dict_parsing_functions import append_parsed_field_or_log_error, datetime_from_str, \
from src.parser.json.dict_parsing_functions import append_parsed_field_or_log_error, \
raise_parsing_error_if_logger_has_messages, construct_or_raise_parsing_error, parse_field_or_log_error, \
parse_field_or_no_assertion
from src.datetime_conversions import datetime_from_str
from src.parser.logger import Logger


Expand Down
12 changes: 0 additions & 12 deletions src/parser/json/dict_parsing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
# 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 typing import Any, Callable, Dict, List, Optional

from src.model.spdx_no_assertion import SpdxNoAssertion
Expand All @@ -18,17 +17,6 @@
from src.parser.logger import Logger


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 json_str_to_enum_name(json_str: str) -> str:
if not isinstance(json_str, str):
raise SPDXParsingError([f"Type for enum must be str not {type(json_str).__name__}"])
Expand Down
3 changes: 2 additions & 1 deletion src/parser/json/package_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
from src.parser.error import SPDXParsingError
from src.parser.json.actor_parser import ActorParser
from src.parser.json.checksum_parser import ChecksumParser
from src.parser.json.dict_parsing_functions import append_parsed_field_or_log_error, datetime_from_str, \
from src.parser.json.dict_parsing_functions import append_parsed_field_or_log_error, \
raise_parsing_error_if_logger_has_messages, json_str_to_enum_name, construct_or_raise_parsing_error, \
parse_field_or_log_error, parse_field_or_no_assertion_or_none, parse_field_or_no_assertion
from src.datetime_conversions import datetime_from_str
from src.parser.json.license_expression_parser import LicenseExpressionParser
from src.parser.logger import Logger

Expand Down
11 changes: 11 additions & 0 deletions src/writer/tagvalue/__init__.py
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.

26 changes: 26 additions & 0 deletions src/writer/tagvalue/annotation_writer.py
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)
31 changes: 31 additions & 0 deletions src/writer/tagvalue/checksum_writer.py
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
Copy link
Collaborator

@armintaenzertng armintaenzertng Dec 30, 2022

Choose a reason for hiding this comment

The 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)


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"
}
46 changes: 46 additions & 0 deletions src/writer/tagvalue/creation_info_writer.py
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)
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)
28 changes: 28 additions & 0 deletions src/writer/tagvalue/extracted_licensing_info_writer.py
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)
42 changes: 42 additions & 0 deletions src/writer/tagvalue/file_writer.py
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)
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)
86 changes: 86 additions & 0 deletions src/writer/tagvalue/package_writer.py
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)

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):
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
Loading