-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsetup.py
executable file
·401 lines (331 loc) · 12.6 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import distutils.core
from setuptools import setup, find_packages, Extension
import os
import shlex
import sysconfig
import sys
import shutil
import subprocess
from os.path import dirname, abspath, join, exists
import distutils.sysconfig
import platform
def _info(s):
sys.stderr.write(s + "\n")
on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
here = abspath(dirname(__file__))
IS_MACOSX = platform.system().lower().strip() == "darwin"
IS_LINUX = platform.system().lower().strip() == "linux"
IS_WINDOWS = platform.system().lower().strip() == "XXXX"
disutils_sysconfig = distutils.sysconfig.get_config_vars()
if IS_MACOSX:
# don't build useless i386 architecture
disutils_sysconfig['LDSHARED'] = disutils_sysconfig['LDSHARED'].replace('-arch i386', '')
disutils_sysconfig['CFLAGS'] = disutils_sysconfig['CFLAGS'].replace('-arch i386', '')
# suppress painful warnings
disutils_sysconfig['CFLAGS'] = disutils_sysconfig['CFLAGS'].replace('-Wstrict-prototypes', '')
class Dependency(object):
def __init__(self):
self.name = None
self.static = True
self.thisdir = abspath(dirname(__file__))
self.external_dir = join(self.thisdir, 'includes')
if not exists(self.external_dir):
os.mkdir(self.external_dir)
self._include_dirs = None
self._library_dirs = None
self._extra_objects = None
self._install_dir = None
self._extra_link_args = None
def build(self):
raise NotImplementedError()
def extra_compile_args(self):
return []
def install_dir(self):
return self._install_dir
def include_dirs(self):
if self._include_dirs:
return self._include_dirs
return []
def extra_objects(self):
if self._extra_objects:
return self._extra_objects
return []
def library_dirs(self):
if self._library_dirs:
return self._library_dirs
return []
def extra_link_args(self):
if self._extra_link_args:
return self._extra_link_args
return []
def add_to_extension(self, ext):
ext.include_dirs.extend(self.include_dirs())
ext.library_dirs.extend(self.library_dirs())
if self.name:
ext.libraries.append(self.name)
ext.extra_objects.extend(self.extra_objects())
ext.extra_link_args.extend(self.extra_link_args())
class LibpcapDep(Dependency):
def __init__(self):
super(LibpcapDep, self).__init__()
self.src_dir = join(self.external_dir, "libpcap")
self.name = 'pcap'
self.build()
def build(self):
old_dir = os.getcwd()
self._install_dir = join(self.src_dir, 'build')
os.chdir(self.src_dir)
if exists('Makefile'):
subprocess.call(shlex.split("make clean"))
if not exists(
join(self._install_dir, 'lib', 'libpcap.dylib')
) and not exists(
join(self._install_dir, 'lib', 'libpcap.so')
):
_info("Building libpcap as a shared library\n")
subprocess.call(shlex.split("./configure --enable-bluetooth=no --prefix='%s'" % self._install_dir))
subprocess.call("make")
subprocess.call(shlex.split("make install"))
self._include_dirs = [join(self._install_dir, 'include')]
self._library_dirs = [join(self._install_dir, 'lib')]
if IS_LINUX:
self._extra_link_args = ["-Wl,-rpath,$ORIGIN/"]
shutil.copy(
join(self._install_dir, 'lib', 'libpcap.so.1'),
join(self.thisdir, 'cycapture', 'libpcap')
)
os.symlink(
join(self.thisdir, 'cycapture', 'libpcap', 'libpcap.so.1'),
join(self.thisdir, 'cycapture', 'libpcap', 'libpcap.so')
)
if IS_MACOSX:
self._extra_link_args = ["-Wl,-rpath", "-Wl,@loader_path/"]
shutil.copy(
join(self._install_dir, 'lib', 'libpcap.dylib'),
join(self.thisdir, 'cycapture', 'libpcap')
)
os.chdir(old_dir)
class LibtinsDep(Dependency):
def __init__(self, pcap_dep):
super(LibtinsDep, self).__init__()
self.pcap_dep = pcap_dep
self.name = "tins"
self.src_dir = join(self.external_dir, "libtins")
self.build()
def build(self):
old_dir = os.getcwd()
os.chdir(self.src_dir)
if not exists('build'):
os.mkdir('build')
os.chdir('build')
cmake_options = {
'CMAKE_CXX_FLAGS': "'-fPIC'",
'LIBTINS_BUILD_SHARED': "'1'",
'PCAP_ROOT_DIR': "'{}'".format(self.pcap_dep.install_dir())
}
if IS_MACOSX:
if bool(os.environ["SDKROOT"]):
# path to the macosx SDK that was used to compile python
cmake_options['CMAKE_OSX_SYSROOT'] = "'{}'".format(os.environ["SDKROOT"])
_info('CMAKE_OSX_SYSROOT: {}'.format(os.environ["SDKROOT"]))
# libtins.dylib will have install dir name using rpath
cmake_options['CMAKE_MACOSX_RPATH'] = "'true'"
_info('CMAKE_MACOSX_RPATH: true')
cmake_options = ' '.join(['-D{}={}'.format(opt_name, opt_value) for opt_name, opt_value in cmake_options.items()])
subprocess.call(shlex.split(
"cmake ../ " + cmake_options
))
subprocess.call('make')
os.chdir(old_dir)
files_to_remove = [
join(self.thisdir, 'cycapture', 'libtins', 'libtins.3.2.dylib'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.dylib'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.3.2.dylib'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.dylib'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.so'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.so.3.2'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.3.2.so')
]
for fname in files_to_remove:
try:
os.remove(fname)
except OSError:
pass
if IS_MACOSX:
shutil.copy(
join(self.src_dir, 'build', 'lib', 'libtins.3.2.dylib'),
join(self.thisdir, 'cycapture', 'libtins')
)
os.symlink(
join(self.thisdir, 'cycapture', 'libtins', 'libtins.3.2.dylib'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.dylib')
)
shutil.copy(
join(self.thisdir, 'cycapture', 'libpcap', 'libpcap.dylib'),
join(self.thisdir, 'cycapture', 'libtins')
)
self._extra_link_args = ["-Wl,-rpath", "-Wl,@loader_path/"]
if IS_LINUX:
shutil.copy(
join(self.src_dir, 'build', 'lib', 'libtins.so.3.2'),
join(self.thisdir, 'cycapture', 'libtins')
)
os.symlink(
join(self.thisdir, 'cycapture', 'libtins', 'libtins.so.3.2'),
join(self.thisdir, 'cycapture', 'libtins', 'libtins.so')
)
shutil.copy(
join(self.thisdir, 'cycapture', 'libpcap', 'libpcap.so.1'),
join(self.thisdir, 'cycapture', 'libtins')
)
self._extra_link_args = ["-Wl,-rpath,$ORIGIN/"]
self._include_dirs = [
join(self.src_dir, 'include'),
join(self.pcap_dep.install_dir(), 'include')
]
self._library_dirs = [
join(self.thisdir, 'cycapture', 'libtins'),
join(self.thisdir, 'cycapture', 'libpcap')
]
if IS_MACOSX:
python_config_vars = sysconfig.get_config_vars()
# use the same SDK as python executable
if not exists(python_config_vars['UNIVERSALSDK']):
_info("'{}' SDK does not exist. Aborting.".format(python_config_vars['UNIVERSALSDK']))
sys.exit(-1)
_info("Building for MacOSX SDK: {}".format(python_config_vars["MACOSX_DEPLOYMENT_TARGET"]))
os.environ["MACOSX_DEPLOYMENT_TARGET"] = python_config_vars["MACOSX_DEPLOYMENT_TARGET"]
os.environ["SDKROOT"] = python_config_vars["UNIVERSALSDK"]
with open('README.rst') as readme_file:
readme = readme_file.read()
with open('HISTORY.rst') as history_file:
history = history_file.read().replace('.. :changelog:', '')
requirements = ['enum34']
remove_requirements_if_rtd = []
if on_rtd:
for ext in remove_requirements_if_rtd:
requirements.remove(ext)
test_requirements = ['pytest']
extensions = []
if not on_rtd:
# utility module to make python memoryviews from char* buffers
make_mview_extension = Extension(
name="cycapture._make_mview",
sources=["cycapture/_make_mview.c"]
)
extensions.append(make_mview_extension)
pthread_extension = Extension(
name="cycapture._pthreadwrap",
sources=["cycapture/_pthreadwrap.c", "cycapture/murmur.c"]
)
extensions.append(pthread_extension)
if not IS_WINDOWS:
signal_extension = Extension(
name="cycapture._signal",
sources=["cycapture/_signal.c"]
)
extensions.append(signal_extension)
# build libpcap and the cycapture.libpcap python extension
pcap_extension = Extension(
name="cycapture.libpcap._pcap",
sources=["cycapture/libpcap/_pcap.c"]
)
libpcap_dep = LibpcapDep()
# noinspection PyTypeChecker
libpcap_dep.add_to_extension(pcap_extension)
extensions.append(pcap_extension)
tins_dep = LibtinsDep(libpcap_dep)
tins_exceptions_extension = Extension(
name="cycapture.libtins._py_exceptions",
sources=[
"cycapture/libtins/_py_exceptions.cpp",
"cycapture/libtins/custom_exception_handler.cpp"
],
language="c++"
)
# noinspection PyTypeChecker
tins_dep.add_to_extension(tins_exceptions_extension)
extensions.append(tins_exceptions_extension)
# build libtins and cycapture.libtins python extension
tins_extension = Extension(
name="cycapture.libtins._tins",
sources=[
"cycapture/libtins/_tins.cpp",
"cycapture/libtins/wrap.cpp",
"cycapture/libtins/py_tcp_stream_functor.cpp",
"cycapture/libtins/py_pdu_iterator.cpp"
],
language="c++"
)
# noinspection PyTypeChecker
tins_dep.add_to_extension(tins_extension)
extensions.append(tins_extension)
def check_cmake():
try:
subprocess.call(['cmake', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return False
return True
def check_flex():
try:
subprocess.call(['flex', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return False
return True
def check_yacc():
try:
subprocess.call(['yacc', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except OSError:
return False
return True
def run_setup():
setup(
name='cycapture',
version='0.3',
description='Cython bindings for libpcap and libtins',
long_description=readme + '\n\n' + history,
author='Stephane Martin',
author_email='stephane.martin_github@vesperal.eu',
url='https://github.com/stephane-martin/cycapture',
packages=find_packages(exclude=['tests']),
package_data={'': ['libtins.*', 'libpcap.*']},
setup_requires=[
'setuptools_git', 'setuptools', 'twine', 'wheel', 'nose'
],
include_package_data=True,
exclude_package_data={},
install_requires=requirements,
license="LGPLv3+",
zip_safe=False,
keywords='libpcap cython python libtins',
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)',
'Natural Language :: English',
'Programming Language :: Python :: 2.7',
'Environment :: Console',
'Operating System :: POSIX :: Linux'
],
entry_points={
'console_scripts': []
},
data_files=[],
test_suite='tests',
tests_require=test_requirements,
ext_modules=extensions
)
if __name__ == '__main__':
if not check_cmake():
_info("Please install cmake first")
sys.exit(1)
if not check_flex():
_info("Please install flex first")
sys.exit(1)
if not check_yacc():
_info("Please install yacc first")
sys.exit(1)
run_setup()