File: paths.py

package info (click to toggle)
cypari2 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 908 kB
  • sloc: python: 972; makefile: 34; ansic: 5
file content (77 lines) | stat: -rw-r--r-- 2,656 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
"""
Find out installation paths of PARI/GP
"""

#*****************************************************************************
#       Copyright (C) 2017 Jeroen Demeyer <jdemeyer@cage.ugent.be>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#                  http://www.gnu.org/licenses/
#*****************************************************************************

from __future__ import absolute_import, unicode_literals

import os
from glob import glob
from distutils.spawn import find_executable


# find_executable() returns None if nothing was found
gppath = find_executable("gp")
if gppath is None:
    # This almost certainly won't work, but we need to put something here
    prefix = "."
else:
    # Assume gppath is ${prefix}/bin/gp
    prefix = os.path.dirname(os.path.dirname(gppath))


def pari_share():
    r"""
    Return the directory where the PARI data files are stored.

    EXAMPLES::

        >>> import os
        >>> from autogen.parser import pari_share
        >>> os.path.isfile(os.path.join(pari_share(), "pari.desc"))
        True
    """
    from subprocess import Popen, PIPE
    if not gppath:
        raise EnvironmentError("cannot find an installation of PARI/GP: make sure that the 'gp' program is in your $PATH")
    # Ignore GP_DATA_DIR environment variable
    env = dict(os.environ)
    env.pop("GP_DATA_DIR", None)
    gp = Popen([gppath, "-f", "-q"], stdin=PIPE, stdout=PIPE, env=env)
    out = gp.communicate(b"print(default(datadir))")[0]
    # Convert out to str if needed
    if not isinstance(out, str):
        from sys import getfilesystemencoding
        out = out.decode(getfilesystemencoding(), "surrogateescape")
    datadir = out.strip()
    if not os.path.isdir(datadir):
        # As a fallback, try a path relative to the prefix
        datadir = os.path.join(prefix, "share", "pari")
        if not os.path.isdir(datadir):
            raise EnvironmentError("PARI data directory {!r} does not exist".format(datadir))
    return datadir


def include_dirs():
    """
    Return a list of directories containing PARI include files.
    """
    dirs = [os.path.join(prefix, "include")]
    return [d for d in dirs if os.path.isdir(os.path.join(d, "pari"))]


def library_dirs():
    """
    Return a list of directories containing PARI library files.
    """
    dirs = [os.path.join(prefix, s) for s in ("lib", "lib32", "lib64")]
    return [d for d in dirs if glob(os.path.join(d, "libpari*"))]