Skip to content

Instantly share code, notes, and snippets.

@paveljurca
Last active July 19, 2022 16:42

Revisions

  1. paveljurca revised this gist Jul 19, 2022. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions get_bin_path.py
    Original file line number Diff line number Diff line change
    @@ -47,6 +47,6 @@ def get_bin_path(arg, opt_dirs=None, required=None):
    return bin_path


    print 'Searching in PATH: %s' % (os.environ.get('PATH', ''))
    print get_bin_path('unzip')
    print get_bin_path('zipinfo')
    print('Searching in PATH: %s' % (os.environ.get('PATH', '')))
    print(get_bin_path('unzip'))
    print(get_bin_path('zipinfo'))
  2. paveljurca revised this gist Jul 19, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions get_bin_path.py
    Original file line number Diff line number Diff line change
    @@ -49,3 +49,4 @@ def get_bin_path(arg, opt_dirs=None, required=None):

    print 'Searching in PATH: %s' % (os.environ.get('PATH', ''))
    print get_bin_path('unzip')
    print get_bin_path('zipinfo')
  3. paveljurca revised this gist Jul 19, 2022. No changes.
  4. paveljurca created this gist Jul 19, 2022.
    51 changes: 51 additions & 0 deletions get_bin_path.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # Copyright (c) 2018, Ansible Project
    # Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

    #from __future__ import (absolute_import, division, print_function)
    #__metaclass__ = type

    import os

    #from ansible.module_utils.common.file import is_executable


    def get_bin_path(arg, opt_dirs=None, required=None):
    '''
    Find system executable in PATH. Raises ValueError if executable is not found.
    Optional arguments:
    - required: [Deprecated] Prior to 2.10, if executable is not found and required is true it raises an Exception.
    In 2.10 and later, an Exception is always raised. This parameter will be removed in 2.14.
    - opt_dirs: optional list of directories to search in addition to PATH
    In addition to PATH and opt_dirs, this function also looks through /sbin, /usr/sbin and /usr/local/sbin. A lot of
    modules, especially for gathering facts, depend on this behaviour.
    If found return full path, otherwise raise ValueError.
    '''
    opt_dirs = [] if opt_dirs is None else opt_dirs

    sbin_paths = ['/sbin', '/usr/sbin', '/usr/local/sbin']
    paths = []
    for d in opt_dirs:
    if d is not None and os.path.exists(d):
    paths.append(d)
    paths += os.environ.get('PATH', '').split(os.pathsep)
    bin_path = None
    # mangle PATH to include /sbin dirs
    for p in sbin_paths:
    if p not in paths and os.path.exists(p):
    paths.append(p)
    for d in paths:
    if not d:
    continue
    path = os.path.join(d, arg)
    #if os.path.exists(path) and not os.path.isdir(path) and is_executable(path):
    if os.path.exists(path) and not os.path.isdir(path):
    bin_path = path
    break
    if bin_path is None:
    raise ValueError('Failed to find required executable "%s" in paths: %s' % (arg, os.pathsep.join(paths)))

    return bin_path


    print 'Searching in PATH: %s' % (os.environ.get('PATH', ''))
    print get_bin_path('unzip')