Skip to content

Commit 58b1880

Browse files
committed
Merge branch '660-rpms' into develop
2 parents 0fdcc10 + aacc22f commit 58b1880

File tree

7 files changed

+160
-24
lines changed

7 files changed

+160
-24
lines changed

src/packagedcode/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def recognize(cls, location):
892892
893893
Sub-classes should override to implement their own package recognition.
894894
"""
895-
return
895+
return cls()
896896

897897
@property
898898
def component_version(self):

src/packagedcode/recognize.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2015 nexB Inc. and others. All rights reserved.
2+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
33
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
44
# The ScanCode software is licensed under the Apache License version 2.0.
55
# Data generated with ScanCode require an acknowledgment.
@@ -26,17 +26,29 @@
2626
from __future__ import print_function
2727

2828
import logging
29+
import sys
2930

3031
from commoncode import filetype
31-
from typecode import contenttype
32-
33-
from packagedcode import PACKAGE_TYPES
3432
from commoncode.system import on_linux
3533
from commoncode.fileutils import path_to_bytes
34+
from packagedcode import PACKAGE_TYPES
35+
from typecode import contenttype
3636

3737

38+
TRACE = False
39+
40+
def logger_debug(*args):
41+
pass
42+
3843
logger = logging.getLogger(__name__)
3944

45+
if TRACE:
46+
logging.basicConfig(stream=sys.stdout)
47+
logger.setLevel(logging.DEBUG)
48+
49+
def logger_debug(*args):
50+
return logger.debug(' '.join(isinstance(a, basestring) and a or repr(a) for a in args))
51+
4052

4153
"""
4254
Recognize packages in files or directories.
@@ -56,24 +68,25 @@ def recognize_package(location):
5668
mtype = T.mimetype_file
5769

5870

59-
for package in PACKAGE_TYPES:
71+
for package_type in PACKAGE_TYPES:
6072
# Note: default to True if there is nothing to match against
61-
metafiles = package.metafiles
73+
metafiles = package_type.metafiles
6274
if on_linux:
6375
metafiles = (path_to_bytes(m) for m in metafiles)
6476
if location.endswith(tuple(metafiles)):
65-
return package.recognize(location)
77+
logger_debug('metafile matching: package_type is of type:', package_type)
78+
return package_type.recognize(location)
6679

67-
if package.filetypes:
68-
type_matched = any(t in ftype for t in package.filetypes)
80+
if package_type.filetypes:
81+
type_matched = any(t in ftype for t in package_type.filetypes)
6982
else:
7083
type_matched = False
71-
if package.mimetypes:
72-
mime_matched = any(m in mtype for m in package.mimetypes)
84+
if package_type.mimetypes:
85+
mime_matched = any(m in mtype for m in package_type.mimetypes)
7386
else:
7487
mime_matched = False
7588

76-
extensions = package.extensions
89+
extensions = package_type.extensions
7790
if extensions:
7891
if on_linux:
7992
extensions = tuple(path_to_bytes(e) for e in extensions)
@@ -83,4 +96,9 @@ def recognize_package(location):
8396

8497
if type_matched and mime_matched and extension_matched:
8598
# we return the first match in the order of PACKAGE_TYPES
86-
return package(location=location)
99+
logger_debug('all matching: package is of type:', package_type)
100+
recognized = package_type.recognize(location)
101+
logger_debug('all matching: recognized as:', repr(recognized))
102+
return recognized
103+
104+
logger_debug('no match: package is not of known type:', package_type)

src/packagedcode/rpm.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2016 nexB Inc. and others. All rights reserved.
2+
# Copyright (c) 2017 nexB Inc. and others. All rights reserved.
33
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
44
# The ScanCode software is licensed under the Apache License version 2.0.
55
# Data generated with ScanCode require an acknowledgment.
@@ -26,14 +26,33 @@
2626
from __future__ import print_function
2727

2828
from collections import namedtuple
29+
import logging
2930
import string
31+
import sys
3032

31-
import typecode.contenttype
3233

3334
from packagedcode import models
3435
from packagedcode import nevra
3536
from packagedcode.pyrpm.rpm import RPM
3637

38+
import typecode.contenttype
39+
40+
41+
TRACE = False
42+
43+
def logger_debug(*args):
44+
pass
45+
46+
logger = logging.getLogger(__name__)
47+
48+
if TRACE:
49+
logging.basicConfig(stream=sys.stdout)
50+
logger.setLevel(logging.DEBUG)
51+
52+
def logger_debug(*args):
53+
return logger.debug(' '.join(isinstance(a, basestring) and a or repr(a) for a in args))
54+
55+
3756
# TODO: retrieve dependencies
3857

3958
# TODO: parse spec files see:
@@ -99,6 +118,7 @@ def info(location, include_desc=False):
99118
the long RPM description value if include_desc is True.
100119
"""
101120
tgs = tags(location, include_desc)
121+
print(tgs)
102122
return tgs and RPMInfo(**tgs) or None
103123

104124

@@ -169,7 +189,7 @@ class RpmPackage(models.Package):
169189
related_packages = models.ListType(models.ModelType(RPMRelatedPackage))
170190

171191
@classmethod
172-
def recognize(location):
192+
def recognize(cls, location):
173193
return parse(location)
174194

175195

@@ -179,6 +199,7 @@ def parse(location):
179199
not an RPM.
180200
"""
181201
infos = info(location, include_desc=True)
202+
logger_debug('parse: infos', infos)
182203
if not infos:
183204
return
184205

@@ -192,9 +213,8 @@ def parse(location):
192213
if infos.source_rpm:
193214
epoch, name, version, release, _arch = nevra.from_name(infos.source_rpm)
194215
evr = EVR(version, release, epoch)
195-
related_packages = [RPMRelatedPackage(name=name,
196-
version=evr,
197-
payload_type=models.payload_src)]
216+
related_packages = [
217+
RPMRelatedPackage(name=name, version=evr, payload_type=models.payload_src)]
198218

199219
package = RpmPackage(
200220
summary=infos.summary,
@@ -204,9 +224,7 @@ def parse(location):
204224
homepage_url=infos.url,
205225
distributors=[models.Party(name=infos.distribution)],
206226
vendors=[models.Party(name=infos.vendor)],
207-
208227
asserted_licenses=asserted_licenses,
209-
210228
related_packages=related_packages
211229
)
212230
return package

tests/packagedcode/test_recognize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def test_recognize_package_tarball(self):
8585
package = recognize_package(test_file)
8686
assert isinstance(package, packagedcode.models.TarPackage)
8787

88-
def test_recognize_cpan_manifest_is_not_yet_supported(self):
88+
def test_recognize_cpan_manifest_as_plain_package(self):
8989
test_file = self.get_test_loc('cpan/MANIFEST')
9090
package = recognize_package(test_file)
91-
assert not package
91+
assert isinstance(package, packagedcode.models.CpanModule)
9292

9393
def test_recognize_maven_dot_pom(self):
9494
test_file = self.get_test_loc('m2/aspectj/aspectjrt/1.5.3/aspectjrt-1.5.3.pom')
2.93 KB
Binary file not shown.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"scancode_notice": "Generated with ScanCode and provided on an \"AS IS\" BASIS, WITHOUT WARRANTIES\nOR CONDITIONS OF ANY KIND, either express or implied. No content created from\nScanCode should be considered or used as legal advice. Consult an Attorney\nfor any legal advice.\nScanCode is a free software code scanning tool from nexB Inc. and others.\nVisit https://github.com/nexB/scancode-toolkit/ for support and download.",
3+
"scancode_options": {
4+
"--package": true,
5+
"--license-score": 0,
6+
"--format": "json"
7+
},
8+
"files_count": 1,
9+
"files": [
10+
{
11+
"path": "rpm/fping-2.4-0.b2.rhfc1.dag.i386.rpm",
12+
"scan_errors": [],
13+
"packages": [
14+
{
15+
"type": "RPM",
16+
"name": "fping",
17+
"version": "2.4-0.b2.rhfc1.dag",
18+
"primary_language": null,
19+
"packaging": "archive",
20+
"summary": "A utility to ping multiple hosts at once.",
21+
"description": "fping is a ping-like program which uses the Internet Control Message\nProtocol (ICMP) echo request to determine if a target host is responding.\n\nfping is different from ping in that you can specify any number of hosts\non the command line, or specify a file containing the lists of hosts to\nping. Instead of trying one host until it timeouts or replies, fping will\nsend out a ping packet and move on to the next host in a round-robin fashion.\nIf a host replies, it is noted and removed from the list of hosts to check.\nIf a host does not respond within a certain time limit and/or retry limit it\nwill be considered unreachable.",
22+
"payload_type": null,
23+
"size": null,
24+
"release_date": null,
25+
"authors": [],
26+
"maintainers": [],
27+
"contributors": [],
28+
"owners": [],
29+
"packagers": [],
30+
"distributors": [
31+
{
32+
"type": null,
33+
"name": "",
34+
"email": null,
35+
"url": null
36+
}
37+
],
38+
"vendors": [
39+
{
40+
"type": null,
41+
"name": "Dag Apt Repository, http://dag.wieers.com/apt/",
42+
"email": null,
43+
"url": null
44+
}
45+
],
46+
"keywords": [],
47+
"keywords_doc_url": null,
48+
"metafile_locations": [],
49+
"metafile_urls": [],
50+
"homepage_url": "http://www.fping.com/",
51+
"notes": null,
52+
"download_urls": [],
53+
"download_sha1": null,
54+
"download_sha256": null,
55+
"download_md5": null,
56+
"bug_tracking_url": null,
57+
"support_contacts": [],
58+
"code_view_url": null,
59+
"vcs_tool": null,
60+
"vcs_repository": null,
61+
"vcs_revision": null,
62+
"copyright_top_level": null,
63+
"copyrights": [],
64+
"asserted_licenses": [
65+
{
66+
"license": "distributable",
67+
"url": null,
68+
"text": null,
69+
"notice": null
70+
}
71+
],
72+
"legal_file_locations": [],
73+
"license_expression": null,
74+
"license_texts": [],
75+
"notice_texts": [],
76+
"dependencies": {},
77+
"related_packages": [
78+
{
79+
"type": "RPM",
80+
"name": "fping",
81+
"version": "2.4-0.b2.rhfc1.dag",
82+
"payload_type": "source"
83+
}
84+
]
85+
}
86+
]
87+
}
88+
]
89+
}

tests/scancode/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,17 @@ def test_scan_does_scan_php_composer():
667667
check_json_scan(expected_file, result_file)
668668

669669

670+
def test_scan_does_scan_rpm():
671+
test_file = test_env.get_test_loc('rpm/fping-2.4-0.b2.rhfc1.dag.i386.rpm')
672+
expected_file = test_env.get_test_loc('rpm/fping-2.4-0.b2.rhfc1.dag.i386.rpm.expected.json')
673+
result_file = test_env.get_temp_file('results.json')
674+
675+
result = run_scan_click(['--package', test_file, result_file])
676+
assert result.exit_code == 0
677+
assert 'Scanning done' in result.output
678+
check_json_scan(expected_file, result_file, regen=False)
679+
680+
670681
class TestFixedWidthFilename(TestCase):
671682

672683
def test_fixed_width_file_name_with_file_name_larger_than_max_length_is_shortened(self):

0 commit comments

Comments
 (0)