Skip to content

Commit a3946df

Browse files
Merge pull request #3299 from nexB/license-detection-follow-up
Fix misc license detection related bugs
2 parents ac47da2 + 5c9d5a8 commit a3946df

File tree

48 files changed

+108784
-136249
lines changed

Some content is hidden

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

48 files changed

+108784
-136249
lines changed

sample.json

Lines changed: 4673 additions & 0 deletions
Large diffs are not rendered by default.

src/licensedcode/detection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def detections_from_license_detection_mappings(
522522
return license_detections
523523

524524

525-
def get_new_identifier_from_detections(initial_detection, detections_added):
525+
def get_new_identifier_from_detections(initial_detection, detections_added, license_expression):
526526
"""
527527
Return a new UUID based on two sets of detections: `initial_detection` is
528528
the detection being modified with a list of detections (from another file region)
@@ -533,7 +533,8 @@ def get_new_identifier_from_detections(initial_detection, detections_added):
533533
for detection_mapping in detections_added
534534
]
535535
identifiers.append(initial_detection["identifier"])
536-
return get_uuid_on_content(content=sorted(identifiers))
536+
uuid = get_uuid_on_content(content=sorted(identifiers))
537+
return f"{license_expression}-{uuid}"
537538

538539

539540
@attr.s

src/licensedcode/plugin_license.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ def add_referenced_filenames_license_matches_for_detections(resource, codebase):
354354
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
355355
initial_detection=license_detection_mapping,
356356
detections_added=detections_added,
357+
license_expression=license_expression,
357358
)
358359

359360
if modified:

src/licensedcode/plugin_license_policy.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class LicensePolicy(PostScanPlugin):
4949
detected license key that is found in the license_policy.yml file
5050
"""
5151

52-
resource_attributes = dict(license_policy=attr.ib(default=attr.Factory(dict)))
52+
resource_attributes = dict(license_policy=attr.ib(default=attr.Factory(list)))
5353

5454
sort_order = 9
5555

@@ -90,16 +90,19 @@ def process_codebase(self, codebase, license_policy, **kwargs):
9090

9191
except AttributeError:
9292
# add license_policy regardless if there is license info or not
93-
resource.license_policy = {}
93+
resource.license_policy = []
9494
codebase.save_resource(resource)
9595
continue
9696

97+
license_policies = []
9798
for key in resource_license_keys:
9899
for policy in policies:
99100
if key == policy.get('license_key'):
100101
# Apply the policy to the Resource
101-
resource.license_policy = policy
102-
codebase.save_resource(resource)
102+
license_policies.append(policy)
103+
104+
resource.license_policy = sorted(license_policies, key=lambda d: d['license_key'])
105+
codebase.save_resource(resource)
103106

104107

105108
def has_policy_duplicates(license_policy_location):

src/packagedcode/licensing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def add_referenced_license_matches_for_package(resource, codebase, no_licenses):
136136
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
137137
initial_detection=license_detection_mapping,
138138
detections_added=referenced_license_detections,
139+
license_expression=license_expression,
139140
)
140141

141142
if modified:
@@ -252,6 +253,7 @@ def add_referenced_license_detection_from_package(resource, codebase, no_license
252253
license_detection_mapping["identifier"] = get_new_identifier_from_detections(
253254
initial_detection=license_detection_mapping,
254255
detections_added=detections_added,
256+
license_expression=license_expression,
255257
)
256258

257259
if modified:

src/scancode_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def _create_dir(location):
9595
from subprocess import CalledProcessError
9696

9797
# this may fail with exceptions
98-
cmd = 'git', 'describe',
98+
cmd = 'git', 'describe', '--tags',
9999
try:
100100
output = check_output(cmd, stderr=STDOUT)
101101
__version__ = output.decode('utf-8').strip()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
path,type,declared_license_expression,scan_errors
2-
apache-2.0.LICENSE,file,apache-2.0 AND mit,
1+
path,type,declared_license_expression,declared_license_expression_spdx,scan_errors
2+
apache-2.0.LICENSE,file,apache-2.0 AND mit,Apache-2.0 AND MIT,

tests/formattedcode/data/csv/expressions/scan.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"type": "file",
66
"license_detections": [],
77
"declared_license_expression": "apache-2.0 AND mit",
8+
"declared_license_expression_spdx": "Apache-2.0 AND MIT",
9+
"license_clues": [],
810
"scan_errors": []
911
}
1012
]

tests/formattedcode/test_output_csv.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ def test_can_process_live_scan_for_packages_with_root():
124124

125125

126126
def test_output_can_handle_non_ascii_paths():
127+
# very small file with selective attributes just to test unicode, no need
128+
# run scan again, update manually if required
127129
test_file = test_env.get_test_loc('csv/unicode.json')
128130
result_file = test_env.get_temp_file(extension='csv', file_name='test_csv')
129131
run_scan_click(['--from-json', test_file, '--csv', result_file])
@@ -250,6 +252,8 @@ def test_can_process_live_scan_for_packages_strip_root():
250252

251253
@pytest.mark.scanslow
252254
def test_output_contains_license_expression():
255+
# very small file with selective attributes just to test license_expressions,
256+
# no need to run scan again, update manually if required
253257
test_file = test_env.get_test_loc('csv/expressions/scan.json')
254258
result_file = test_env.get_temp_file('csv')
255259
args = ['--from-json', test_file, '--csv', result_file]
@@ -260,6 +264,7 @@ def test_output_contains_license_expression():
260264

261265
@pytest.mark.scanslow
262266
def test_output_handles_non_standard_data():
267+
# non standard data from other tool, update manually if required
263268
test_file = test_env.get_test_loc('csv/non-standard/identified.json')
264269
result_file = test_env.get_temp_file('csv')
265270
args = ['--from-json', test_file, '--csv', result_file]

tests/licensedcode/data/plugin_license/license_reference/license-ref-see-copying.expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"detection_log": [
8585
"unknown-reference-to-local-file"
8686
],
87-
"identifier": "1c807a43-2040-70af-75aa-c343d5f2b90c"
87+
"identifier": "apache-2.0-1c807a43-2040-70af-75aa-c343d5f2b90c"
8888
}
8989
],
9090
"license_clues": [],

0 commit comments

Comments
 (0)