Completed
Push — master ( b1e450...5ca4d8 )
by
unknown
17s queued 10s
created

gvmtools.script.main()   B

Complexity

Conditions 5

Size

Total Lines 60
Code Lines 41

Duplication

Lines 13
Ratio 21.67 %

Importance

Changes 0
Metric Value
eloc 41
dl 13
loc 60
rs 8.4293
c 0
b 0
f 0
cc 5
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2018 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 os
20
import sys
21
22
from argparse import Namespace
23
24
from gvm import get_version as get_gvm_version
25
from gvm.protocols.latest import Gmp, Osp
26
from gvm.transforms import EtreeCheckCommandTransform
27
28
from gvmtools import get_version
29
from gvmtools.helper import authenticate, run_script
30
from gvmtools.parser import create_parser, create_connection, PROTOCOL_OSP
31
32
HELP_TEXT = """
33
    Command line tool to execute custom GMP (Greenbone Management
34
    Protocol) and OSP (Open Scanner Protocol) scripts.
35
36
    The protocol specifications for GMP and OSP are available at:
37
      https://docs.greenbone.net/index.html#api_documentation
38
"""
39
40
__version__ = get_version()
41
__api_version__ = get_gvm_version()
42
43
def main():
44
    parser = create_parser(
45
        description=HELP_TEXT, logfilename='gvm-script.log')
46
47
    parser.add_protocol_argument()
48
49
    parser.add_argument(
50
        'scriptname', metavar="SCRIPT",
51
        help='Path to script to be executed (example: myscript.gmp)')
52
    parser.add_argument(
53
        'scriptargs', nargs='*', metavar="ARG",
54
        help='Arguments for the script')
55
56
    args = parser.parse_args()
57
58
    print(args)
59
60
    if 'socket' in args.connection_type and args.sockpath:
61
        print('The --sockpath parameter has been deprecated. Please use '
62
              '--socketpath instead', file=sys.stderr)
63
64
    connection = create_connection(**vars(args))
65
66
    transform = EtreeCheckCommandTransform()
67
68
    global_vars = {
69
        '__version__': __version__,
70
        '__api_version__': __api_version__,
71
    }
72
73
    username = None
74
    password = None
75
76 View Code Duplication
    if args.protocol == PROTOCOL_OSP:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
77
        protocol = Osp(connection, transform=transform)
78
        global_vars['osp'] = protocol
79
        global_vars['__name__'] = '__osp__'
80
    else:
81
        protocol = Gmp(connection, transform=transform)
82
        global_vars['gmp'] = protocol
83
        global_vars['__name__'] = '__gmp__'
84
85
        if args.gmp_username:
86
            (username, password) = authenticate(
87
                protocol, username=args.gmp_username,
88
                password=args.gmp_password)
89
90
    argv = [os.path.abspath(args.scriptname), *args.scriptargs]
91
92
    shell_args = Namespace(
93
        username=username,
94
        password=password,
95
        argv=argv,
96
        # for backwards compatibility we add script here
97
        script=argv,
98
    )
99
100
    global_vars['args'] = shell_args
101
102
    run_script(args.scriptname, global_vars)
103
104
105
106
if __name__ == '__main__':
107
    main()
108