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,12 @@ 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 = []
83+ if hasattr (codebase .attributes , 'license_references' ):
84+ license_references = codebase .attributes .license_references
6285 template_loc = join (TEMPLATES_DIR , 'html' , 'template.html' )
6386 output_file = html
64- write_templated (output_file , results , version , template_loc )
87+ write_templated (output_file , results , license_references , version , template_loc )
6588
6689
6790@output_impl
@@ -98,20 +121,23 @@ def is_enabled(self, custom_output, custom_template, **kwargs):
98121 def process_codebase (self , codebase , custom_output , custom_template , ** kwargs ):
99122 results = self .get_files (codebase , ** kwargs )
100123 version = codebase .get_or_create_current_header ().tool_version
124+ license_references = []
125+ if hasattr (codebase .attributes , 'license_references' ):
126+ license_references = codebase .attributes .license_references
101127 template_loc = custom_template
102128 output_file = custom_output
103- write_templated (output_file , results , version , template_loc )
129+ write_templated (output_file , results , license_references , version , template_loc )
104130
105131
106- def write_templated (output_file , results , version , template_loc ):
132+ def write_templated (output_file , results , license_references , version , template_loc ):
107133 """
108134 Write scan output `results` to the `output_file` opened file using a template
109135 file at `template_loc`.
110136 Raise an exception on errors.
111137 """
112138 template = get_template (template_loc )
113139
114- for template_chunk in generate_output (results , version , template ):
140+ for template_chunk in generate_output (results , license_references , version , template ):
115141 assert isinstance (template_chunk , str )
116142 try :
117143 output_file .write (template_chunk )
@@ -138,23 +164,18 @@ def get_template(location):
138164 return env .get_template (template_name )
139165
140166
141- def generate_output (results , version , template ):
167+ def generate_output (results , license_references , version , template ):
142168 """
143169 Yield unicode strings from incrementally rendering `results` and `version`
144170 with the Jinja `template` object.
145171 """
146172 # FIXME: This code is highly coupled with actual scans and may not
147173 # support adding new scans at all
148-
149- from licensedcode .cache import get_licenses_db
150- licenses_db = get_licenses_db ()
151-
152174 converted = {}
153175 converted_infos = {}
154176 converted_packages = {}
155- licenses = {}
156177
157- LICENSES = 'licenses '
178+ LICENSES = 'license_detections '
158179 COPYRIGHTS = 'copyrights'
159180 PACKAGES = 'package_data'
160181
@@ -175,6 +196,8 @@ def generate_output(results, version, template):
175196 for match in get_matches_from_detection_mappings (scanned_file [LICENSES ]):
176197 # make copy
177198 match = dict (match )
199+ if TRACE :
200+ logger_debug (f"match: { match } " )
178201 license_expression = match ['license_expression' ]
179202 results .append ({
180203 'start' : match ['start_line' ],
@@ -183,11 +206,6 @@ def generate_output(results, version, template):
183206 'value' : license_expression ,
184207 })
185208
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 )
191209 if results :
192210 converted [path ] = sorted (results , key = itemgetter ('start' ))
193211
@@ -204,15 +222,13 @@ def generate_output(results, version, template):
204222 if PACKAGES in scanned_file :
205223 converted_packages [path ] = scanned_file [PACKAGES ]
206224
207- licenses = dict (sorted (licenses .items ()))
208-
209225 files = {
210226 'license_copyright' : converted ,
211227 'infos' : converted_infos ,
212228 'package_data' : converted_packages
213229 }
214230
215- return template .generate (files = files , licenses = licenses , version = version )
231+ return template .generate (files = files , licenses = license_references , version = version )
216232
217233
218234@output_impl
0 commit comments