Skip to content

Commit

Permalink
Review comments for ws-patches PR
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolaus Weidner <[email protected]>
  • Loading branch information
nicoweidner committed Nov 2, 2022
1 parent 29c5dc8 commit aedd02e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 53 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_suite():
long_description = fh.read()

setup(
name='ws-spdx-tools',
version='0.7.0a3.post7',
name='spdx-tools',
version='0.7.0a3',
description='SPDX parser and tools.',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
16 changes: 5 additions & 11 deletions spdx/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,11 @@ def validate_str_fields(self, fields, optional, messages):
return messages

def validate_checksum(self, messages):
try:
if self.check_sum is not None:
if not isinstance(self.check_sum, checksum.Algorithm):
messages.append(
"Package checksum must be instance of spdx.checksum.Algorithm"
)
# this check was added by the WhiteSouse PS Team ( for cases where SHA1 is not available at WhiteSource )
if not self.check_sum.value:
delattr(self, 'check_sum')
except:
pass
if self.check_sum is not None:
if not isinstance(self.check_sum, checksum.Algorithm):
messages.append(
"Package checksum must be instance of spdx.checksum.Algorithm"
)

return messages

Expand Down
5 changes: 4 additions & 1 deletion spdx/relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ def __init__(self, relationship=None, relationship_comment=None):
self.relationship_comment = relationship_comment

def __eq__(self, other):
return True if self.relationship == other.relationship else False
return (
isinstance(other, Relationship)
and self.relationship == other.relationship
)

@property
def has_comment(self):
Expand Down
14 changes: 4 additions & 10 deletions spdx/writers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@
from spdx.writers.tagvalue import InvalidDocumentError
from spdx.writers.jsonyamlxml import Writer
from spdx.parsers.loggers import ErrorMessages
import numpy as np
import datetime


# this method has been added by the WhiteSouse PS Team.
def json_converter(obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, datetime.datetime):
return obj.__str__()
if isinstance(obj, datetime.datetime):
return str(obj)
else:
raise TypeError("No implementation available to serialize objects of type " + type(obj).__name__)


def write_document(document, out, validate=True):
Expand Down
29 changes: 3 additions & 26 deletions spdx/writers/jsonyamlxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def checksum(self, checksum_field):
"""
checksum_object = dict()
checksum_object["algorithm"] = (
#"checksumAlgorithm_" + checksum_field.identifier.lower()
# Changed by the WhiteSouse PS Team.
checksum_field.identifier
)
checksum_object["checksumValue"] = checksum_field.value
Expand Down Expand Up @@ -151,8 +149,6 @@ def create_package_info(self, package):

if package.has_optional_field("check_sum"):
package_object["checksums"] = [self.checksum(package.check_sum)]
# package_object["sha1"] = package.check_sum.value
# commented by the WS PS Team, sha1 is already exist in check_sum

if package.has_optional_field("description"):
package_object["description"] = package.description
Expand Down Expand Up @@ -378,10 +374,6 @@ def __init__(self, document):

def create_extracted_license(self):
extracted_license_objects = []
# commented by the WS PS Team
# extracted_licenses = self.document.extracted_licenses

# filter by unique identifier attribute in ExtractedLicense object, added by the WhiteSouse PS Team
unique_extracted_licenses = {}
for lic in self.document.extracted_licenses:
if lic.identifier not in unique_extracted_licenses.keys():
Expand Down Expand Up @@ -485,38 +477,25 @@ def create_document(self):
self.document_object["SPDXID"] = self.spdx_id(self.document.spdx_id)
self.document_object["name"] = self.document.name

# filter by unique spdx_id attribute in Package object, added by the WhiteSouse PS Team
unique_doc_packages = {}
for doc_package in self.document.packages:
if doc_package.spdx_id not in unique_doc_packages.keys():
unique_doc_packages[doc_package.spdx_id] = doc_package

# commented by the WS PS Team
# package_objects = []
# for package in self.document.packages:
# package_info_object = self.create_package_info(package)
# package_info_object["files"] = self.create_file_info(package)
# package_objects.append({"Package": package_info_object})
#
# self.document_object["documentDescribes"] = package_objects

# the previous code block has been adjusted by the WhiteSouse PS Team.
# adding documentDescribes and packages sections.
package_objects = []
document_describes_objects = []
for package in unique_doc_packages.values():
package_info_object = self.create_package_info(package)
# SPDX 2.2 says to omit if filesAnalyzed = False
if package.files:
package_info_object["files"] = self.create_file_info(package)
# create a list of the packages' names for the 'documentDescribes' field
package_spdxid = package_info_object.get("SPDXID")
document_describes_objects.append(package_spdxid)
# create a list of the packages' ids for the 'documentDescribes' field
package_spdx_id = package_info_object.get("SPDXID")
document_describes_objects.append(package_spdx_id)
package_objects.append(package_info_object)

self.document_object["documentDescribes"] = document_describes_objects
self.document_object["packages"] = package_objects
# end

if self.document.has_comment:
self.document_object["comment"] = self.document.comment
Expand All @@ -543,6 +522,4 @@ def create_document(self):
if self.document.relationships:
self.document_object["relationships"] = self.create_relationship_info()

# return {"Document": self.document_object}. No need in "Document" key for json and tv format.
# Changed by the WhiteSouse PS Team.
return self.document_object
2 changes: 1 addition & 1 deletion spdx/writers/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def relationships(self):
"""
Return a list of relationship nodes
"""
return list(map(self.create_relationship_node, self.document.relationships))
return map(self.create_relationship_node, self.document.relationships)


class CreationInfoWriter(BaseWriter):
Expand Down
2 changes: 0 additions & 2 deletions spdx/writers/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ def write_document(document, out, validate=True):
raise InvalidDocumentError(messages)

writer = Writer(document)
# document_object = {"SpdxDocument": writer.create_document()}.
# hanged by the WhiteSouse PS Team
document_object = {"Document": writer.create_document()}

xmltodict.unparse(document_object, out, encoding="utf-8", pretty=True)

0 comments on commit aedd02e

Please sign in to comment.