-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
pkg_systems.py
194 lines (158 loc) · 6.78 KB
/
pkg_systems.py
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
r"""
Features for testing the presence of package systems ``sage_spkg``, ``conda``, ``pip``, ``debian``, ``fedora`` etc.
"""
# *****************************************************************************
# Copyright (C) 2021-2022 Matthias Koeppe
#
# Distributed under the terms of the GNU General Public License (GPL)
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
# *****************************************************************************
from . import Feature
class PackageSystem(Feature):
r"""
A :class:`Feature` describing a system package manager.
EXAMPLES::
sage: from sage.features.pkg_systems import PackageSystem
sage: PackageSystem('conda')
Feature('conda')
"""
def _is_present(self):
r"""
Test whether ``self`` appears in the list of available package systems.
EXAMPLES::
sage: from sage.features.pkg_systems import PackageSystem
sage: debian = PackageSystem('debian')
sage: debian.is_present() # indirect doctest, random
True
"""
from . import package_systems
return self in package_systems()
def spkg_installation_hint(self, spkgs, *, prompt=" !", feature=None):
r"""
Return a string that explains how to install ``feature``.
EXAMPLES::
sage: from sage.features.pkg_systems import PackageSystem
sage: homebrew = PackageSystem('homebrew')
sage: homebrew.spkg_installation_hint('openblas') # optional - SAGE_ROOT
'To install openblas using the homebrew package manager, you can try to run:\n!brew install openblas'
"""
if isinstance(spkgs, (tuple, list)):
spkgs = ' '.join(spkgs)
if feature is None:
feature = spkgs
return self._spkg_installation_hint(spkgs, prompt, feature)
def _spkg_installation_hint(self, spkgs, prompt, feature):
r"""
Return a string that explains how to install ``feature``.
Override this method in derived classes.
EXAMPLES::
sage: from sage.features.pkg_systems import PackageSystem
sage: fedora = PackageSystem('fedora')
sage: fedora.spkg_installation_hint('openblas') # optional - SAGE_ROOT
'To install openblas using the fedora package manager, you can try to run:\n!sudo yum install openblas-devel'
"""
from subprocess import run, CalledProcessError, PIPE
lines = []
system = self.name
try:
proc = run(f'sage-get-system-packages {system} {spkgs}',
shell=True, capture_output=True, text=True, check=True)
system_packages = proc.stdout.strip()
print_sys = f'sage-print-system-package-command {system} --verbose --sudo --prompt="{prompt}"'
command = f'{print_sys} update && {print_sys} install {system_packages}'
proc = run(command, shell=True, capture_output=True, text=True, check=True)
command = proc.stdout.strip()
if command:
lines.append(f'To install {feature} using the {system} package manager, you can try to run:')
lines.append(command)
return '\n'.join(lines)
except CalledProcessError:
pass
return f'No equivalent system packages for {system} are known to Sage.'
class SagePackageSystem(PackageSystem):
r"""
A :class:`Feature` describing the package manager of the SageMath distribution.
EXAMPLES::
sage: from sage.features.pkg_systems import SagePackageSystem
sage: SagePackageSystem()
Feature('sage_spkg')
"""
@staticmethod
def __classcall__(cls):
r"""
Normalize initargs.
TESTS::
sage: from sage.features.pkg_systems import SagePackageSystem
sage: SagePackageSystem() is SagePackageSystem() # indirect doctest
True
"""
return PackageSystem.__classcall__(cls, "sage_spkg")
def _is_present(self):
r"""
Test whether ``sage-spkg`` is available.
EXAMPLES::
sage: from sage.features.pkg_systems import SagePackageSystem
sage: bool(SagePackageSystem().is_present()) # indirect doctest, optional - sage_spkg
True
"""
from subprocess import run, DEVNULL, CalledProcessError
try:
# "sage -p" is a fast way of checking whether sage-spkg is available.
run('sage -p', shell=True, stdout=DEVNULL, stderr=DEVNULL, check=True)
except CalledProcessError:
return False
# Check if there are any installation records.
try:
from sage.misc.package import installed_packages
except ImportError:
return False
for pkg in installed_packages(exclude_pip=True):
return True
return False
def _spkg_installation_hint(self, spkgs, prompt, feature):
r"""
Return a string that explains how to install ``feature``.
EXAMPLES::
sage: from sage.features.pkg_systems import SagePackageSystem
sage: print(SagePackageSystem().spkg_installation_hint(['foo', 'bar'], prompt="### ", feature='foobarability')) # indirect doctest
To install foobarability using the Sage package manager, you can try to run:
### sage -i foo bar
"""
lines = []
lines.append(f'To install {feature} using the Sage package manager, you can try to run:')
lines.append(f'{prompt}sage -i {spkgs}')
return '\n'.join(lines)
class PipPackageSystem(PackageSystem):
r"""
A :class:`Feature` describing the Pip package manager.
EXAMPLES::
sage: from sage.features.pkg_systems import PipPackageSystem
sage: PipPackageSystem()
Feature('pip')
"""
@staticmethod
def __classcall__(cls):
r"""
Normalize initargs.
TESTS::
sage: from sage.features.pkg_systems import PipPackageSystem
sage: PipPackageSystem() is PipPackageSystem() # indirect doctest
True
"""
return PackageSystem.__classcall__(cls, "pip")
def _is_present(self):
r"""
Test whether ``pip`` is available.
EXAMPLES::
sage: from sage.features.pkg_systems import PipPackageSystem
sage: bool(PipPackageSystem().is_present()) # indirect doctest
True
"""
from subprocess import run, DEVNULL, CalledProcessError
try:
run('sage -pip --version', shell=True, stdout=DEVNULL, stderr=DEVNULL, check=True)
return True
except CalledProcessError:
return False