Test Failed
Pull Request — master (#36)
by Jose
02:22
created

build.setup   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 112
dl 0
loc 197
rs 10
c 0
b 0
f 0
wmc 17

12 Methods

Rating   Name   Duplication   Size   Complexity  
A InstallMode.run() 0 3 1
A CITest.run() 0 6 1
A KytosInstall.enable_core_napps() 0 9 2
A Cleaner.run() 0 5 1
A Linter.run() 0 4 1
A DevelopMode._create_folder_symlinks() 0 15 4
A DevelopMode._create_file_symlinks() 0 6 1
A SimpleCommand.initialize_options() 0 3 1
A SimpleCommand.run() 0 7 1
A DevelopMode.run() 0 9 2
A TestCoverage.run() 0 5 1
A SimpleCommand.finalize_options() 0 3 1
1
"""Setup script.
2
3
Run "python3 setup --help-commands" to list all available commands and their
4
descriptions.
5
"""
6
import os
7
import shutil
8
import sys
9
from abc import abstractmethod
10
from pathlib import Path
11
from subprocess import call, check_call
12
13
from setuptools import Command, setup
14
from setuptools.command.develop import develop
15
from setuptools.command.install import install
16
17
if 'bdist_wheel' in sys.argv:
18
    raise RuntimeError("This setup.py does not support wheels")
19
20
# Paths setup with virtualenv detection
21
if 'VIRTUAL_ENV' in os.environ:
22
    BASE_ENV = Path(os.environ['VIRTUAL_ENV'])
23
else:
24
    BASE_ENV = Path('/')
25
# Kytos var folder
26
VAR_PATH = BASE_ENV / 'var' / 'lib' / 'kytos'
27
# Path for enabled NApps
28
ENABL_PATH = VAR_PATH / 'napps'
29
# Path to install NApps
30
INSTL_PATH = VAR_PATH / 'napps' / '.installed'
31
CURR_DIR = Path('.').resolve()
32
33
# NApps enabled by default
34
CORE_NAPPS = ['of_core']
35
36
37
class SimpleCommand(Command):
38
    """Make Command implementation simpler."""
39
40
    user_options = []
41
42
    @abstractmethod
43
    def run(self):
44
        """Run when command is invoked.
45
46
        Use *call* instead of *check_call* to ignore failures.
47
        """
48
        pass
49
50
    def initialize_options(self):
51
        """Set default values for options."""
52
        pass
53
54
    def finalize_options(self):
55
        """Post-process options."""
56
        pass
57
58
59
class Cleaner(SimpleCommand):
60
    """Custom clean command to tidy up the project root."""
61
62
    description = 'clean build, dist, pyc and egg from package and docs'
63
64
    def run(self):
65
        """Clean build, dist, pyc and egg from package and docs."""
66
        call('rm -vrf ./build ./dist ./*.egg-info', shell=True)
67
        call('find . -name __pycache__ -type d | xargs rm -rf', shell=True)
68
        call('make -C docs/ clean', shell=True)
69
70
71
class TestCoverage(SimpleCommand):
72
    """Display test coverage."""
73
74
    description = 'run unit tests and display code coverage'
75
76
    def run(self):
77
        """Run unittest quietly and display coverage report."""
78
        cmd = 'coverage3 run -m unittest discover -qs napps/kytos' \
79
              ' && coverage3 report'
80
        call(cmd, shell=True)
81
82
83
class Linter(SimpleCommand):
84
    """Code linters."""
85
86
    description = 'lint Python source code'
87
88
    def run(self):
89
        """Run pylama."""
90
        print('Pylama is running. It may take several seconds...')
91
        check_call('pylama setup.py tests kytos', shell=True)
92
93
94
class CITest(SimpleCommand):
95
    """Run all CI tests."""
96
97
    description = 'run all CI tests: unit and doc tests, linter'
98
99
    def run(self):
100
        """Run unit tests with coverage, doc tests and linter."""
101
        cmds = ['python setup.py ' + cmd
102
                for cmd in ('coverage', 'lint')]
103
        cmd = ' && '.join(cmds)
104
        check_call(cmd, shell=True)
105
106
107
class KytosInstall:
108
    """Common code for all install types."""
109
110
    @staticmethod
111
    def enable_core_napps():
112
        """Enable a NAPP by creating a symlink."""
113
        (ENABL_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
114
        for napp in CORE_NAPPS:
115
            napp_path = Path('kytos', napp)
116
            src = ENABL_PATH / napp_path
117
            dst = INSTL_PATH / napp_path
118
            src.symlink_to(dst)
119
120
121
class InstallMode(install):
122
    """Create files in var/lib/kytos."""
123
124
    description = 'To install NApps, use kytos-utils. Devs, see "develop".'
125
126
    def run(self):
127
        """Create of_core as default napps enabled."""
128
        print(self.description)
129
130
131
class DevelopMode(develop):
132
    """Recommended setup for kytos-napps developers.
133
134
    Instead of copying the files to the expected directories, a symlink is
135
    created on the system aiming the current source code.
136
    """
137
138
    description = 'install NApps in development mode'
139
140
    def run(self):
141
        """Install the package in a developer mode."""
142
        super().run()
143
        if self.uninstall:
144
            shutil.rmtree(str(ENABL_PATH), ignore_errors=True)
145
        else:
146
            self._create_folder_symlinks()
147
            self._create_file_symlinks()
148
            KytosInstall.enable_core_napps()
149
150
    @staticmethod
151
    def _create_folder_symlinks():
152
        """Symlink to all Kytos NApps folders.
153
154
        ./napps/kytos/napp_name will generate a link in
155
        var/lib/kytos/napps/.installed/kytos/napp_name.
156
        """
157
        links = INSTL_PATH / 'kytos'
158
        links.mkdir(parents=True, exist_ok=True)
159
        code = CURR_DIR / 'napps' / 'kytos'
160
        for path in code.iterdir():
161
            last_folder = path.parts[-1]
162
            if path.is_dir() and last_folder != '__pycache__':
163
                src = links / last_folder
164
                src.symlink_to(path)
165
166
    @staticmethod
167
    def _create_file_symlinks():
168
        """Symlink to required files."""
169
        src = ENABL_PATH / '__init__.py'
170
        dst = CURR_DIR / 'napps' / '__init__.py'
171
        src.symlink_to(dst)
172
173
setup(name='kytos-napps',
174
      version='2017.1b3',
175
      description='Core Napps developed by Kytos Team',
176
      url='http://github.com/kytos/kytos-napps',
177
      author='Kytos Team',
178
      author_email='[email protected]',
179
      license='MIT',
180
      install_requires=[line.strip()
181
                        for line in open("requirements/run.txt").readlines()
182
                        if not line.startswith('#')],
183
      cmdclass={
184
          'clean': Cleaner,
185
          'ci': CITest,
186
          'coverage': TestCoverage,
187
          'develop': DevelopMode,
188
          'install': InstallMode,
189
          'lint': Linter,
190
      },
191
      zip_safe=False,
192
      classifiers=[
193
          'License :: OSI Approved :: MIT License',
194
          'Operating System :: POSIX :: Linux',
195
          'Programming Language :: Python :: 3.6',
196
          'Topic :: System :: Networking',
197
      ])
198