def test_whichgen_multiple(self): testdir0 = self.testdirs[0].name testdir1 = self.testdirs[1].name arg = "whichtestapp1" matches = list(_which.whichgen(arg, path=[testdir0, testdir1], exts=self.exts)) assert len(matches) == 2 assert self._file_match(matches[0][0], os.path.join(testdir0, arg)) assert self._file_match(matches[1][0], os.path.join(testdir1, arg))
def test_whichgen_verbose(self): testdir = self.testdirs[0].name arg = "whichtestapp1" matches = list( _which.whichgen(arg, path=[testdir], exts=self.exts, verbose=True)) assert len(matches) == 1 match, from_where = matches[0] assert self._file_match(match, os.path.join(testdir, arg)) assert from_where == "from given path element 0"
def test_whichgen_verbose(self): testdir = self.testdirs[0].name arg = 'whichtestapp1' matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts, verbose=True)) assert len(matches) == 1 match, from_where = matches[0] assert self._file_match(match, os.path.join(testdir, arg)) assert from_where == 'from given path element 0'
def test_whichgen(self): testdir = self.testdirs[0].name arg = "whichtestapp1" matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts)) assert len(matches) == 1 assert self._file_match(matches[0][0], os.path.join(testdir, arg))
def which(args, stdin=None, stdout=None, stderr=None): """ Checks if each arguments is a xonsh aliases, then if it's an executable, then finally return an error code equal to the number of misses. If '-a' flag is passed, run both to return both `xonsh` match and `which` match. """ desc = "Parses arguments to which wrapper" parser = argparse.ArgumentParser('which', description=desc) parser.add_argument('args', type=str, nargs='+', help='The executables or aliases to search for') parser.add_argument('-a','--all', action='store_true', dest='all', 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__), help='Display the version of the python which module ' 'used by xonsh') parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', help='Print out how matches were located and show ' 'near misses on stderr') parser.add_argument('-p', '--plain', action='store_true', dest='plain', help='Do not display alias expansions or location of ' 'where binaries are found. This is the ' 'default behavior, but the option can be used to ' 'override the --verbose option') parser.add_argument('--very-small-rocks', action=AWitchAWitch) if ON_WINDOWS: parser.add_argument('-e', '--exts', nargs='*', type=str, help='Specify a list of extensions to use instead ' 'of the standard list for this system. This can ' 'effectively be used as an optimization to, for ' 'example, avoid stat\'s of "foo.vbs" when ' 'searching for "foo" and you know it is not a ' 'VisualBasic script but ".vbs" is on PATHEXT. ' 'This option is only supported on Windows', dest='exts') if len(args) == 0: parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) if pargs.all: pargs.verbose = True if ON_WINDOWS: if pargs.exts: exts = pargs.exts else: exts = builtins.__xonsh_env__['PATHEXT'] else: exts = None failures = [] for arg in pargs.args: nmatches = 0 # skip alias check if user asks to skip if (arg in builtins.aliases and not pargs.skip): if pargs.plain or not pargs.verbose: if isinstance(builtins.aliases[arg], list): print(' '.join(builtins.aliases[arg]), file=stdout) else: print(arg, file=stdout) else: print("aliases['{}'] = {}".format(arg, builtins.aliases[arg]), file=stdout) nmatches += 1 if not pargs.all: continue # which.whichgen gives the nicest 'verbose' output if PATH is taken # from os.environ so we temporarily override it with # __xosnh_env__['PATH'] original_os_path = os.environ['PATH'] os.environ['PATH'] = builtins.__xonsh_env__.detype()['PATH'] matches = _which.whichgen(arg, exts=exts, verbose=pargs.verbose) for abs_name, from_where in matches: if ON_WINDOWS: # Use list dir to get correct case for the filename # i.e. windows is case insensitive but case preserving p, f = os.path.split(abs_name) f = next(s.name for s in scandir(p) if s.name.lower() == f.lower()) 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.plain or not pargs.verbose: print(abs_name, file=stdout) else: print('{} ({})'.format(abs_name, from_where), file=stdout) nmatches += 1 if not pargs.all: break os.environ['PATH'] = original_os_path if not nmatches: failures.append(arg) if len(failures) == 0: return 0 else: print('{} not in $PATH'.format(', '.join(failures)), file=stderr, end='') if not pargs.skip: print(' or xonsh.builtins.aliases', file=stderr, end='') print('', end='\n') return len(failures)
def test_whichgen_failure(self): testdir = self.testdirs[0].name arg = 'not_a_file' matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts)) assert len(matches) == 0
def which(args, stdin=None, stdout=None, stderr=None): """ Checks if each arguments is a xonsh aliases, then if it's an executable, then finally return an error code equal to the number of misses. If '-a' flag is passed, run both to return both `xonsh` match and `which` match. """ desc = "Parses arguments to which wrapper" parser = ArgumentParser("which", description=desc) parser.add_argument("args", type=str, nargs="+", help="The executables or aliases to search for") parser.add_argument( "-a", "--all", action="store_true", dest="all", 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__), help="Display the version of the python which module " "used by xonsh", ) parser.add_argument( "-v", "--verbose", action="store_true", dest="verbose", help="Print out how matches were located and show " "near misses on stderr", ) parser.add_argument( "-p", "--plain", action="store_true", dest="plain", help="Do not display alias expansions or location of " "where binaries are found. This is the " "default behavior, but the option can be used to " "override the --verbose option", ) parser.add_argument("--very-small-rocks", action=AWitchAWitch) if ON_WINDOWS: parser.add_argument( "-e", "--exts", nargs="*", type=str, help="Specify a list of extensions to use instead " "of the standard list for this system. This can " "effectively be used as an optimization to, for " 'example, avoid stat\'s of "foo.vbs" when ' 'searching for "foo" and you know it is not a ' 'VisualBasic script but ".vbs" is on PATHEXT. ' "This option is only supported on Windows", dest="exts", ) if len(args) == 0: parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) if pargs.all: pargs.verbose = True if ON_WINDOWS: if pargs.exts: exts = pargs.exts else: exts = builtins.__xonsh_env__.get("PATHEXT", [".COM", ".EXE", ".BAT"]) else: exts = None failures = [] for arg in pargs.args: nmatches = 0 # skip alias check if user asks to skip if arg in builtins.aliases and not pargs.skip: if pargs.plain or not pargs.verbose: if isinstance(builtins.aliases[arg], list): print(" ".join(builtins.aliases[arg]), file=stdout) else: print(arg, file=stdout) else: print("aliases['{}'] = {}".format(arg, builtins.aliases[arg]), file=stdout) nmatches += 1 if not pargs.all: continue # which.whichgen gives the nicest 'verbose' output if PATH is taken # from os.environ so we temporarily override it with # __xosnh_env__['PATH'] original_os_path = os.environ["PATH"] os.environ["PATH"] = builtins.__xonsh_env__.detype()["PATH"] matches = _which.whichgen(arg, exts=exts, verbose=pargs.verbose) os.environ["PATH"] = original_os_path for abs_name, from_where in matches: if ON_WINDOWS: # Use list dir to get correct case for the filename # i.e. windows is case insensitive but case preserving p, f = os.path.split(abs_name) f = next(s.name for s in scandir(p) if s.name.lower() == f.lower()) 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.plain or not pargs.verbose: print(abs_name, file=stdout) else: print("{} ({})".format(abs_name, from_where), file=stdout) nmatches += 1 if not pargs.all: break if not nmatches: failures.append(arg) if len(failures) == 0: return 0 else: print("{} not in $PATH".format(", ".join(failures)), file=stderr, end="") if not pargs.skip: print(" or xonsh.builtins.aliases", file=stderr, end="") print("", end="\n") return len(failures)
def which(args, stdin=None, stdout=None, stderr=None, spec=None): """ Checks if each arguments is a xonsh aliases, then if it's an executable, then finally return an error code equal to the number of misses. If '-a' flag is passed, run both to return both `xonsh` match and `which` match. """ parser = _which_create_parser() if len(args) == 0: parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) verbose = pargs.verbose or pargs.all if spec is not None: captured = spec.captured in xpp.STDOUT_CAPTURE_KINDS else: captured = False if pargs.plain: verbose = False if xp.ON_WINDOWS: if pargs.exts: exts = pargs.exts else: exts = XSH.env["PATHEXT"] else: exts = None failures = [] for arg in pargs.args: nmatches = 0 if pargs.all and arg in XSH.ctx: print_global_object(arg, stdout) nmatches += 1 if arg in XSH.aliases and not pargs.skip: print_alias(arg, stdout, verbose) nmatches += 1 if not pargs.all: continue # which.whichgen gives the nicest 'verbose' output if PATH is taken # from os.environ so we temporarily override it with # __xosnh_env__['PATH'] original_os_path = xp.os_environ["PATH"] xp.os_environ["PATH"] = XSH.env.detype()["PATH"] matches = _which.whichgen(arg, exts=exts, verbose=verbose) for abs_name, from_where in matches: print_path(abs_name, from_where, stdout, verbose, captured) nmatches += 1 if not pargs.all: break xp.os_environ["PATH"] = original_os_path if not nmatches: failures.append(arg) if len(failures) == 0: return 0 else: print("{} not in ".format(", ".join(failures)), file=stderr, end="") if pargs.all: print("globals or ", file=stderr, end="") print("$PATH", file=stderr, end="") if not pargs.skip: print(" or xonsh.builtins.aliases", file=stderr, end="") print("", file=stderr, end="\n") return len(failures)
def test_whichgen_ext_failure(self): testdir = self.testdirs[0].name arg = 'whichtestapp2' matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts)) assert len(matches) == 0
def test_whichgen(self): testdir = self.testdirs[0].name arg = 'whichtestapp1' matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts)) assert len(matches) == 1 assert self._file_match(matches[0][0], os.path.join(testdir, arg))
def test_whichgen_failure(self): testdir = self.testdirs[0].name arg = "not_a_file" matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts)) assert len(matches) == 0
def which(args, stdin=None, stdout=None, stderr=None): """ Checks if each arguments is a xonsh aliases, then if it's an executable, then finally return an error code equal to the number of misses. If '-a' flag is passed, run both to return both `xonsh` match and `which` match. """ desc = "Parses arguments to which wrapper" parser = ArgumentParser('which', description=desc) parser.add_argument('args', type=str, nargs='+', help='The executables or aliases to search for') parser.add_argument('-a', action='store_true', dest='all', 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('-p', '--plain', action='store_true', dest='plain', help='Do not display alias expansions') parser.add_argument('--very-small-rocks', action=AWitchAWitch) if ON_WINDOWS: parser.add_argument('-e', '--exts', nargs='*', type=str, help='Specify a list of extensions to use instead ' 'of the standard list for this system. This can ' 'effectively be used as an optimization to, for ' 'example, avoid stat\'s of "foo.vbs" when ' 'searching for "foo" and you know it is not a ' 'VisualBasic script but ".vbs" is on PATHEXT. ' 'This option is only supported on Windows', dest='exts') if len(args) == 0: parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) if ON_WINDOWS: if pargs.exts: exts = pargs.exts else: exts = builtins.__xonsh_env__.get('PATHEXT', ['.COM', '.EXE', '.BAT']) else: exts = None failures = [] for arg in pargs.args: nmatches = 0 # skip alias check if user asks to skip if (arg in builtins.aliases and not pargs.skip): if pargs.plain: print(arg, file=stdout) else: print('{} -> {}'.format(arg, builtins.aliases[arg]), file=stdout) 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, '') if ON_WINDOWS: # Use list dir to get correct case for the filename # i.e. windows is case insesitive but case preserving p, f = os.path.split(abs_name) f = next(s for s in os.listdir(p) if s.lower() == f.lower()) 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) else: print(abs_name, file=stdout) nmatches += 1 if not pargs.all: break if not nmatches: failures.append(arg) if len(failures) == 0: return 0 else: print('{} not in $PATH'.format(', '.join(failures)), file=stderr, end='') if not pargs.skip: print(' or xonsh.builtins.aliases', file=stderr, end='') print('', end='\n') return len(failures)
def test_whichgen_ext_failure(self): testdir = self.testdirs[0].name arg = "whichtestapp2" matches = list(_which.whichgen(arg, path=[testdir], exts=self.exts)) assert len(matches) == 0
def test_whichgen_ext_success(self): testdir = self.testdirs[0].name arg = 'whichtestapp2' matches = list(_which.whichgen(arg, path=[testdir], exts=['.wta'])) assert len(matches) == 1 assert self._file_match(matches[0][0], os.path.join(testdir, arg))
def test_whichgen_ext_success(self): testdir = self.testdirs[0].name arg = "whichtestapp2" matches = list(_which.whichgen(arg, path=[testdir], exts=[".wta"])) assert len(matches) == 1 assert self._file_match(matches[0][0], os.path.join(testdir, arg))
def which(args, stdin=None, stdout=None, stderr=None): """ Checks if each arguments is a xonsh aliases, then if it's an executable, then finally return an error code equal to the number of misses. If '-a' flag is passed, run both to return both `xonsh` match and `which` match. """ parser = _which_create_parser() if len(args) == 0: parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) verbose = pargs.verbose or pargs.all if pargs.plain: verbose = False if xp.ON_WINDOWS: if pargs.exts: exts = pargs.exts else: exts = builtins.__xonsh_env__['PATHEXT'] else: exts = None failures = [] for arg in pargs.args: nmatches = 0 if pargs.all and arg in builtins.__xonsh_ctx__: print_global_object(arg, stdout) nmatches += 1 if arg in builtins.aliases and not pargs.skip: print_alias(arg, stdout, verbose) nmatches += 1 if not pargs.all: continue # which.whichgen gives the nicest 'verbose' output if PATH is taken # from os.environ so we temporarily override it with # __xosnh_env__['PATH'] original_os_path = os.environ['PATH'] os.environ['PATH'] = builtins.__xonsh_env__.detype()['PATH'] matches = _which.whichgen(arg, exts=exts, verbose=verbose) for abs_name, from_where in matches: print_path(abs_name, from_where, stdout, verbose) nmatches += 1 if not pargs.all: break os.environ['PATH'] = original_os_path if not nmatches: failures.append(arg) if len(failures) == 0: return 0 else: print('{} not in '.format(', '.join(failures)), file=stderr, end='') if pargs.all: print('globals or ') print('$PATH', file=stderr, end='') if not pargs.skip: print(' or xonsh.builtins.aliases', file=stderr, end='') print('', end='\n') return len(failures)
def which(args, stdin=None, stdout=None, stderr=None): """ Checks if each arguments is a xonsh aliases, then if it's an executable, then finally return an error code equal to the number of misses. If '-a' flag is passed, run both to return both `xonsh` match and `which` match. """ desc = "Parses arguments to which wrapper" parser = ArgumentParser('which', description=desc) parser.add_argument('args', type=str, nargs='+', help='The executables or aliases to search for') parser.add_argument('-a','--all', action='store_true', dest='all', 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__), help='Display the version of the python which module ' 'used by xonsh') parser.add_argument('-v', '--verbose', action='store_true', dest='verbose', help='Print out how matches were located and show ' 'near misses on stderr') parser.add_argument('-p', '--plain', action='store_true', dest='plain', help='Do not display alias expansions or location of ' 'where binaries are found. This is the ' 'default behavior, but the option can be used to ' 'override the --verbose option') parser.add_argument('--very-small-rocks', action=AWitchAWitch) if ON_WINDOWS: parser.add_argument('-e', '--exts', nargs='*', type=str, help='Specify a list of extensions to use instead ' 'of the standard list for this system. This can ' 'effectively be used as an optimization to, for ' 'example, avoid stat\'s of "foo.vbs" when ' 'searching for "foo" and you know it is not a ' 'VisualBasic script but ".vbs" is on PATHEXT. ' 'This option is only supported on Windows', dest='exts') if len(args) == 0: parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) if pargs.all: pargs.verbose = True if ON_WINDOWS: if pargs.exts: exts = pargs.exts else: exts = builtins.__xonsh_env__['PATHEXT'] else: exts = None failures = [] for arg in pargs.args: nmatches = 0 # skip alias check if user asks to skip if (arg in builtins.aliases and not pargs.skip): if pargs.plain or not pargs.verbose: if isinstance(builtins.aliases[arg], list): print(' '.join(builtins.aliases[arg]), file=stdout) else: print(arg, file=stdout) else: print("aliases['{}'] = {}".format(arg, builtins.aliases[arg]), file=stdout) nmatches += 1 if not pargs.all: continue # which.whichgen gives the nicest 'verbose' output if PATH is taken # from os.environ so we temporarily override it with # __xosnh_env__['PATH'] original_os_path = os.environ['PATH'] os.environ['PATH'] = builtins.__xonsh_env__.detype()['PATH'] matches = _which.whichgen(arg, exts=exts, verbose=pargs.verbose) for abs_name, from_where in matches: if ON_WINDOWS: # Use list dir to get correct case for the filename # i.e. windows is case insensitive but case preserving p, f = os.path.split(abs_name) f = next(s.name for s in scandir(p) if s.name.lower() == f.lower()) 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.plain or not pargs.verbose: print(abs_name, file=stdout) else: print('{} ({})'.format(abs_name, from_where), file=stdout) nmatches += 1 if not pargs.all: break os.environ['PATH'] = original_os_path if not nmatches: failures.append(arg) if len(failures) == 0: return 0 else: print('{} not in $PATH'.format(', '.join(failures)), file=stderr, end='') if not pargs.skip: print(' or xonsh.builtins.aliases', file=stderr, end='') print('', end='\n') return len(failures)
def which(args, stdin=None, stdout=None, stderr=None, spec=None): """ Checks if each arguments is a xonsh aliases, then if it's an executable, then finally return an error code equal to the number of misses. If '-a' flag is passed, run both to return both `xonsh` match and `which` match. """ parser = _which_create_parser() if len(args) == 0: parser.print_usage(file=stderr) return -1 pargs = parser.parse_args(args) verbose = pargs.verbose or pargs.all if spec is not None: captured = spec.captured in xproc.STDOUT_CAPTURE_KINDS else: captured = False if pargs.plain: verbose = False if xp.ON_WINDOWS: if pargs.exts: exts = pargs.exts else: exts = builtins.__xonsh__.env["PATHEXT"] else: exts = None failures = [] for arg in pargs.args: nmatches = 0 if pargs.all and arg in builtins.__xonsh__.ctx: print_global_object(arg, stdout) nmatches += 1 if arg in builtins.aliases and not pargs.skip: print_alias(arg, stdout, verbose) nmatches += 1 if not pargs.all: continue # which.whichgen gives the nicest 'verbose' output if PATH is taken # from os.environ so we temporarily override it with # __xosnh_env__['PATH'] original_os_path = xp.os_environ["PATH"] xp.os_environ["PATH"] = builtins.__xonsh__.env.detype()["PATH"] matches = _which.whichgen(arg, exts=exts, verbose=verbose) for abs_name, from_where in matches: print_path(abs_name, from_where, stdout, verbose, captured) nmatches += 1 if not pargs.all: break xp.os_environ["PATH"] = original_os_path if not nmatches: failures.append(arg) if len(failures) == 0: return 0 else: print("{} not in ".format(", ".join(failures)), file=stderr, end="") if pargs.all: print("globals or ", file=stderr, end="") print("$PATH", file=stderr, end="") if not pargs.skip: print(" or xonsh.builtins.aliases", file=stderr, end="") print("", file=stderr, end="\n") return len(failures)