44from __future__ import print_function
55
66from distutils .core import setup , Extension
7+ import sys
8+
79try :
810 import commands
911except ImportError :
1012 import subprocess as commands
11- import sys
1213
1314version = '0.12'
1415
15- def pkgconfig (pkg ):
16- def _str2list (pkgstr , onlystr ):
16+
17+ class PkgConfigExtension (Extension ):
18+ '''Extension with lazy properties taken from pkg-config'''
19+ def __init__ (self , * args , ** kwargs ):
20+ '''Behaves like Extension's __init__ but you should not use
21+ include_dirs, library_dirs and libraries arguments.
22+
23+ Extra arguments:
24+ pkg : string
25+ The name of the pkg-config package to use for querying
26+ extra_libraris : [string]
27+ This will be added to the libraries attribute. Optional.
28+ '''
29+ self ._pkg = kwargs ['pkg' ]
30+ del kwargs ['pkg' ]
31+ if 'extra_libraries' in kwargs :
32+ self ._extra_libraries = kwargs ['extra_libraries' ]
33+ del kwargs ['extra_libraries' ]
34+ else :
35+ self ._extra_libraries = []
36+
37+ Extension .__init__ (self , * args , ** kwargs )
38+
39+ try :
40+ # on Python 2 we need to delete those now
41+ del self .include_dirs
42+ del self .library_dirs
43+ del self .libraries
44+ except AttributeError :
45+ # on Python 3, that's not needed or possible
46+ pass
47+
48+ @classmethod
49+ def _str2list (cls , pkgstr , onlystr ):
1750 res = []
1851 for l in pkgstr .split (" " ):
1952 if l .find (onlystr ) == 0 :
2053 res .append (l .replace (onlystr , "" , 1 ))
2154 return res
2255
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 )
56+ @classmethod
57+ def _run (cls , command_string ):
58+ res , output = commands .getstatusoutput (command_string )
59+ if res != 0 :
60+ print ('Failed to query %s' % command_string )
61+ sys .exit (1 )
62+ return output
2763
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 )
64+ @ property
65+ def include_dirs ( self ) :
66+ includes = self . _run ( ' pkg-config --cflags-only-I %s' % self . _pkg )
67+ return self . _str2list ( includes , '-I' )
3268
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 )
69+ @ property
70+ def library_dirs ( self ) :
71+ libdirs = self . _run ( ' pkg-config --libs-only-L %s' % self . _pkg )
72+ return self . _str2list ( libdirs , '-L' )
3773
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 )
74+ @ property
75+ def libraries ( self ) :
76+ libs = self . _run ( ' pkg-config --libs-only-l %s' % self . _pkg )
77+ return self . _str2list ( libs , '-l' ) + self . _extra_libraries
4278
79+ @include_dirs .setter
80+ def include_dirs (self , value ):
81+ pass
4382
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- }
83+ @library_dirs .setter
84+ def library_dirs (self , value ):
85+ pass
5086
87+ @libraries .setter
88+ def libraries (self , value ):
89+ pass
5190
52- libnl = pkgconfig ('libnl-3.0' )
53- libnl ['libs' ].append ('nl-route-3' )
5491
5592with open ('README.rst' ) as f :
5693 long_description = f .read ()
@@ -86,7 +123,7 @@ def _str2list(pkgstr, onlystr):
86123 'Topic :: System :: Networking' ,
87124 ],
88125 ext_modules = [
89- Extension (
126+ PkgConfigExtension (
90127 'ethtool' ,
91128 sources = [
92129 'python-ethtool/ethtool.c' ,
@@ -95,10 +132,9 @@ def _str2list(pkgstr, onlystr):
95132 'python-ethtool/netlink.c' ,
96133 'python-ethtool/netlink-address.c' ],
97134 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- ]
135+ define_macros = [('VERSION' , '"%s"' % version )],
136+ pkg = 'libnl-3.0' ,
137+ extra_libraries = ['nl-route-3' ],
138+ )
139+ ]
104140)
0 commit comments