Create where_is utility to locate a binary in the standard places

This is particularly useful on Mac OS X where Gramps is passed a PATH
that does not include elements added by the terminal shell.
This commit is contained in:
prculley 2017-10-09 11:31:53 -05:00 committed by Nick Hall
parent 700f15be75
commit 90de264ccf
2 changed files with 21 additions and 7 deletions

View File

@ -44,7 +44,7 @@ from subprocess import Popen, PIPE
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale from ...const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
from ...utils.file import search_for from ...utils.file import search_for, where_is
from . import BaseDoc from . import BaseDoc
from ..menu import NumberOption, TextOption, EnumeratedListOption, \ from ..menu import NumberOption, TextOption, EnumeratedListOption, \
BooleanOption BooleanOption
@ -103,11 +103,7 @@ if win():
_GS_CMD = "" _GS_CMD = ""
else: else:
_DOT_FOUND = search_for("dot") _DOT_FOUND = search_for("dot")
_GS_CMD = where_is("gs")
if search_for("gs") == 1:
_GS_CMD = "gs"
else:
_GS_CMD = ""
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------

View File

@ -61,7 +61,7 @@ def find_file( filename):
try: try:
if os.path.isfile(filename): if os.path.isfile(filename):
return(filename) return(filename)
except UnicodeError: except UnicodeError as err:
LOG.error("Filename %s raised a Unicode Error %s.", repr(filename), err) LOG.error("Filename %s raised a Unicode Error %s.", repr(filename), err)
LOG.debug("Filename %s not found.", repr(filename)) LOG.debug("Filename %s not found.", repr(filename))
@ -228,6 +228,24 @@ def search_for(name):
return 1 return 1
return 0 return 0
def where_is(name):
""" This command is similar to the Linux "whereis -b file" command.
It looks for an executable file (name) in the PATH python is using, as
well as several likely other paths. It returns the first file found,
or an empty string if not found.
"""
paths = set(os.environ['PATH'].split(os.pathsep))
if not win():
paths.update(("/bin", "/usr/bin", "/usr/local/bin", "/opt/local/bin",
"/opt/bin"))
for i in paths:
fname = os.path.join(i, name)
if os.access(fname, os.X_OK) and not os.path.isdir(fname):
return fname
return ""
def create_checksum(full_path): def create_checksum(full_path):
""" """
Create a md5 hash for the given file. Create a md5 hash for the given file.