Ticket #14253: 14253_have_program.patch

File 14253_have_program.patch, 2.5 KB (added by jdemeyer, 7 years ago)
  • sage/misc/sage_ostools.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1363006402 -3600
    # Node ID 6400f8af57137e6d53f4d4462161c88cfbe6643c
    # Parent  f1e3878747300d5eb3f24e9f14c13455f3ac7984
    Use os.access() instead of "command" shell builtin
    
    diff --git a/sage/misc/sage_ostools.py b/sage/misc/sage_ostools.py
    a b  
    44
    55def have_program(program, path=None):
    66    """
    7     Return ``True`` if ``program`` is found in the path.
     7    Return ``True`` if a ``program`` executable is found in the path
     8    given by ``path``.
    89
    910    INPUT:
    1011
    1112    - ``program`` - a string, the name of the program to check.
    12     - ``path`` - string or None.  If a nonempty string, use this as
    13       the setting for the PATH environment variable.
     13
     14    - ``path`` - string or None. Paths to search for ``program``,
     15      separated by ``os.pathsep``. If ``None``, use the :envvar:`PATH`
     16      environment variable.
    1417
    1518    OUTPUT: bool
    1619
    17     This uses the shell command ``command -v``, which is Posix
    18     standard and seems to be present and behave consistently on
    19     various platforms (including Linux, BSD, Mac, Solaris, Cygwin).
    20     This is in contrast to ``which``, which does not use seem to use
    21     return codes consistently: if ``program`` does not exist, then on
    22     some platforms, ``which program`` has a return code of 0, on some
    23     a return code of 1.
    24 
    2520    EXAMPLES::
    2621
    2722        sage: from sage.misc.sage_ostools import have_program
     
    2924        True
    3025        sage: have_program('there_is_not_a_program_with_this_name')
    3126        False
     27        sage: have_program('sage', path=SAGE_ROOT)
     28        True
     29        sage: have_program('ls', path=SAGE_ROOT)
     30        False
    3231    """
    33     from subprocess import call, PIPE
    3432    import os
    35     try:
    36         if path:
    37             # env is a copy of the current environment, so modifying
    38             # it won't affect os.environ.
    39             env = os.environ.copy()
    40             env['PATH'] = path
    41             return not call('command -v %s' % program, shell=True,
    42                             stdout=PIPE, env=env)
    43         else:
    44             return not call('command -v %s' % program, shell=True,
    45                             stdout=PIPE)
    46     except OSError:
    47         return False
     33    if path is None:
     34        path = os.environ.get('PATH', "")
     35    for p in path.split(os.pathsep):
     36        try:
     37            if os.access(os.path.join(p, program), os.X_OK):
     38                return True
     39        except OSError:
     40            pass
     41    return False