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