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 )
@@ -207,7 +209,7 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
207209 # If we don't detect license in package_data but there is license detected in file
208210 # we add the license expression from the file to a package
209211 modified = add_license_from_file (resource , codebase )
210- if TRACE and modified :
212+ if TRACE_LICENSE and modified :
211213 logger_debug (f'packagedcode: process_codebase: add_license_from_file: modified: { modified } ' )
212214
213215 if codebase .has_single_resource :
@@ -216,25 +218,26 @@ def process_codebase(self, codebase, strip_root=False, license_text=False, licen
216218 # If there is referenced files in a extracted license statement, we follow
217219 # the references, look for license detections and add them back
218220 modified = list (add_referenced_license_matches_for_package (resource , codebase ))
219- if TRACE and modified :
221+ if TRACE_LICENSE and modified :
220222 logger_debug (f'packagedcode: process_codebase: add_referenced_license_matches_for_package: modified: { modified } ' )
221223
222224 # If there is a LICENSE file on the same level as the manifest, and no license
223225 # is detected in the package_data, we add the license from the file
224226 modified = add_license_from_sibling_file (resource , codebase )
225- if TRACE and modified :
227+ if TRACE_LICENSE and modified :
226228 logger_debug (f'packagedcode: process_codebase: add_license_from_sibling_file: modified: { modified } ' )
227229
228230 # Create codebase-level packages and dependencies
229231 create_package_and_deps (codebase , strip_root = strip_root , ** kwargs )
232+ #raise Exception()
230233
231234 if has_licenses :
232235 # This step is dependent on top level packages
233236 for resource in codebase .walk (topdown = False ):
234237 # If there is a unknown reference to a package we add the license
235238 # from the package license detection
236239 modified = list (add_referenced_license_detection_from_package (resource , codebase ))
237- if TRACE and modified :
240+ if TRACE_LICENSE and modified :
238241 logger_debug (f'packagedcode: process_codebase: add_referenced_license_matches_from_package: modified: { modified } ' )
239242
240243
@@ -244,15 +247,15 @@ def add_license_from_file(resource, codebase):
244247 and the file has license detections, and if so, populate the package_data license
245248 expression and detection fields from the file license.
246249 """
247- if TRACE :
250+ if TRACE_LICENSE :
248251 logger_debug (f'packagedcode.plugin_package: add_license_from_file: resource: { resource .path } ' )
249252
250253 if not resource .is_file :
251254 return
252255
253256 license_detections_file = resource .license_detections
254257
255- if TRACE :
258+ if TRACE_LICENSE :
256259 logger_debug (f'add_license_from_file: license_detections_file: { license_detections_file } ' )
257260 if not license_detections_file :
258261 return
@@ -263,7 +266,7 @@ def add_license_from_file(resource, codebase):
263266
264267 for pkg in package_data :
265268 license_detections_pkg = pkg ["license_detections" ]
266- if TRACE :
269+ if TRACE_LICENSE :
267270 logger_debug (f'add_license_from_file: license_detections_pkg: { license_detections_pkg } ' )
268271
269272 if not license_detections_pkg :
@@ -329,16 +332,7 @@ def create_package_and_deps(codebase, package_adder=add_to_package, strip_root=F
329332 ** kwargs
330333 )
331334
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 )
335+ codebase .attributes .packages .extend (package .to_dict () for package in packages )
342336 codebase .attributes .dependencies .extend (dep .to_dict () for dep in dependencies )
343337
344338
@@ -361,20 +355,20 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
361355 if resource .path in seen_resource_paths :
362356 continue
363357
364- if TRACE :
358+ if TRACE_ASSEMBLY :
365359 logger_debug ('get_package_and_deps: location:' , resource .location )
366360
367361 for package_data in resource .package_data :
368362 try :
369363 package_data = PackageData .from_dict (mapping = package_data )
370364
371- if TRACE :
365+ if TRACE_ASSEMBLY :
372366 logger_debug (' get_package_and_deps: package_data:' , package_data )
373367
374368 # Find a handler for this package datasource to assemble collect
375369 # packages and deps
376370 handler = get_package_handler (package_data )
377- if TRACE :
371+ if TRACE_ASSEMBLY :
378372 logger_debug (' get_package_and_deps: handler:' , handler )
379373
380374 items = handler .assemble (
@@ -385,7 +379,7 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
385379 )
386380
387381 for item in items :
388- if TRACE :
382+ if TRACE_ASSEMBLY :
389383 logger_debug (' get_package_and_deps: item:' , item )
390384
391385 if isinstance (item , Package ):
@@ -404,7 +398,7 @@ def get_package_and_deps(codebase, package_adder=add_to_package, strip_root=Fals
404398 elif isinstance (item , Resource ):
405399 seen_resource_paths .add (item .path )
406400
407- if TRACE :
401+ if TRACE_ASSEMBLY :
408402 logger_debug (
409403 ' get_package_and_deps: seen_resource_path:' ,
410404 seen_resource_paths ,
0 commit comments