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