Passed
Pull Request — master (#69)
by Gleyberson
02:31
created

build.setup.TestCommand.initialize_options()   A

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