Passed
Push — master ( 842882...dc4417 )
by Humberto
01:37 queued 11s
created

BugReportAPI._parse_github_install()   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 2
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
"""Translate cli commands to non-cli code."""
2
import re
3
import subprocess
4
5
6
class BugReportAPI:
7
    """Retrieve Kytos environment information.
8
9
    Collect system information, python environment, python packages and
10
    installed NApps to print this report to the user, aiming to improve
11
    bug reports.
12
    """
13
14
    @classmethod
15
    def bug_report(cls, _):
16
        """Run all reports."""
17
        cls.system_report()
18
        cls.python_environment()
19
        cls.python_packages_report()
20
        cls.kytos_environment_report()
21
22
    @classmethod
23
    def system_report(cls):
24
        """Display system information.
25
26
        Print distribution release system information.
27
        """
28
        print('# Platform')
29
        lsb_release = cls._execute('lsb_release -a 2> /dev/null')
30
        uname = cls._execute('uname -a')
31
        print('## Release information')
32
        print(lsb_release)
33
        print('## System Information')
34
        print(uname)
35
36
    @classmethod
37
    def python_environment(cls):
38
        """Display python environment report.
39
40
        This method shows the path and version of pip and python.
41
        """
42
        cls._print_path_and_version('python')
43
        cls._print_path_and_version('pip')
44
45
    @classmethod
46
    def _print_path_and_version(cls, package):
47
        """Display a package path and version."""
48
        print('## '+package.title())
49
        path = cls._execute(f'which {package}')
50
        version = cls._execute(f'{package} --version')
51
        print(f'path={path}')
52
        print(f'version={version}')
53
54
    @classmethod
55
    def python_packages_report(cls):
56
        """Display all installed packages using pip freeze.
57
58
        This method will modify the output and print:
59
60
        pypi packages : 'Package Name | Version'
61
        git repository package: 'Package Name | Repository | Version'
62
        """
63
        lines = cls._execute('pip  freeze').split('\n')
64
        print('# Python Packages')
65
        for line in lines:
66
            if 'kytos' in line or 'python-openflow' in line:
67
                if '==' in line:
68
                    name, version = line.split('==')
69
                    print(f'{name:<30} | {version:<30}')
70
                if line.startswith('-e '):
71
                    url, name = line.split('#egg=')
72
                    url = url.replace('-e ', '')
73
                    repository, hash_number = cls._parse_github_install(url)
74
                    print(f'{name:<30} | {repository:<30} | {hash_number:<30}')
75
76
    @classmethod
77
    def _parse_github_install(cls, url):
78
        """Parse the repository url and get the github path and commit hash."""
79
        pattern = "(github.com[:/].*).git@(.{8})"
80
        result = re.search(pattern, url)
81
        if result:
82
            return result.groups()
83
        return (url, '')
84
85
    @classmethod
86
    def kytos_environment_report(cls):
87
        """Display the kytos environment.
88
89
        This method shows the path and version of kytos and kytosd.
90
        After that shows all installed napps.
91
        """
92
        print('# Kytos environment')
93
        cls._print_path_and_version('kytosd')
94
        cls._print_path_and_version('kytos')
95
        print('## Installed napps')
96
        napps = cls._execute('kytos napps list')
97
        print(napps)
98
99
    @classmethod
100
    def _execute(cls, command):
101
        """Call a subprocess and return the result."""
102
        return subprocess.check_output(command, shell=True).decode().strip()
103