Skip to content

Commit 5fa4d00

Browse files
committed
Create plugin for following reference files
Signed-off-by: akugarg <[email protected]>
1 parent c8cf696 commit 5fa4d00

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/licensedcode/plugin_license.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
from functools import partial
1111

1212
import attr
13+
import os
1314

15+
from commoncode import fileutils
1416
from commoncode.cliutils import PluggableCommandLineOption
1517
from plugincode.scan import ScanPlugin
1618
from plugincode.scan import scan_impl
@@ -116,3 +118,54 @@ def get_scanner(
116118
license_text_diagnostics=license_text_diagnostics,
117119
license_url_template=license_url_template
118120
)
121+
def process_codebase(self, codebase, **kwargs):
122+
123+
for resource in codebase.walk(topdown=False):
124+
match_reference_license(resource,codebase)
125+
126+
127+
def match_reference_license(resource, codebase):
128+
"""
129+
Find instances for any licenses in referenced filenames
130+
"""
131+
licenses = resource.licenses
132+
license_expressions = resource.license_expressions
133+
if not licenses:
134+
return
135+
136+
location = resource.location
137+
138+
from licensedcode import cache
139+
from scancode.api import get_licenses
140+
idx = cache.get_index()
141+
matches = idx.match(
142+
location=location, min_score=0)
143+
144+
modified = False
145+
146+
for match in matches:
147+
ref_files=match.rule.referenced_filenames
148+
if len(ref_files) != 0:
149+
for i in range(len(ref_files)):
150+
if not ref_files[i].startswith('usr/share/common-licenses'):
151+
new_loc=find_reference_file(location,ref_files[i])
152+
if new_loc != None:
153+
new_lic=get_licenses(new_loc, min_score=0)
154+
licenses.extend(new_lic['licenses'])
155+
license_expressions.extend(new_lic['license_expressions'])
156+
modified = True
157+
158+
if modified:
159+
codebase.save_resource(resource)
160+
return resource
161+
162+
def find_reference_file(location,referenced_filename):
163+
file_name=referenced_filename
164+
par_dir=fileutils.parent_directory(location)
165+
166+
for root, dirs, files in os.walk(par_dir):
167+
if file_name in files:
168+
path_file = os.path.join(root,file_name)
169+
return path_file
170+
171+
return None

0 commit comments

Comments
 (0)