Test Failed
Pull Request — master (#33)
by
unknown
02:21
created

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