Passed
Push — master ( c5e88f...55f662 )
by Humberto
01:18 queued 11s
created

build.setup.EggInfo.run()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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