Skip to content

Commit acefe82

Browse files
committed
Detect and use PARI/GP installation prefix
1 parent 07eab50 commit acefe82

File tree

4 files changed

+76
-30
lines changed

4 files changed

+76
-30
lines changed

autogen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from os.path import join, getmtime, exists
66

77
from .generator import PariFunctionGenerator
8-
from .parser import pari_share
8+
from .paths import pari_share
99

1010

1111
def rebuild(force=False):

autogen/parser.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,7 @@
2323

2424
from .args import pari_arg_types
2525
from .ret import pari_ret_types
26-
27-
28-
def pari_share():
29-
r"""
30-
Return the directory where the PARI data files are stored.
31-
32-
EXAMPLES::
33-
34-
sage: from sage_setup.autogen.pari.parser import pari_share
35-
sage: pari_share()
36-
'.../share/pari'
37-
"""
38-
from subprocess import Popen, PIPE
39-
gp = Popen(["gp", "-f", "-q"], stdin=PIPE, stdout=PIPE)
40-
out = gp.communicate(b"print(default(datadir))")[0]
41-
datadir = out.strip()
42-
if not os.path.isdir(datadir):
43-
# As a fallback, try a path relative to the gp executable.
44-
# This is useful for broken Conda versions, see
45-
# https://github.com/conda-forge/pari-feedstock/issues/4
46-
from distutils.spawn import find_executable
47-
gppath = find_executable("gp")
48-
if gppath is not None:
49-
datadir = os.path.join(os.path.dirname(gppath), "..", "share", "pari")
50-
if not os.path.isdir(datadir):
51-
raise EnvironmentError("PARI data directory {!r} does not exist".format(datadir))
52-
return datadir
26+
from .paths import pari_share
5327

5428

5529
paren_re = re.compile(r"[(](.*)[)]")

autogen/paths.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
Find out installation paths of PARI/GP
3+
"""
4+
5+
#*****************************************************************************
6+
# Copyright (C) 2017 Jeroen Demeyer <[email protected]>
7+
#
8+
# This program is free software: you can redistribute it and/or modify
9+
# it under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation, either version 2 of the License, or
11+
# (at your option) any later version.
12+
# http://www.gnu.org/licenses/
13+
#*****************************************************************************
14+
15+
from __future__ import absolute_import
16+
17+
import os
18+
from glob import glob
19+
from distutils.spawn import find_executable
20+
21+
22+
# find_executable() returns None if nothing was found
23+
gppath = find_executable("gp") or ""
24+
if gppath is None:
25+
# This almost certainly won't work, but we need to put something here
26+
prefix = "."
27+
else:
28+
# Assume gppath is ${prefix}/bin/gp
29+
prefix = os.path.dirname(os.path.dirname(gppath))
30+
31+
32+
def pari_share():
33+
r"""
34+
Return the directory where the PARI data files are stored.
35+
36+
EXAMPLES::
37+
38+
sage: from sage_setup.autogen.pari.parser import pari_share
39+
sage: pari_share()
40+
'.../share/pari'
41+
"""
42+
from subprocess import Popen, PIPE
43+
if not gppath:
44+
raise EnvironmentError("cannot find an installation of PARI/GP: make sure that the 'gp' program is in your $PATH")
45+
gp = Popen([gppath, "-f", "-q"], stdin=PIPE, stdout=PIPE)
46+
out = gp.communicate(b"print(default(datadir))")[0]
47+
datadir = out.strip()
48+
if not os.path.isdir(datadir):
49+
# As a fallback, try a path relative to the prefix
50+
datadir = os.path.join(prefix, "share", "pari")
51+
if not os.path.isdir(datadir):
52+
raise EnvironmentError("PARI data directory {!r} does not exist".format(datadir))
53+
return datadir
54+
55+
56+
def include_dirs():
57+
"""
58+
Return the directory containing PARI include files.
59+
"""
60+
dirs = [os.path.join(prefix, "include")]
61+
return [d for d in dirs if os.path.isdir(os.path.join(d, "pari"))]
62+
63+
64+
def library_dirs():
65+
"""
66+
Return the directory containing PARI library files.
67+
"""
68+
dirs = [os.path.join(prefix, s) for s in ("lib", "lib32", "lib64")]
69+
return [d for d in dirs if glob(os.path.join(d, "libpari*"))]

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
from setuptools.command.bdist_egg import bdist_egg as _bdist_egg
2424
from setuptools.extension import Extension
2525

26+
from autogen import rebuild
27+
from autogen.paths import include_dirs, library_dirs
28+
2629

2730
# Adapted from Cython's new_build_ext
2831
class build_ext(_build_ext):
@@ -44,7 +47,6 @@ def finalize_options(self):
4447
sys.exit(1)
4548

4649
# Generate auto-generated sources from pari.desc
47-
from autogen import rebuild
4850
rebuild()
4951

5052
self.directives = dict(binding=True)
@@ -70,7 +72,8 @@ def run(self):
7072
author='Many people',
7173
author_email="[email protected]",
7274
license='GNU General Public License, version 2 or later',
73-
ext_modules=[Extension("*", ["cypari2/*.pyx"])],
75+
ext_modules=[Extension("*", ["cypari2/*.pyx"],
76+
include_dirs=include_dirs(), library_dirs=library_dirs())],
7477
keywords='PARI/GP number theory',
7578
packages=['cypari2'],
7679
package_dir={'cypari2': 'cypari2'},

0 commit comments

Comments
 (0)