Skip to content

Commit fd2bff3

Browse files
committed
Allow lazy evalution of some of the Extension's attributes
Fixes #12
1 parent f3447f7 commit fd2bff3

File tree

1 file changed

+88
-46
lines changed

1 file changed

+88
-46
lines changed

setup.py

Lines changed: 88 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,89 @@
1212

1313
version = '0.12'
1414

15-
def pkgconfig(pkg):
16-
def _str2list(pkgstr, onlystr):
17-
res = []
18-
for l in pkgstr.split(" "):
19-
if l.find(onlystr) == 0:
20-
res.append(l.replace(onlystr, "", 1))
21-
return res
22-
23-
(res, cflags) = commands.getstatusoutput('pkg-config --cflags-only-other %s' % pkg)
24-
if res != 0:
25-
print('Failed to query pkg-config --cflags-only-other %s' % pkg)
26-
sys.exit(1)
27-
28-
(res, includes) = commands.getstatusoutput('pkg-config --cflags-only-I %s' % pkg)
29-
if res != 0:
30-
print('Failed to query pkg-config --cflags-only-I %s' % pkg)
31-
sys.exit(1)
32-
33-
(res, libs) = commands.getstatusoutput('pkg-config --libs-only-l %s' % pkg)
34-
if res != 0:
35-
print('Failed to query pkg-config --libs-only-l %s' % pkg)
36-
sys.exit(1)
37-
38-
(res, libdirs) = commands.getstatusoutput('pkg-config --libs-only-L %s' % pkg)
39-
if res != 0:
40-
print('Failed to query pkg-config --libs-only-L %s' % pkg)
41-
sys.exit(1)
42-
43-
44-
# Clean up the results and return what we've extracted from pkg-config
45-
return {'cflags': cflags,
46-
'include': _str2list(includes, '-I'),
47-
'libs': _str2list(libs, '-l'),
48-
'libdirs': _str2list(libdirs, '-L')
49-
}
50-
51-
52-
libnl = pkgconfig('libnl-3.0')
53-
libnl['libs'].append('nl-route-3')
15+
16+
class PkgConfigExtension(Extension):
17+
'''Extension with lazy properties taken from pkg-config'''
18+
def __init__(self, *args, **kwargs):
19+
pkg = kwargs['pkg']
20+
del kwargs['pkg']
21+
Extension.__init__(self, *args, **kwargs)
22+
try:
23+
# on Python 2 we need to delete those now
24+
del self.include_dirs
25+
del self.library_dirs
26+
del self.libraries
27+
except AttributeError:
28+
# on Python 3, that's not needed or possible
29+
pass
30+
self._pkg = pkg
31+
self._pkgconfig_result = None
32+
33+
@property
34+
def pkgconfig(self):
35+
if self._pkgconfig_result is None:
36+
self._pkgconfig_result = self._pkgconfig(self._pkg)
37+
return self._pkgconfig_result
38+
39+
def _pkgconfig(self, pkg):
40+
def _str2list(pkgstr, onlystr):
41+
res = []
42+
for l in pkgstr.split(" "):
43+
if l.find(onlystr) == 0:
44+
res.append(l.replace(onlystr, "", 1))
45+
return res
46+
47+
(res, cflags) = commands.getstatusoutput('pkg-config --cflags-only-other %s' % pkg)
48+
if res != 0:
49+
print('Failed to query pkg-config --cflags-only-other %s' % pkg)
50+
sys.exit(1)
51+
52+
(res, includes) = commands.getstatusoutput('pkg-config --cflags-only-I %s' % pkg)
53+
if res != 0:
54+
print('Failed to query pkg-config --cflags-only-I %s' % pkg)
55+
sys.exit(1)
56+
57+
(res, libs) = commands.getstatusoutput('pkg-config --libs-only-l %s' % pkg)
58+
if res != 0:
59+
print('Failed to query pkg-config --libs-only-l %s' % pkg)
60+
sys.exit(1)
61+
62+
(res, libdirs) = commands.getstatusoutput('pkg-config --libs-only-L %s' % pkg)
63+
if res != 0:
64+
print('Failed to query pkg-config --libs-only-L %s' % pkg)
65+
sys.exit(1)
66+
67+
# Clean up the results and return what we've extracted from pkg-config
68+
return {'cflags': cflags,
69+
'include': _str2list(includes, '-I'),
70+
'libs': _str2list(libs, '-l'),
71+
'libdirs': _str2list(libdirs, '-L')
72+
}
73+
74+
@property
75+
def include_dirs(self):
76+
return self.pkgconfig['include']
77+
78+
@property
79+
def library_dirs(self):
80+
return self.pkgconfig['libdirs']
81+
82+
@property
83+
def libraries(self):
84+
return self.pkgconfig['libs'] + ['nl-route-3']
85+
86+
@include_dirs.setter
87+
def include_dirs(self, value):
88+
pass
89+
90+
@library_dirs.setter
91+
def library_dirs(self, value):
92+
pass
93+
94+
@libraries.setter
95+
def libraries(self, value):
96+
pass
97+
5498

5599
with open('README.rst') as f:
56100
long_description = f.read()
@@ -86,7 +130,7 @@ def _str2list(pkgstr, onlystr):
86130
'Topic :: System :: Networking',
87131
],
88132
ext_modules=[
89-
Extension(
133+
PkgConfigExtension(
90134
'ethtool',
91135
sources = [
92136
'python-ethtool/ethtool.c',
@@ -95,10 +139,8 @@ def _str2list(pkgstr, onlystr):
95139
'python-ethtool/netlink.c',
96140
'python-ethtool/netlink-address.c'],
97141
extra_compile_args=['-fno-strict-aliasing'],
98-
include_dirs = libnl['include'],
99-
library_dirs = libnl['libdirs'],
100-
libraries = libnl['libs'],
101-
define_macros = [('VERSION', '"%s"' % version)]
102-
)
103-
]
142+
define_macros = [('VERSION', '"%s"' % version)],
143+
pkg = 'libnl-3.0',
144+
)
145+
]
104146
)

0 commit comments

Comments
 (0)