Passed
Push — master ( 8463c2...73f6a9 )
by Antonio
03:31
created

build.setup.SimpleCommand.initialize_options()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nop 1
dl 0
loc 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 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 = 'mef_eline'
25
NAPP_VERSION = '2022.1.5'
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 = f"python3 -m pytest tests/ {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 = f"python3 -m pytest --cov=. tests/ {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
        try:
134
            cmd = "yala *.py tests"
135
            check_call(cmd, shell=True)
136
            print("No linter error found.")
137
        except CalledProcessError:
138
            print("Linter check failed. Fix the error(s) above and try again.")
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
    def __str__(self):
156
        return self.__class__.__name__
157
158
159
class InstallMode(install):
160
    """Create files in var/lib/kytos."""
161
162
    description = 'To install NApps, use kytos-utils. Devs, see "develop".'
163
164
    def run(self):
165
        """Direct users to use kytos-utils to install NApps."""
166
        print(self.description)
167
168
169
class EggInfo(egg_info):
170
    """Prepare files to be packed."""
171
172
    def run(self):
173
        """Build css."""
174
        self._install_deps_wheels()
175
        super().run()
176
177
    @staticmethod
178
    def _install_deps_wheels():
179
        """Python wheels are much faster (no compiling)."""
180
        print("Installing dependencies...")
181
        check_call(
182
            [
183
                sys.executable,
184
                "-m",
185
                "pip",
186
                "install",
187
                "-r",
188
                "requirements/run.txt",
189
            ]
190
        )
191
192
193
class DevelopMode(develop):
194
    """Recommended setup for kytos-napps developers.
195
196
    Instead of copying the files to the expected directories, a symlink is
197
    created on the system aiming the current source code.
198
    """
199
200
    description = "Install NApps in development mode"
201
202
    def run(self):
203
        """Install the package in a developer mode."""
204
        super().run()
205
        if self.uninstall:
206
            shutil.rmtree(str(ENABLED_PATH), ignore_errors=True)
207
        else:
208
            self._create_folder_symlinks()
209
            # self._create_file_symlinks()
210
            KytosInstall.enable_core_napps()
211
212
    @staticmethod
213
    def _create_folder_symlinks():
214
        """Symlink to all Kytos NApps folders.
215
216
        ./napps/kytos/napp_name will generate a link in
217
        var/lib/kytos/napps/.installed/kytos/napp_name.
218
        """
219
        links = INSTALLED_PATH / "kytos"
220
        links.mkdir(parents=True, exist_ok=True)
221
        code = CURRENT_DIR
222
        src = links / NAPP_NAME
223
        symlink_if_different(src, code)
224
225
        (ENABLED_PATH / "kytos").mkdir(parents=True, exist_ok=True)
226
        dst = ENABLED_PATH / Path("kytos", NAPP_NAME)
227
        symlink_if_different(dst, src)
228
229
    @staticmethod
230
    def _create_file_symlinks():
231
        """Symlink to required files."""
232
        src = ENABLED_PATH / "__init__.py"
233
        dst = CURRENT_DIR / "napps" / "__init__.py"
234
        symlink_if_different(src, dst)
235
236
237
def symlink_if_different(path, target):
238
    """Force symlink creation if it points anywhere else."""
239
    # print(f"symlinking {path} to target: {target}...", end=" ")
240
    if not path.exists():
241
        # print(f"path doesn't exist. linking...")
242
        path.symlink_to(target)
243
    elif not path.samefile(target):
244
        # print(f"path exists, but is different. removing and linking...")
245
        # Exists but points to a different file, so let's replace it
246
        path.unlink()
247
        path.symlink_to(target)
248
249
250
def read_requirements(path="requirements/run.txt"):
251
    """Read requirements file and return a list."""
252
    with open(path, "r", encoding="utf8") as file:
253
        return [
254
            line.strip()
255
            for line in file.readlines()
256
            if not line.startswith("#")
257
        ]
258
259
260
setup(
261
    name=f"kytos_{NAPP_NAME}",
262
    version=NAPP_VERSION,
263
    description="Core NApps developed by Kytos Team",
264
    url="http://github.com/kytos/{NAPP_NAME}",
265
    author="Kytos Team",
266
    author_email="[email protected]",
267
    license="MIT",
268
    install_requires=read_requirements() + ["setuptools >= 36.0.1"],
269
    packages=[],
270
    extras_require={
271
        "dev": [
272
            "pytest==7.0.0",
273
            "pytest-cov==3.0.0",
274
            "pip-tools",
275
            "yala",
276
            "tox",
277
        ],
278
    },
279
    cmdclass={
280
        "clean": Cleaner,
281
        "coverage": TestCoverage,
282
        "develop": DevelopMode,
283
        "install": InstallMode,
284
        "lint": Linter,
285
        "egg_info": EggInfo,
286
        "test": Test,
287
    },
288
    zip_safe=False,
289
    classifiers=[
290
        "License :: OSI Approved :: MIT License",
291
        "Operating System :: POSIX :: Linux",
292
        "Topic :: System :: Networking",
293
    ],
294
)
295