3
3
"""
4
4
Setup helpers for PyGraphviz.
5
5
"""
6
- # Copyright (C) 2006-2010 by
6
+ # Copyright (C) 2006-2015 by
7
7
# Aric Hagberg <[email protected] >
8
8
9
9
# Manos Renieris, http://www.cs.brown.edu/~er/
14
14
from __future__ import absolute_import
15
15
import subprocess as S
16
16
import sys
17
+ import os
17
18
18
19
19
20
def _b2str (buffer ):
@@ -23,7 +24,7 @@ def _b2str(buffer):
23
24
if not encoding :
24
25
# can be run without stdout
25
26
if sys .stdout and sys .stdout .encoding :
26
- # encoding is nont None only staring Python 3.2
27
+ # encoding is not None only staring Python 3.2
27
28
encoding = sys .stdout .encoding
28
29
else :
29
30
# fall back to default encoding ( normally it should not happen)
@@ -32,23 +33,50 @@ def _b2str(buffer):
32
33
result = buffer .decode (encoding )
33
34
return result
34
35
else :
36
+ # in Python 2 conversion is implicit
35
37
return buffer
36
38
37
39
40
+ def _dpkg_config ():
41
+ # attempt to find graphviz installation with pkg-config
42
+ # should work with modern versions of graphviz
43
+ include_dirs = None
44
+ library_dirs = None
45
+
46
+ try :
47
+ output = S .check_output (['dpkg' , '-S' , 'graphviz' ])
48
+ output = _b2str (output )
49
+ lines = output .split ('\n ' )
50
+ for line in lines :
51
+ if not include_dirs and line .endswith ('.h' ):
52
+ include_dirs = os .path .dirname (line .split (':' )[1 ].strip ())
53
+ include_dirs = include_dirs .strip () or None
54
+ if not library_dirs and line .endswith ('.so' ):
55
+ library_dirs = os .path .dirname (line .split (':' )[1 ].strip ())
56
+ library_dirs = library_dirs .strip () or None
57
+ if include_dirs and library_dirs :
58
+ break
59
+ except OSError :
60
+ print ("Failed to find dpkg" )
61
+ return include_dirs , library_dirs
62
+
63
+
38
64
def _pkg_config ():
39
65
# attempt to find graphviz installation with pkg-config
40
66
# should work with modern versions of graphviz
41
- library_path = None
42
67
include_path = None
68
+ library_path = None
43
69
try :
44
70
output = S .check_output (['pkg-config' , '--libs-only-L' , 'libcgraph' ])
45
71
output = _b2str (output )
46
72
if output :
47
- library_path = output .strip ()[2 :]
73
+ library_path = output .strip ()[2 :]
74
+ library_path = library_path .strip () or None
48
75
output = S .check_output (['pkg-config' , '--cflags-only-I' , 'libcgraph' ])
49
76
output = _b2str (output )
50
77
if output :
51
- include_path = output .strip ()[2 :]
78
+ include_path = output .strip ()[2 :]
79
+ include_path = include_path .strip () or None
52
80
except OSError :
53
81
print ("Failed to find pkg-config" )
54
82
return include_path , library_path
@@ -65,28 +93,35 @@ def _dotneato_config():
65
93
output = _b2str (output )
66
94
if output :
67
95
include_path , library_path = output .split ()
68
- library_path = library_path .strip ()[2 :]
69
- include_path = include_path .strip ()[2 :]
96
+ library_path = library_path .strip ()[2 :]. strip () or None
97
+ include_path = include_path .strip ()[2 :]. strip () or None
70
98
except OSError :
71
99
print ("Failed to find dotneato-config" )
72
100
# fall through and test the other syntax
73
- if not include_path and not library_path :
101
+ if not include_path or not library_path :
74
102
try :
75
103
output = S .check_output (['dotneato-config' , '--libs' , '--cflags' ])
76
104
output = _b2str (output )
77
105
if output :
78
106
include_path , library_path = output .split ('\n ' ,1 )
79
- library_path = library_path .strip ()[2 :]
80
- include_path = include_path .strip ()[2 :]
107
+ library_path = library_path or library_path .strip ()[2 :]. strip () or None
108
+ include_path = include_path or include_path .strip ()[2 :]. strip () or None
81
109
except OSError :
82
110
print ("Failed to find dotneato-config" )
83
111
84
112
return include_path , library_path
85
113
114
+ def _try_configure (include_dirs , library_dirs , try_function ):
115
+ i , l = try_function ()
116
+ i = include_dirs or i
117
+ l = library_dirs or l
118
+ return i , l
119
+
120
+
86
121
def get_graphviz_dirs ():
87
122
"""
88
123
First try to read include_dirs from
89
- :return: tuple of lists ([include_dirs], [library_dirs], [define_macros] )
124
+ :return: tuple of lists ([include_dirs], [library_dirs])
90
125
"""
91
126
92
127
# If the setup script couldn't find your graphviz installation you can
@@ -116,13 +151,17 @@ def get_graphviz_dirs():
116
151
117
152
if sys .platform != "win32" :
118
153
# Attempt to find Graphviz installation
119
- if library_dirs is None and include_dirs is None :
154
+ if library_dirs is None or include_dirs is None :
155
+ print ("Trying dpkg" )
156
+ include_dirs , library_dirs = _try_configure (include_dirs , library_dirs , _dpkg_config )
157
+
158
+ if library_dirs is None or include_dirs is None :
120
159
print ("Trying pkg-config" )
121
- include_dirs , library_dirs = _pkg_config ( )
160
+ include_dirs , library_dirs = _try_configure ( include_dirs , library_dirs , _pkg_config )
122
161
123
- if library_dirs is None and include_dirs is None :
162
+ if library_dirs is None or include_dirs is None :
124
163
print ("Trying dotneato-config" )
125
- include_dirs , library_dirs = _dotneato_config ( )
164
+ include_dirs , library_dirs = _try_configure ( include_dirs , library_dirs , _dotneato_config )
126
165
127
166
if library_dirs is None or include_dirs is None :
128
167
print ()
@@ -140,8 +179,9 @@ def get_graphviz_dirs():
140
179
http://networkx.lanl.gov/pygraphviz/reference/faq.html
141
180
142
181
If you think your installation is correct you will need to manually
143
- change the include_dirs and library_dirs variables in setup.py to
144
- point to the correct locations of your graphviz installation.
182
+ provide path to graphviz include and library. For example:
183
+
184
+ pip install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/"
145
185
146
186
The current setting of library_dirs and include_dirs is:""" )
147
187
print ("library_dirs=%s" % library_dirs )
0 commit comments