Test Failed
Push — master ( 0d7af6...292dca )
by Beraldo
04:07 queued 01:46
created

build.setup   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 127
dl 0
loc 223
ccs 0
cts 92
cp 0
rs 10
c 0
b 0
f 0
wmc 16

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