Commit f7b9bdd8 by dorp

smaller refactoring

parent c34caf96
...@@ -44,10 +44,16 @@ def _setup_jinja_filters(environment): ...@@ -44,10 +44,16 @@ def _setup_jinja_filters(environment):
def generate_meta_data_code(environment, meta_data): def generate_meta_data_code(environment, meta_data):
template = environment.get_template('{}.tex'.format('meta_data')) template = environment.get_template('meta_data.tex')
return template.render(meta_data=meta_data) return template.render(meta_data=meta_data)
def generate_main_code(firmware_analyses, firmware_meta_data, jinja_environment):
template = jinja_environment.get_template('main.tex')
return template.render(analysis=firmware_analyses, meta_data=firmware_meta_data)
def generate_analysis_codes(environment, analysis): def generate_analysis_codes(environment, analysis):
return [('{}.tex'.format(analysis_plugin), _render_analysis_result(analysis[analysis_plugin], environment, analysis_plugin)) for analysis_plugin in analysis] return [('{}.tex'.format(analysis_plugin), _render_analysis_result(analysis[analysis_plugin], environment, analysis_plugin)) for analysis_plugin in analysis]
...@@ -77,7 +83,14 @@ def _copy_fact_image(target): ...@@ -77,7 +83,14 @@ def _copy_fact_image(target):
shutil.copy(Path(__file__).parent.parent / 'templates' / 'fact_logo.png', Path(target) / 'fact_logo.png') shutil.copy(Path(__file__).parent.parent / 'templates' / 'fact_logo.png', Path(target) / 'fact_logo.png')
def main(firmware_uid="bab8d95fc42176abc9126393b6035e4012ebccc82c91e521b91d3bcba4832756_3801088"): def execute_pdflatex(tmp_dir):
current_dir = os.getcwd()
os.chdir(tmp_dir)
output, return_code = execute_shell_command_get_return_code('env buf_size=1000000 pdflatex main.tex')
os.chdir(current_dir)
def generate_pdf_report(firmware_uid="bab8d95fc42176abc9126393b6035e4012ebccc82c91e521b91d3bcba4832756_3801088"):
request_url = create_request_url(firmware_uid) request_url = create_request_url(firmware_uid)
firmware_analyses, firmware_meta_data = request_firmware_data(request_url) firmware_analyses, firmware_meta_data = request_firmware_data(request_url)
...@@ -86,21 +99,15 @@ def main(firmware_uid="bab8d95fc42176abc9126393b6035e4012ebccc82c91e521b91d3bcba ...@@ -86,21 +99,15 @@ def main(firmware_uid="bab8d95fc42176abc9126393b6035e4012ebccc82c91e521b91d3bcba
with TemporaryDirectory() as tmp_dir: with TemporaryDirectory() as tmp_dir:
Path(tmp_dir, 'meta.tex').write_text(generate_meta_data_code(environment=jinja_environment, meta_data=firmware_meta_data)) Path(tmp_dir, 'meta.tex').write_text(generate_meta_data_code(environment=jinja_environment, meta_data=firmware_meta_data))
for filename, result_code in generate_analysis_codes(environment=jinja_environment, analysis=firmware_analyses): for filename, result_code in generate_analysis_codes(environment=jinja_environment, analysis=firmware_analyses):
Path(tmp_dir, filename).write_text(result_code) Path(tmp_dir, filename).write_text(result_code)
# main_template_code = Path(Path(__file__).parent.parent, 'templates', 'default', 'main.tex').read_text() Path(tmp_dir, 'main.tex').write_text(generate_main_code(firmware_analyses, firmware_meta_data, jinja_environment))
template = jinja_environment.get_template('main.tex')
main_code = template.render(analysis=firmware_analyses, meta_data=firmware_meta_data)
Path(tmp_dir, 'main.tex').write_text(main_code)
pdf_filename = create_report_filename(firmware_meta_data)
_copy_fact_image(tmp_dir) _copy_fact_image(tmp_dir)
current_dir = os.getcwd() execute_pdflatex(tmp_dir)
os.chdir(tmp_dir)
output, return_code = execute_shell_command_get_return_code('env buf_size=1000000 pdflatex main.tex') pdf_filename = create_report_filename(firmware_meta_data)
os.chdir(current_dir)
shutil.move(Path(tmp_dir, 'main.pdf'), Path('.', pdf_filename)) shutil.move(Path(tmp_dir, 'main.pdf'), Path('.', pdf_filename))
...@@ -18,6 +18,8 @@ import logging ...@@ -18,6 +18,8 @@ import logging
import os import os
import sys import sys
from latex_code_generation.code_generation import generate_pdf_report
PROGRAM_NAME = 'FACT PDF Report Generator' PROGRAM_NAME = 'FACT PDF Report Generator'
PROGRAM_VERSION = '0.1' PROGRAM_VERSION = '0.1'
...@@ -76,5 +78,6 @@ if __name__ == '__main__': ...@@ -76,5 +78,6 @@ if __name__ == '__main__':
logging.info(args.config_file) logging.info(args.config_file)
# insert your program here # insert your program here
generate_pdf_report(args.UID)
sys.exit() sys.exit()
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from pathlib import Path from pathlib import Path
from latex_code_generation.code_generation import _set_jinja_env, _render_analysis_result, _create_tex_files, main from latex_code_generation.code_generation import _set_jinja_env, _render_analysis_result, _create_tex_files, generate_pdf_report
def test_render_template(): def test_render_template():
...@@ -21,5 +21,5 @@ def test_create_tex_files(): ...@@ -21,5 +21,5 @@ def test_create_tex_files():
def test_main(): def test_main():
main(firmware_uid='b79ea608e2f0390744642bad472f8d9fd7e4713791857da5d5fcabf70a009e50_29626948') generate_pdf_report(firmware_uid='b79ea608e2f0390744642bad472f8d9fd7e4713791857da5d5fcabf70a009e50_29626948')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment