Completed
Pull Request — master (#67)
by
unknown
02:37
created

build.setup.SimpleCommand.initialize_options()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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