build.setup.Linter.run()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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.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
BASE_ENV = Path(os.environ.get('VIRTUAL_ENV', '/'))
23
24
NAPP_NAME = 'of_lldp'
25
NAPP_VERSION = '1.1.1'
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
51
    def initialize_options(self):
52
        """Set default values for options."""
53
54
    def finalize_options(self):
55
        """Post-process options."""
56
57
58
# pylint: disable=attribute-defined-outside-init, abstract-method
59
class TestCommand(Command):
60
    """Test tags decorators."""
61
62
    user_options = [
63
        ('size=', None, 'Specify the size of tests to be executed.'),
64
        ('type=', None, 'Specify the type of tests to be executed.'),
65
    ]
66
67
    sizes = ('small', 'medium', 'large', 'all')
68
    types = ('unit', 'integration', 'e2e')
69
70
    def get_args(self):
71
        """Return args to be used in test command."""
72
        return '--size %s --type %s' % (self.size, self.type)
73
74
    def initialize_options(self):
75
        """Set default size and type args."""
76
        self.size = 'all'
77
        self.type = 'unit'
78
79
    def finalize_options(self):
80
        """Post-process."""
81
        try:
82
            assert self.size in self.sizes, ('ERROR: Invalid size:'
83
                                             f':{self.size}')
84
            assert self.type in self.types, ('ERROR: Invalid type:'
85
                                             f':{self.type}')
86
        except AssertionError as exc:
87
            print(exc)
88
            sys.exit(-1)
89
90
91
class Cleaner(SimpleCommand):
92
    """Custom clean command to tidy up the project root."""
93
94
    description = 'clean build, dist, pyc and egg from package and docs'
95
96
    def run(self):
97
        """Clean build, dist, pyc and egg from package and docs."""
98
        call('rm -vrf ./build ./dist ./*.egg-info', shell=True)
99
        call('find . -name __pycache__ -type d | xargs rm -rf', shell=True)
100
        call('make -C docs/ clean', shell=True)
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
            print('Unit tests failed. Fix the errors above and try again.')
124
            sys.exit(-1)
125
126
127
class TestCoverage(Test):
128
    """Display test coverage."""
129
130
    description = 'run tests and display code coverage'
131
132
    def run(self):
133
        """Run tests quietly and display coverage report."""
134
        cmd = 'coverage3 run setup.py pytest %s' % self.get_args()
135
        cmd += '&& coverage3 report'
136
        try:
137
            check_call(cmd, shell=True)
138
        except CalledProcessError as exc:
139
            print(exc)
140
            print('Coverage tests failed. Fix the errors above and try again.')
141
            sys.exit(-1)
142
143
144
class Linter(SimpleCommand):
145
    """Code linters."""
146
147
    description = 'lint Python source code'
148
149
    def run(self):
150
        """Run yala."""
151
        print('Yala is running. It may take several seconds...')
152
        check_call('yala *.py tests', shell=True)
153
154
155
class CITest(TestCommand):
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
        coverage_cmd = 'python3.6 setup.py coverage %s' % self.get_args()
163
        lint_cmd = 'python3.6 setup.py lint'
164
        cmd = '%s && %s' % (coverage_cmd, lint_cmd)
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
            symlink_if_different(src, 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
        """Direct users to use kytos-utils to install NApps."""
189
        print(self.description)
190
191
192
class EggInfo(egg_info):
193
    """Prepare files to be packed."""
194
195
    def run(self):
196
        """Build css."""
197
        self._install_deps_wheels()
198
        super().run()
199
200
    @staticmethod
201
    def _install_deps_wheels():
202
        """Python wheels are much faster (no compiling)."""
203
        print('Installing dependencies...')
204
        check_call([sys.executable, '-m', 'pip', 'install', '-r',
205
                    'requirements/run.in'])
206
207
208
class DevelopMode(develop):
209
    """Recommended setup for kytos-napps developers.
210
211
    Instead of copying the files to the expected directories, a symlink is
212
    created on the system aiming the current source code.
213
    """
214
215
    description = 'Install NApps in development mode'
216
217
    def run(self):
218
        """Install the package in a developer mode."""
219
        super().run()
220
        if self.uninstall:
221
            shutil.rmtree(str(ENABLED_PATH), ignore_errors=True)
222
        else:
223
            self._create_folder_symlinks()
224
            # self._create_file_symlinks()
225
            KytosInstall.enable_core_napps()
226
227
    @staticmethod
228
    def _create_folder_symlinks():
229
        """Symlink to all Kytos NApps folders.
230
231
        ./napps/kytos/napp_name will generate a link in
232
        var/lib/kytos/napps/.installed/kytos/napp_name.
233
        """
234
        links = INSTALLED_PATH / 'kytos'
235
        links.mkdir(parents=True, exist_ok=True)
236
        code = CURRENT_DIR
237
        src = links / NAPP_NAME
238
        symlink_if_different(src, code)
239
240
        (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
241
        dst = ENABLED_PATH / Path('kytos', NAPP_NAME)
242
        symlink_if_different(dst, src)
243
244
    @staticmethod
245
    def _create_file_symlinks():
246
        """Symlink to required files."""
247
        src = ENABLED_PATH / '__init__.py'
248
        dst = CURRENT_DIR / 'napps' / '__init__.py'
249
        symlink_if_different(src, dst)
250
251
252
def symlink_if_different(path, target):
253
    """Force symlink creation if it points anywhere else."""
254
    # print(f"symlinking {path} to target: {target}...", end=" ")
255
    if not path.exists():
256
        # print(f"path doesn't exist. linking...")
257
        path.symlink_to(target)
258
    elif not path.samefile(target):
259
        # print(f"path exists, but is different. removing and linking...")
260
        # Exists but points to a different file, so let's replace it
261
        path.unlink()
262
        path.symlink_to(target)
263
264
265
setup(name=f'kytos_{NAPP_NAME}',
266
      version=NAPP_VERSION,
267
      description='Core NApps developed by the Kytos Team',
268
      url=f'http://github.com/kytos/{NAPP_NAME}',
269
      author='Kytos Team',
270
      author_email='[email protected]',
271
      license='MIT',
272
      install_requires=['setuptools >= 36.0.1'],
273
      setup_requires=['pytest-runner'],
274
      tests_require=['pytest'],
275
      extras_require={
276
          'dev': [
277
              'coverage',
278
              'pip-tools',
279
              'yala',
280
              'tox',
281
          ],
282
      },
283
      cmdclass={
284
          'clean': Cleaner,
285
          'ci': CITest,
286
          'coverage': TestCoverage,
287
          'develop': DevelopMode,
288
          'install': InstallMode,
289
          'lint': Linter,
290
          'egg_info': EggInfo,
291
          'test': Test,
292
      },
293
      zip_safe=False,
294
      classifiers=[
295
          'License :: OSI Approved :: MIT License',
296
          'Operating System :: POSIX :: Linux',
297
          'Programming Language :: Python :: 3.6',
298
          'Topic :: System :: Networking',
299
      ])
300