Passed
Pull Request — master (#147)
by Italo Valcy
12:43 queued 05:51
created

setup   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 149
dl 0
loc 279
rs 10
c 0
b 0
f 0
wmc 22

16 Methods

Rating   Name   Duplication   Size   Complexity  
A EggInfo.run() 0 4 1
A TestCommand.finalize_options() 0 10 2
A EggInfo._install_deps_wheels() 0 6 1
A TestCoverage.run() 0 10 2
A Test.get_args() 0 7 3
A Cleaner.run() 0 3 1
A DocTest.run() 0 4 1
A SimpleCommand.run() 0 3 1
A TestCommand.get_args() 0 3 1
A SimpleCommand.finalize_options() 0 2 1
A Linter.run() 0 9 2
A CITest.run() 0 8 1
A SimpleCommand.__init__() 0 5 1
A Test.run() 0 9 2
A TestCommand.initialize_options() 0 4 1
A SimpleCommand.initialize_options() 0 2 1
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 re
8
import sys
9
from abc import abstractmethod
10
# Disabling checks due to https://github.com/PyCQA/pylint/issues/73
11
from pathlib import Path
12
from subprocess import CalledProcessError, call, check_call
13
14
try:
15
    # Check if pip is installed
16
    # pylint: disable=unused-import
17
    import pip  # noqa
18
    from setuptools import Command, find_packages, setup
19
    from setuptools.command.egg_info import egg_info
20
except ModuleNotFoundError:
21
    print('Please install python3-pip and run setup.py again.')
22
    sys.exit(-1)
23
24
BASE_ENV = Path(os.environ.get('VIRTUAL_ENV', '/'))
25
ETC_FILES = []
26
27
NEEDS_PYTEST = {'pytest', 'test', 'coverage'}.intersection(sys.argv)
28
PYTEST_RUNNER = ['pytest-runner'] if NEEDS_PYTEST else []
29
30
31
class SimpleCommand(Command):
32
    """Make Command implementation simpler."""
33
34
    user_options = []
35
36
    def __init__(self, *args, **kwargs):
37
        """Store arguments so it's possible to call other commands later."""
38
        super().__init__(*args, **kwargs)
39
        self._args = args
40
        self._kwargs = kwargs
41
42
    @abstractmethod
43
    def run(self):
44
        """Run when command is invoked.
45
46
        Use *call* instead of *check_call* to ignore failures.
47
        """
48
49
    def initialize_options(self):
50
        """Set default values for options."""
51
52
    def finalize_options(self):
53
        """Post-process options."""
54
55
56
class EggInfo(egg_info):
57
    """Prepare files to be packed."""
58
59
    def run(self):
60
        """Build css."""
61
        self._install_deps_wheels()
62
        super().run()
63
64
    @staticmethod
65
    def _install_deps_wheels():
66
        """Python wheels are much faster (no compiling)."""
67
        print('Installing dependencies...')
68
        check_call([sys.executable, '-m', 'pip', 'install', '-r',
69
                    'requirements/run.txt'])
70
71
72
# pylint: disable=attribute-defined-outside-init, abstract-method
73
class TestCommand(Command):
74
    """Test tags decorators."""
75
76
    user_options = [
77
        ('size=', None, 'Specify the size of tests to be executed.'),
78
        ('type=', None, 'Specify the type of tests to be executed.'),
79
    ]
80
81
    sizes = ('small', 'medium', 'large', 'all')
82
    types = ('unit', 'integration', 'e2e')
83
84
    def get_args(self):
85
        """Return args to be used in test command."""
86
        return '--size %s --type %s' % (self.size, self.type)
87
88
    def initialize_options(self):
89
        """Set default size and type args."""
90
        self.size = 'all'
91
        self.type = 'unit'
92
93
    def finalize_options(self):
94
        """Post-process."""
95
        try:
96
            assert self.size in self.sizes, ('ERROR: Invalid size:'
97
                                             f':{self.size}')
98
            assert self.type in self.types, ('ERROR: Invalid type:'
99
                                             f':{self.type}')
100
        except AssertionError as exc:
101
            print(exc)
102
            sys.exit(-1)
103
104
105
class Cleaner(SimpleCommand):
106
    """Custom clean command to tidy up the project root."""
107
108
    description = 'clean build, dist, pyc and egg from package and docs'
109
110
    def run(self):
111
        """Clean build, dist, pyc and egg from package and docs."""
112
        call('make clean', shell=True)
113
114
115
class Test(TestCommand):
116
    """Run all tests."""
117
118
    description = 'run tests and display results'
119
120
    def get_args(self):
121
        """Return args to be used in test command."""
122
        markers = self.size
123
        if markers == "small":
124
            markers = 'not medium and not large'
125
        size_args = "" if self.size == "all" else "-m '%s'" % markers
126
        return '--addopts="tests/%s %s"' % (self.type, size_args)
127
128
    def run(self):
129
        """Run tests."""
130
        cmd = 'python setup.py pytest %s' % self.get_args()
131
        try:
132
            check_call(cmd, shell=True)
133
        except CalledProcessError as exc:
134
            print(exc)
135
            print('Unit tests failed. Fix the error(s) above and try again.')
136
            sys.exit(-1)
137
138
139
class TestCoverage(Test):
140
    """Display test coverage."""
141
142
    description = 'run tests and display code coverage'
143
144
    def run(self):
145
        """Run tests quietly and display coverage report."""
146
        cmd = 'coverage3 run setup.py pytest %s' % self.get_args()
147
        cmd += '&& coverage3 report'
148
        try:
149
            check_call(cmd, shell=True)
150
        except CalledProcessError as exc:
151
            print(exc)
152
            print('Coverage tests failed. Fix the errors above and try again.')
153
            sys.exit(-1)
154
155
156
class DocTest(SimpleCommand):
157
    """Run documentation tests."""
158
159
    description = 'run documentation tests'
160
161
    def run(self):
162
        """Run doctests using Sphinx Makefile."""
163
        cmd = 'make -C docs/ default doctest'
164
        check_call(cmd, shell=True)
165
166
167
class Linter(SimpleCommand):
168
    """Code linters."""
169
170
    description = 'Lint Python source code'
171
172
    def run(self):
173
        """Run yala."""
174
        print('Yala is running. It may take several seconds...')
175
        try:
176
            check_call('yala setup.py kytos tests', shell=True)
177
            print('No linter error found.')
178
        except CalledProcessError:
179
            print('Linter check failed. Fix the error(s) above and try again.')
180
            sys.exit(-1)
181
182
183
class CITest(TestCommand):
184
    """Run all CI tests."""
185
186
    description = 'run all CI tests: unit and doc tests, linter'
187
188
    # pylint: disable=fixme
189
    def run(self):
190
        """Run unit tests with coverage, doc tests and linter."""
191
        coverage_cmd = 'python setup.py coverage %s' % self.get_args()
192
        # TODO see issue 141
193
        # doctest_cmd = 'python setup.py doctest'
194
        lint_cmd = 'python setup.py lint'
195
        cmd = '%s && %s' % (coverage_cmd, lint_cmd)
196
        check_call(cmd, shell=True)
197
198
199
# class InstallMode(install):
200
#     """Class used to overwrite the default installation using setuptools."""
201
202
#     def run(self):
203
#         """Install the package in install mode.
204
205
#         super().run() does not install dependencies when running
206
#         ``python setup.py install`` (pypa/setuptools#456).
207
#         """
208
#         if 'bdist_wheel' in sys.argv:
209
#             # do not use eggs, but wheels
210
#             super().run()
211
#         else:
212
#             # force install of deps' eggs during setup.py install
213
#             self.do_egg_install()
214
215
216
# class DevelopMode(develop):
217
#    """Recommended setup for developers.
218
#
219
#    The following feature are temporarily remove from code:
220
#    Instead of copying the files to the expected directories, a symlink is
221
#    created on the system aiming the current source code.
222
#    """
223
#
224
#    def run(self):
225
#        """Install the package in a developer mode."""
226
#        super().run()
227
228
229
# We are parsing the metadata file as if it was a text file because if we
230
# import it as a python module, necessarily the kytos.core module would be
231
# initialized, which means that kyots/core/__init__.py would be run and, then,
232
# kytos.core.controller.Controller would be called and it will try to import
233
# some modules that are dependencies from this project and that were not yet
234
# installed, since the requirements installation from this project hasn't yet
235
# happened.
236
META_FILE = open("kytos/core/metadata.py").read()
237
METADATA = dict(re.findall(r"(__[a-z]+__)\s*=\s*'([^']+)'", META_FILE))
238
239
setup(name='kytos',
240
      version=METADATA.get('__version__'),
241
      description=METADATA.get('__description__'),
242
      long_description=open("README.pypi.rst", "r").read(),
243
      long_description_content_type='text/x-rst',
244
      url=METADATA.get('__url__'),
245
      author=METADATA.get('__author__'),
246
      author_email=METADATA.get('__author_email__'),
247
      license=METADATA.get('__license__'),
248
      test_suite='tests',
249
      scripts=['bin/kytosd'],
250
      include_package_data=True,
251
      data_files=[(os.path.join(BASE_ENV, 'etc/kytos'), ETC_FILES)],
252
      packages=find_packages(exclude=['tests']),
253
      install_requires=[line.strip()
254
                        for line in open("requirements/run.txt").readlines()
255
                        if not line.startswith('#')],
256
      setup_requires=PYTEST_RUNNER,
257
      tests_require=['pytest'],
258
      cmdclass={
259
          'clean': Cleaner,
260
          'ci': CITest,
261
          'coverage': TestCoverage,
262
          'doctest': DocTest,
263
          'egg_info': EggInfo,
264
          'lint': Linter,
265
          'test': Test
266
      },
267
      zip_safe=False,
268
      classifiers=[
269
          'License :: OSI Approved :: MIT License',
270
          'Operating System :: POSIX :: Linux',
271
          'Programming Language :: Python :: 3.6',
272
          'Programming Language :: Python :: 3.7',
273
          'Programming Language :: Python :: 3.8',
274
          'Programming Language :: Python :: 3.9',
275
          'Topic :: System :: Networking',
276
          'Development Status :: 4 - Beta',
277
          'Environment :: Console',
278
          'Environment :: No Input/Output (Daemon)',
279
      ])
280