build.setup   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 150
dl 0
loc 282
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A SimpleCommand.initialize_options() 0 2 1
A SimpleCommand.run() 0 3 1
A SimpleCommand.finalize_options() 0 2 1
A Cleaner.run() 0 5 1
A TestCommand.finalize_options() 0 3 1
A TestCommand.get_args() 0 5 2
A Test.run() 0 9 2
A TestCommand.initialize_options() 0 3 1
A InstallMode.run() 0 3 1
A KytosInstall.enable_core_napps() 0 9 2
A Linter.run() 0 10 2
A DevelopMode._create_folder_symlinks() 0 16 1
A EggInfo._install_deps_wheels() 0 6 1
A DevelopMode.run() 0 9 2
A EggInfo.run() 0 4 1
A TestCoverage.run() 0 10 2

2 Functions

Rating   Name   Duplication   Size   Complexity  
A symlink_if_different() 0 11 3
A read_requirements() 0 7 2
1
"""Setup script.
2
3
Run "python3 setup.py --help-commands" to list all available commands and their
4
descriptions.
5
"""
6
7
# pylint: disable=consider-using-f-string,consider-using-sys-exit
8
import os
9
import shutil
10
import sys
11
from abc import abstractmethod
12
from pathlib import Path
13
from subprocess import CalledProcessError, call, check_call
14
15
from setuptools import Command, setup
16
from setuptools.command.develop import develop
17
from setuptools.command.egg_info import egg_info
18
from setuptools.command.install import install
19
20
if 'bdist_wheel' in sys.argv:
21
    raise RuntimeError("This setup.py does not support wheels")
22
23
# Paths setup with virtualenv detection
24
BASE_ENV = Path(os.environ.get('VIRTUAL_ENV', '/'))
25
26
NAPP_NAME = 'pathfinder'
27
NAPP_VERSION = '2022.3.0'
28
29
# Kytos var folder
30
VAR_PATH = BASE_ENV / 'var' / 'lib' / 'kytos'
31
# Path for enabled NApps
32
ENABLED_PATH = VAR_PATH / 'napps'
33
# Path to install NApps
34
INSTALLED_PATH = VAR_PATH / 'napps' / '.installed'
35
CURRENT_DIR = Path('.').resolve()
36
37
# NApps enabled by default
38
CORE_NAPPS = ['topology']
39
40
41
# def read_version_from_json():
42
#     """Read the NApp version from NApp kytos.json file."""
43
#     file = Path('kytos.json')
44
#     metadata = json.loads(file.read_text())
45
#     return metadata['version']
46
47
48
class SimpleCommand(Command):
49
    """Make Command implementation simpler."""
50
51
    user_options = []
52
53
    @abstractmethod
54
    def run(self):
55
        """Run when command is invoked.
56
57
        Use *call* instead of *check_call* to ignore failures.
58
        """
59
60
    def initialize_options(self):
61
        """Set default values for options."""
62
63
    def finalize_options(self):
64
        """Post-process options."""
65
66
67
# pylint: disable=attribute-defined-outside-init,abstract-method
68
class TestCommand(Command):
69
    """Test tags decorators."""
70
71
    user_options = [
72
        ('k=', None, 'Specify a pytest -k expression.'),
73
    ]
74
75
    def get_args(self):
76
        """Return args to be used in test command."""
77
        if self.k:
78
            return f"-k '{self.k}'"
79
        return ""
80
81
    def initialize_options(self):
82
        """Set default size and type args."""
83
        self.k = ""
84
85
    def finalize_options(self):
86
        """Post-process."""
87
        pass
88
89
90
class Cleaner(SimpleCommand):
91
    """Custom clean command to tidy up the project root."""
92
93
    description = 'clean build, dist, pyc and egg from package and docs'
94
95
    def run(self):
96
        """Clean build, dist, pyc and egg from package and docs."""
97
        call('rm -vrf ./build ./dist ./*.egg-info', shell=True)
98
        call('find . -name __pycache__ -type d | xargs rm -rf', shell=True)
99
        call('make -C docs/ clean', shell=True)
100
101
102
class Test(TestCommand):
103
    """Run all tests."""
104
105
    description = 'run tests and display results'
106
107
    def run(self):
108
        """Run tests."""
109
        cmd = 'python3 -m pytest tests/ %s' % self.get_args()
110
        try:
111
            check_call(cmd, shell=True)
112
        except CalledProcessError as exc:
113
            print(exc)
114
            print('Unit tests failed. Fix the errors above and try again.')
115
            sys.exit(-1)
116
117
118
class TestCoverage(Test):
119
    """Display test coverage."""
120
121
    description = 'run tests and display code coverage'
122
123
    def run(self):
124
        """Run tests quietly and display coverage report."""
125
        cmd = "python3 -m pytest --cov=. tests/ --cov-report term-missing"
126
        cmd += f" {self.get_args()}"
127
        try:
128
            check_call(cmd, shell=True)
129
        except CalledProcessError as exc:
130
            print(exc)
131
            print('Coverage tests failed. Fix the errors above and try again.')
132
            sys.exit(-1)
133
134
135
class Linter(SimpleCommand):
136
    """Code linters."""
137
138
    description = 'lint Python source code'
139
140
    def run(self):
141
        """Run Yala."""
142
        print('Yala is running. It may take several seconds...')
143
        try:
144
            check_call('yala *.py tests', shell=True)
145
            print('No linter error found.')
146
        except RuntimeError as error:
147
            print('Linter check failed. Fix the error(s) above and try again.')
148
            print(error)
149
            exit(-1)
150
151
152
class KytosInstall:
153
    """Common code for all install types."""
154
155
    @staticmethod
156
    def enable_core_napps():
157
        """Enable a NAPP by creating a symlink."""
158
        (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
159
        for napp in CORE_NAPPS:
160
            napp_path = Path('kytos', napp)
161
            src = ENABLED_PATH / napp_path
162
            dst = INSTALLED_PATH / napp_path
163
            symlink_if_different(src, dst)
164
165
166
class InstallMode(install):
167
    """Create files in var/lib/kytos."""
168
169
    description = 'To install NApps, use kytos-utils. Devs, see "develop".'
170
171
    def run(self):
172
        """Direct users to use kytos-utils to install NApps."""
173
        print(self.description)
174
175
176
class EggInfo(egg_info):
177
    """Prepare files to be packed."""
178
179
    def run(self):
180
        """Build css."""
181
        self._install_deps_wheels()
182
        super().run()
183
184
    @staticmethod
185
    def _install_deps_wheels():
186
        """Python wheels are much faster (no compiling)."""
187
        print('Installing dependencies...')
188
        check_call([sys.executable, '-m', 'pip', 'install', '-r',
189
                    'requirements/run.txt'])
190
191
192
class DevelopMode(develop):
193
    """Recommended setup for kytos-napps developers.
194
195
    Instead of copying the files to the expected directories, a symlink is
196
    created on the system aiming the current source code.
197
    """
198
199
    description = 'Install NApps in development mode'
200
201
    def run(self):
202
        """Install the package in a developer mode."""
203
        super().run()
204
        if self.uninstall:
205
            shutil.rmtree(str(ENABLED_PATH), ignore_errors=True)
206
        else:
207
            self._create_folder_symlinks()
208
            # self._create_file_symlinks()
209
            KytosInstall.enable_core_napps()
210
211
    @staticmethod
212
    def _create_folder_symlinks():
213
        """Symlink to all Kytos NApps folders.
214
215
        ./napps/kytos/napp_name will generate a link in
216
        var/lib/kytos/napps/.installed/kytos/napp_name.
217
        """
218
        links = INSTALLED_PATH / 'kytos'
219
        links.mkdir(parents=True, exist_ok=True)
220
        code = CURRENT_DIR
221
        src = links / NAPP_NAME
222
        symlink_if_different(src, code)
223
224
        (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
225
        dst = ENABLED_PATH / Path('kytos', NAPP_NAME)
226
        symlink_if_different(dst, src)
227
228
    # @staticmethod
229
    # def _create_file_symlinks():
230
    #     """Symlink to required files."""
231
    #     src = ENABLED_PATH / '__init__.py'
232
    #     dst = CURRENT_DIR / 'napps' / '__init__.py'
233
    #     symlink_if_different(src, dst)
234
235
236
def symlink_if_different(path, target):
237
    """Force symlink creation if it points anywhere else."""
238
    # print(f"symlinking {path} to target: {target}...", end=" ")
239
    if not path.exists():
240
        # print(f"path doesn't exist. linking...")
241
        path.symlink_to(target)
242
    elif not path.samefile(target):
243
        # print(f"path exists, but is different. removing and linking...")
244
        # Exists but points to a different file, so let's replace it
245
        path.unlink()
246
        path.symlink_to(target)
247
248
249
def read_requirements(path="requirements/run.txt"):
250
    """Read requirements file and return a list."""
251
    with open(path, "r", encoding="utf8") as file:
252
        return [
253
            line.strip()
254
            for line in file.readlines()
255
            if not line.startswith("#")
256
        ]
257
258
259
setup(name=f'kytos_{NAPP_NAME}',
260
      version=NAPP_VERSION,
261
      description='Core NApps developed by Kytos Team',
262
      url='http://github.com/kytos/{NAPP_NAME}',
263
      author='Kytos Team',
264
      author_email='[email protected]',
265
      license='MIT',
266
      install_requires=read_requirements(),
267
      packages=[],
268
      cmdclass={
269
          'clean': Cleaner,
270
          'coverage': TestCoverage,
271
          'develop': DevelopMode,
272
          'install': InstallMode,
273
          'lint': Linter,
274
          'egg_info': EggInfo,
275
          'test': Test,
276
      },
277
      zip_safe=False,
278
      classifiers=[
279
          'License :: OSI Approved :: MIT License',
280
          'Operating System :: POSIX :: Linux',
281
          'Topic :: System :: Networking',
282
      ])
283