Skip to content

Commit

Permalink
refactor: Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyharrison committed Oct 20, 2022
1 parent a3548b3 commit 8903d07
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 28 deletions.
6 changes: 4 additions & 2 deletions sbom4python/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from sbom4python.scanner import SBOMScanner
from sbom4python.version import VERSION


# CLI processing


Expand Down Expand Up @@ -143,7 +142,9 @@ def main(argv=None):
sbom_scan.analyze(sbom_scan.get("Name"), sbom_scan.get("Requires"))

# Generate SBOM file
sbom_gen = SBOMGenerator(args["exclude_license"], args["sbom"], bom_format, app_name, VERSION, "pypi")
sbom_gen = SBOMGenerator(
args["exclude_license"], args["sbom"], bom_format, app_name, VERSION, "pypi"
)
sbom_out = SBOMOutput(args["output_file"], bom_format)

if args["sbom"] == "spdx":
Expand All @@ -161,5 +162,6 @@ def main(argv=None):

return 0


if __name__ == "__main__":
sys.exit(main())
37 changes: 24 additions & 13 deletions sbom4python/cyclonedxgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from sbom4python.license import LicenseScanner


class CycloneDXGenerator:
"""
Generate CycloneDX SBOM.
Expand All @@ -19,7 +20,13 @@ class CycloneDXGenerator:
PACKAGE_PREAMBLE = "SPDXRef-Package-"
LICENSE_PREAMBLE = "LicenseRef-"

def __init__(self, include_license: False, cyclonedx_format="json", application="sbom4python", version="0.1"):
def __init__(
self,
include_license: False,
cyclonedx_format="json",
application="sbom4python",
version="0.1",
):
self.doc = []
self.package_id = 0
self.include_license = include_license
Expand Down Expand Up @@ -50,7 +57,7 @@ def getBOM(self):
# Now process dependencies
self.store("<dependencies>")
for element in self.relationship:
item=element["ref"]
item = element["ref"]
self.store(f'<dependency ref="{item}">')
for depends in element["dependsOn"]:
self.store(f'<dependency ref="{depends}"/>')
Expand Down Expand Up @@ -102,7 +109,7 @@ def generateJSONDocumentHeader(self, project_name):
"version": self.application_version,
}
],
}
},
}

def generateXMLDocumentHeader(self, project_name):
Expand All @@ -111,13 +118,13 @@ def generateXMLDocumentHeader(self, project_name):
self.store("<bom xmlns='http://cyclonedx.org/schema/bom/1.4'")
self.store(f'serialNumber="{urn}"')
self.store('version="1">')
self.store('<metadata>')
self.store(f'<timestamp>{self.generateTime()}</timestamp>')
self.store('<tools>')
self.store(f'<name>{self.application}</name>')
self.store(f'<version>{self.application_version}</version>')
self.store('</tools>')
self.store('</metadata>')
self.store("<metadata>")
self.store(f"<timestamp>{self.generateTime()}</timestamp>")
self.store("<tools>")
self.store(f"<name>{self.application}</name>")
self.store(f"<version>{self.application_version}</version>")
self.store("</tools>")
self.store("</metadata>")
self.store("<components>")

def generateRelationship(self, parent_id, package_id):
Expand All @@ -142,7 +149,9 @@ def generateComponent(self, id, type, name, supplier, version, licence):
else:
self.generateJSONComponent(id, type, name, supplier, version, licence)

def generateJSONComponent(self, id, type, name, supplier, version, identified_licence):
def generateJSONComponent(
self, id, type, name, supplier, version, identified_licence
):
component = dict()
component["type"] = type
component["bom-ref"] = id
Expand All @@ -161,12 +170,14 @@ def generateJSONComponent(self, id, type, name, supplier, version, identified_li
license["url"] = license_url
item = dict()
item["license"] = license
component["licenses"] = [ item ]
component["licenses"] = [item]
if self.include_purl:
component["purl"] = f"pkg:{self.package_manager}/{name}@{version}"
self.component.append(component)

def generateXMLComponent(self, id, type, name, supplier, version, identified_licence):
def generateXMLComponent(
self, id, type, name, supplier, version, identified_licence
):
self.store(f'<component type="{type}" bom-ref="{id}">')
self.store(f"<name>{name}</name>")
self.store(f"<version>{version}</version>")
Expand Down
2 changes: 1 addition & 1 deletion sbom4python/dotgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_package(self, package_id):
return package_id[package_id[startpos:].find("-") + startpos + 1 :]
elif "-" in package_id:
# Format is n-<package>
return package_id[package_id.find("-") + 1:]
return package_id[package_id.find("-") + 1 :]
return package_id

def set_colour(self, colour):
Expand Down
14 changes: 11 additions & 3 deletions sbom4python/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ class SBOMGenerator:
Simple SBOM Generator.
"""

def __init__(self, include_license: False, sbom_type="spdx", format="tag", application="sbom4python", version="0.1", package_manager = "pypi"):
def __init__(
self,
include_license: False,
sbom_type="spdx",
format="tag",
application="sbom4python",
version="0.1",
package_manager="pypi",
):
if sbom_type == "spdx":
self.bom = SPDXGenerator(include_license, format, application, version)
self.bom.set_purl(package_manager)
Expand Down Expand Up @@ -105,5 +113,5 @@ def generate_cyclonedx(self, project_name, packages):
else:
if parent != "-":
self.bom.generateRelationship(
package_set[parent], package_set[product]
)
package_set[parent], package_set[product]
)
12 changes: 9 additions & 3 deletions sbom4python/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

class LicenseScanner:

APACHE_SYNOYMNS = ["Apache Software License", "Apache License, Version 2.0", "Apache 2.0", "Apache 2"]
APACHE_SYNOYMNS = [
"Apache Software License",
"Apache License, Version 2.0",
"Apache 2.0",
"Apache 2",
]
DEFAULT_LICENSE = "UNKNOWN"
SPDX_LICENSE_VERSION = "3.18"

Expand Down Expand Up @@ -41,7 +46,8 @@ def get_license_url(self, license_id):
# Assume that license_id is a valid SPDX id
if license_id != self.DEFAULT_LICENSE:
for lic in self.licenses["licenses"]:
# License URL is in the seeAlso field. If multiple entries, just return first one
# License URL is in the seeAlso field.
# If multiple entries, just return first one
if lic["licenseId"] == license_id:
return lic["seeAlso"][0]
return None # License not found
return None # License not found
30 changes: 24 additions & 6 deletions sbom4python/spdxgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from sbom4python.license import LicenseScanner


class SPDXGenerator:
"""
Generate SPDX Tag/Value SBOM.
Expand All @@ -19,7 +20,13 @@ class SPDXGenerator:
PACKAGE_PREAMBLE = "SPDXRef-Package-"
LICENSE_PREAMBLE = "LicenseRef-"

def __init__(self, include_license: False, spdx_format="tag", application="sbom4python", version="0.1"):
def __init__(
self,
include_license: False,
spdx_format="tag",
application="sbom4python",
version="0.1",
):

self.package_id = 0
self.include_license = include_license
Expand Down Expand Up @@ -78,7 +85,9 @@ def generateTagDocumentHeader(self, project_name):
+ str(uuid.uuid4()),
)
self.generateTag("LicenseListVersion", self.license.get_license_version())
self.generateTag("Creator: Tool", self.application + "-" + self.application_version)
self.generateTag(
"Creator: Tool", self.application + "-" + self.application_version
)
self.generateTag("Created", self.generateTime())
self.generateTag(
"CreatorComment",
Expand All @@ -92,7 +101,9 @@ def generateJSONDocumentHeader(self, project_name):
self.doc["spdxVersion"] = self.SPDX_VERSION
creation_info = dict()
creation_info["comment"] = "This document has been automatically generated."
creation_info["creators"] = ["Tool: " + self.application + "-" + self.application_version]
creation_info["creators"] = [
"Tool: " + self.application + "-" + self.application_version
]
creation_info["created"] = self.generateTime()
creation_info["licenseListVersion"] = self.license.get_license_version()
self.doc["creationInfo"] = creation_info
Expand Down Expand Up @@ -138,7 +149,9 @@ def generateTagPackageDetails(
# Attempt to detect an organization
if len(supplier.split()) > 2:
# Supplier name mustn't have spaces in. Covert spaces to '_'
self.generateTag("PackageSupplier: Organization", supplier.replace(" ", "_"))
self.generateTag(
"PackageSupplier: Organization", supplier.replace(" ", "_")
)
else:
# Supplier name mustn't have spaces in. Covert spaces to '_'
self.generateTag("PackageSupplier: Person", supplier.replace(" ", "_"))
Expand All @@ -150,7 +163,10 @@ def generateTagPackageDetails(
self.generateTag("PackageLicenseDeclared", self.license_ident(license))
self.generateTag("PackageCopyrightText", "NOASSERTION")
if self.include_purl:
self.generateTag("ExternalRef", f"PACKAGE-MANAGER pkg:{self.package_manager}/{package}@{version}")
self.generateTag(
"ExternalRef",
f"PACKAGE-MANAGER pkg:{self.package_manager}/{package}@{version}",
)
self.generateRelationship(
self.package_ident(parent_id), package_id, relationship
)
Expand Down Expand Up @@ -178,7 +194,9 @@ def generateJSONPackageDetails(
if self.include_purl:
purl_data = dict()
purl_data["referenceCategory"] = "PACKAGE-MANAGER"
purl_data["referenceLocator"] = f"pkg:{self.package_manager}/{package}@{version}"
purl_data[
"referenceLocator"
] = f"pkg:{self.package_manager}/{package}@{version}"
purl_data["referenceType"] = "purl"
component["externalRefs"] = [purl_data]
self.component.append(component)
Expand Down

0 comments on commit 8903d07

Please sign in to comment.