Passed
Pull Request — master (#344)
by Jaspar
01:14
created

send-schedules.gmp.error_and_exit()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018-2019 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 lxml import etree as e
22
23
from gvmtools.helper import create_xml_tree, error_and_exit
24
25
26
def check_args(args):
27
    len_args = len(args.script) - 1
28
    if len_args != 1:
29
        message = """
30
        This script pulls schedule data from an xml document and feeds it to \
31
    a desired GSM
32
        One parameter after the script name is required.
33
34
        1. <xml_doc>  -- .xml file containing schedules
35
36
        Example:
37
            $ gvm-script --gmp-username name --gmp-password pass \
38
    ssh --hostname <gsm> scripts/send-schedules.gmp.py example_file.xml
39
40
        """
41
        print(message)
42
        quit()
43
    if int(gmp.get_protocol_version()[0]) < 8:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable gmp does not seem to be defined.
Loading history...
44
        print("This script requires GMP version 8")
45
        quit()
46
47
48
def parse_send_xml_tree(gmp, xml_tree):
49
    for schedule in xml_tree.xpath('schedule'):
50
        name = schedule.find('name').text
51
52
        comment = schedule.find('comment').text
53
        if comment is None:
54
            comment = ''
55
56
        ical = schedule.find('icalendar').text
57
58
        timezone = schedule.find('timezone').text
59
60
        gmp.create_schedule(
61
            name=name, comment=comment, timezone=timezone, icalendar=ical
62
        )
63
64
65
def main(gmp, args):
66
    # pylint: disable=undefined-variable
67
68
    check_args(args)
69
70
    xml_doc = args.script[1]
71
72
    print('\nSending schedules...')
73
74
    xml_tree = create_xml_tree(xml_doc)
75
    parse_send_xml_tree(gmp, xml_tree)
76
77
    print('\n  Schedule(s) created!\n')
78
79
80
if __name__ == '__gmp__':
81
    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...
82