Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
fact_pdf_report
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
fact_pdf_report
Commits
653701f3
Commit
653701f3
authored
Jul 17, 2019
by
dorp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
removed unused pre processing module
parent
1a3b38b5
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
15 additions
and
96 deletions
+15
-96
generator.py
pdf_generator/generator.py
+5
-22
__init__.py
pdf_generator/pre_processing/__init__.py
+0
-0
rest.py
pdf_generator/pre_processing/rest.py
+0
-20
template_engine.py
pdf_generator/tex_generation/template_engine.py
+1
-1
test_generator.py
test/integration/test_generator.py
+4
-11
__init__.py
test/unit/pre_processing/__init__.py
+0
-0
test_rest.py
test/unit/pre_processing/test_rest.py
+0
-37
test_generator.py
test/unit/test_generator.py
+3
-3
test_template_engine.py
test/unit/tex_generation/test_template_engine.py
+2
-2
No files found.
pdf_generator/generator.py
View file @
653701f3
import
logging
import
os
import
shutil
from
pathlib
import
Path
from
tempfile
import
TemporaryDirectory
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
Engine
from
pdf_generator.tex_generation.template_engine
import
TemplateEngine
def
execute_latex
(
tmp_dir
):
...
...
@@ -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'
))
def
generate
_analysis_templates
(
engine
,
analysis
):
def
render
_analysis_templates
(
engine
,
analysis
):
return
[
(
'{}.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):
def
create_templates
(
analysis
,
meta_data
,
tmp_dir
):
engine
=
Engine
(
tmp_dir
=
tmp_dir
)
engine
=
Template
Engine
(
tmp_dir
=
tmp_dir
)
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
))
for
filename
,
result_code
in
generate_analysis_templates
(
engine
=
engine
,
analysis
=
analysis
):
Path
(
tmp_dir
,
filename
)
.
write_text
(
result_code
)
def
generate_report
(
firmware_uid
,
server_url
=
None
):
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
for
filename
,
rendered_template
in
render_analysis_templates
(
engine
=
engine
,
analysis
=
analysis
):
Path
(
tmp_dir
,
filename
)
.
write_text
(
rendered_template
)
pdf_generator/pre_processing/__init__.py
deleted
100644 → 0
View file @
1a3b38b5
pdf_generator/pre_processing/rest.py
deleted
100644 → 0
View file @
1a3b38b5
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
pdf_generator/tex_generation/template_engine.py
View file @
653701f3
...
...
@@ -144,7 +144,7 @@ def _add_filters_to_jinja(environment):
environment
.
filters
[
'contains'
]
=
item_contains_string
class
Engine
:
class
Template
Engine
:
def
__init__
(
self
,
template_folder
=
None
,
tmp_dir
=
None
):
self
.
_environment
=
create_jinja_environment
(
template_folder
if
template_folder
else
'default'
)
self
.
_tmp_dir
=
tmp_dir
...
...
test/integration/test_generator.py
View file @
653701f3
import
pytest
from
pdf_generator.generator
import
generate_report
from
pdf_generator.tex_generation.template_engine
import
Engine
from
pdf_generator.tex_generation.template_engine
import
TemplateEngine
from
test.data.test_dict
import
TEST_DICT
# pylint: disable=redefined-outer-name
...
...
@@ -13,23 +12,17 @@ TEST_DATA = {
@pytest.fixture
(
scope
=
'function'
)
def
stub_engine
():
return
Engine
()
return
Template
Engine
()
def
test_latex_code_generation
(
stub_engine
:
Engine
):
def
test_latex_code_generation
(
stub_engine
:
Template
Engine
):
result
=
stub_engine
.
render_meta_template
(
TEST_DICT
)
assert
result
def
test_render_template
(
tmpdir
):
engine
=
Engine
(
template_folder
=
'test'
,
tmp_dir
=
tmpdir
)
engine
=
Template
Engine
(
template_folder
=
'test'
,
tmp_dir
=
tmpdir
)
test_data
=
{
'meta_data'
:
'123'
,
'analysis'
:
'456'
}
output
=
engine
.
render_analysis_template
(
plugin
=
'render_test'
,
analysis
=
test_data
)
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'
)
test/unit/pre_processing/__init__.py
deleted
100644 → 0
View file @
1a3b38b5
test/unit/pre_processing/test_rest.py
deleted
100644 → 0
View file @
1a3b38b5
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'
test/unit/test_generator.py
View file @
653701f3
...
...
@@ -2,7 +2,7 @@ import json
from
pathlib
import
Path
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():
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
...
...
@@ -57,7 +57,7 @@ def test_create_analysis_templates():
def
test_create_templates
(
monkeypatch
,
tmpdir
):
monkeypatch
.
setattr
(
'pdf_generator.generator.Engine'
,
MockEngine
)
monkeypatch
.
setattr
(
'pdf_generator.generator.
Template
Engine'
,
MockEngine
)
create_templates
(
analysis
=
{
'test'
:
{
'result'
:
'data'
}},
meta_data
=
{},
tmp_dir
=
str
(
tmpdir
))
assert
Path
(
str
(
tmpdir
),
'main.tex'
)
.
exists
()
...
...
test/unit/tex_generation/test_template_engine.py
View file @
653701f3
...
...
@@ -2,7 +2,7 @@ from pathlib import Path
import
pytest
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
,
Template
Engine
,
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
)
...
...
@@ -11,7 +11,7 @@ from pdf_generator.tex_generation.template_engine import (
@pytest.fixture
(
scope
=
'function'
)
def
stub_engine
(
tmpdir
):
return
Engine
(
template_folder
=
'test'
,
tmp_dir
=
tmpdir
)
return
Template
Engine
(
template_folder
=
'test'
,
tmp_dir
=
tmpdir
)
def
test_byte_number_filter
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment