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 = "sdntrace" |
27
|
|
|
NAPP_USERNAME = "amlight" |
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
|
|
|
ENABLED_NAPPS = [("amlight", "coloring")] |
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 = f"python3 -m pytest tests/ --cov-report term-missing {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 = f"python3 -m pytest --cov=. tests/ --cov-report term-missing {self.get_args()}" |
119
|
|
|
try: |
120
|
|
|
check_call(cmd, shell=True) |
121
|
|
|
except CalledProcessError as exc: |
122
|
|
|
print(exc) |
123
|
|
|
print("Coverage tests failed. Fix the errors above and try again.") |
124
|
|
|
sys.exit(-1) |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
class Linter(SimpleCommand): |
128
|
|
|
"""Code linters.""" |
129
|
|
|
|
130
|
|
|
description = "lint Python source code" |
131
|
|
|
|
132
|
|
|
def run(self): |
133
|
|
|
"""Run yala.""" |
134
|
|
|
print("Yala is running. It may take several seconds...") |
135
|
|
|
try: |
136
|
|
|
cmd = "yala *.py tests" |
137
|
|
|
check_call(cmd, shell=True) |
138
|
|
|
print("No linter error found.") |
139
|
|
|
except CalledProcessError: |
140
|
|
|
print("Linter check failed. Fix the error(s) above and try again.") |
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 / NAPP_USERNAME).mkdir(parents=True, exist_ok=True) |
151
|
|
|
for user, napp in ENABLED_NAPPS: |
152
|
|
|
napp_path = Path(user, napp) |
153
|
|
|
src = ENABLED_PATH / napp_path |
154
|
|
|
dst = INSTALLED_PATH / napp_path |
155
|
|
|
symlink_if_different(src, dst) |
156
|
|
|
|
157
|
|
|
def __str__(self): |
158
|
|
|
return self.__class__.__name__ |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
class InstallMode(install): |
162
|
|
|
"""Create files in var/lib/kytos.""" |
163
|
|
|
|
164
|
|
|
description = 'To install NApps, use kytos-utils. Devs, see "develop".' |
165
|
|
|
|
166
|
|
|
def run(self): |
167
|
|
|
"""Direct users to use kytos-utils to install NApps.""" |
168
|
|
|
print(self.description) |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
class EggInfo(egg_info): |
172
|
|
|
"""Prepare files to be packed.""" |
173
|
|
|
|
174
|
|
|
def run(self): |
175
|
|
|
"""Build css.""" |
176
|
|
|
self._install_deps_wheels() |
177
|
|
|
super().run() |
178
|
|
|
|
179
|
|
|
@staticmethod |
180
|
|
|
def _install_deps_wheels(): |
181
|
|
|
"""Python wheels are much faster (no compiling).""" |
182
|
|
|
print("Installing dependencies...") |
183
|
|
|
check_call( |
184
|
|
|
[ |
185
|
|
|
sys.executable, |
186
|
|
|
"-m", |
187
|
|
|
"pip", |
188
|
|
|
"install", |
189
|
|
|
"-r", |
190
|
|
|
"requirements/run.txt", |
191
|
|
|
] |
192
|
|
|
) |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
class DevelopMode(develop): |
196
|
|
|
"""Recommended setup for kytos-napps developers. |
197
|
|
|
|
198
|
|
|
Instead of copying the files to the expected directories, a symlink is |
199
|
|
|
created on the system aiming the current source code. |
200
|
|
|
""" |
201
|
|
|
|
202
|
|
|
description = "Install NApps in development mode" |
203
|
|
|
|
204
|
|
|
def run(self): |
205
|
|
|
"""Install the package in a developer mode.""" |
206
|
|
|
super().run() |
207
|
|
|
if self.uninstall: |
208
|
|
|
shutil.rmtree(str(ENABLED_PATH), ignore_errors=True) |
209
|
|
|
else: |
210
|
|
|
self._create_folder_symlinks() |
211
|
|
|
KytosInstall.enable_core_napps() |
212
|
|
|
|
213
|
|
|
@staticmethod |
214
|
|
|
def _create_folder_symlinks(): |
215
|
|
|
"""Symlink to all Kytos NApps folders. |
216
|
|
|
|
217
|
|
|
./napps/kytos/napp_name will generate a link in |
218
|
|
|
var/lib/kytos/napps/.installed/kytos/napp_name. |
219
|
|
|
""" |
220
|
|
|
links = INSTALLED_PATH / NAPP_USERNAME |
221
|
|
|
links.mkdir(parents=True, exist_ok=True) |
222
|
|
|
code = CURRENT_DIR |
223
|
|
|
src = links / NAPP_NAME |
224
|
|
|
symlink_if_different(src, code) |
225
|
|
|
|
226
|
|
|
(ENABLED_PATH / NAPP_USERNAME).mkdir(parents=True, exist_ok=True) |
227
|
|
|
dst = ENABLED_PATH / Path(NAPP_USERNAME, NAPP_NAME) |
228
|
|
|
symlink_if_different(dst, src) |
229
|
|
|
|
230
|
|
|
@staticmethod |
231
|
|
|
def _create_file_symlinks(): |
232
|
|
|
"""Symlink to required files.""" |
233
|
|
|
src = ENABLED_PATH / "__init__.py" |
234
|
|
|
dst = CURRENT_DIR / "__init__.py" |
235
|
|
|
symlink_if_different(src, dst) |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
def read_version_from_json(): |
239
|
|
|
"""Read the NApp version from NApp kytos.json file.""" |
240
|
|
|
file = Path("kytos.json") |
241
|
|
|
metadata = json.loads(file.read_text(encoding="utf8")) |
242
|
|
|
return metadata["version"] |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
def read_requirements(path="requirements/run.txt"): |
246
|
|
|
"""Read requirements file and return a list.""" |
247
|
|
|
with open(path, "r", encoding="utf8") as file: |
248
|
|
|
return [line.strip() for line in file.readlines() if not line.startswith("#")] |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
def symlink_if_different(path, target): |
252
|
|
|
"""Force symlink creation if it points anywhere else.""" |
253
|
|
|
# print(f"symlinking {path} to target: {target}...", end=" ") |
254
|
|
|
if not path.exists(): |
255
|
|
|
# print(f"path doesn't exist. linking...") |
256
|
|
|
path.symlink_to(target) |
257
|
|
|
elif not path.samefile(target): |
258
|
|
|
# print(f"path exists, but is different. removing and linking...") |
259
|
|
|
# Exists but points to a different file, so let's replace it |
260
|
|
|
path.unlink() |
261
|
|
|
path.symlink_to(target) |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
setup( |
265
|
|
|
name=f"{NAPP_USERNAME}_{NAPP_NAME}", |
266
|
|
|
version=read_version_from_json(), |
267
|
|
|
description="An OpenFlow Path Trace for the Kytos SDN controller", |
268
|
|
|
url="http://github.com/kytos-ng/sdntrace", |
269
|
|
|
author="Jeronimo Bezerra", |
270
|
|
|
author_email="[email protected]", |
271
|
|
|
license="MIT", |
272
|
|
|
install_requires=read_requirements() + ["importlib_metadata"], |
273
|
|
|
packages=[], |
274
|
|
|
cmdclass={ |
275
|
|
|
"clean": Cleaner, |
276
|
|
|
"coverage": TestCoverage, |
277
|
|
|
"develop": DevelopMode, |
278
|
|
|
"install": InstallMode, |
279
|
|
|
"lint": Linter, |
280
|
|
|
"egg_info": EggInfo, |
281
|
|
|
"test": Test, |
282
|
|
|
}, |
283
|
|
|
zip_safe=False, |
284
|
|
|
classifiers=[ |
285
|
|
|
"License :: OSI Approved :: MIT License", |
286
|
|
|
"Operating System :: POSIX :: Linux", |
287
|
|
|
"Programming Language :: Python :: 3.11", |
288
|
|
|
"Topic :: System :: Networking", |
289
|
|
|
], |
290
|
|
|
) |
291
|
|
|
|