Passed
Push — master ( cf7895...62b7de )
by Vinicius
05:24 queued 02:49
created

build.setup.read_version_from_json()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 5
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 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 = 'of_lldp'
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 controllers db 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_version_from_json():
234
    """Read the NApp version from NApp kytos.json file."""
235
    file = Path('kytos.json')
236
    metadata = json.loads(file.read_text())
237
    return metadata['version']
238
239
240
def read_requirements(path="requirements/run.txt"):
241
    """Read requirements file and return a list."""
242
    with open(path, "r", encoding="utf8") as file:
243
        return [
244
            line.strip()
245
            for line in file.readlines()
246
            if not line.startswith("#")
247
        ]
248
249
250
setup(name=f'kytos_{NAPP_NAME}',
251
      version=read_version_from_json(),
252
      description='Core NApps developed by the Kytos Team',
253
      url=f'http://github.com/kytos/{NAPP_NAME}',
254
      author='Kytos Team',
255
      author_email='[email protected]',
256
      license='MIT',
257
      install_requires=read_requirements() + ['setuptools >= 36.0.1'],
258
      packages=[],
259
      extras_require={
260
          'dev': [
261
              'pytest==7.0.0',
262
              'pytest-cov==3.0.0',
263
              'pip-tools',
264
              'yala',
265
              'tox',
266
          ],
267
      },
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