Passed
Push — master ( 667973...d237fc )
by Beraldo
01:46
created

build.setup.InstallMode.run()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 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
39
40
class SimpleCommand(Command):
41
    """Make Command implementation simpler."""
42
43
    user_options = []
44
45
    @abstractmethod
46
    def run(self):
47
        """Run when command is invoked.
48
49
        Use *call* instead of *check_call* to ignore failures.
50
        """
51
        pass
52
53
    def initialize_options(self):
54
        """Set default values for options."""
55
        pass
56
57
    def finalize_options(self):
58
        """Post-process options."""
59
        pass
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 v0x??/*.py tests/*.py tests/v0x??/*.py',
94
                   shell=True)
95
96
97
class CITest(SimpleCommand):
98
    """Run all CI tests."""
99
100
    description = 'run all CI tests: unit and doc tests, linter'
101
102
    def run(self):
103
        """Run unit tests with coverage, doc tests and linter."""
104
        cmds = ['python3.6 setup.py ' + cmd
105
                for cmd in ('coverage', 'lint')]
106
        cmd = ' && '.join(cmds)
107
        check_call(cmd, shell=True)
108
109
110
# class KytosInstall:
111
#     """Common code for all install types."""
112
#
113
#     @staticmethod
114
#     def enable_core_napps():
115
#         """Enable a NAPP by creating a symlink."""
116
#         (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
117
#         for napp in CORE_NAPPS:
118
#             napp_path = Path('kytos', napp)
119
#             src = ENABLED_PATH / napp_path
120
#             dst = INSTALLED_PATH / napp_path
121
#             src.symlink_to(dst)
122
123
124
class InstallMode(install):
125
    """Create files in var/lib/kytos."""
126
127
    description = 'To install NApps, use kytos-utils. Devs, see "develop".'
128
129
    def run(self):
130
        """Create of_core as default napps enabled."""
131
        print(self.description)
132
133
134
# class EggInfo(egg_info):
135
#     """Prepare files to be packed."""
136
#
137
#     def run(self):
138
#         """Build css."""
139
#         self._install_deps_wheels()
140
#         super().run()
141
#
142
#     @staticmethod
143
#     def _install_deps_wheels():
144
#         """Python wheels are much faster (no compiling)."""
145
#         print('Installing dependencies...')
146
#         check_call([sys.executable, '-m', 'pip', 'install', '-r',
147
#                     'requirements/run.in'])
148
149
150
class DevelopMode(develop):
151
    """Recommended setup for kytos-napps developers.
152
153
    Instead of copying the files to the expected directories, a symlink is
154
    created on the system aiming the current source code.
155
    """
156
157
    description = 'install NApps in development mode'
158
159
    def run(self):
160
        """Install the package in a developer mode."""
161
        super().run()
162
        if self.uninstall:
163
            shutil.rmtree(str(ENABLED_PATH), ignore_errors=True)
164
        else:
165
            self._create_folder_symlinks()
166
            # self._create_file_symlinks()
167
            # KytosInstall.enable_core_napps()
168
169
    @staticmethod
170
    def _create_folder_symlinks():
171
        """Symlink to all Kytos NApps folders.
172
173
        ./napps/kytos/napp_name will generate a link in
174
        var/lib/kytos/napps/.installed/kytos/napp_name.
175
        """
176
        links = INSTALLED_PATH / 'kytos'
177
        links.mkdir(parents=True, exist_ok=True)
178
        code = CURRENT_DIR
179
        src = links / 'of_core'
180
        symlink_if_different(src, code)
181
182
        (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
183
        dst = ENABLED_PATH / Path('kytos', 'of_core')
184
        symlink_if_different(dst, src)
185
186
    # @staticmethod
187
    # def _create_file_symlinks():
188
    #     """Symlink to required files."""
189
    #     src = ENABLED_PATH / '__init__.py'
190
    #     dst = CURRENT_DIR / 'napps' / '__init__.py'
191
    #     symlink_if_different(src, dst)
192
193
194
def symlink_if_different(path, target):
195
    """Force symlink creation if it points anywhere else."""
196
    # print(f"symlinking {path} to target: {target}...", end=" ")
197
    if not path.exists():
198
        # print(f"path doesn't exist. linking...")
199
        path.symlink_to(target)
200
    elif not path.samefile(target):
201
        # print(f"path exists, but is different. removing and linking...")
202
        # Exists but points to a different file, so let's replace it
203
        path.unlink()
204
        path.symlink_to(target)
205
206
207
def read_version_from_json():
208
    """Read the NApp version from NApp kytos.json file."""
209
    file = Path('kytos.json')
210
    metadata = json.loads(file.read_text())
211
    return metadata['version']
212
213
214
setup(name='kytos_of_core',
215
      version=read_version_from_json(),
216
      description='Core NApps developed by Kytos Team',
217
      url='http://github.com/kytos/of_core',
218
      author='Kytos Team',
219
      author_email='[email protected]',
220
      license='MIT',
221
      install_requires=['setuptools >= 36.0.1'],
222
      extras_require={
223
          'dev': [
224
              'coverage',
225
              'pip-tools',
226
              'yala',
227
              'tox',
228
          ],
229
      },
230
      cmdclass={
231
          'clean': Cleaner,
232
          'ci': CITest,
233
          'coverage': TestCoverage,
234
235
          'develop': DevelopMode,
236
          'install': InstallMode,
237
          'lint': Linter,
238
          # 'egg_info': EggInfo,
239
      },
240
      zip_safe=False,
241
      classifiers=[
242
          'License :: OSI Approved :: MIT License',
243
          'Operating System :: POSIX :: Linux',
244
          'Programming Language :: Python :: 3.6',
245
          'Topic :: System :: Networking',
246
      ])
247