clean-sensor.gmp   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 7

2 Functions

Rating   Name   Duplication   Size   Complexity  
B clean_sensor() 0 49 6
A main() 0 10 1
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2017-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
20
def clean_sensor(gmp):
21
    tasks = gmp.get_tasks(
22
        filter="rows=-1 not status=Running and "
23
        "not status=Requested and not "
24
        "status=&quot;Stop Requested&quot;"
25
    )
26
27
    for tid in tasks.xpath('task/@id'):
28
        print('Removing task %s ... ' % tid)
29
        status_text = gmp.delete_task(tid, ultimate=True).xpath('@status_text')[
30
            0
31
        ]
32
        print(status_text)
33
34
    targets = gmp.get_targets(filter="rows=-1 not _owner=&quot;&quot;")
35
    for tid in targets.xpath('target/@id'):
36
        print('Removing target %s ... ' % tid)
37
        status_text = gmp.delete_target(tid, ultimate=True).xpath(
38
            '@status_text'
39
        )[0]
40
        print(status_text)
41
42
    configs = gmp.get_configs(filter="rows=-1 not _owner=&quot;&quot;")
43
    for cid in configs.xpath('config/@id'):
44
        print('Removing config %s ... ' % cid)
45
        status_text = gmp.delete_config(cid, ultimate=True).xpath(
46
            '@status_text'
47
        )[0]
48
        print(status_text)
49
50
    port_lists = gmp.get_port_lists(filter="rows=-1 not _owner=&quot;&quot;")
51
    for pid in port_lists.xpath('port_list/@id'):
52
        print('Removing port_list %s ... ' % pid)
53
        status_text = gmp.delete_port_list(pid, ultimate=True).xpath(
54
            '@status_text'
55
        )[0]
56
        print(status_text)
57
58
    credentials = gmp.get_credentials(filter="rows=-1 not _owner=&quot;&quot;")
59
    for cid in credentials.xpath('credential/@id'):
60
        print('Removing credential %s ... ' % cid)
61
        status_text = gmp.delete_credential(cid, ultimate=True).xpath(
62
            '@status_text'
63
        )[0]
64
        print(status_text)
65
66
    print('Emptying trash... ')
67
    status_text = gmp.empty_trashcan().xpath('@status_text')[0]
68
    print(status_text)
69
70
71
def main(gmp, args):
72
    # pylint: disable=unused-argument
73
74
    message = """
75
    This script removes all resources from a sensor, except active tasks.
76
77
    """
78
    print(message)
79
80
    clean_sensor(gmp)
81
82
83
if __name__ == '__gmp__':
84
    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...
85