77# See https://aboutcode.org for more information about nexB OSS projects.
88#
99import io
10+ import os
11+ import logging
1012from operator import itemgetter
1113from os .path import abspath
1214from os .path import dirname
4143TEMPLATES_DIR = join (dirname (__file__ ), 'templates' )
4244
4345
46+ TRACE = False
47+
48+
49+ def logger_debug (* args ):
50+ pass
51+
52+
53+ logger = logging .getLogger (__name__ )
54+
55+ if TRACE :
56+ import sys
57+ logging .basicConfig (stream = sys .stdout )
58+ logger .setLevel (logging .DEBUG )
59+
60+ def logger_debug (* args ):
61+ return logger .debug (' ' .join (isinstance (a , str ) and a or repr (a ) for a in args ))
62+
63+
4464@output_impl
4565class HtmlOutput (OutputPlugin ):
4666
@@ -59,9 +79,10 @@ def is_enabled(self, html, **kwargs):
5979 def process_codebase (self , codebase , html , ** kwargs ):
6080 results = self .get_files (codebase , ** kwargs )
6181 version = codebase .get_or_create_current_header ().tool_version
82+ license_references = codebase .attributes .license_references
6283 template_loc = join (TEMPLATES_DIR , 'html' , 'template.html' )
6384 output_file = html
64- write_templated (output_file , results , version , template_loc )
85+ write_templated (output_file , results , license_references , version , template_loc )
6586
6687
6788@output_impl
@@ -98,20 +119,21 @@ def is_enabled(self, custom_output, custom_template, **kwargs):
98119 def process_codebase (self , codebase , custom_output , custom_template , ** kwargs ):
99120 results = self .get_files (codebase , ** kwargs )
100121 version = codebase .get_or_create_current_header ().tool_version
122+ license_references = codebase .attributes .license_references
101123 template_loc = custom_template
102124 output_file = custom_output
103- write_templated (output_file , results , version , template_loc )
125+ write_templated (output_file , results , license_references , version , template_loc )
104126
105127
106- def write_templated (output_file , results , version , template_loc ):
128+ def write_templated (output_file , results , license_references , version , template_loc ):
107129 """
108130 Write scan output `results` to the `output_file` opened file using a template
109131 file at `template_loc`.
110132 Raise an exception on errors.
111133 """
112134 template = get_template (template_loc )
113135
114- for template_chunk in generate_output (results , version , template ):
136+ for template_chunk in generate_output (results , license_references , version , template ):
115137 assert isinstance (template_chunk , str )
116138 try :
117139 output_file .write (template_chunk )
@@ -138,23 +160,18 @@ def get_template(location):
138160 return env .get_template (template_name )
139161
140162
141- def generate_output (results , version , template ):
163+ def generate_output (results , license_references , version , template ):
142164 """
143165 Yield unicode strings from incrementally rendering `results` and `version`
144166 with the Jinja `template` object.
145167 """
146168 # FIXME: This code is highly coupled with actual scans and may not
147169 # support adding new scans at all
148-
149- from licensedcode .cache import get_licenses_db
150- licenses_db = get_licenses_db ()
151-
152170 converted = {}
153171 converted_infos = {}
154172 converted_packages = {}
155- licenses = {}
156173
157- LICENSES = 'licenses '
174+ LICENSES = 'license_detections '
158175 COPYRIGHTS = 'copyrights'
159176 PACKAGES = 'package_data'
160177
@@ -175,6 +192,8 @@ def generate_output(results, version, template):
175192 for match in get_matches_from_detection_mappings (scanned_file [LICENSES ]):
176193 # make copy
177194 match = dict (match )
195+ if TRACE :
196+ logger_debug (f"match: { match } " )
178197 license_expression = match ['license_expression' ]
179198 results .append ({
180199 'start' : match ['start_line' ],
@@ -183,11 +202,6 @@ def generate_output(results, version, template):
183202 'value' : license_expression ,
184203 })
185204
186- # FIXME: we should NOT rely on license objects: only use what is in the JSON instead
187- if license_expression not in licenses :
188- licenses [license_expression ] = match
189- # we were modifying the scan data in place ....
190- match ['object' ] = licenses_db .get (license_expression )
191205 if results :
192206 converted [path ] = sorted (results , key = itemgetter ('start' ))
193207
@@ -204,15 +218,13 @@ def generate_output(results, version, template):
204218 if PACKAGES in scanned_file :
205219 converted_packages [path ] = scanned_file [PACKAGES ]
206220
207- licenses = dict (sorted (licenses .items ()))
208-
209221 files = {
210222 'license_copyright' : converted ,
211223 'infos' : converted_infos ,
212224 'package_data' : converted_packages
213225 }
214226
215- return template .generate (files = files , licenses = licenses , version = version )
227+ return template .generate (files = files , licenses = license_references , version = version )
216228
217229
218230@output_impl
0 commit comments