forked from pynetwork/pypcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
118 lines (97 loc) · 3.26 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
#!/usr/bin/env python
#
# $Id$
from __future__ import with_statement
from setuptools import setup, Extension
from Pyrex.Distutils import build_ext
from Pyrex.Compiler import Scanning
import glob
import os
import sys
import re
# This is a hack to avoid Pyrex violating the setuptools sandbox when
# installing pypcap as a dependency.
def pickle_lexicon():
print "Skipping pickling"
Scanning.pickle_lexicon = pickle_lexicon
def recursive_search_dirs(dirs, target_files):
for d in dirs:
r = recursive_search(d, target_files)
if r:
return r
def recursive_search(path, target_files):
for root, dirs, files in os.walk(path):
for filename in files:
if filename in target_files:
return os.path.join(root, filename)
dirs = ['/usr', sys.prefix] + glob.glob('/opt/libpcap*') + \
glob.glob('../libpcap*') + glob.glob('../wpdpack*') + \
['/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/'] + \
glob.glob('/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/*')
for d in dirs:
# This makes sure that we first search inside of */include/pcap
search_dirs = [os.path.join(d, 'usr', 'include', 'pcap'),
os.path.join(d, 'include', 'pcap'),
d]
pcap_h = recursive_search_dirs(search_dirs, ['pcap.h'])
if pcap_h:
print "Found pcap headers in %s" % pcap_h
break
if not pcap_h:
print "pcap.h not found"
sys.exit(1)
include_dirs = os.path.dirname(pcap_h)
lib_sub_dirs = [os.path.join(d, sub_dir) \
for sub_dir in ('lib', 'lib64', \
'lib/x86_64-linux-gnu', 'lib/i386-linux-gnu', '')]
lib_files = [
'libpcap.a',
'libpcap.so',
'libpcap.dylib',
'wpcap.lib'
]
lib_file_path = recursive_search_dirs(lib_sub_dirs, lib_files)
print "Found libraries in %s" % lib_file_path
lib_file = os.path.basename(lib_file_path)
extra_compile_args = []
if re.match(r"libpcap\.(a|so|dylib)", lib_file):
libraries = ('pcap',)
elif lib_file == "wpcap.lib":
libraries = ('wpcap', 'iphlpapi')
extra_compile_args = ['-DWIN32', '-DWPCAP']
define_macros = []
if recursive_search_dirs(dirs, ['pcap-int.h']):
define_macros.append(('HAVE_PCAP_INT_H', 1))
else:
print "No pcap-int.h found"
pcap_h_file = open(pcap_h).readlines()
for line in pcap_h_file:
if 'pcap_file(' in line:
print "found pcap_file function"
define_macros.append(('HAVE_PCAP_FILE', 1))
if 'pcap_compile_nopcap(' in line:
print "found pcap_compile_nopcap function"
define_macros.append(('HAVE_PCAP_COMPILE_NOPCAP', 1))
if 'pcap_setnonblock(' in line:
print "found pcap_setnonblock"
define_macros.append(('HAVE_PCAP_SETNONBLOCK', 1))
pcap = Extension(
name='pcap',
sources=['pcap.pyx', 'pcap_ex.c'],
include_dirs=[include_dirs],
define_macros=define_macros,
libraries=list(libraries),
extra_compile_args=extra_compile_args,
)
setup(
name='pypcap',
version='1.1.1',
author='Dug Song',
author_email='[email protected]',
url='http://monkey.org/~dugsong/pypcap/',
description='packet capture library',
cmdclass={
'build_ext': build_ext
},
ext_modules=[pcap],
)