-
Notifications
You must be signed in to change notification settings - Fork 10
/
setup.py
422 lines (361 loc) · 14.5 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import setuptools
import os
import sys
import distutils.spawn
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.errors import *
import pickle
__version__ = "0.8"
# Disable hard links, otherwise building distributions fails on OS X
try:
del os.link
except:
pass
# On Windows, we need ws2_32 and iphlpapi
if getattr(sys, 'getwindowsversion', None):
libraries = ['ws2_32', 'iphlpapi']
def_macros = [('WIN32', 1)]
else:
mos = getattr(sys, 'platform', None)
libraries = []
if mos.startswith('sunos'):
libraries = ['socket', 'nsl']
def_macros = []
def_macros.append(("NETIFACES_VERSION", __version__))
iface_mod = Extension('netifaces', sources=['netifaces.c'],
libraries=libraries,
define_macros=def_macros)
#
# There must be a better way to do this...
#
class my_build_ext(build_ext):
def build_extensions(self):
self.check_requirements()
build_ext.build_extensions(self)
def test_build(self, contents, link=True, execute=False, libraries=None,
include_dirs=None, library_dirs=None):
name = os.path.join(self.build_temp, 'conftest-%s.c' % self.conftestidx)
self.conftestidx += 1
if os.path.exists(name):
os.unlink(name)
thefile = open(name, 'w')
print >>thefile, contents
thefile.close()
sys.stdout.flush()
sys.stderr.flush()
mystdout = os.dup(1)
mystderr = os.dup(2)
result = True
try:
os.dup2(self.ctout, 1)
os.dup2(self.ctout, 2)
try:
objects = self.compiler.compile([name],
output_dir=self.build_temp,
include_dirs=include_dirs,
debug=self.debug)
if link:
self.compiler.link_executable(objects,
'conftest',
output_dir=self.build_temp,
library_dirs=library_dirs,
libraries=libraries,
debug=self.debug)
if execute:
abspath = os.path.abspath(os.path.join(self.build_temp,
'conftest'))
pipe = os.popen(abspath, 'r')
result = pipe.read().strip()
status = pipe.close()
if status is None:
status = 0
if result == '':
result = True
if status != 0:
result = False
finally:
os.dup2(mystdout, 1)
os.dup2(mystderr, 2)
except CompileError:
return False
except DistutilsExecError:
return False
return result
def check_requirements(self):
# Load the cached config data from a previous run if possible; compiling
# things to test for features is slow
cache_file = os.path.join(self.build_temp, 'config.cache')
if os.path.exists(cache_file):
myfile = open(cache_file, 'r')
try:
results = pickle.load(myfile)
finally:
myfile.close()
else:
results = {}
self.conftestidx = 0
print "checking for getifaddrs...",
result = results.get('have_getifaddrs', None)
if result is not None:
cached = '(cached)'
else:
cached = ''
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
outname = os.path.join(self.build_temp, 'conftest.out')
self.ctout = os.open(outname, os.O_RDWR | os.O_CREAT | os.O_TRUNC)
testrig = """
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
int main(void) {
struct ifaddrs *addrs;
int ret;
ret = getifaddrs(&addrs);
freeifaddrs (addrs);
return 0;
}
"""
if self.test_build(testrig):
result = True
else:
result = False
if result:
print "found. %s" % cached
self.compiler.define_macro('HAVE_GETIFADDRS', 1)
else:
print "not found. %s" % cached
results['have_getifaddrs'] = result
print "checking for getnameinfo...",
result = results.get('have_getnameinfo', None)
if result is not None:
cached = '(cached)'
else:
cached = ''
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
outname = os.path.join(self.build_temp, 'conftest2.out')
self.ctout = os.open(outname, os.O_RDWR | os.O_CREAT | os.O_TRUNC)
testrig = """
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
int main(void) {
struct sockaddr_in sin;
char buffer[256];
int ret;
sin.sin_family = AF_INET;
sin.sin_port = 0;
sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
ret = getnameinfo ((struct sockaddr *)&sin, sizeof (sin),
buffer, sizeof (buffer),
NULL, 0,
NI_NUMERICHOST);
return 0;
}
"""
if self.test_build(testrig,libraries=libraries):
result = True
else:
result = False
if result:
print "found. %s" % cached
self.compiler.define_macro('HAVE_GETNAMEINFO', 1)
else:
print "not found. %s" % cached
results['have_getnameinfo'] = result
if not results['have_getifaddrs']:
print "checking for socket IOCTLs...",
result = results.get('have_socket_ioctls', None)
if result is not None:
cached = '(cached)'
else:
cached = ''
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
outname = os.path.join(self.build_temp, 'conftest3.out')
self.ctout = os.open(outname, os.O_RDWR | os.O_CREAT | os.O_TRUNC)
result = []
ioctls = ('SIOCGIFCONF',
'SIOCGSIZIFCONF',
'SIOCGIFHWADDR',
'SIOCGIFADDR',
'SIOCGIFFLAGS',
'SIOCGIFDSTADDR',
'SIOCGIFBRDADDR',
'SIOCGIFNETMASK',
'SIOCGLIFNUM',
'SIOCGLIFCONF',
'SIOCGLIFFLAGS')
added_includes = ""
if mos.startswith('sunos'):
added_includes = """
#include <unistd.h>
#include <stropts.h>
#include <sys/sockio.h>
"""
for ioctl in ioctls:
testrig = """
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
%(addedinc)s
int main(void) {
int fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP);
struct ifreq ifreq;
ioctl(fd, %(ioctl)s, &ifreq);
return 0;
}
""" % { 'ioctl': ioctl , 'addedinc': added_includes}
if self.test_build(testrig,libraries=libraries):
result.append(ioctl)
if result:
print "%r. %s" % (result, cached)
for ioctl in result:
self.compiler.define_macro('HAVE_%s' % ioctl, 1)
self.compiler.define_macro('HAVE_SOCKET_IOCTLS', 1)
else:
print "not found. %s" % cached
results['have_socket_ioctls'] = result
print "checking for optional header files...",
result = results.get('have_headers', None)
if result is not None:
cached = '(cached)'
else:
cached = ''
result =[]
headers = ('net/if_dl.h', 'netash/ash.h',
'netatalk/at.h', 'netax25/ax25.h',
'neteconet/ec.h', 'netipx/ipx.h',
'netpacket/packet.h', 'netrose/rose.h',
'linux/irda.h', 'linux/atm.h',
'linux/llc.h', 'linux/tipc.h',
'linux/dn.h')
for header in headers:
testrig = """
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <%s>
int main (void) { return 0; }
""" % header
if self.test_build(testrig, link=False):
result.append(header)
if result:
print "%s. %s" % (' '.join(result), cached)
for header in result:
macro = header.upper().replace('.', '_').replace('/', '_')
self.compiler.define_macro('HAVE_%s' % macro, 1)
else:
print "none found. %s" % cached
optional_headers = result
results['have_headers'] = result
print "checking whether struct sockaddr has a length field...",
result = results.get('have_sockaddr_sa_len', None)
if result is not None:
cached = '(cached)'
else:
cached = ''
testrig = """
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
int main (void) {
struct sockaddr sa;
sa.sa_len = 5;
return 0;
}
"""
result = self.test_build(testrig)
if result:
print 'yes. %s' % cached
self.compiler.define_macro('HAVE_SOCKADDR_SA_LEN', 1)
else:
print 'no. %s' % cached
results['have_sockaddr_sa_len'] = result
if not results['have_sockaddr_sa_len']:
# GAK! On certain stupid platforms (Linux), there's no sa_len.
# Macho Linux programmers apparently think that it's not needed,
# however, unfortunately, getifaddrs() doesn't return the
# lengths, because they're in the sa_len field on just about
# everything but Linux.
print "checking which sockaddr_xxx structs are defined...",
result = results.get('have_sockaddrs', None)
if result is not None:
cached = '(cached)'
else:
cached = ''
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
outname = os.path.join(self.build_temp, 'conftest4.out')
self.ctout = os.open(outname, os.O_RDWR | os.O_CREAT | os.O_TRUNC)
sockaddrs = ('at', 'ax25', 'dl', 'eon', 'in', 'in6',
'inarp', 'ipx', 'iso', 'ns', 'un', 'x25',
'rose', 'ash', 'ec', 'll', 'atmpvc', 'atmsvc',
'dn', 'irda', 'llc')
result = []
for sockaddr in sockaddrs:
testrig = """
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <net/if.h>
#include <netinet/in.h>
%(includes)s
int main (void) {
struct sockaddr_%(sockaddr)s sa;
return 0;
}
""" % { 'includes': '\n'.join(["#include <%s>" % header
for header
in optional_headers]),
'sockaddr': sockaddr }
if self.test_build(testrig):
result.append(sockaddr)
if result:
print '%s. %s' % (' '.join(result), cached)
for sockaddr in result:
self.compiler.define_macro('HAVE_SOCKADDR_%s' \
% sockaddr.upper(), 1)
else:
print 'none! %s' % cached
results['have_sockaddrs'] = result
# Save the results to our config.cache file
myfile = open(cache_file, 'w')
try:
pickle.dump(results, myfile)
finally:
myfile.close()
# Don't bother detecting socket ioctls on Windows
if not getattr(sys, 'getwindowsversion', None):
setuptools.command.build_ext.build_ext = my_build_ext
setup (name='netifaces',
version=__version__,
description="Portable network interface information.",
license="MIT License",
long_description="""\
netifaces provides a (hopefully portable-ish) way for Python programmers to
get access to a list of the network interfaces on the local machine, and to
obtain the addresses of those network interfaces.
The package has been tested on Mac OS X, Windows XP, Windows Vista, Linux
and Solaris.
It should work on other UNIX-like systems provided they implement
either getifaddrs() or support the SIOCGIFxxx socket options, although the
data provided by the socket options is normally less complete.
""",
author='Alastair Houghton',
author_email='[email protected]',
url='http://alastairs-place.net/netifaces',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Topic :: System :: Networking',
],
ext_modules=[iface_mod])