def Lookup(self, name, exec_required=True): """ Returns the path itself (for relative path), the resolve path, or None. """ if '/' in name: if path_stat.exists(name): return name else: return None # TODO: Could cache this computation to avoid allocating every time for all # the splitting. path_val = self.mem.GetVar('PATH') if path_val.tag == value_e.Str and path_val.s: path_list = path_val.s.split(':') else: path_list = [] # treat as empty path for path_dir in path_list: full_path = os_path.join(path_dir, name) # NOTE: dash and bash only check for EXISTENCE in 'command -v' (and 'type # -t'). OSH follows mksh and zsh. Note that we can still get EPERM if # the permissions are changed between check and use. if exec_required: found = posix.access(full_path, posix.X_OK) else: found = path_stat.exists(full_path) # for 'source' if found: return full_path return None
def _ResolveNames(names, funcs, path_val): if path_val.tag == value_e.Str: path_list = path_val.s.split(':') else: path_list = [] # treat as empty path results = [] for name in names: if name in funcs: kind = ('function', name) elif Resolve(name) != builtin_e.NONE: kind = ('builtin', name) elif ResolveSpecial(name) != builtin_e.NONE: kind = ('builtin', name) elif lex.IsOtherBuiltin(name): # declare, continue, etc. kind = ('builtin', name) elif lex.IsKeyword(name): kind = ('keyword', name) else: # Now look for files. found = False for path_dir in path_list: full_path = os_path.join(path_dir, name) if path_stat.exists(full_path): kind = ('file', full_path) found = True break if not found: # Nothing printed, but status is 1. kind = (None, None) results.append(kind) return results
def _ResolveFile(name, path_list): # Now look for files. for path_dir in path_list: full_path = os_path.join(path_dir, name) if path_stat.exists(full_path): return ('file', full_path) # Nothing printed, but status is 1. return (None, None)
def testIsDir(self): self.assertEqual(True, path_stat.exists('/')) self.assertEqual(False, path_stat.exists('/nonexistent__ZZZZ'))