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,34 @@ 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+ unique_files = []
145+
146+ for license in licenses :
147+ referenced_filenames = license ['matched_rule' ]['referenced_filenames' ]
148+ for referenced_filename in referenced_filenames :
149+ if referenced_filename .startswith ('usr/share/common-licenses' ) or referenced_filename in unique_files :
150+ continue
151+ unique_files .append (referenced_filename )
152+ new_resource = find_reference_licenses (referenced_filename = referenced_filename , resource = resource , codebase = codebase )
153+ if new_resource :
154+ referenced_licenses .extend (new_resource .licenses )
155+ referenced_license_expressions .extend (new_resource .license_expressions )
156+
157+ licenses .extend (referenced_licenses )
158+ license_expressions .extend (referenced_license_expressions )
163159 codebase .save_resource (resource )
164160 return resource
165161
166162
167- def find_reference_file ( location , referenced_filename ):
163+ def find_reference_licenses ( referenced_filename , resource , codebase , ** kwargs ):
168164 """
169- Searches for the referenced filename in the same directory and
170- returns the found location
165+ Return the resource object for matching referenced-filename given a resource in codebase
171166 """
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-
167+ parent = resource .parent (codebase )
168+
169+ for child in parent .children (codebase ):
170+ path = child .path
171+ if path .endswith (referenced_filename ):
172+ return child
0 commit comments