Passed
Pull Request — master (#111)
by
unknown
01:50
created

ospd.main.main()   B

Complexity

Conditions 5

Size

Total Lines 61
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 39
nop 2
dl 0
loc 61
rs 8.4773
c 0
b 0
f 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
# Copyright (C) 2019 Greenbone Networks GmbH
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
#
3
# SPDX-License-Identifier: GPL-2.0-or-later
4
#
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
import logging
20
21
from logging.handlers import SysLogHandler, WatchedFileHandler
22
23
import os
24
import sys
25
26
27
from ospd.misc import go_to_background
28
from ospd.parser import create_args_parser, get_common_args
29
30
31
def print_version(wrapper, file=sys.stdout):
32
    """ Prints the server version and license information."""
33
34
    scanner_name = wrapper.get_scanner_name()
35
    server_version = wrapper.get_server_version()
36
    protocol_version = wrapper.get_protocol_version()
37
    daemon_name = wrapper.get_daemon_name()
38
    daemon_version = wrapper.get_daemon_version()
39
40
    file.write(
41
        "OSP Server for {0} version {1}".format(scanner_name, server_version)
42
    )
43
    file.write("OSP Version: {0}".format(protocol_version))
44
    file.write("Using: {0} {1}".format(daemon_name, daemon_version))
45
    file.write(
46
        "Copyright (C) 2014, 2015 Greenbone Networks GmbH\n"
47
        "License GPLv2+: GNU GPL version 2 or later\n"
48
        "This is free software: you are free to change"
49
        " and redistribute it.\n"
50
        "There is NO WARRANTY, to the extent permitted by law."
51
    )
52
53
54
def main(name, klass):
55
    """ OSPD Main function. """
56
57
    # Common args parser.
58
    parser = create_args_parser(name)
59
60
    # Common args
61
    cargs = get_common_args(parser)
62
63
    logging.getLogger().setLevel(cargs['log_level'])
64
65
    wrapper = klass(
66
        certfile=cargs['certfile'],
67
        keyfile=cargs['keyfile'],
68
        cafile=cargs['cafile'],
69
        niceness=cargs['niceness'],
70
    )
71
72
    if cargs['version']:
73
        print_version(wrapper)
74
        sys.exit()
75
76
    if cargs['foreground']:
77
        console = logging.StreamHandler()
78
        console.setFormatter(
79
            logging.Formatter(
80
                '%(asctime)s {}: %(levelname)s: (%(name)s) %(message)s'.format(
81
                    name
82
                )
83
            )
84
        )
85
        logging.getLogger().addHandler(console)
86
    elif cargs['log_file']:
87
        logfile = WatchedFileHandler(cargs['log_file'])
88
        logfile.setFormatter(
89
            logging.Formatter(
90
                '%(asctime)s {}: %(levelname)s: (%(name)s) %(message)s'.format(
91
                    name
92
                )
93
            )
94
        )
95
        logging.getLogger().addHandler(logfile)
96
        go_to_background()
97
    else:
98
        syslog = SysLogHandler('/dev/log')
99
        syslog.setFormatter(
100
            logging.Formatter(
101
                '{}: %(levelname)s: (%(name)s) %(message)s'.format(name)
102
            )
103
        )
104
        logging.getLogger().addHandler(syslog)
105
        # Duplicate syslog's file descriptor to stout/stderr.
106
        syslog_fd = syslog.socket.fileno()
107
        os.dup2(syslog_fd, 1)
108
        os.dup2(syslog_fd, 2)
109
        go_to_background()
110
111
    if not wrapper.check():
112
        return 1
113
114
    return wrapper.run(cargs['address'], cargs['port'], cargs['unix_socket'])
115