Passed
Push — master ( 52cd8b...61efad )
by Humberto
03:03 queued 01:05
created

build.setup.DevelopMode._create_file_symlinks()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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