Skip to content

Commit 2f80ae3

Browse files
Address feedback and suggestions
Reference: #3620 Signed-off-by: Ayan Sinha Mahapatra <[email protected]>
1 parent c631e73 commit 2f80ae3

File tree

169 files changed

+2189
-2173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+2189
-2173
lines changed

src/licensedcode/detection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,15 @@ class LicenseDetection:
165165
license_expression = attr.ib(
166166
default=None,
167167
metadata=dict(
168-
help='Full license expression string '
169-
'using the SPDX license expression syntax and ScanCode license keys.')
168+
help='A license expression string using the SPDX license expression'
169+
' syntax and ScanCode license keys, the effective license expression'
170+
' for this license detection.')
170171
)
171172

172173
license_expression_spdx = attr.ib(
173174
default=None,
174175
metadata=dict(
175-
help='Full license expression string with SPDX license keys.')
176+
help='SPDX license expression string with SPDX ids.')
176177
)
177178

178179
matches = attr.ib(
@@ -955,7 +956,7 @@ def dict_fields(attr, value):
955956
return True
956957

957958
detection_mapping = attr.asdict(self, filter=dict_fields)
958-
detection_mapping["sample_matches"] = [
959+
detection_mapping["reference_matches"] = [
959960
match.to_dict(
960961
include_text=include_text,
961962
license_text_diagnostics=license_text_diagnostics,

src/licensedcode/licenses_reference.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def collect_license_and_rule_references(codebase, include_packages=True, include
9797
if TRACE:
9898
logger_debug(f'collect_references_from_packages: license keys: {pks}')
9999
logger_debug(f'collect_references_from_packages: rules by id: {prules}')
100+
license_keys.update(pks)
100101
rules_by_identifier.update(prules)
101102

102103
if include_files:

src/licensedcode/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,8 @@ def from_file(cls, rule_file, is_builtin=True):
21282128

21292129
@property
21302130
def pysafe_expression(self):
2131+
"""
2132+
Return a python safe identifier, for use in rule identifiers"""
21312133
return python_safe_name(self.license_expression)
21322134

21332135
def load_data(self, rule_file):

src/packagedcode/debian_copyright.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,6 @@ def parse_paras_with_license_text(paras_with_license):
12591259
matches=text_matches, reference_match=common_license_match
12601260
):
12611261
# TODO: Add unknown matches if matches are not consistent
1262-
# raise Exception(f'Inconsistent Licenses: {common_license_match} {matches}')
12631262
pass
12641263

12651264
# TODO: Add unknown matches if matches are weak

src/packagedcode/models.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,6 @@ def assemble_from_many_datafiles_in_directory(
13361336
package_data = PackageData.from_dict(package_data)
13371337
pkgdata_resources.append((package_data, resource,))
13381338

1339-
#raise Exception(pkgdata_resources)
13401339
if pkgdata_resources:
13411340
if TRACE:
13421341
logger_debug(f' assemble_from_many_datafiles: pkgdata_resources: {pkgdata_resources!r}')
@@ -1499,6 +1498,12 @@ def from_package_data(cls, package_data, datafile_path):
14991498
package_data_mapping['datafile_paths'] = [datafile_path]
15001499
package_data_mapping['datasource_ids'] = [dsid]
15011500

1501+
license_detections = package_data_mapping['license_detections']
1502+
for detection in license_detections:
1503+
for license_match in detection['matches']:
1504+
if not license_match['from_file']:
1505+
license_match['from_file'] = datafile_path
1506+
15021507
return cls.from_dict(package_data_mapping)
15031508

15041509
@classmethod

src/packagedcode/plugin_package.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
from packagedcode.models import PackageWithResources
3939

4040
TRACE = os.environ.get('SCANCODE_DEBUG_PACKAGE_API', False)
41+
TRACE_ASSEMBLY = os.environ.get('SCANCODE_DEBUG_PACKAGE_ASSEMBLY', False)
42+
TRACE_LICENSE = os.environ.get('SCANCODE_DEBUG_PACKAGE_LICENSE', False)
4143

4244

4345
def logger_debug(*args):
@@ -46,7 +48,7 @@ def logger_debug(*args):
4648

4749
logger = logging.getLogger(__name__)
4850

49-
if TRACE:
51+
if TRACE or TRACE_LICENSE or TRACE_ASSEMBLY:
5052
import sys
5153

5254
logging.basicConfig(stream=sys.stdout)
@@ -185,7 +187,7 @@ def get_scanner(self, package=True, system_package=False, **kwargs):
185187
system=system_package,
186188
)
187189

188-
def process_codebase(self, codebase, strip_root=False, license_text=False, license_diagnostics=False, license_text_diagnostics=False, **kwargs):
190+
def process_codebase(self, codebase, strip_root=False, **kwargs):
189191
"""
190192
Populate the ``codebase`` top level ``packages`` and ``dependencies``
191193
with package and dependency instances, assembling parsed package data
@@ -199,6 +201,20 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
199201
# These steps add proper license detections to package_data and hence
200202
# this is performed before top level packages creation
201203
for resource in codebase.walk(topdown=False):
204+
# populate `from_file` attribute in matches
205+
for package_data in resource.package_data:
206+
for detection in package_data['license_detections']:
207+
populate_matches_with_path(
208+
matches=detection['matches'],
209+
path=resource.path,
210+
)
211+
212+
for detection in package_data['other_license_detections']:
213+
populate_matches_with_path(
214+
matches=detection['matches'],
215+
path=resource.path,
216+
)
217+
202218
if not has_licenses:
203219
#TODO: Add the steps where we detect licenses from files for only a package scan
204220
# in the multiprocessing get_package_data API function
@@ -207,7 +223,7 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
207223
# If we don't detect license in package_data but there is license detected in file
208224
# we add the license expression from the file to a package
209225
modified = add_license_from_file(resource, codebase)
210-
if TRACE and modified:
226+
if TRACE_LICENSE and modified:
211227
logger_debug(f'packagedcode: process_codebase: add_license_from_file: modified: {modified}')
212228

213229
if codebase.has_single_resource:
@@ -216,25 +232,26 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
216232
# If there is referenced files in a extracted license statement, we follow
217233
# the references, look for license detections and add them back
218234
modified = list(add_referenced_license_matches_for_package(resource, codebase))
219-
if TRACE and modified:
235+
if TRACE_LICENSE and modified:
220236
logger_debug(f'packagedcode: process_codebase: add_referenced_license_matches_for_package: modified: {modified}')
221237

222238
# If there is a LICENSE file on the same level as the manifest, and no license
223239
# is detected in the package_data, we add the license from the file
224240
modified = add_license_from_sibling_file(resource, codebase)
225-
if TRACE and modified:
241+
if TRACE_LICENSE and modified:
226242
logger_debug(f'packagedcode: process_codebase: add_license_from_sibling_file: modified: {modified}')
227243

228244
# Create codebase-level packages and dependencies
229245
create_package_and_deps(codebase, strip_root=strip_root, **kwargs)
246+
#raise Exception()
230247

231248
if has_licenses:
232249
# This step is dependent on top level packages
233250
for resource in codebase.walk(topdown=False):
234251
# If there is a unknown reference to a package we add the license
235252
# from the package license detection
236253
modified = list(add_referenced_license_detection_from_package(resource, codebase))
237-
if TRACE and modified:
254+
if TRACE_LICENSE and modified:
238255
logger_debug(f'packagedcode: process_codebase: add_referenced_license_matches_from_package: modified: {modified}')
239256

240257

@@ -244,15 +261,15 @@ def add_license_from_file(resource, codebase):
244261
and the file has license detections, and if so, populate the package_data license
245262
expression and detection fields from the file license.
246263
"""
247-
if TRACE:
264+
if TRACE_LICENSE:
248265
logger_debug(f'packagedcode.plugin_package: add_license_from_file: resource: {resource.path}')
249266

250267
if not resource.is_file:
251268
return
252269

253270
license_detections_file = resource.license_detections
254271

255-
if TRACE:
272+
if TRACE_LICENSE:
256273
logger_debug(f'add_license_from_file: license_detections_file: {license_detections_file}')
257274
if not license_detections_file:
258275
return
@@ -263,7 +280,7 @@ def add_license_from_file(resource, codebase):
263280

264281
for pkg in package_data:
265282
license_detections_pkg = pkg["license_detections"]
266-
if TRACE:
283+
if TRACE_LICENSE:
267284
logger_debug(f'add_license_from_file: license_detections_pkg: {license_detections_pkg}')
268285

269286
if not license_detections_pkg:
@@ -329,16 +346,7 @@ def create_package_and_deps(codebase, package_adder=add_to_package, strip_root=F
329346
**kwargs
330347
)
331348

332-
package_mappings = []
333-
for package in packages:
334-
for detection in package.license_detections:
335-
populate_matches_with_path(
336-
matches=detection["matches"],
337-
path=package.datafile_paths[0],
338-
)
339-
package_mappings.append(package.to_dict())
340-
341-
codebase.attributes.packages.extend(package_mappings)
349+
codebase.attributes.packages.extend(package.to_dict() for package in packages)
342350
codebase.attributes.dependencies.extend(dep.to_dict() for dep in dependencies)
343351

344352

@@ -361,20 +369,20 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
361369
if resource.path in seen_resource_paths:
362370
continue
363371

364-
if TRACE:
372+
if TRACE_ASSEMBLY:
365373
logger_debug('get_package_and_deps: location:', resource.location)
366374

367375
for package_data in resource.package_data:
368376
try:
369377
package_data = PackageData.from_dict(mapping=package_data)
370378

371-
if TRACE:
379+
if TRACE_ASSEMBLY:
372380
logger_debug(' get_package_and_deps: package_data:', package_data)
373381

374382
# Find a handler for this package datasource to assemble collect
375383
# packages and deps
376384
handler = get_package_handler(package_data)
377-
if TRACE:
385+
if TRACE_ASSEMBLY:
378386
logger_debug(' get_package_and_deps: handler:', handler)
379387

380388
items = handler.assemble(
@@ -385,7 +393,7 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
385393
)
386394

387395
for item in items:
388-
if TRACE:
396+
if TRACE_ASSEMBLY:
389397
logger_debug(' get_package_and_deps: item:', item)
390398

391399
if isinstance(item, Package):
@@ -404,7 +412,7 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
404412
elif isinstance(item, Resource):
405413
seen_resource_paths.add(item.path)
406414

407-
if TRACE:
415+
if TRACE_ASSEMBLY:
408416
logger_debug(
409417
' get_package_and_deps: seen_resource_path:',
410418
seen_resource_paths,

src/summarycode/todo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def process_codebase(self, codebase, **kwargs):
105105
license_text_diagnostics = kwargs.get("license_text_diagnostics")
106106
if not license_diagnostics or not license_text or not license_text_diagnostics:
107107
usage_suggestion_message = (
108-
"The --review option, whe paired with --license option should be used with the folowing "
108+
"The --todo option, whe paired with --license option should be used with the folowing "
109109
"additional CLI options for maximum benifit: [`--license-text`, `--license-text-diagnostics`,"
110110
"--license-diagnostics`] as these show additional diagnostic information to help review the issues."
111111
)
@@ -118,7 +118,7 @@ def process_codebase(self, codebase, **kwargs):
118118

119119
if not has_packages and not has_licenses:
120120
usage_suggestion_message = (
121-
"The --review option should be used with atleast one of the license [`--license`], "
121+
"The --todo option should be used with atleast one of the license [`--license`], "
122122
"or package [`--package`] options."
123123
)
124124
warnings.simplefilter('always', ToDoPluginUsageWarning)

tests/cluecode/data/plugin_filter_clues/filtered-expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license_expression": "apache-1.1",
66
"license_expression_spdx": "Apache-1.1",
77
"detection_count": 1,
8-
"sample_matches": [
8+
"reference_matches": [
99
{
1010
"license_expression": "apache-1.1",
1111
"license_expression_spdx": "Apache-1.1",

tests/cluecode/data/plugin_filter_clues/filtered-expected2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license_expression": "pygres-2.2",
66
"license_expression_spdx": "LicenseRef-scancode-pygres-2.2",
77
"detection_count": 1,
8-
"sample_matches": [
8+
"reference_matches": [
99
{
1010
"license_expression": "pygres-2.2",
1111
"license_expression_spdx": "LicenseRef-scancode-pygres-2.2",

tests/cluecode/data/plugin_filter_clues/filtered-expected3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license_expression": "pcre",
66
"license_expression_spdx": "LicenseRef-scancode-pcre",
77
"detection_count": 1,
8-
"sample_matches": [
8+
"reference_matches": [
99
{
1010
"license_expression": "pcre",
1111
"license_expression_spdx": "LicenseRef-scancode-pcre",

tests/formattedcode/data/common/manifests-expected.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@
483483
"license_expression": "apache-2.0",
484484
"license_expression_spdx": "Apache-2.0",
485485
"detection_count": 3,
486-
"sample_matches": [
486+
"reference_matches": [
487487
{
488488
"license_expression": "apache-2.0",
489489
"license_expression_spdx": "Apache-2.0",
@@ -505,7 +505,7 @@
505505
"license_expression": "apache-2.0",
506506
"license_expression_spdx": "Apache-2.0",
507507
"detection_count": 1,
508-
"sample_matches": [
508+
"reference_matches": [
509509
{
510510
"license_expression": "apache-2.0",
511511
"license_expression_spdx": "Apache-2.0",
@@ -527,7 +527,7 @@
527527
"license_expression": "cddl-1.0",
528528
"license_expression_spdx": "CDDL-1.0",
529529
"detection_count": 1,
530-
"sample_matches": [
530+
"reference_matches": [
531531
{
532532
"license_expression": "unknown-license-reference",
533533
"license_expression_spdx": "LicenseRef-scancode-unknown-license-reference",
@@ -563,7 +563,7 @@
563563
"license_expression": "cddl-1.0",
564564
"license_expression_spdx": "CDDL-1.0",
565565
"detection_count": 1,
566-
"sample_matches": [
566+
"reference_matches": [
567567
{
568568
"license_expression": "cddl-1.0",
569569
"license_expression_spdx": "CDDL-1.0",
@@ -599,7 +599,7 @@
599599
"license_expression": "lgpl-3.0",
600600
"license_expression_spdx": "LGPL-3.0-only",
601601
"detection_count": 2,
602-
"sample_matches": [
602+
"reference_matches": [
603603
{
604604
"license_expression": "lgpl-3.0",
605605
"license_expression_spdx": "LGPL-3.0-only",
@@ -621,7 +621,7 @@
621621
"license_expression": "lgpl-3.0",
622622
"license_expression_spdx": "LGPL-3.0-only",
623623
"detection_count": 1,
624-
"sample_matches": [
624+
"reference_matches": [
625625
{
626626
"license_expression": "lgpl-3.0",
627627
"license_expression_spdx": "LGPL-3.0-only",
@@ -643,7 +643,7 @@
643643
"license_expression": "lgpl-3.0",
644644
"license_expression_spdx": "LGPL-3.0-only",
645645
"detection_count": 1,
646-
"sample_matches": [
646+
"reference_matches": [
647647
{
648648
"license_expression": "lgpl-3.0",
649649
"license_expression_spdx": "LGPL-3.0-only",
@@ -665,7 +665,7 @@
665665
"license_expression": "lgpl-3.0",
666666
"license_expression_spdx": "LGPL-3.0-only",
667667
"detection_count": 1,
668-
"sample_matches": [
668+
"reference_matches": [
669669
{
670670
"license_expression": "lgpl-3.0",
671671
"license_expression_spdx": "LGPL-3.0-only",
@@ -687,7 +687,7 @@
687687
"license_expression": "mit",
688688
"license_expression_spdx": "MIT",
689689
"detection_count": 1,
690-
"sample_matches": [
690+
"reference_matches": [
691691
{
692692
"license_expression": "mit",
693693
"license_expression_spdx": "MIT",
@@ -709,7 +709,7 @@
709709
"license_expression": "mit",
710710
"license_expression_spdx": "MIT",
711711
"detection_count": 1,
712-
"sample_matches": [
712+
"reference_matches": [
713713
{
714714
"license_expression": "mit",
715715
"license_expression_spdx": "MIT",

0 commit comments

Comments
 (0)