Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
sage.features.Executable.absolute_path: New
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed Mar 1, 2022
1 parent 1e8ba0a commit 32ce831
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/sage/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
As can be seen above, features try to produce helpful error messages.
"""

from __future__ import annotations

import os
import shutil

Expand Down Expand Up @@ -461,8 +463,11 @@ def _is_present(self):
sage: Executable(name="sh", executable="sh").is_present()
FeatureTestResult('sh', True)
"""
if shutil.which(self.executable) is None:
return FeatureTestResult(self, False, "Executable {executable!r} not found on PATH.".format(executable=self.executable))
try:
abspath = self.absolute_path()
return FeatureTestResult(self, True, reason="Found at `{abspath}`.".format(abspath=abspath))
except FeatureNotPresentError as e:
return FeatureTestResult(self, False, reason=e.reason, resolution=e.resolution)
return self.is_functional()

def is_functional(self):
Expand All @@ -479,6 +484,31 @@ def is_functional(self):
"""
return FeatureTestResult(self, True)

def absolute_path(self) -> str:
r"""
The absolute path of the executable.
EXAMPLES::
sage: from sage.features import Executable
sage: Executable(name="sh", executable="sh").absolute_path()
'/bin/sh'
A :class:`FeatureNotPresentError` is raised if the file cannot be found::
sage: Executable(name="does-not-exist", executable="does-not-exist-xxxxyxyyxyy").absolute_path()
Traceback (most recent call last):
...
sage.features.FeatureNotPresentError: does-not-exist is not available.
Executable 'does-not-exist-xxxxyxyyxyy' not found on PATH.
"""
path = shutil.which(self.executable)
if path is not None:
return path
raise FeatureNotPresentError(self,
reason="Executable {executable!r} not found on PATH.".format(executable=self.executable),
resolution=self.resolution())


class StaticFile(Feature):
r"""
Expand Down Expand Up @@ -525,7 +555,7 @@ def _is_present(self):
except FeatureNotPresentError as e:
return FeatureTestResult(self, False, reason=e.reason, resolution=e.resolution)

def absolute_path(self):
def absolute_path(self) -> str:
r"""
The absolute path of the file.
Expand All @@ -541,7 +571,7 @@ def absolute_path(self):
sage: feature.absolute_path() == file_path
True
A ``FeatureNotPresentError`` is raised if the file cannot be found::
A :class:`FeatureNotPresentError` is raised if the file cannot be found::
sage: from sage.features import StaticFile
sage: StaticFile(name="no_such_file", filename="KaT1aihu", search_path=(), spkg="some_spkg", url="http://rand.om").absolute_path() # optional - sage_spkg
Expand Down

0 comments on commit 32ce831

Please sign in to comment.