1010from functools import partial
1111
1212import attr
13- import os
1413
1514from commoncode import fileutils
1615from commoncode .cliutils import PluggableCommandLineOption
17- from licensedcode .models import BasicRule , License
1816from plugincode .scan import ScanPlugin
1917from plugincode .scan import scan_impl
2018from commoncode .cliutils import MISC_GROUP
@@ -131,9 +129,8 @@ def process_codebase(self, codebase, **kwargs):
131129
132130def 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
0 commit comments