diff --git a/tests/test_aliases.py b/tests/test_aliases.py index 00695b769..2d6b8f8a1 100644 --- a/tests/test_aliases.py +++ b/tests/test_aliases.py @@ -85,7 +85,7 @@ class TestWhich: arg = 'whichtestapp1' matches = list(_which.whichgen(arg, path=[testdir])) assert len(matches) == 1 - assert self._file_match(matches[0], os.path.join(testdir, arg)) + assert self._file_match(matches[0][0], os.path.join(testdir, arg)) def test_whichgen_failure(self): testdir = self.testdirs[0].name @@ -108,8 +108,8 @@ class TestWhich: arg = 'whichtestapp1' matches = list(_which.whichgen(arg, path=[testdir0, testdir1])) assert len(matches) == 2 - assert self._file_match(matches[0], os.path.join(testdir0, arg)) - assert self._file_match(matches[1], os.path.join(testdir1, arg)) + assert self._file_match(matches[0][0], os.path.join(testdir0, arg)) + assert self._file_match(matches[1][0], os.path.join(testdir1, arg)) if ON_WINDOWS: def test_whichgen_ext_failure(self): @@ -123,7 +123,7 @@ class TestWhich: arg = 'whichtestapp2' matches = list(_which.whichgen(arg, path=[testdir], exts = ['.wta'])) assert len(matches) == 1 - assert self._file_match(matches[0], os.path.join(testdir, arg)) + assert self._file_match(matches[0][0], os.path.join(testdir, arg)) def _file_match(self, path1, path2): if ON_WINDOWS: diff --git a/xonsh/aliases.py b/xonsh/aliases.py index f272f4aa8..3581b1241 100644 --- a/xonsh/aliases.py +++ b/xonsh/aliases.py @@ -357,11 +357,15 @@ def which(args, stdin=None, stdout=None, stderr=None): help='Show all matches in $PATH and xonsh.aliases') parser.add_argument('-s', '--skip-alias', action='store_true', help='Do not search in xonsh.aliases', dest='skip') - parser.add_argument('-V', '--version', action='version', - version='{}'.format(_which.__version__)) - parser.add_argument('-v', '--verbose', action='store_true', dest='verbose') + parser.add_argument('-V', '-v', '--version', action='version', + version='{}'.format(_which.__version__), + help='Display the version of the python which module ' + 'used by xonsh') + parser.add_argument('--verbose', action='store_true', dest='verbose', + help='Show extra information on for example near misses') parser.add_argument('-p', '--plain', action='store_true', dest='plain', - help='Do not display alias expansions') + help='Do not display alias expansions or location of ' + 'where binaries are found.') parser.add_argument('--very-small-rocks', action=AWitchAWitch) if ON_WINDOWS: parser.add_argument('-e', '--exts', nargs='*', type=str, @@ -377,7 +381,7 @@ def which(args, stdin=None, stdout=None, stderr=None): parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) - + if ON_WINDOWS: if pargs.exts: exts = pargs.exts @@ -398,9 +402,9 @@ def which(args, stdin=None, stdout=None, stderr=None): nmatches += 1 if not pargs.all: continue - for match in _which.whichgen(arg, path=builtins.__xonsh_env__['PATH'], - exts=exts, verbose=pargs.verbose): - abs_name, from_where = match if pargs.verbose else (match, '') + macthes = _which.whichgen(arg, exts=exts, verbose=pargs.verbose, + path=builtins.__xonsh_env__['PATH']) + for abs_name, from_where in macthes: if ON_WINDOWS: # Use list dir to get correct case for the filename # i.e. windows is case insesitive but case preserving @@ -409,8 +413,8 @@ def which(args, stdin=None, stdout=None, stderr=None): abs_name = os.path.join(p, f) if builtins.__xonsh_env__.get('FORCE_POSIX_PATHS', False): abs_name.replace(os.sep, os.altsep) - if pargs.verbose: - print('{} ({})'.format(abs_name, from_where), file=stdout) + if not pargs.plain: + print('{} -> ({})'.format(abs_name, from_where), file=stdout) else: print(abs_name, file=stdout) nmatches += 1 diff --git a/xonsh/xoreutils/_which.py b/xonsh/xoreutils/_which.py index 9f5f4e516..e08b76cd0 100644 --- a/xonsh/xoreutils/_which.py +++ b/xonsh/xoreutils/_which.py @@ -84,8 +84,7 @@ _cmdlnUsage = """ files without executable access. """ -__revision__ = "$Id: which.py 1448 2007-02-28 19:13:06Z trentm $" -__version_info__ = (1, 1, 3) +__version_info__ = (1, 2, 0) __version__ = '.'.join(map(str, __version_info__)) __all__ = ["which", "whichall", "whichgen", "WhichError"] @@ -177,8 +176,7 @@ def whichgen(command, path=None, verbose=0, exts=None): not a VisualBasic script but ".vbs" is on PATHEXT. This option is only supported on Windows. - This method returns a generator which yields either full paths to - the given command or, if verbose, tuples of the form (, ). """ matches = [] @@ -214,10 +212,7 @@ def whichgen(command, path=None, verbose=0, exts=None): if os.sep in command or os.altsep and os.altsep in command: if os.path.exists(command): match = _cull((command, "explicit path given"), matches, verbose) - if verbose: - yield match - else: - yield match[0] + yield match else: for i in range(len(path)): dirName = path[i] @@ -239,18 +234,12 @@ def whichgen(command, path=None, verbose=0, exts=None): fromWhere = "from PATH element %d" % (i-1) match = _cull((absName, fromWhere), matches, verbose) if match: - if verbose: - yield match - else: - yield match[0] + yield match match = _getRegisteredExecutable(command) if match is not None: match = _cull(match, matches, verbose) if match: - if verbose: - yield match - else: - yield match[0] + yield match def which(command, path=None, verbose=0, exts=None): @@ -272,10 +261,13 @@ def which(command, path=None, verbose=0, exts=None): If no match is found for the command, a WhichError is raised. """ try: - match = whichgen(command, path, verbose, exts).next() + absName, fromWhere = whichgen(command, path, verbose, exts).next() except StopIteration: raise WhichError("Could not find '%s' on the path." % command) - return match + if verbose: + return absName, fromWhere + else: + return absName def whichall(command, path=None, verbose=0, exts=None): @@ -295,7 +287,10 @@ def whichall(command, path=None, verbose=0, exts=None): not a VisualBasic script but ".vbs" is on PATHEXT. This option is only supported on Windows. """ - return list( whichgen(command, path, verbose, exts) ) + if verbose: + return list( whichgen(command, path, verbose, exts) ) + else: + return list( absName for absName, _ in whichgen(command, path, verbose, exts)) @@ -345,11 +340,11 @@ def main(argv): for arg in args: #print "debug: search for %r" % arg nmatches = 0 - for match in whichgen(arg, path=altpath, verbose=verbose, exts=exts): + for absName, fromWhere in whichgen(arg, path=altpath, verbose=verbose, exts=exts): if verbose: - print( "%s (%s)" % match) + print( "%s (%s)" % (absName, fromWhere)) else: - print(match) + print(absName) nmatches += 1 if not all: break