Skip to content

Commit a84b1ed

Browse files
committed
define process_codebase function for matching references
Signed-off-by: akugarg <[email protected]>
1 parent 6f751e9 commit a84b1ed

File tree

8 files changed

+385
-85
lines changed

8 files changed

+385
-85
lines changed

src/licensedcode/plugin_license.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010
from functools import partial
1111

1212
import attr
13-
import os
1413

1514
from commoncode import fileutils
1615
from commoncode.cliutils import PluggableCommandLineOption
17-
from licensedcode.models import BasicRule, License
1816
from plugincode.scan import ScanPlugin
1917
from plugincode.scan import scan_impl
2018
from commoncode.cliutils import MISC_GROUP
@@ -131,9 +129,8 @@ def process_codebase(self, codebase, **kwargs):
131129

132130
def match_reference_license(resource, codebase):
133131
"""
134-
Find instances for any licenses in referenced filenames
132+
Return the updated license matches having reference to another files
135133
"""
136-
137134
if not resource.is_file:
138135
return
139136

@@ -142,37 +139,35 @@ def match_reference_license(resource, codebase):
142139
if not licenses:
143140
return
144141

145-
location = resource.location
146-
from licensedcode import cache
147-
from scancode.api import get_licenses
148-
idx = cache.get_index()
149-
matches = idx.match(
150-
location=location, min_score=0)
151-
152-
for match in matches:
153-
ref_files=match.rule.referenced_filenames
154-
if len(ref_files) != 0:
155-
for i in range(len(ref_files)):
156-
if not ref_files[i].startswith('usr/share/common-licenses'):
157-
new_loc=find_reference_file(location,ref_files[i])
158-
if new_loc:
159-
new_lic=get_licenses(new_loc, min_score=0)
160-
licenses.extend(new_lic['licenses'])
161-
license_expressions.extend(new_lic['license_expressions'])
162-
142+
referenced_licenses = []
143+
referenced_license_expressions = []
144+
referenced_filenames = []
145+
146+
for license in licenses:
147+
referenced_files = license['matched_rule']['referenced_filenames']
148+
for referenced_filename in referenced_files:
149+
if not referenced_filename in referenced_filenames:
150+
referenced_filenames.append(referenced_filename)
151+
152+
for referenced_filename in referenced_filenames:
153+
new_resource = find_reference_licenses(referenced_filename=referenced_filename, resource=resource, codebase=codebase)
154+
if new_resource:
155+
referenced_licenses.extend(new_resource.licenses)
156+
referenced_license_expressions.extend(new_resource.license_expressions)
157+
158+
licenses.extend(referenced_licenses)
159+
license_expressions.extend(referenced_license_expressions)
163160
codebase.save_resource(resource)
164161
return resource
165162

166163

167-
def find_reference_file(location,referenced_filename):
164+
def find_reference_licenses(referenced_filename, resource, codebase, **kwargs):
168165
"""
169-
Searches for the referenced filename in the same directory and
170-
returns the found location
166+
Return the resource object for matching referenced-filename given a resource in codebase
171167
"""
172-
file_name=referenced_filename
173-
par_dir=fileutils.parent_directory(location)
174-
for root, dirs, files in os.walk(par_dir):
175-
if file_name in files:
176-
path_file = os.path.join(root,file_name)
177-
return path_file
178-
168+
parent = resource.parent(codebase)
169+
170+
for child in parent.children(codebase):
171+
path = child.path
172+
if path.endswith(referenced_filename) or fileutils.file_base_name(child.path) == referenced_filename:
173+
return child

tests/licensedcode/data/plugin_license/license_reference/scan-ref.expected.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"short_name": "MIT License",
3131
"category": "Permissive",
3232
"is_exception": false,
33+
"is_unknown": false,
3334
"owner": "MIT",
3435
"homepage_url": "http://opensource.org/licenses/mit-license.php",
3536
"text_url": "http://opensource.org/licenses/mit-license.php",
@@ -46,11 +47,13 @@
4647
"licenses": [
4748
"mit"
4849
],
50+
"referenced_filenames": [],
4951
"is_license_text": false,
5052
"is_license_notice": true,
5153
"is_license_reference": false,
5254
"is_license_tag": false,
5355
"is_license_intro": false,
56+
"has_unknown": false,
5457
"matcher": "1-hash",
5558
"rule_length": 10,
5659
"matched_length": 10,
@@ -77,6 +80,7 @@
7780
"short_name": "Unknown License reference",
7881
"category": "Unstated License",
7982
"is_exception": false,
83+
"is_unknown": true,
8084
"owner": "Unspecified",
8185
"homepage_url": null,
8286
"text_url": "",
@@ -93,11 +97,15 @@
9397
"licenses": [
9498
"unknown-license-reference"
9599
],
100+
"referenced_filenames": [
101+
"LICENSE"
102+
],
96103
"is_license_text": false,
97104
"is_license_notice": false,
98105
"is_license_reference": false,
99106
"is_license_tag": true,
100107
"is_license_intro": false,
108+
"has_unknown": true,
101109
"matcher": "2-aho",
102110
"rule_length": 5,
103111
"matched_length": 5,
@@ -113,6 +121,7 @@
113121
"short_name": "MIT License",
114122
"category": "Permissive",
115123
"is_exception": false,
124+
"is_unknown": false,
116125
"owner": "MIT",
117126
"homepage_url": "http://opensource.org/licenses/mit-license.php",
118127
"text_url": "http://opensource.org/licenses/mit-license.php",
@@ -129,17 +138,20 @@
129138
"licenses": [
130139
"mit"
131140
],
141+
"referenced_filenames": [],
132142
"is_license_text": false,
133143
"is_license_notice": true,
134144
"is_license_reference": false,
135145
"is_license_tag": false,
136146
"is_license_intro": false,
147+
"has_unknown": false,
137148
"matcher": "1-hash",
138149
"rule_length": 10,
139150
"matched_length": 10,
140151
"match_coverage": 100.0,
141152
"rule_relevance": 100
142-
}
153+
},
154+
"matched_text": "that is licensed under [MIT](http://opensource.org/licenses/MIT)."
143155
}
144156
],
145157
"license_expressions": [

tests/licensedcode/data/plugin_license/license_reference/scan-wref.expected.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"short_name": "Unknown License reference",
3131
"category": "Unstated License",
3232
"is_exception": false,
33+
"is_unknown": true,
3334
"owner": "Unspecified",
3435
"homepage_url": null,
3536
"text_url": "",
@@ -46,11 +47,15 @@
4647
"licenses": [
4748
"unknown-license-reference"
4849
],
50+
"referenced_filenames": [
51+
"LICENSE"
52+
],
4953
"is_license_text": false,
5054
"is_license_notice": false,
5155
"is_license_reference": false,
5256
"is_license_tag": true,
5357
"is_license_intro": false,
58+
"has_unknown": true,
5459
"matcher": "2-aho",
5560
"rule_length": 5,
5661
"matched_length": 5,

0 commit comments

Comments
 (0)