Passed
Push — master ( 1469ae...e090fd )
by Humberto
03:42 queued 10s
created

build.setup.TestCommand.finalize_options()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nop 1
1
"""Setup script.
2
3
Run "python3 setup.py --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 CalledProcessError, 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
26
NAPP_NAME = 'of_stats'
27
NAPP_VERSION = '1.1.0'
28
# Kytos var folder
29
VAR_PATH = BASE_ENV / 'var' / 'lib' / 'kytos'
30
# Path for enabled NApps
31
ENABLED_PATH = VAR_PATH / 'napps'
32
# Path to install NApps
33
INSTALLED_PATH = VAR_PATH / 'napps' / '.installed'
34
CURRENT_DIR = Path('.').resolve()
35
36
# NApps enabled by default
37
# CORE_NAPPS = ['of_core']
38
39
40
class SimpleCommand(Command):
41
    """Make Command implementation simpler."""
42
43
    user_options = []
44
45
    @abstractmethod
46
    def run(self):
47
        """Run when command is invoked.
48
49
        Use *call* instead of *check_call* to ignore failures.
50
        """
51
    def initialize_options(self):
52
        """Set default values for options."""
53
54
    def finalize_options(self):
55
        """Post-process options."""
56
57
58
class Cleaner(SimpleCommand):
59
    """Custom clean command to tidy up the project root."""
60
61
    description = 'clean build, dist, pyc and egg from package and docs'
62
63
    def run(self):
64
        """Clean build, dist, pyc and egg from package and docs."""
65
        call('rm -vrf ./build ./dist ./*.egg-info', shell=True)
66
        call('find . -name __pycache__ -type d | xargs rm -rf', shell=True)
67
        call('make -C docs/ clean', shell=True)
68
69
70
# pylint: disable=attribute-defined-outside-init, abstract-method
71
class TestCommand(Command):
72
    """Test tags decorators."""
73
74
    user_options = [
75
        ('size=', None, 'Specify the size of tests to be executed.'),
76
        ('type=', None, 'Specify the type of tests to be executed.'),
77
    ]
78
79
    sizes = ('small', 'medium', 'large', 'all')
80
    types = ('unit', 'integration', 'e2e')
81
82
    def get_args(self):
83
        """Return args to be used in test command."""
84
        return '--size %s --type %s' % (self.size, self.type)
85
86
    def initialize_options(self):
87
        """Set default size and type args."""
88
        self.size = 'all'
89
        self.type = 'unit'
90
91
    def finalize_options(self):
92
        """Post-process."""
93
        try:
94
            assert self.size in self.sizes, ('ERROR: Invalid size:'
95
                                             f':{self.size}')
96
            assert self.type in self.types, ('ERROR: Invalid type:'
97
                                             f':{self.type}')
98
        except AssertionError as exc:
99
            print(exc)
100
            sys.exit(-1)
101
102
103
class Test(TestCommand):
104
    """Run all tests."""
105
106
    description = 'run tests and display results'
107
108
    def get_args(self):
109
        """Return args to be used in test command."""
110
        markers = self.size
111
        if markers == "small":
112
            markers = 'not medium and not large'
113
        size_args = "" if self.size == "all" else "-m '%s'" % markers
114
        return '--addopts="tests/%s %s"' % (self.type, size_args)
115
116
    def run(self):
117
        """Run tests."""
118
        cmd = 'python setup.py pytest %s' % self.get_args()
119
        try:
120
            check_call(cmd, shell=True)
121
        except CalledProcessError as exc:
122
            print(exc)
123
124
125
class TestCoverage(Test):
126
    """Display test coverage."""
127
128
    description = 'run tests and display code coverage'
129
130
    def run(self):
131
        """Run unittest quietly and display coverage report."""
132
        cmd = 'coverage3 run setup.py pytest %s' % self.get_args()
133
        cmd += ' && coverage3 report'
134
        try:
135
            check_call(cmd, shell=True)
136
        except CalledProcessError as exc:
137
            print(exc)
138
139
140
class Linter(SimpleCommand):
141
    """Code linters."""
142
143
    description = 'lint Python source code'
144
145
    def run(self):
146
        """Run yala."""
147
        print('Yala is running. It may take several seconds...')
148
        try:
149
            check_call('yala *.py', shell=True)
150
        except CalledProcessError:
151
            print('Linter check failed. Fix the error(s) above and try again.')
152
            sys.exit(-1)
153
154
155
class CITest(SimpleCommand):
156
    """Run all CI tests."""
157
158
    description = 'run all CI tests: unit and doc tests, linter'
159
160
    def run(self):
161
        """Run unit tests with coverage, doc tests and linter."""
162
        cmds = ['python3.6 setup.py ' + cmd
163
                for cmd in ('coverage', 'lint')]
164
        cmd = ' && '.join(cmds)
165
        check_call(cmd, shell=True)
166
167
168
# class KytosInstall:
169
#     """Common code for all install types."""
170
#
171
#     @staticmethod
172
#     def enable_core_napps():
173
#         """Enable a NAPP by creating a symlink."""
174
#         (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
175
#         for napp in CORE_NAPPS:
176
#             napp_path = Path('kytos', napp)
177
#             src = ENABLED_PATH / napp_path
178
#             dst = INSTALLED_PATH / napp_path
179
#             src.symlink_to(dst)
180
181
182
class InstallMode(install):
183
    """Create files in var/lib/kytos."""
184
185
    description = 'To install NApps, use kytos-utils. Devs, see "develop".'
186
187
    def run(self):
188
        """Create of_stats as default napps enabled."""
189
        print(self.description)
190
191
192
class DevelopMode(develop):
193
    """Recommended setup for kytos-napps developers.
194
195
    Instead of copying the files to the expected directories, a symlink is
196
    created on the system aiming the current source code.
197
    """
198
199
    description = 'Install NApps in development mode'
200
201
    def run(self):
202
        """Install the package in a developer mode."""
203
        super().run()
204
        if self.uninstall:
205
            shutil.rmtree(str(ENABLED_PATH), ignore_errors=True)
206
        else:
207
            self._create_folder_symlinks()
208
            # self._create_file_symlinks()
209
            # KytosInstall.enable_core_napps()
210
211
    @staticmethod
212
    def _create_folder_symlinks():
213
        """Symlink to all Kytos NApps folders.
214
215
        ./napps/kytos/napp_name will generate a link in
216
        var/lib/kytos/napps/.installed/kytos/napp_name.
217
        """
218
        links = INSTALLED_PATH / 'kytos'
219
        links.mkdir(parents=True, exist_ok=True)
220
        code = CURRENT_DIR
221
        src = links / 'of_stats'
222
        symlink_if_different(src, code)
223
224
        (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
225
        dst = ENABLED_PATH / Path('kytos', 'of_stats')
226
        symlink_if_different(dst, src)
227
228
229
def symlink_if_different(path, target):
230
    """Force symlink creation if it points anywhere else."""
231
    # print(f"symlinking {path} to target: {target}...", end=" ")
232
    if not path.exists():
233
        # print(f"path doesn't exist. linking...")
234
        path.symlink_to(target)
235
    elif not path.samefile(target):
236
        # print(f"path exists, but is different. removing and linking...")
237
        # Exists but points to a different file, so let's replace it
238
        path.unlink()
239
        path.symlink_to(target)
240
241
242
setup(name=NAPP_NAME,
243
      version=NAPP_VERSION,
244
      description='Core NApps developed by Kytos Team',
245
      url='http://github.com/kytos/of_stats',
246
      author='Kytos Team',
247
      author_email='[email protected]',
248
      license='MIT',
249
      install_requires=['setuptools >= 36.0.1'],
250
      setup_requires=['pytest-runner'],
251
      tests_require=['pytest'],
252
      extras_require={
253
          'dev': [
254
              'coverage',
255
              'pip-tools',
256
              'yala',
257
              'tox',
258
          ],
259
      },
260
      cmdclass={
261
          'clean': Cleaner,
262
          'ci': CITest,
263
          'coverage': TestCoverage,
264
          'develop': DevelopMode,
265
          'install': InstallMode,
266
          'lint': Linter,
267
      },
268
      zip_safe=False,
269
      classifiers=[
270
          'License :: OSI Approved :: MIT License',
271
          'Operating System :: POSIX :: Linux',
272
          'Programming Language :: Python :: 3.6',
273
          'Topic :: System :: Networking',
274
      ])
275