def lazy_locate_binary(self, name, ignore_alias=False): """Locates an executable in the cache, without checking its validity. Arguments --------- name : str name of binary to search for ignore_alias : bool, optional Force return of binary path even if alias of ``name`` exists (default ``False``) """ possibilities = self.get_possible_names(name) if ON_WINDOWS: # Windows users expect to be able to execute files in the same # directory without `./` local_bin = next((fn for fn in possibilities if os.path.isfile(fn)), None) if local_bin: return os.path.abspath(local_bin) cached = next((cmd for cmd in possibilities if cmd in self._cmds_cache), None) if cached: (path, alias) = self._cmds_cache[cached] ispure = path == pathbasename(path) if alias and ignore_alias and ispure: # pure alias, which we are ignoring return None else: return path elif os.path.isfile(name) and name != pathbasename(name): return name
def __iter__(self): for cmd, (path, is_alias) in self.all_commands.items(): if ON_WINDOWS and path is not None: # All command keys are stored in uppercase on Windows. # This ensures the original command name is returned. cmd = pathbasename(path) yield cmd
def cached_name(self, name): """Returns the name that would appear in the cache, if it exists.""" if name is None: return None cached = pathbasename(name) if ON_WINDOWS: keys = self.get_possible_names(cached) cached = next((k for k in keys if k in self._cmds_cache), None) return cached
def predict_threadable(self, cmd): """Predicts whether a command list is able to be run on a background thread, rather than the main thread. """ name = self.cached_name(cmd[0]) if ON_WINDOWS: # On all names (keys) are stored in upper case so instead # we get the original cmd or alias name path, _ = self.lazyget(name, (None, None)) if path is None: return True else: name = pathbasename(path) predictor = self.threadable_predictors[name] return predictor(cmd[1:])
def lazy_locate_binary(self, name): """Locates an executable in the cache, without checking its validity.""" possibilities = self.get_possible_names(name) if ON_WINDOWS: # Windows users expect to be able to execute files in the same # directory without `./` local_bin = next((fn for fn in possibilities if os.path.isfile(fn)), None) if local_bin: return os.path.abspath(local_bin) cached = next((cmd for cmd in possibilities if cmd in self._cmds_cache), None) if cached: (path, is_alias) = self._cmds_cache[cached] return path if not is_alias else None elif os.path.isfile(name) and name != pathbasename(name): return name
def lazy_locate_binary(self, name): """Locates an executable in the cache, without checking its validity.""" possibilities = self.get_possible_names(name) if ON_WINDOWS: # Windows users expect to be able to execute files in the same # directory without `./` local_bin = next( (fn for fn in possibilities if os.path.isfile(fn)), None) if local_bin: return os.path.abspath(local_bin) cached = next( (cmd for cmd in possibilities if cmd in self._cmds_cache), None) if cached: (path, is_alias) = self._cmds_cache[cached] return path if not is_alias else None elif os.path.isfile(name) and name != pathbasename(name): return name
def get_predictor_threadable(self, cmd0): """Return the predictor whether a command list is able to be run on a background thread, rather than the main thread. """ name = self.cached_name(cmd0) predictors = self.threadable_predictors if ON_WINDOWS: # On all names (keys) are stored in upper case so instead # we get the original cmd or alias name path, _ = self.lazyget(name, (None, None)) if path is None: return predict_true else: name = pathbasename(path) if name not in predictors: pre, ext = os.path.splitext(name) if pre in predictors: predictors[name] = predictors[pre] if name not in predictors: predictors[name] = self.default_predictor(name, cmd0) predictor = predictors[name] return predictor