3838from packagedcode .models import PackageWithResources
3939
4040TRACE = os .environ .get ('SCANCODE_DEBUG_PACKAGE_API' , False )
41+ TRACE_ASSEMBLY = os .environ .get ('SCANCODE_DEBUG_PACKAGE_ASSEMBLY' , False )
42+ TRACE_LICENSE = os .environ .get ('SCANCODE_DEBUG_PACKAGE_LICENSE' , False )
4143
4244
4345def logger_debug (* args ):
@@ -46,7 +48,7 @@ def logger_debug(*args):
4648
4749logger = logging .getLogger (__name__ )
4850
49- if TRACE :
51+ if TRACE or TRACE_LICENSE or TRACE_ASSEMBLY :
5052 import sys
5153
5254 logging .basicConfig (stream = sys .stdout )
@@ -185,7 +187,7 @@ def get_scanner(self, package=True, system_package=False, **kwargs):
185187 system = system_package ,
186188 )
187189
188- def process_codebase (self , codebase , strip_root = False , license_text = False , license_diagnostics = False , license_text_diagnostics = False , ** kwargs ):
190+ def process_codebase (self , codebase , strip_root = False , ** kwargs ):
189191 """
190192 Populate the ``codebase`` top level ``packages`` and ``dependencies``
191193 with package and dependency instances, assembling parsed package data
@@ -199,6 +201,20 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
199201 # These steps add proper license detections to package_data and hence
200202 # this is performed before top level packages creation
201203 for resource in codebase .walk (topdown = False ):
204+ # populate `from_file` attribute in matches
205+ for package_data in resource .package_data :
206+ for detection in package_data ['license_detections' ]:
207+ populate_matches_with_path (
208+ matches = detection ['matches' ],
209+ path = resource .path ,
210+ )
211+
212+ for detection in package_data ['other_license_detections' ]:
213+ populate_matches_with_path (
214+ matches = detection ['matches' ],
215+ path = resource .path ,
216+ )
217+
202218 if not has_licenses :
203219 #TODO: Add the steps where we detect licenses from files for only a package scan
204220 # in the multiprocessing get_package_data API function
@@ -207,7 +223,7 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
207223 # If we don't detect license in package_data but there is license detected in file
208224 # we add the license expression from the file to a package
209225 modified = add_license_from_file (resource , codebase )
210- if TRACE and modified :
226+ if TRACE_LICENSE and modified :
211227 logger_debug (f'packagedcode: process_codebase: add_license_from_file: modified: { modified } ' )
212228
213229 if codebase .has_single_resource :
@@ -216,25 +232,26 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
216232 # If there is referenced files in a extracted license statement, we follow
217233 # the references, look for license detections and add them back
218234 modified = list (add_referenced_license_matches_for_package (resource , codebase ))
219- if TRACE and modified :
235+ if TRACE_LICENSE and modified :
220236 logger_debug (f'packagedcode: process_codebase: add_referenced_license_matches_for_package: modified: { modified } ' )
221237
222238 # If there is a LICENSE file on the same level as the manifest, and no license
223239 # is detected in the package_data, we add the license from the file
224240 modified = add_license_from_sibling_file (resource , codebase )
225- if TRACE and modified :
241+ if TRACE_LICENSE and modified :
226242 logger_debug (f'packagedcode: process_codebase: add_license_from_sibling_file: modified: { modified } ' )
227243
228244 # Create codebase-level packages and dependencies
229245 create_package_and_deps (codebase , strip_root = strip_root , ** kwargs )
246+ #raise Exception()
230247
231248 if has_licenses :
232249 # This step is dependent on top level packages
233250 for resource in codebase .walk (topdown = False ):
234251 # If there is a unknown reference to a package we add the license
235252 # from the package license detection
236253 modified = list (add_referenced_license_detection_from_package (resource , codebase ))
237- if TRACE and modified :
254+ if TRACE_LICENSE and modified :
238255 logger_debug (f'packagedcode: process_codebase: add_referenced_license_matches_from_package: modified: { modified } ' )
239256
240257
@@ -244,15 +261,15 @@ def add_license_from_file(resource, codebase):
244261 and the file has license detections, and if so, populate the package_data license
245262 expression and detection fields from the file license.
246263 """
247- if TRACE :
264+ if TRACE_LICENSE :
248265 logger_debug (f'packagedcode.plugin_package: add_license_from_file: resource: { resource .path } ' )
249266
250267 if not resource .is_file :
251268 return
252269
253270 license_detections_file = resource .license_detections
254271
255- if TRACE :
272+ if TRACE_LICENSE :
256273 logger_debug (f'add_license_from_file: license_detections_file: { license_detections_file } ' )
257274 if not license_detections_file :
258275 return
@@ -263,7 +280,7 @@ def add_license_from_file(resource, codebase):
263280
264281 for pkg in package_data :
265282 license_detections_pkg = pkg ["license_detections" ]
266- if TRACE :
283+ if TRACE_LICENSE :
267284 logger_debug (f'add_license_from_file: license_detections_pkg: { license_detections_pkg } ' )
268285
269286 if not license_detections_pkg :
@@ -329,16 +346,7 @@ def create_package_and_deps(codebase, package_adder=add_to_package, strip_root=F
329346 ** kwargs
330347 )
331348
332- package_mappings = []
333- for package in packages :
334- for detection in package .license_detections :
335- populate_matches_with_path (
336- matches = detection ["matches" ],
337- path = package .datafile_paths [0 ],
338- )
339- package_mappings .append (package .to_dict ())
340-
341- codebase .attributes .packages .extend (package_mappings )
349+ codebase .attributes .packages .extend (package .to_dict () for package in packages )
342350 codebase .attributes .dependencies .extend (dep .to_dict () for dep in dependencies )
343351
344352
@@ -361,20 +369,20 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
361369 if resource .path in seen_resource_paths :
362370 continue
363371
364- if TRACE :
372+ if TRACE_ASSEMBLY :
365373 logger_debug ('get_package_and_deps: location:' , resource .location )
366374
367375 for package_data in resource .package_data :
368376 try :
369377 package_data = PackageData .from_dict (mapping = package_data )
370378
371- if TRACE :
379+ if TRACE_ASSEMBLY :
372380 logger_debug (' get_package_and_deps: package_data:' , package_data )
373381
374382 # Find a handler for this package datasource to assemble collect
375383 # packages and deps
376384 handler = get_package_handler (package_data )
377- if TRACE :
385+ if TRACE_ASSEMBLY :
378386 logger_debug (' get_package_and_deps: handler:' , handler )
379387
380388 items = handler .assemble (
@@ -385,7 +393,7 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
385393 )
386394
387395 for item in items :
388- if TRACE :
396+ if TRACE_ASSEMBLY :
389397 logger_debug (' get_package_and_deps: item:' , item )
390398
391399 if isinstance (item , Package ):
@@ -404,7 +412,7 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
404412 elif isinstance (item , Resource ):
405413 seen_resource_paths .add (item .path )
406414
407- if TRACE :
415+ if TRACE_ASSEMBLY :
408416 logger_debug (
409417 ' get_package_and_deps: seen_resource_path:' ,
410418 seen_resource_paths ,
0 commit comments