Skip to content

Commit

Permalink
addded support to parse labels in dockerfile
Browse files Browse the repository at this point in the history
Signed-off-by: Varsha U N <[email protected]>
  • Loading branch information
VarshaUN committed Nov 16, 2024
1 parent 15b76ea commit c90069a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/packagedcode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from packagedcode import debian
from packagedcode import debian_copyright
from packagedcode import distro
from packagedcode import dockerfileLABEL
from packagedcode import conda
from packagedcode import conan
from packagedcode import cocoapods
Expand Down Expand Up @@ -97,6 +98,7 @@
debian.DebianSourcePackageTarballHandler,

distro.EtcOsReleaseHandler,
dockerfileLABEL.DockerfileHandler,

freebsd.CompactManifestHandler,

Expand Down
72 changes: 72 additions & 0 deletions src/packagedcode/dockerfileLABEL.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#



import io
from pathlib import Path
from dockerfile_parse import DockerfileParser
from packagedcode import models
from packagedcode import utils


class DockerfileHandler(models.DatafileHandler):
datasource_id = 'dockerfile'
default_package_type = 'docker-image'
path_patterns = ('Dockerfile', 'containerfile', '*.dockerfile')
description = 'Dockerfile (OCI) metadata handler'
documentation_url = 'https://docs.docker.com/engine/reference/builder/'

@classmethod
def parse(cls, location, package_only=False):
"""
Parse a Dockerfile and yield one or more PackageData objects with OCI labels and metadata.
"""
labels = cls.extract_oci_labels_from_dockerfile(location)


package_data = {
'datasource_id': cls.datasource_id,
'type': cls.default_package_type,
'name': labels.get('name', 'unknown'),
'version': labels.get('version', 'unknown'),
'license_expression': labels.get('license', 'unknown'),
'labels': labels,
}

yield models.PackageData.from_data(package_data, package_only)

@classmethod
def extract_oci_labels_from_dockerfile(cls, dockerfile_path):
"""
Extract OCI labels from the Dockerfile using container-inspector.
"""
labels = {}
parser = DockerfileParser()
parser.parse(dockerfile_path)
labels = parser.labels
return labels

@classmethod
def assemble(cls, package_data, resource, codebase, package_adder):
"""
Assemble a Package from the parsed Dockerfile data.
"""
if package_data.purl:
package = models.Package.from_package_data(package_data=package_data, datafile_path=resource.path)


package.populate_license_fields()

yield package


package_adder(package.package_uid, resource, codebase)

yield resource

0 comments on commit c90069a

Please sign in to comment.