File: setup_extra.py

package info (click to toggle)
python-pygraphviz 1.4~rc1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 684 kB
  • ctags: 981
  • sloc: ansic: 5,061; python: 2,628; makefile: 156
file content (197 lines) | stat: -rw-r--r-- 7,036 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Setup helpers for PyGraphviz.
"""
#    Copyright (C) 2006-2015 by
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Manos Renieris, http://www.cs.brown.edu/~er/
#    Distributed with BSD license.     
#    All rights reserved, see LICENSE for details.

from __future__ import print_function
from __future__ import absolute_import
import subprocess as S
import sys
import os


def _b2str(buffer):
    result = u''
    if sys.version_info >= (3, 0):
        encoding = sys.getfilesystemencoding()
        if not encoding:
            # can be run without stdout
            if sys.stdout and sys.stdout.encoding:
                # encoding is not None only staring Python 3.2
                encoding = sys.stdout.encoding
            else:
                # fall back to default encoding ( normally it should not happen)
                encoding = 'utf8'
        if buffer:
            result = buffer.decode(encoding)
        return result
    else:
        # in Python 2 conversion is implicit
        return buffer


def _dpkg_config():
    # attempt to find graphviz installation with pkg-config
    # should work with modern versions of graphviz
    include_dirs=None
    library_dirs=None
    
    try:
        output = S.check_output(['dpkg', '-S', 'graphviz'])
        output = _b2str(output)
        lines = output.split('\n')
        for line in lines:
            if not include_dirs and line.endswith('.h'):
                include_dirs = os.path.dirname(line.split(':')[1].strip())
                include_dirs = include_dirs.strip() or None
            if not library_dirs and line.endswith('.so'):
                library_dirs = os.path.dirname(line.split(':')[1].strip())
                library_dirs = library_dirs.strip() or None
            if include_dirs and library_dirs:
                break
    except OSError:
        print("Failed to find dpkg")
    return include_dirs, library_dirs


def _pkg_config():
    # attempt to find graphviz installation with pkg-config
    # should work with modern versions of graphviz
    include_path=None
    library_path=None
    try:
        output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph'])
        output = _b2str(output)
        if output:
            library_path = output.strip()[2:]
            library_path = library_path.strip() or None
        output = S.check_output(['pkg-config', '--cflags-only-I', 'libcgraph'])
        output = _b2str(output)
        if output:
            include_path = output.strip()[2:]
            include_path = include_path.strip() or None
    except OSError:
        print("Failed to find pkg-config")
    return include_path, library_path

def _dotneato_config():
    # find graphviz configuration with dotneato-config
    # works with older versions of graphviz
    # attempt to find graphviz installation with pkg-config
    # should work with modern versions of graphviz
    library_path=None
    include_path=None
    try:
        output = S.check_output(['dotneato-config', '--ldflags', '--cflags'])
        output = _b2str(output)
        if output:
            include_path, library_path = output.split()
            library_path = library_path.strip()[2:].strip() or None
            include_path = include_path.strip()[2:].strip() or None
    except OSError:
        print("Failed to find dotneato-config")
        # fall through and test the other syntax
    if not include_path or not library_path:
        try:
            output = S.check_output(['dotneato-config', '--libs', '--cflags'])
            output = _b2str(output)
            if output:
                include_path, library_path = output.split('\n',1)
                library_path = library_path or library_path.strip()[2:].strip() or None
                include_path = include_path or include_path.strip()[2:].strip() or None
        except OSError:
            print("Failed to find dotneato-config")

    return include_path, library_path

def _try_configure(include_dirs, library_dirs, try_function):
    i, l = try_function()
    i = include_dirs or i
    l = library_dirs or l
    return i, l


def get_graphviz_dirs():
    """
    First try to read include_dirs from
    :return: tuple of lists ([include_dirs], [library_dirs])
    """

    # If the setup script couldn't find your graphviz installation you can
    # specify it here by uncommenting these lines or providing your own:
    # You must set both 'library_dirs' and 'include_dirs'

    # Linux, generic UNIX
    #library_dirs='/usr/lib/graphviz'
    #include_dirs='/usr/include/graphviz'

    # OSX, Linux, alternate location
    #library_dirs='/usr/local/lib/graphviz'
    #include_dirs='/usr/local/include/graphviz'

    # OSX (Fink)
    #library_dirs='/sw/lib/graphviz'
    #include_dirs='/sw/include/graphviz'

    # OSX (MacPorts)
    #library_dirs='/opt/local/lib/graphviz'
    #include_dirs='/opt/local/include/graphviz'

    # Windows
    # Unknown - use command line -I and -L switches to set
    include_dirs = None
    library_dirs = None

    if sys.platform != "win32":
        # Attempt to find Graphviz installation
        if library_dirs is None or include_dirs is None:
            print("Trying dpkg")
            include_dirs, library_dirs = _try_configure(include_dirs, library_dirs, _dpkg_config)

        if library_dirs is None or include_dirs is None:
            print("Trying pkg-config")
            include_dirs, library_dirs = _try_configure(include_dirs, library_dirs, _pkg_config)

        if library_dirs is None or include_dirs is None:
            print("Trying dotneato-config")
            include_dirs, library_dirs = _try_configure(include_dirs, library_dirs, _dotneato_config)

        if library_dirs is None or include_dirs is None:
            print()
            print("""Your Graphviz installation could not be found.

        1) You don't have Graphviz installed:
           Install Graphviz (http://graphviz.org)

        2) Your Graphviz package might incomplete.
           Install the binary development subpackage (e.g. libgraphviz-dev or similar.)

        3) You are using Windows
           There are no PyGraphviz binary packages for Windows but you might be
           able to build it from this source.  See
           http://networkx.lanl.gov/pygraphviz/reference/faq.html

        If you think your installation is correct you will need to manually
        provide path to graphviz include and library. For example:

        pip install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/"

        The current setting of library_dirs and include_dirs is:""")
            print("library_dirs=%s"%library_dirs)
            print("include_dirs=%s"%include_dirs)
            print()
            raise OSError("Error locating graphviz.")

    print("include_dirs=%s" % include_dirs)
    print("library_dirs=%s" % library_dirs)


    return include_dirs, library_dirs