Skip to content

Commit 9b77403

Browse files
Fix licenses in HTML output #3272
Fix an issue in HTML output where no licenses were being shown, now we also use license references here. Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent fca7b72 commit 9b77403

2 files changed

Lines changed: 39 additions & 23 deletions

File tree

src/formattedcode/output_html.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99
import io
10+
import os
11+
import logging
1012
from operator import itemgetter
1113
from os.path import abspath
1214
from os.path import dirname
@@ -41,6 +43,24 @@
4143
TEMPLATES_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
4565
class 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

src/formattedcode/templates/html/template.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,19 @@
276276
</tr>
277277
</thead>
278278
<tbody>
279-
{% for key, license in licenses.items() %}
279+
{% for license in licenses %}
280280
<tr id="license_{{ license.key }}">
281281
<td>{{ license.key }}</td>
282282
<td>{{ license.short_name }}</td>
283283
<td>{{ license.category }}</td>
284284
<td>{{ license.owner }}</td>
285-
<td>{{ license.reference_url|urlize(target='_blank') }}</td>
286-
<td>{{ license.homepage_url|urlize(target='_blank') }}</td>
287-
<td>{{ license.text_url|urlize(target='_blank') }}</td>
285+
<td>{{ license.scancode_url|urlize(target='_blank') }}</td>
286+
<td>{{ license.licensedb_url|urlize(target='_blank') }}</td>
287+
<td>
288+
{% for text_url in license.text_urls %}
289+
{{ text_url|urlize(target='_blank') }}
290+
{% endfor %}
291+
</td>
288292
<td>{{ license.spdx_license_key }}</td>
289293
<td>{{ license.spdx_url|urlize(target='_blank') }}</td>
290294
</tr>

0 commit comments

Comments
 (0)