Commit 653701f3 by dorp

removed unused pre processing module

parent 1a3b38b5
import logging
import os import os
import shutil import shutil
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory
from common_helper_process import execute_shell_command from common_helper_process import execute_shell_command
from pdf_generator.pre_processing.rest import create_request_url, request_firmware_data from pdf_generator.tex_generation.template_engine import TemplateEngine
from pdf_generator.tex_generation.template_engine import Engine
def execute_latex(tmp_dir): def execute_latex(tmp_dir):
...@@ -20,7 +17,7 @@ def copy_fact_image(target): ...@@ -20,7 +17,7 @@ def copy_fact_image(target):
shutil.copy(str(Path(__file__).parent / 'templates' / 'fact_logo.png'), str(Path(target) / 'fact_logo.png')) shutil.copy(str(Path(__file__).parent / 'templates' / 'fact_logo.png'), str(Path(target) / 'fact_logo.png'))
def generate_analysis_templates(engine, analysis): def render_analysis_templates(engine, analysis):
return [ return [
('{}.tex'.format(analysis_plugin), engine.render_analysis_template(analysis_plugin, analysis[analysis_plugin])) for analysis_plugin in analysis ('{}.tex'.format(analysis_plugin), engine.render_analysis_template(analysis_plugin, analysis[analysis_plugin])) for analysis_plugin in analysis
] ]
...@@ -41,24 +38,10 @@ def compile_pdf(meta_data, tmp_dir): ...@@ -41,24 +38,10 @@ def compile_pdf(meta_data, tmp_dir):
def create_templates(analysis, meta_data, tmp_dir): def create_templates(analysis, meta_data, tmp_dir):
engine = Engine(tmp_dir=tmp_dir) engine = TemplateEngine(tmp_dir=tmp_dir)
Path(tmp_dir, 'main.tex').write_text(engine.render_main_template(analysis=analysis, meta_data=meta_data)) Path(tmp_dir, 'main.tex').write_text(engine.render_main_template(analysis=analysis, meta_data=meta_data))
Path(tmp_dir, 'meta.tex').write_text(engine.render_meta_template(meta_data)) Path(tmp_dir, 'meta.tex').write_text(engine.render_meta_template(meta_data))
for filename, result_code in generate_analysis_templates(engine=engine, analysis=analysis):
Path(tmp_dir, filename).write_text(result_code)
for filename, rendered_template in render_analysis_templates(engine=engine, analysis=analysis):
def generate_report(firmware_uid, server_url=None): Path(tmp_dir, filename).write_text(rendered_template)
request_url = create_request_url(firmware_uid, server_url)
try:
analysis, meta_data = request_firmware_data(request_url)
except RuntimeError as error:
logging.warning('No firmware found with UID {}: {}'.format(firmware_uid, error))
return 1
with TemporaryDirectory() as tmp_dir:
create_templates(analysis, meta_data, tmp_dir)
compile_pdf(meta_data, tmp_dir)
return 0
from json import JSONDecodeError
import requests
DEFAULT_URL = 'http://localhost:5000'
def create_request_url(firmware_uid, server_url):
return '{}/rest/firmware/{}?summary=true'.format(server_url if server_url else DEFAULT_URL, firmware_uid)
def request_firmware_data(request_url):
try:
response = requests.get(request_url)
firmware_data = response.json()
return firmware_data['firmware']['analysis'], firmware_data['firmware']['meta_data']
except (JSONDecodeError, requests.ConnectionError):
raise RuntimeError('FACT server is not reachable') from None
except KeyError:
raise RuntimeError('Response did not contain a valid firmware') from None
...@@ -144,7 +144,7 @@ def _add_filters_to_jinja(environment): ...@@ -144,7 +144,7 @@ def _add_filters_to_jinja(environment):
environment.filters['contains'] = item_contains_string environment.filters['contains'] = item_contains_string
class Engine: class TemplateEngine:
def __init__(self, template_folder=None, tmp_dir=None): def __init__(self, template_folder=None, tmp_dir=None):
self._environment = create_jinja_environment(template_folder if template_folder else 'default') self._environment = create_jinja_environment(template_folder if template_folder else 'default')
self._tmp_dir = tmp_dir self._tmp_dir = tmp_dir
......
import pytest import pytest
from pdf_generator.generator import generate_report from pdf_generator.tex_generation.template_engine import TemplateEngine
from pdf_generator.tex_generation.template_engine import Engine
from test.data.test_dict import TEST_DICT from test.data.test_dict import TEST_DICT
# pylint: disable=redefined-outer-name # pylint: disable=redefined-outer-name
...@@ -13,23 +12,17 @@ TEST_DATA = { ...@@ -13,23 +12,17 @@ TEST_DATA = {
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def stub_engine(): def stub_engine():
return Engine() return TemplateEngine()
def test_latex_code_generation(stub_engine: Engine): def test_latex_code_generation(stub_engine: TemplateEngine):
result = stub_engine.render_meta_template(TEST_DICT) result = stub_engine.render_meta_template(TEST_DICT)
assert result assert result
def test_render_template(tmpdir): def test_render_template(tmpdir):
engine = Engine(template_folder='test', tmp_dir=tmpdir) engine = TemplateEngine(template_folder='test', tmp_dir=tmpdir)
test_data = {'meta_data': '123', 'analysis': '456'} test_data = {'meta_data': '123', 'analysis': '456'}
output = engine.render_analysis_template(plugin='render_test', analysis=test_data) output = engine.render_analysis_template(plugin='render_test', analysis=test_data)
assert output == 'Test - ' assert output == 'Test - '
def test_main(monkeypatch):
monkeypatch.setattr('pdf_generator.generator.request_firmware_data', lambda *_: (TEST_DATA['analysis'], TEST_DATA['meta_data']))
monkeypatch.setattr('pdf_generator.generator.shutil.move', lambda *_: None)
generate_report(firmware_uid='b79ea608e2f0390744642bad472f8d9fd7e4713791857da5d5fcabf70a009e50_29626948')
from json import JSONDecodeError
import pytest
from pdf_generator.pre_processing.rest import DEFAULT_URL, create_request_url, request_firmware_data
class MockResponse:
def __init__(self, response, raises=False):
self._response = response
self._raises = raises
def json(self):
if self._raises:
raise JSONDecodeError('non json response', '', 0)
return self._response
def test_request_firmware_data(monkeypatch):
monkeypatch.setattr('pdf_generator.pre_processing.rest.requests.get', lambda x: MockResponse({'firmware': {'analysis': 'A', 'meta_data': 'B'}}))
assert request_firmware_data(None) == ('A', 'B')
def test_request_firmware_data_no_connection(monkeypatch): # pylint: disable=invalid-name
monkeypatch.setattr('pdf_generator.pre_processing.rest.requests.get', lambda x: MockResponse(None, True))
with pytest.raises(RuntimeError):
request_firmware_data(None)
def test_request_firmware_data_bad_response(monkeypatch): # pylint: disable=invalid-name
monkeypatch.setattr('pdf_generator.pre_processing.rest.requests.get', lambda x: MockResponse({'unknown': 'message'}))
with pytest.raises(RuntimeError):
request_firmware_data(None)
def test_create_request_url():
assert create_request_url('X', None) == '{}/rest/firmware/X?summary=true'.format(DEFAULT_URL)
assert create_request_url('X', 'Y') == 'Y/rest/firmware/X?summary=true'
...@@ -2,7 +2,7 @@ import json ...@@ -2,7 +2,7 @@ import json
from pathlib import Path from pathlib import Path
from pdf_generator.generator import ( from pdf_generator.generator import (
copy_fact_image, create_report_filename, create_templates, execute_latex, generate_analysis_templates copy_fact_image, create_report_filename, create_templates, execute_latex, render_analysis_templates
) )
...@@ -47,7 +47,7 @@ def test_create_report_filename(): ...@@ -47,7 +47,7 @@ def test_create_report_filename():
def test_create_analysis_templates(): def test_create_analysis_templates():
templates = generate_analysis_templates(engine=MockEngine(), analysis={'test': {'result': 'data'}}) templates = render_analysis_templates(engine=MockEngine(), analysis={'test': {'result': 'data'}})
assert len(templates) == 1 assert len(templates) == 1
...@@ -57,7 +57,7 @@ def test_create_analysis_templates(): ...@@ -57,7 +57,7 @@ def test_create_analysis_templates():
def test_create_templates(monkeypatch, tmpdir): def test_create_templates(monkeypatch, tmpdir):
monkeypatch.setattr('pdf_generator.generator.Engine', MockEngine) monkeypatch.setattr('pdf_generator.generator.TemplateEngine', MockEngine)
create_templates(analysis={'test': {'result': 'data'}}, meta_data={}, tmp_dir=str(tmpdir)) create_templates(analysis={'test': {'result': 'data'}}, meta_data={}, tmp_dir=str(tmpdir))
assert Path(str(tmpdir), 'main.tex').exists() assert Path(str(tmpdir), 'main.tex').exists()
......
...@@ -2,7 +2,7 @@ from pathlib import Path ...@@ -2,7 +2,7 @@ from pathlib import Path
import pytest import pytest
from pdf_generator.tex_generation.template_engine import ( from pdf_generator.tex_generation.template_engine import (
Engine, byte_number_filter, convert_base64_to_png_filter, filter_chars_in_list, filter_latex_special_chars, TemplateEngine, byte_number_filter, convert_base64_to_png_filter, filter_chars_in_list, filter_latex_special_chars,
nice_number_filter, nice_unix_time, split_hash, split_output_lines nice_number_filter, nice_unix_time, split_hash, split_output_lines
) )
...@@ -11,7 +11,7 @@ from pdf_generator.tex_generation.template_engine import ( ...@@ -11,7 +11,7 @@ from pdf_generator.tex_generation.template_engine import (
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def stub_engine(tmpdir): def stub_engine(tmpdir):
return Engine(template_folder='test', tmp_dir=tmpdir) return TemplateEngine(template_folder='test', tmp_dir=tmpdir)
def test_byte_number_filter(): def test_byte_number_filter():
......
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