def SearchForExecutableOnPath(executable, path=None): """Tries to find all 'executable' in the directories listed in the PATH. This is mostly copied from distutils.spawn.find_executable() but with a few differences. It does not check the current directory for the executable. We only want to find things that are actually on the path, not based on what the CWD is. It also returns a list of all matching executables. If there are multiple versions of an executable on the path it will return all of them at once. Args: executable: The name of the executable to find path: A path to search. If none, the system PATH will be used. Returns: A list of full paths to matching executables or an empty list if none are found. """ if not path: path = console_attr.GetEncodedValue(os.environ, 'PATH') paths = path.split(os.pathsep) matching = [] for p in paths: f = os.path.join(p, executable) if os.path.isfile(f): matching.append(f) return matching
def __init__(self): self.sdk_root = config.Paths().sdk_root self.release_channel = config.INSTALLATION_CONFIG.release_channel self.repo_url = config.INSTALLATION_CONFIG.snapshot_url repos = properties.VALUES.component_manager.additional_repositories.Get( validate=False) self.additional_repos = repos.split(',') if repos else [] self.path = console_attr.GetEncodedValue(os.environ, 'PATH', '') if self.sdk_root: manager = update_manager.UpdateManager() self.components = manager.GetCurrentVersionsInformation() self.old_tool_paths = manager.FindAllOldToolsOnPath() paths = [os.path.realpath(p) for p in self.path.split(os.pathsep)] this_path = os.path.realpath( os.path.join(self.sdk_root, update_manager.UpdateManager.BIN_DIR_NAME)) # TODO(user): Validate symlinks in /usr/local/bin when we start # creating them. self.on_path = this_path in paths else: self.components = {} self.old_tool_paths = [] self.on_path = False self.kubectl = file_utils.SearchForExecutableOnPath('kubectl') if self.kubectl: self.kubectl = self.kubectl[0]
def _GetGlobalConfigDir(): """Returns the path to the user's global config area. Returns: str: The path to the user's global config area. """ # Name of the directory that roots a cloud SDK workspace. global_config_dir = console_attr.GetEncodedValue(os.environ, CLOUDSDK_CONFIG) if global_config_dir: return global_config_dir if platforms.OperatingSystem.Current() != platforms.OperatingSystem.WINDOWS: return os.path.join(os.path.expanduser('~'), '.config', _CLOUDSDK_GLOBAL_CONFIG_DIR_NAME) appdata = console_attr.GetEncodedValue(os.environ, 'APPDATA') if appdata: return os.path.join(appdata, _CLOUDSDK_GLOBAL_CONFIG_DIR_NAME) # This shouldn't happen unless someone is really messing with things. drive = os.environ.get('SystemDrive', 'C:') return os.path.join(drive, os.path.sep, _CLOUDSDK_GLOBAL_CONFIG_DIR_NAME)
def FindExecutableOnPath(executable, path=None, pathext=None): """Searches for `executable` in the directories listed in `path` or $PATH. Executable must not contain a directory or an extension. Args: executable: The name of the executable to find. path: A list of directories to search separated by 'os.pathsep'. If None then the system PATH is used. pathext: An iterable of file name extensions to use. If None then platform specific extensions are used. Returns: The path of 'executable' (possibly with a platform-specific extension) if found and executable, None if not found. Raises: ValueError: if executable has an extension or a path, or there's an internal error. """ if os.path.splitext(executable)[1]: raise ValueError( u'FindExecutableOnPath({0},...) failed because first ' u'argument must not have an extension.'.format(executable)) if os.path.dirname(executable): raise ValueError(u'FindExecutableOnPath({0},...) failed because first ' u'argument must not have a path.'.format(executable)) if path is None: effective_path = console_attr.GetEncodedValue(os.environ, 'PATH') else: effective_path = path effective_pathext = (pathext if pathext is not None else _PlatformExecutableExtensions( platforms.OperatingSystem.Current())) return _FindExecutableOnPath(executable, effective_path, effective_pathext)