build.setup.DevelopMode._create_folder_symlinks()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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