lib/util/fs.py
def resolve_binary_path(binpath, workingdir=None, *pathdirs):
    '''
    Resolves the binary path `binpath` to an absolute path based on the
    supplied parameters. The following rules are applied until a path is found:
    1.  If it is already an absolute path, it is returned as-is.
    2.  If a file exists at the location relative to the working directory,
        then the absolute path to that file is returned. When workingdir is
        `None`, the current working directory is used (probably '.').
    3.  For each path directory in pathdirs, apply same relative location step
        as above. Only the first match will be returned, even if there would be
        more matches. When no pathdirs are provided, directories in the PATH
        environment variable are used.
    If no path is found, this will return `None`.
    '''
    if not isinstance(binpath, str):
        raise TypeError('binpath must be a str: %r' % binpath)

    if os.path.isabs(binpath):
        logger.debug(
            'binpath already absolute, returning as-is: %r', binpath
        )
        return binpath

    if workingdir is None:
        curdir = os.curdir
        workingdir = curdir
        logger.debug('filling in current working directory: %s', curdir)

    if not pathdirs:
        rawpath = os.getenv('PATH', default=None)
        if rawpath is None:
            logger.warning('cannot read PATH environment variable, might not '
                           'be able to resolve binary paths correctly')
            # just in case, assign it a dummy iterable value too
            pathdirs = tuple()
        else:
            assert isinstance(rawpath, str), \
                '[internal] rawpath from os.getenv is not a str: %r' % rawpath
            pathdirs = rawpath.split(os.path.pathsep)

    def generate_binpaths():
        '''
        Provides an iterator for all possible absolute locations for binpath.
        Platform dependent suffixes are automatically added, if applicable.
        '''
        should_add_win_exts = os.name == 'nt'
        win_exts = ['.exe', '.cmd', '.bat']
        for pathdir in itertools.chain([workingdir], pathdirs):
            assert isinstance(pathdir, str), \
                '[internal] pathdir is not a str: %r' % pathdir
            yield os.path.join(pathdir, binpath)
            if should_add_win_exts:
                for win_ext in win_exts:
                    filebasename = '%s%s' % (binpath, win_ext)
                    yield os.path.join(pathdir, filebasename)

    found_files = filter(os.path.isfile, generate_binpaths())

    result_binpath = None
    try:
        result_binpath = next(found_files)
    except StopIteration:
        logger.debug('%s not found in %s, %s', binpath, workingdir, pathdirs)
    else:
        logger.debug('%s found at %s', binpath, result_binpath)

    return result_binpath

    Resolves the binary path `binpath` to an absolute path based on the
    supplied parameters. The following rules are applied until a path is found:
    1.  If it is already an absolute path, it is returned as-is.
    2.  If a file exists at the location relative to the working directory,
        then the absolute path to that file is returned. When workingdir is
        `None`, the current working directory is used (probably '.').
    3.  For each path directory in pathdirs, apply same relative location step
        as above. Only the first match will be returned, even if there would be
        more matches. When no pathdirs are provided, directories in the PATH
        environment variable are used.
    If no path is found, this will return `None`.
    
Resolves the binary path `binpath` to an absolute path based on the
supplied parameters. The following rules are applied until a path is found:
1.  If it is already an absolute path, it is returned as-is.
2.  If a file exists at the location relative to the working directory,
then the absolute path to that file is returned. When workingdir is
`None`, the current working directory is used (probably '.').
3.  For each path directory in pathdirs, apply same relative location step
as above. Only the first match will be returned, even if there would be
more matches. When no pathdirs are provided, directories in the PATH
environment variable are used.
If no path is found, this will return `None`.
Resolves the binary path ` binpath ` to an absolute path based on the supplied parameters . The following rules are applied until a path is found : 1 . If it is already an absolute path it is returned as - is . 2 . If a file exists at the location relative to the working directory then the absolute path to that file is returned . When workingdir is ` None ` the current working directory is used ( probably ' . ' ) . 3 . For each path directory in pathdirs apply same relative location step as above . Only the first match will be returned even if there would be more matches . When no pathdirs are provided directories in the PATH environment variable are used . If no path is found this will return ` None ` .
def resolve_binary_path(binpath, workingdir=None, *pathdirs):
    if not isinstance(binpath, str):
        raise TypeError('binpath must be a str: %r' % binpath)
    if os.path.isabs(binpath):
        logger.debug(
            'binpath already absolute, returning as-is: %r', binpath
        )
        return binpath
    if workingdir is None:
        curdir = os.curdir
        workingdir = curdir
        logger.debug('filling in current working directory: %s', curdir)
    if not pathdirs:
        rawpath = os.getenv('PATH', default=None)
        if rawpath is None:
            logger.warning('cannot read PATH environment variable, might not '
                           'be able to resolve binary paths correctly')
            pathdirs = tuple()
        else:
            assert isinstance(rawpath, str), \
                '[internal] rawpath from os.getenv is not a str: %r' % rawpath
            pathdirs = rawpath.split(os.path.pathsep)
    def generate_binpaths():
        should_add_win_exts = os.name == 'nt'
        win_exts = ['.exe', '.cmd', '.bat']
        for pathdir in itertools.chain([workingdir], pathdirs):
            assert isinstance(pathdir, str), \
                '[internal] pathdir is not a str: %r' % pathdir
            yield os.path.join(pathdir, binpath)
            if should_add_win_exts:
                for win_ext in win_exts:
                    filebasename = '%s%s' % (binpath, win_ext)
                    yield os.path.join(pathdir, filebasename)
    found_files = filter(os.path.isfile, generate_binpaths())
    result_binpath = None
    try:
        result_binpath = next(found_files)
    except StopIteration:
        logger.debug('%s not found in %s, %s', binpath, workingdir, pathdirs)
    else:
        logger.debug('%s found at %s', binpath, result_binpath)
    return result_binpath
def resolve_binary_path ( binpath , workingdir = None , * pathdirs ) : if not isinstance ( binpath , str ) : raise TypeError ( 'binpath must be a str: %r' % binpath ) if os . path . isabs ( binpath ) : logger . debug ( 'binpath already absolute, returning as-is: %r' , binpath ) return binpath if workingdir is None : curdir = os . curdir workingdir = curdir logger . debug ( 'filling in current working directory: %s' , curdir ) if not pathdirs : rawpath = os . getenv ( 'PATH' , default = None ) if rawpath is None : logger . warning ( 'cannot read PATH environment variable, might not ' 'be able to resolve binary paths correctly' ) pathdirs = tuple ( ) else : assert isinstance ( rawpath , str ) , '[internal] rawpath from os.getenv is not a str: %r' % rawpath pathdirs = rawpath . split ( os . path . pathsep ) def generate_binpaths ( ) : '''
        Provides an iterator for all possible absolute locations for binpath.
        Platform dependent suffixes are automatically added, if applicable.
        ''' should_add_win_exts = os . name == 'nt' win_exts = [ '.exe' , '.cmd' , '.bat' ] for pathdir in itertools . chain ( [ workingdir ] , pathdirs ) : assert isinstance ( pathdir , str ) , '[internal] pathdir is not a str: %r' % pathdir yield os . path . join ( pathdir , binpath ) if should_add_win_exts : for win_ext in win_exts : filebasename = '%s%s' % ( binpath , win_ext ) yield os . path . join ( pathdir , filebasename ) found_files = filter ( os . path . isfile , generate_binpaths ( ) ) result_binpath = None try : result_binpath = next ( found_files ) except StopIteration : logger . debug ( '%s not found in %s, %s' , binpath , workingdir , pathdirs ) else : logger . debug ( '%s found at %s' , binpath , result_binpath ) return result_binpath
Resolves the binary path `binpath` to an absolute path based on the
supplied parameters.
Resolves the binary path ` binpath ` to an absolute path based on the supplied parameters .
'''
    Resolves the binary path `binpath` to an absolute path based on the
    supplied parameters. The following rules are applied until a path is found:
    1.  If it is already an absolute path, it is returned as-is.
    2.  If a file exists at the location relative to the working directory,
        then the absolute path to that file is returned. When workingdir is
        `None`, the current working directory is used (probably '.').
    3.  For each path directory in pathdirs, apply same relative location step
        as above. Only the first match will be returned, even if there would be
        more matches. When no pathdirs are provided, directories in the PATH
        environment variable are used.
    If no path is found, this will return `None`.
    ''' # just in case, assign it a dummy iterable value too '''
        Provides an iterator for all possible absolute locations for binpath.
        Platform dependent suffixes are automatically added, if applicable.
        '''
binpath
workingdir