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 d98db27dcc
commit da1c942509
2 changed files with 24 additions and 9 deletions

View File

@ -44,7 +44,7 @@ import sys
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
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,13 +103,10 @@ if win():
DETACHED_PROCESS = DWORD(0x00000008).value DETACHED_PROCESS = DWORD(0x00000008).value
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 = ""
#------------------------------------------------------------------------------- #------------------------------------------------------------------------------
# #
# GVOptions # GVOptions
# #

View File

@ -62,7 +62,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))
@ -185,6 +185,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.