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)
| 305 | |
| 306 | |
| 307 | def 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) |
no test coverage detected