Passed
Push — master ( b856bf...b77076 )
by Vinicius
01:43 queued 12s
created

build.setup.read_requirements()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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