Skip to content

Commit 610689c

Browse files
AyanSinhaMahapatraVarshaUN
authored andcommitted
Reference: #3954
Signed-off-by: Ayan Sinha Mahapatra <[email protected]> Signed-off-by: Jono Yang <[email protected]> Signed-off-by: Jono Yang <[email protected]> addded support to parse labels in dockerfile Signed-off-by: Varsha U N <[email protected]>
1 parent bc78721 commit 610689c

16 files changed

+297
-5
lines changed

requirements-linux.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
packagedcode-msitools==0.101.210706
22
regipy==3.1.0
33
rpm-inspector-rpm==4.16.1.3.210404
4-
go-inspector==0.3.1
4+
go-inspector==0.5.0

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jaraco.functools==4.1.0
3535
javaproperties==0.8.1
3636
Jinja2==3.1.3
3737
jsonstreams==0.6.0
38-
license-expression==30.3.0
38+
license-expression==30.4.0
3939
lxml==5.1.0
4040
MarkupSafe==2.1.5
4141
more-itertools==8.13.0

setup-mini.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ packages =
149149
rpm_inspector_rpm >= 4.16.1.3; platform_system == 'Linux'
150150
regipy >= 3.1.0; platform_system == 'Linux'
151151
packagedcode_msitools >= 0.101.210706; platform_system == 'Linux'
152-
go-inspector >= 0.3.1; platform_system == 'Linux'
152+
go-inspector >= 0.5.0; platform_system == 'Linux'
153153

154154

155155
[options.entry_points]

setup.cfg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ install_requires =
8484
javaproperties >= 0.5
8585
jinja2 >= 2.7.0
8686
jsonstreams >= 0.5.0
87-
license_expression >= 30.1.1
87+
license_expression >= 30.4.0
8888
lxml >= 4.9.2
8989
MarkupSafe >= 2.1.2
9090
packageurl_python >= 0.9.0
@@ -149,7 +149,7 @@ packages =
149149
rpm_inspector_rpm >= 4.16.1.3; platform_system == 'Linux'
150150
regipy >= 3.1.0; platform_system == 'Linux'
151151
packagedcode_msitools >= 0.101.210706; platform_system == 'Linux'
152-
go-inspector >= 0.3.1; platform_system == 'Linux'
152+
go-inspector >= 0.5.0; platform_system == 'Linux'
153153

154154

155155
[options.entry_points]

src/licensedcode/tokenize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ def select_ngrams(ngrams, with_pos=False):
336336
>>> list(select_ngrams(x for x in [(2, 1, 3), (1, 1, 3), (5, 1, 3), (2, 6, 1), (7, 3, 4)]))
337337
[(2, 1, 3), (1, 1, 3), (5, 1, 3), (2, 6, 1), (7, 3, 4)]
338338
"""
339+
ngram = None
339340
last = None
340341
for pos, ngram in enumerate(ngrams):
341342
# FIXME: use a proper hash

src/packagedcode/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from packagedcode import debian
2121
from packagedcode import debian_copyright
2222
from packagedcode import distro
23+
from packagedcode import dockerfile
2324
from packagedcode import conda
2425
from packagedcode import conan
2526
from packagedcode import cocoapods
@@ -97,6 +98,7 @@
9798
debian.DebianSourcePackageTarballHandler,
9899

99100
distro.EtcOsReleaseHandler,
101+
dockerfile.DockerfileHandler,
100102

101103
freebsd.CompactManifestHandler,
102104

src/packagedcode/dockerfile.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# ScanCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/nexB/scancode-toolkit for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
11+
12+
import io
13+
from pathlib import Path
14+
from dockerfile_parse import DockerfileParser
15+
from packagedcode import models
16+
from packagedcode import utils
17+
import fnmatch
18+
19+
20+
class DockerfileHandler(models.DatafileHandler):
21+
datasource_id = 'dockerfile_oci_labels'
22+
23+
@classmethod
24+
def is_datafile(cls, path):
25+
patterns = ['Dockerfile', 'containerfile', '*.dockerfile']
26+
filename=os.path.basename(path)
27+
for pattern in patterns:
28+
if fnmatch.fnmatch(filename, pattern):
29+
return True
30+
return False
31+
32+
@classmethod
33+
def parse(cls, location, package_only=False):
34+
"""
35+
Parse a Dockerfile and yield one or more PackageData objects with OCI labels and metadata.
36+
"""
37+
labels = cls.extract_oci_labels_from_dockerfile(location)
38+
package_data = {
39+
'datasource_id': cls.datasource_id,
40+
'type': cls.default_package_type,
41+
'name': labels.get('name', 'None'),
42+
'version': labels.get('version', 'None'),
43+
'license_expression': labels.get('license', 'None'),
44+
'labels': labels,
45+
}
46+
47+
yield models.PackageData.from_data(package_data, package_only)
48+
49+
@classmethod
50+
def extract_oci_labels_from_dockerfile(cls, dockerfile_path):
51+
"""
52+
Extract OCI labels from the Dockerfile using DockerfileParser.
53+
"""
54+
labels = {}
55+
parser = DockerfileParser()
56+
with open(dockerfile_path, 'r') as dockerfile:
57+
parser.content = dockerfile.read()
58+
labels = parser.labels
59+
return labels

src/packagedcode/recognize.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@ def _parse(
113113

114114
if TRACE:
115115
raise
116+
117+
except Exception as e:
118+
# We should continue when an Exception has occured when trying to
119+
# recognize a package
120+
if TRACE:
121+
logger_debug(f'_parse: Exception: {str(e)}')
122+
123+
continue
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[
2+
{
3+
"datasource_id": "dockerfile_oci_labels",
4+
"type": "default",
5+
"name": "Unknown",
6+
"version": "Unknown",
7+
"license_expression": "GPL-2.0-only AND BSD-2-Clause",
8+
"labels": {
9+
"source": "https://github.com/kubernetes-sigs/blixt",
10+
"licenses": "GPL-2.0-only,BSD-2-Clause"
11+
}
12+
}
13+
]

0 commit comments

Comments
 (0)