1
|
|
|
"""Setup script. |
2
|
|
|
|
3
|
|
|
Run "python3 setup.py --help-commands" to list all available commands and their |
4
|
|
|
descriptions. |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
import json |
8
|
|
|
import os |
9
|
|
|
import shutil |
10
|
|
|
import sys |
11
|
|
|
from abc import abstractmethod |
12
|
|
|
from pathlib import Path |
13
|
|
|
from subprocess import CalledProcessError, call, check_call |
14
|
|
|
|
15
|
|
|
from setuptools import Command, setup |
16
|
|
|
from setuptools.command.develop import develop |
17
|
|
|
from setuptools.command.egg_info import egg_info |
18
|
|
|
from setuptools.command.install import install |
19
|
|
|
|
20
|
|
|
if "bdist_wheel" in sys.argv: |
21
|
|
|
raise RuntimeError("This setup.py does not support wheels") |
22
|
|
|
|
23
|
|
|
# Paths setup with virtualenv detection |
24
|
|
|
BASE_ENV = Path(os.environ.get("VIRTUAL_ENV", "/")) |
25
|
|
|
|
26
|
|
|
NAPP_NAME = "maintenance" |
27
|
|
|
|
28
|
|
|
# Kytos var folder |
29
|
|
|
VAR_PATH = BASE_ENV / "var" / "lib" / "kytos" |
30
|
|
|
# Path for enabled NApps |
31
|
|
|
ENABLED_PATH = VAR_PATH / "napps" |
32
|
|
|
# Path to install NApps |
33
|
|
|
INSTALLED_PATH = VAR_PATH / "napps" / ".installed" |
34
|
|
|
CURRENT_DIR = Path(".").resolve() |
35
|
|
|
|
36
|
|
|
CORE_NAPPS = [] |
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 = "python3 -m pytest --cov=. tests/ --cov-report term-missing" |
117
|
|
|
cmd += f" {self.get_args()}" |
118
|
|
|
try: |
119
|
|
|
check_call(cmd, shell=True) |
120
|
|
|
except CalledProcessError as exc: |
121
|
|
|
print(exc) |
122
|
|
|
print("Coverage tests failed. Fix the errors above and try again.") |
123
|
|
|
sys.exit(-1) |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
class Linter(SimpleCommand): |
127
|
|
|
"""Code linters.""" |
128
|
|
|
|
129
|
|
|
description = "lint Python source code" |
130
|
|
|
|
131
|
|
|
def run(self): |
132
|
|
|
"""Run Yala.""" |
133
|
|
|
print("Yala is running. It may take several seconds...") |
134
|
|
|
try: |
135
|
|
|
cmd = "yala *.py tests/*.py" |
136
|
|
|
check_call(cmd, shell=True) |
137
|
|
|
print("No linter error found.") |
138
|
|
|
except RuntimeError as error: |
139
|
|
|
print("Linter check failed. Fix the error(s) above and try again.") |
140
|
|
|
print(error) |
141
|
|
|
sys.exit(-1) |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
class KytosInstall: |
145
|
|
|
"""Common code for all install types.""" |
146
|
|
|
|
147
|
|
|
@staticmethod |
148
|
|
|
def enable_core_napps(): |
149
|
|
|
"""Enable a NAPP by creating a symlink.""" |
150
|
|
|
(ENABLED_PATH / "kytos").mkdir(parents=True, exist_ok=True) |
151
|
|
|
for napp in CORE_NAPPS: |
152
|
|
|
napp_path = Path("kytos", napp) |
153
|
|
|
src = ENABLED_PATH / napp_path |
154
|
|
|
dst = INSTALLED_PATH / napp_path |
155
|
|
|
symlink_if_different(src, dst) |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
class InstallMode(install): |
159
|
|
|
"""Create files in var/lib/kytos.""" |
160
|
|
|
|
161
|
|
|
description = 'To install NApps, use kytos-utils. Devs, see "develop".' |
162
|
|
|
|
163
|
|
|
def run(self): |
164
|
|
|
"""Direct users to use kytos-utils to install NApps.""" |
165
|
|
|
print(self.description) |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
class EggInfo(egg_info): |
169
|
|
|
"""Prepare files to be packed.""" |
170
|
|
|
|
171
|
|
|
def run(self): |
172
|
|
|
"""Build css.""" |
173
|
|
|
self._install_deps_wheels() |
174
|
|
|
super().run() |
175
|
|
|
|
176
|
|
|
@staticmethod |
177
|
|
|
def _install_deps_wheels(): |
178
|
|
|
"""Python wheels are much faster (no compiling).""" |
179
|
|
|
print("Installing dependencies...") |
180
|
|
|
check_call( |
181
|
|
|
[sys.executable, "-m", "pip", "install", "-r", "requirements/run.txt"] |
182
|
|
|
) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
class DevelopMode(develop): |
186
|
|
|
"""Recommended setup for kytos-napps developers. |
187
|
|
|
|
188
|
|
|
Instead of copying the files to the expected directories, a symlink is |
189
|
|
|
created on the system aiming the current source code. |
190
|
|
|
""" |
191
|
|
|
|
192
|
|
|
description = "Install NApps in development mode" |
193
|
|
|
|
194
|
|
|
def run(self): |
195
|
|
|
"""Install the package in a developer mode.""" |
196
|
|
|
super().run() |
197
|
|
|
if self.uninstall: |
198
|
|
|
shutil.rmtree(str(ENABLED_PATH), ignore_errors=True) |
199
|
|
|
else: |
200
|
|
|
self._create_folder_symlinks() |
201
|
|
|
# self._create_file_symlinks() |
202
|
|
|
KytosInstall.enable_core_napps() |
203
|
|
|
|
204
|
|
|
@staticmethod |
205
|
|
|
def _create_folder_symlinks(): |
206
|
|
|
"""Symlink to all Kytos NApps folders. |
207
|
|
|
|
208
|
|
|
./napps/kytos/napp_name will generate a link in |
209
|
|
|
var/lib/kytos/napps/.installed/kytos/napp_name. |
210
|
|
|
""" |
211
|
|
|
links = INSTALLED_PATH / "kytos" |
212
|
|
|
links.mkdir(parents=True, exist_ok=True) |
213
|
|
|
code = CURRENT_DIR |
214
|
|
|
src = links / NAPP_NAME |
215
|
|
|
symlink_if_different(src, code) |
216
|
|
|
|
217
|
|
|
(ENABLED_PATH / "kytos").mkdir(parents=True, exist_ok=True) |
218
|
|
|
dst = ENABLED_PATH / Path("kytos", NAPP_NAME) |
219
|
|
|
symlink_if_different(dst, src) |
220
|
|
|
|
221
|
|
|
@staticmethod |
222
|
|
|
def _create_file_symlinks(): |
223
|
|
|
"""Symlink to required files.""" |
224
|
|
|
src = ENABLED_PATH / "__init__.py" |
225
|
|
|
dst = CURRENT_DIR / "napps" / "__init__.py" |
226
|
|
|
symlink_if_different(src, dst) |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
def symlink_if_different(path, target): |
230
|
|
|
"""Force symlink creation if it points anywhere else.""" |
231
|
|
|
# print(f"symlinking {path} to target: {target}...", end=" ") |
232
|
|
|
if not path.exists(): |
233
|
|
|
# print(f"path doesn't exist. linking...") |
234
|
|
|
path.symlink_to(target) |
235
|
|
|
elif not path.samefile(target): |
236
|
|
|
# print(f"path exists, but is different. removing and linking...") |
237
|
|
|
# Exists but points to a different file, so let's replace it |
238
|
|
|
path.unlink() |
239
|
|
|
path.symlink_to(target) |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
def read_version_from_json(): |
243
|
|
|
"""Read the NApp version from NApp kytos.json file.""" |
244
|
|
|
file = Path("kytos.json") |
245
|
|
|
metadata = json.loads(file.read_text(encoding="utf8")) |
246
|
|
|
return metadata["version"] |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
def read_requirements(path="requirements/run.txt"): |
250
|
|
|
"""Read requirements file and return a list.""" |
251
|
|
|
with open(path, "r", encoding="utf8") as file: |
252
|
|
|
return [line.strip() for line in file.readlines() if not line.startswith("#")] |
253
|
|
|
|
254
|
|
|
|
255
|
|
|
setup( |
256
|
|
|
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
|
|
|
cmdclass={ |
266
|
|
|
"clean": Cleaner, |
267
|
|
|
"coverage": TestCoverage, |
268
|
|
|
"develop": DevelopMode, |
269
|
|
|
"install": InstallMode, |
270
|
|
|
"lint": Linter, |
271
|
|
|
"egg_info": EggInfo, |
272
|
|
|
"test": Test, |
273
|
|
|
}, |
274
|
|
|
zip_safe=False, |
275
|
|
|
classifiers=[ |
276
|
|
|
"License :: OSI Approved :: MIT License", |
277
|
|
|
"Operating System :: POSIX :: Linux", |
278
|
|
|
"Programming Language :: Python :: 3.6", |
279
|
|
|
"Topic :: System :: Networking", |
280
|
|
|
], |
281
|
|
|
) |
282
|
|
|
|