Skip to content

Commit bfc5b5d

Browse files
committed
Implemented graphviz search using dpkg, it should work for Ubuntu
1 parent 5bede68 commit bfc5b5d

File tree

1 file changed

+57
-17
lines changed

1 file changed

+57
-17
lines changed

setup_extra.py

+57-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
Setup helpers for PyGraphviz.
55
"""
6-
# Copyright (C) 2006-2010 by
6+
# Copyright (C) 2006-2015 by
77
# Aric Hagberg <[email protected]>
88
# Dan Schult <[email protected]>
99
# Manos Renieris, http://www.cs.brown.edu/~er/
@@ -14,6 +14,7 @@
1414
from __future__ import absolute_import
1515
import subprocess as S
1616
import sys
17+
import os
1718

1819

1920
def _b2str(buffer):
@@ -23,7 +24,7 @@ def _b2str(buffer):
2324
if not encoding:
2425
# can be run without stdout
2526
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
2728
encoding = sys.stdout.encoding
2829
else:
2930
# fall back to default encoding ( normally it should not happen)
@@ -32,23 +33,50 @@ def _b2str(buffer):
3233
result = buffer.decode(encoding)
3334
return result
3435
else:
36+
# in Python 2 conversion is implicit
3537
return buffer
3638

3739

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+
3864
def _pkg_config():
3965
# attempt to find graphviz installation with pkg-config
4066
# should work with modern versions of graphviz
41-
library_path=None
4267
include_path=None
68+
library_path=None
4369
try:
4470
output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph'])
4571
output = _b2str(output)
4672
if output:
47-
library_path=output.strip()[2:]
73+
library_path = output.strip()[2:]
74+
library_path = library_path.strip() or None
4875
output = S.check_output(['pkg-config', '--cflags-only-I', 'libcgraph'])
4976
output = _b2str(output)
5077
if output:
51-
include_path=output.strip()[2:]
78+
include_path = output.strip()[2:]
79+
include_path = include_path.strip() or None
5280
except OSError:
5381
print("Failed to find pkg-config")
5482
return include_path, library_path
@@ -65,28 +93,35 @@ def _dotneato_config():
6593
output = _b2str(output)
6694
if output:
6795
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
7098
except OSError:
7199
print("Failed to find dotneato-config")
72100
# 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:
74102
try:
75103
output = S.check_output(['dotneato-config', '--libs', '--cflags'])
76104
output = _b2str(output)
77105
if output:
78106
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
81109
except OSError:
82110
print("Failed to find dotneato-config")
83111

84112
return include_path, library_path
85113

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+
86121
def get_graphviz_dirs():
87122
"""
88123
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])
90125
"""
91126

92127
# If the setup script couldn't find your graphviz installation you can
@@ -116,13 +151,17 @@ def get_graphviz_dirs():
116151

117152
if sys.platform != "win32":
118153
# 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:
120159
print("Trying pkg-config")
121-
include_dirs, library_dirs = _pkg_config()
160+
include_dirs, library_dirs = _try_configure(include_dirs, library_dirs, _pkg_config)
122161

123-
if library_dirs is None and include_dirs is None:
162+
if library_dirs is None or include_dirs is None:
124163
print("Trying dotneato-config")
125-
include_dirs, library_dirs = _dotneato_config()
164+
include_dirs, library_dirs = _try_configure(include_dirs, library_dirs, _dotneato_config)
126165

127166
if library_dirs is None or include_dirs is None:
128167
print()
@@ -140,8 +179,9 @@ def get_graphviz_dirs():
140179
http://networkx.lanl.gov/pygraphviz/reference/faq.html
141180
142181
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/"
145185
146186
The current setting of library_dirs and include_dirs is:""")
147187
print("library_dirs=%s"%library_dirs)

0 commit comments

Comments
 (0)