Passed
Pull Request — master (#52)
by Vinicius
12:20 queued 05:21
created

build.setup.CITest.run()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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