-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
198 lines (152 loc) · 5.54 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"""Setup script.
Run "python3 setup --help-commands" to list all available commands and their
descriptions.
"""
import os
import shutil
import sys
from abc import abstractmethod
from pathlib import Path
from subprocess import call, check_call
from setuptools import Command, setup
from setuptools.command.develop import develop
from setuptools.command.install import install
if 'bdist_wheel' in sys.argv:
raise RuntimeError("This setup.py does not support wheels")
# Paths setup with virtualenv detection
if 'VIRTUAL_ENV' in os.environ:
BASE_ENV = Path(os.environ['VIRTUAL_ENV'])
else:
BASE_ENV = Path('/')
# Kytos var folder
VAR_PATH = BASE_ENV / 'var' / 'lib' / 'kytos'
# Path for enabled NApps
ENABL_PATH = VAR_PATH / 'napps'
# Path to install NApps
INSTL_PATH = VAR_PATH / 'napps' / '.installed'
CURR_DIR = Path('.').resolve()
# NApps enabled by default
CORE_NAPPS = ['of_core']
class SimpleCommand(Command):
"""Make Command implementation simpler."""
user_options = []
@abstractmethod
def run(self):
"""Run when command is invoked.
Use *call* instead of *check_call* to ignore failures.
"""
pass
def initialize_options(self):
"""Set default values for options."""
pass
def finalize_options(self):
"""Post-process options."""
pass
class Cleaner(SimpleCommand):
"""Custom clean command to tidy up the project root."""
description = 'clean build, dist, pyc and egg from package and docs'
def run(self):
"""Clean build, dist, pyc and egg from package and docs."""
call('rm -vrf ./build ./dist ./*.egg-info', shell=True)
call('find . -name __pycache__ -type d | xargs rm -rf', shell=True)
call('make -C docs/ clean', shell=True)
class TestCoverage(SimpleCommand):
"""Display test coverage."""
description = 'run unit tests and display code coverage'
def run(self):
"""Run unittest quietly and display coverage report."""
cmd = 'coverage3 run -m unittest discover -qs napps/kytos' \
' && coverage3 report'
call(cmd, shell=True)
class Linter(SimpleCommand):
"""Code linters."""
description = 'lint Python source code'
def run(self):
"""Run pylama."""
print('Pylama is running. It may take several seconds...')
check_call('pylama setup.py tests kytos', shell=True)
class CITest(SimpleCommand):
"""Run all CI tests."""
description = 'run all CI tests: unit and doc tests, linter'
def run(self):
"""Run unit tests with coverage, doc tests and linter."""
cmds = ['python setup.py ' + cmd
for cmd in ('coverage', 'lint')]
cmd = ' && '.join(cmds)
check_call(cmd, shell=True)
class KytosInstall:
"""Common code for all install types."""
@staticmethod
def enable_core_napps():
"""Enable a NAPP by creating a symlink."""
(ENABL_PATH / 'kytos').mkdir(parents=True, exist_ok=True)
for napp in CORE_NAPPS:
napp_path = Path('kytos', napp)
src = ENABL_PATH / napp_path
dst = INSTL_PATH / napp_path
src.symlink_to(dst)
class InstallMode(install):
"""Create files in var/lib/kytos."""
description = 'To install NApps, use kytos-utils. Devs, see "develop".'
def run(self):
"""Create of_core as default napps enabled."""
print(self.description)
class DevelopMode(develop):
"""Recommended setup for kytos-napps developers.
Instead of copying the files to the expected directories, a symlink is
created on the system aiming the current source code.
"""
description = 'install NApps in development mode'
def run(self):
"""Install the package in a developer mode."""
super().run()
if self.uninstall:
shutil.rmtree(str(ENABL_PATH), ignore_errors=True)
else:
self._create_folder_symlinks()
self._create_file_symlinks()
KytosInstall.enable_core_napps()
@staticmethod
def _create_folder_symlinks():
"""Symlink to all Kytos NApps folders.
./napps/kytos/napp_name will generate a link in
var/lib/kytos/napps/.installed/kytos/napp_name.
"""
links = INSTL_PATH / 'kytos'
links.mkdir(parents=True, exist_ok=True)
code = CURR_DIR / 'napps' / 'kytos'
for path in code.iterdir():
last_folder = path.parts[-1]
if path.is_dir() and last_folder != '__pycache__':
src = links / last_folder
src.symlink_to(path)
@staticmethod
def _create_file_symlinks():
"""Symlink to required files."""
src = ENABL_PATH / '__init__.py'
dst = CURR_DIR / 'napps' / '__init__.py'
src.symlink_to(dst)
requirements = [i.strip() for i in open("requirements.txt").readlines()]
setup(name='kytos-napps',
version='2017.1b3',
description='Core Napps developed by Kytos Team',
url='http://github.com/kytos/kytos-napps',
author='Kytos Team',
author_email='[email protected]',
license='MIT',
install_requires=requirements,
cmdclass={
'clean': Cleaner,
'ci': CITest,
'coverage': TestCoverage,
'develop': DevelopMode,
'install': InstallMode,
'lint': Linter,
},
zip_safe=False,
classifiers=[
'License :: OSI Approved :: MIT License',
'Operating System :: POSIX :: Linux',
'Programming Language :: Python :: 3.6',
'Topic :: System :: Networking',
])