From 90de264ccf89578888c9c653c774632cde545b49 Mon Sep 17 00:00:00 2001 From: prculley Date: Mon, 9 Oct 2017 11:31:53 -0500 Subject: [PATCH] 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. --- gramps/gen/plug/docgen/graphdoc.py | 8 ++------ gramps/gen/utils/file.py | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/gramps/gen/plug/docgen/graphdoc.py b/gramps/gen/plug/docgen/graphdoc.py index b608a24a3..31da27655 100644 --- a/gramps/gen/plug/docgen/graphdoc.py +++ b/gramps/gen/plug/docgen/graphdoc.py @@ -44,7 +44,7 @@ from subprocess import Popen, PIPE #------------------------------------------------------------------------- from ...const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext -from ...utils.file import search_for +from ...utils.file import search_for, where_is from . import BaseDoc from ..menu import NumberOption, TextOption, EnumeratedListOption, \ BooleanOption @@ -103,11 +103,7 @@ if win(): _GS_CMD = "" else: _DOT_FOUND = search_for("dot") - - if search_for("gs") == 1: - _GS_CMD = "gs" - else: - _GS_CMD = "" + _GS_CMD = where_is("gs") #------------------------------------------------------------------------------ diff --git a/gramps/gen/utils/file.py b/gramps/gen/utils/file.py index c819059a4..02bcb5c5e 100644 --- a/gramps/gen/utils/file.py +++ b/gramps/gen/utils/file.py @@ -61,7 +61,7 @@ def find_file( filename): try: if os.path.isfile(filename): return(filename) - except UnicodeError: + except UnicodeError as err: LOG.error("Filename %s raised a Unicode Error %s.", repr(filename), err) LOG.debug("Filename %s not found.", repr(filename)) @@ -228,6 +228,24 @@ def search_for(name): return 1 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): """ Create a md5 hash for the given file.