MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / create_html_app

Function create_html_app

src/formattedcode/output_html.py:307–363  ·  view source on GitHub ↗

Given an html-app output_file, generate that file, create the data.js data file from the results and create the corresponding `_files` directory and copy the data and assets to this directory. The target directory is deleted if it exists. Raise HtmlAppAssetCopyWarning if the ou

(output_file, results, version, scanned_path)

Source from the content-addressed store, hash-verified

305
306
307def create_html_app(output_file, results, version, scanned_path): # NOQA
308 """
309 Given an html-app output_file, generate that file, create the data.js data
310 file from the results and create the corresponding `_files` directory and
311 copy the data and assets to this directory. The target directory is deleted
312 if it exists.
313
314 Raise HtmlAppAssetCopyWarning if the output_file is <stdout> or
315 HtmlAppAssetCopyError if the copy was not possible.
316 """
317 try:
318 if is_stdout(output_file):
319 raise HtmlAppAssetCopyWarning()
320
321 source_assets_dir = join(TEMPLATES_DIR, 'html-app', 'assets')
322
323 # Return a tuple of (parent_dir, dir_name) directory named after the
324 # `output_location` output_locationfile_base_name (stripped from extension) and
325 # a `_files` suffix Return empty strings if output is to stdout.
326 output_location = output_file.name
327 tgt_root_path = dirname(output_location)
328 tgt_assets_dir = file_base_name(output_location) + '_files'
329
330 # delete old assets
331 target_assets_dir = join(tgt_root_path, tgt_assets_dir)
332 if exists(target_assets_dir):
333 delete(target_assets_dir)
334
335 # copy assets
336 copytree(source_assets_dir, target_assets_dir)
337
338 template = get_template(join(TEMPLATES_DIR, 'html-app', 'template.html'))
339 rendered_html = template.render(
340 assets_dir=target_assets_dir,
341 scanned_path=scanned_path,
342 version=version
343 )
344 output_file.write(rendered_html)
345
346 # create help file
347 help_template = get_template(join(TEMPLATES_DIR, 'html-app', 'help_template.html'))
348 rendered_help = help_template.render(main_app=output_location)
349 with io.open(join(target_assets_dir, 'help.html'), 'w', encoding='utf-8') as f:
350 f.write(rendered_help)
351
352 # FIXME: this should a regular JSON scan format
353 with io.open(join(target_assets_dir, 'data.js'), 'w') as f:
354 f.write('data=')
355 json.dump(list(results), f)
356
357 except HtmlAppAssetCopyWarning as w:
358 raise w
359
360 except Exception as e: # NOQA
361 import traceback
362 msg = 'ERROR: cannot create HTML application.\n' + traceback.format_exc()
363 raise HtmlAppAssetCopyError(msg)

Callers 1

process_codebaseMethod · 0.85

Calls 11

file_base_nameFunction · 0.90
deleteFunction · 0.90
copytreeFunction · 0.90
is_stdoutFunction · 0.85
get_templateFunction · 0.85
renderMethod · 0.45
writeMethod · 0.45
openMethod · 0.45
dumpMethod · 0.45

Tested by

no test coverage detected