Issues (69)

scripts/pdf-report.gmp.py (2 issues)

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2019-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
import sys
20
21
from base64 import b64decode
22
from pathlib import Path
23
24
25
def check_args(args):
26
    len_args = len(args.script) - 1
27
    if len_args < 1:
28
        message = """
29
        This script requests the given report and saves it as a pdf file locally.
30
        It needs one parameters after the script name.
31
32
        1. <report_id>     -- ID of the report
33
        
34
        Optional a file name to save the pdf in.
35
36
        Example:
37
            $ gvm-script --gmp-username name --gmp-password pass \
38
ssh --hostname <gsm> scripts/pdf-report.gmp.py <report_id> <pdf_file>
39
        """
40
        print(message)
41
        sys.exit()
42
43
44
def main(gmp, args):
45
    # check if report id and PDF filename are provided to the script
46
    # argv[0] contains the script name
47
    check_args(args)
48
49
    report_id = args.argv[1]
50
    if len(args.argv) == 3:
51
        pdf_filename = args.argv[2]
52
    else:
53
        pdf_filename = args.argv[1] + ".pdf"
54
55
    pdf_report_format_id = "c402cc3e-b531-11e1-9163-406186ea4fc5"
56
57
    response = gmp.get_report(
58
        report_id=report_id, report_format_id=pdf_report_format_id
59
    )
60
61
    report_element = response.find("report")
62
    # get the full content of the report element
63
    content = report_element.find("report_format").tail
64
65
    if not content:
66
        print(
67
            'Requested report is empty. Either the report does not contain any '
68
            ' results or the necessary tools for creating the report are '
69
            'not installed.',
70
            file=sys.stderr,
71
        )
72
        sys.exit(1)
73
74
    # convert content to 8-bit ASCII bytes
75
    binary_base64_encoded_pdf = content.encode('ascii')
76
77
    # decode base64
78
    binary_pdf = b64decode(binary_base64_encoded_pdf)
79
80
    # write to file and support ~ in filename path
81
    pdf_path = Path(pdf_filename).expanduser()
82
83
    pdf_path.write_bytes(binary_pdf)
84
85
    print('Done. PDF created: ' + str(pdf_path))
86
87
88
if __name__ == '__gmp__':
89
    main(gmp, args)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable args does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
90