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