Passed
Pull Request — master (#40)
by Jose
01:58
created

build.setup.read_version_from_json()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
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 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
if 'VIRTUAL_ENV' in os.environ:
23
    BASE_ENV = Path(os.environ['VIRTUAL_ENV'])
24
else:
25
    BASE_ENV = Path('/')
26
27
NAPP_NAME = 'of_stats'
28
NAPP_VERSION = '1.1.0'
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 = ['of_core']
39
40
41
class SimpleCommand(Command):
42
    """Make Command implementation simpler."""
43
44
    user_options = []
45
46
    @abstractmethod
47
    def run(self):
48
        """Run when command is invoked.
49
50
        Use *call* instead of *check_call* to ignore failures.
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
class Cleaner(SimpleCommand):
60
    """Custom clean command to tidy up the project root."""
61
62
    description = 'clean build, dist, pyc and egg from package and docs'
63
64
    def run(self):
65
        """Clean build, dist, pyc and egg from package and docs."""
66
        call('rm -vrf ./build ./dist ./*.egg-info', shell=True)
67
        call('find . -name __pycache__ -type d | xargs rm -rf', shell=True)
68
        call('make -C docs/ clean', shell=True)
69
70
71
class TestCoverage(SimpleCommand):
72
    """Display test coverage."""
73
74
    description = 'run unit tests and display code coverage'
75
76
    def run(self):
77
        """Run unittest quietly and display coverage report."""
78
        cmd = 'coverage3 run -m unittest && coverage3 report'
79
        call(cmd, shell=True)
80
81
82
class Linter(SimpleCommand):
83
    """Code linters."""
84
85
    description = 'lint Python source code'
86
87
    def run(self):
88
        """Run yala."""
89
        print('Yala is running. It may take several seconds...')
90
        check_call('yala *.py',
91
                   shell=True)
92
93
94
class CITest(SimpleCommand):
95
    """Run all CI tests."""
96
97
    description = 'run all CI tests: unit and doc tests, linter'
98
99
    def run(self):
100
        """Run unit tests with coverage, doc tests and linter."""
101
        cmds = ['python3.6 setup.py ' + cmd
102
                for cmd in ('coverage', 'lint')]
103
        cmd = ' && '.join(cmds)
104
        check_call(cmd, shell=True)
105
106
107
# class KytosInstall:
108
#     """Common code for all install types."""
109
#
110
#     @staticmethod
111
#     def enable_core_napps():
112
#         """Enable a NAPP by creating a symlink."""
113
#         (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
114
#         for napp in CORE_NAPPS:
115
#             napp_path = Path('kytos', napp)
116
#             src = ENABLED_PATH / napp_path
117
#             dst = INSTALLED_PATH / napp_path
118
#             src.symlink_to(dst)
119
120
121
class InstallMode(install):
122
    """Create files in var/lib/kytos."""
123
124
    description = 'To install NApps, use kytos-utils. Devs, see "develop".'
125
126
    def run(self):
127
        """Create of_stats as default napps enabled."""
128
        print(self.description)
129
130
131
# class EggInfo(egg_info):
132
#     """Prepare files to be packed."""
133
#
134
#     def run(self):
135
#         """Build css."""
136
#         self._install_deps_wheels()
137
#         super().run()
138
#
139
#     @staticmethod
140
#     def _install_deps_wheels():
141
#         """Python wheels are much faster (no compiling)."""
142
#         print('Installing dependencies...')
143
#         check_call([sys.executable, '-m', 'pip', 'install', '-r',
144
#                     'requirements/run.in'])
145
146
147
class DevelopMode(develop):
148
    """Recommended setup for kytos-napps developers.
149
150
    Instead of copying the files to the expected directories, a symlink is
151
    created on the system aiming the current source code.
152
    """
153
154
    description = 'install NApps in development mode'
155
156
    def run(self):
157
        """Install the package in a developer mode."""
158
        super().run()
159
        if self.uninstall:
160
            shutil.rmtree(str(ENABLED_PATH), ignore_errors=True)
161
        else:
162
            self._create_folder_symlinks()
163
            # self._create_file_symlinks()
164
            # KytosInstall.enable_core_napps()
165
166
    @staticmethod
167
    def _create_folder_symlinks():
168
        """Symlink to all Kytos NApps folders.
169
170
        ./napps/kytos/napp_name will generate a link in
171
        var/lib/kytos/napps/.installed/kytos/napp_name.
172
        """
173
        links = INSTALLED_PATH / 'kytos'
174
        links.mkdir(parents=True, exist_ok=True)
175
        code = CURRENT_DIR
176
        src = links / 'of_stats'
177
        symlink_if_different(src, code)
178
179
        (ENABLED_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
180
        dst = ENABLED_PATH / Path('kytos', 'of_stats')
181
        symlink_if_different(dst, src)
182
183
    # @staticmethod
184
    # def _create_file_symlinks():
185
    #     """Symlink to required files."""
186
    #     src = ENABLED_PATH / '__init__.py'
187
    #     dst = CURRENT_DIR / 'napps' / '__init__.py'
188
    #     symlink_if_different(src, dst)
189
190
191
def symlink_if_different(path, target):
192
    """Force symlink creation if it points anywhere else."""
193
    # print(f"symlinking {path} to target: {target}...", end=" ")
194
    if not path.exists():
195
        # print(f"path doesn't exist. linking...")
196
        path.symlink_to(target)
197
    elif not path.samefile(target):
198
        # print(f"path exists, but is different. removing and linking...")
199
        # Exists but points to a different file, so let's replace it
200
        path.unlink()
201
        path.symlink_to(target)
202
203
204
setup(name=NAPP_NAME,
205
      version=NAPP_VERSION,
206
      description='Core NApps developed by Kytos Team',
207
      url='http://github.com/kytos/of_stats',
208
      author='Kytos Team',
209
      author_email='[email protected]',
210
      license='MIT',
211
      install_requires=['setuptools >= 36.0.1'],
212
      extras_require={
213
          'dev': [
214
              'coverage',
215
              'pip-tools',
216
              'yala',
217
              'tox',
218
          ],
219
      },
220
      cmdclass={
221
          'clean': Cleaner,
222
          'ci': CITest,
223
          'coverage': TestCoverage,
224
225
          'develop': DevelopMode,
226
          'install': InstallMode,
227
          'lint': Linter,
228
          # 'egg_info': EggInfo,
229
      },
230
      zip_safe=False,
231
      classifiers=[
232
          'License :: OSI Approved :: MIT License',
233
          'Operating System :: POSIX :: Linux',
234
          'Programming Language :: Python :: 3.6',
235
          'Topic :: System :: Networking',
236
      ])
237