build.setup   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 293
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 30
eloc 169
dl 0
loc 293
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A Cleaner.run() 0 5 1
A TestCommand.finalize_options() 0 3 1
A SimpleCommand.initialize_options() 0 2 1
A SimpleCommand.run() 0 3 1
A TestCommand.get_args() 0 5 2
A Test.run() 0 9 2
A TestCommand.initialize_options() 0 3 1
A SimpleCommand.finalize_options() 0 2 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 DevelopMode._create_file_symlinks() 0 6 1
A EggInfo._install_deps_wheels() 0 12 1
A DevelopMode.run() 0 9 2
A KytosInstall.__str__() 0 2 1
A EggInfo.run() 0 4 1
A TestCoverage.run() 0 10 2

3 Functions

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