Beispiel #1
0
def _GetUserHomeDir():
  if platforms.OperatingSystem.Current() == platforms.OperatingSystem.WINDOWS:
    # %HOME% has precedence over %USERPROFILE% for os.path.expanduser('~')
    # The Docker config resides under %USERPROFILE% on Windows
    return os.path.expandvars('%USERPROFILE%')
  else:
    return platforms.GetHomePath()
Beispiel #2
0
def GetDockerConfig():
    """Retrieve the path to '.dockercfg' in a docker-py compatible fashion.

  Returns:
    The path on the host machine to '.dockercfg', as expected by the docker
    client.
  """
    # We very intentionally match the paired logic from docker/docker-py.
    return os.path.join(platforms.GetHomePath(), '.dockercfg')
Beispiel #3
0
def GetDockerConfig(force_new=False):
    """Retrieve the path to Docker's configuration file, noting its format.

  Args:
    force_new: bool, whether to force usage of the new config file regardless
               of whether it exists (for testing).

  Returns:
    The path to Docker's configuration file, and whether it is in the
    new configuration format.
  """
    # Starting in Docker 1.7.0, the Docker client moved where it writes
    # credentials to ~/.docker/config.json.  It is half backwards-compatible,
    # if the new file doesn't exist, it falls back on the old file.
    # if the new file exists, it IGNORES the old file.
    # This is a problem when a user has logged into another registry on 1.7.0
    # and then uses 'gcloud docker'.
    # This must remain compatible with: https://github.com/docker/docker-py
    new_path = os.path.join(platforms.GetHomePath(), '.docker', 'config.json')
    old_path = os.path.join(platforms.GetHomePath(), '.dockercfg')
    if os.path.exists(new_path) or force_new:
        return new_path, True
    return old_path, False
Beispiel #4
0
 def __init__(self):
     cfg_paths = config.Paths()
     # Ordered list of replacements. First match wins, more specific paths should
     # be placed before more general ones.
     self._replacements = [
         (re.escape(os.path.normpath(cfg_paths.global_config_dir)),
          '${CLOUDSDK_CONFIG}'),
         (re.escape(platforms.GetHomePath()), '${HOME}'),
         (re.escape(getpass.getuser()), '${USER}')
     ]
     if cfg_paths.sdk_root:
         self._replacements.append(
             (re.escape(os.path.normpath(cfg_paths.sdk_root)),
              '${SDK_ROOT}'))
Beispiel #5
0
def _GetAndUpdateRcPath(completion_update, path_update, rc_path, host_os):
    """Returns an rc path based on the default rc path or user input.

  Gets default rc path based on environment. If prompts are enabled,
  allows user to update to preferred file path. Otherwise, prints a warning
  that the default rc path will be updated.

  Args:
    completion_update: bool, Whether or not to do command completion.
    path_update: bool, Whether or not to update PATH.
    rc_path: str, the rc path given by the user, from --rc-path arg.
    host_os: str, The host os identification string.

  Returns:
    str, A path to the rc file to update.
  """
    # If we aren't updating the RC file for either completions or PATH, there's
    # no point.
    if not (completion_update or path_update):
        return None
    if rc_path:
        return rc_path
    # A first guess at user preferred shell.
    preferred_shell = _GetPreferredShell(
        encoding.GetEncodedValue(os.environ, 'SHELL', '/bin/sh'))
    default_rc_path = os.path.join(
        platforms.GetHomePath(), _GetShellRcFileName(preferred_shell, host_os))
    # If in quiet mode, we'll use default path.
    if not console_io.CanPrompt():
        _TraceAction(
            'You specified that you wanted to update your rc file. The '
            'default file will be updated: [{rc_path}]'.format(
                rc_path=default_rc_path))
        return default_rc_path
    rc_path_update = console_io.PromptResponse(
        ('The Google Cloud SDK installer will now prompt you to update an rc '
         'file to bring the Google Cloud CLIs into your environment.\n\n'
         'Enter a path to an rc file to update, or leave blank to use '
         '[{rc_path}]:  ').format(rc_path=default_rc_path))
    return (os.path.expanduser(rc_path_update)
            if rc_path_update else default_rc_path)
def _GetRcPaths(command_completion, path_update, rc_path, sdk_root, host_os):
    """Returns an _RcPaths object for the preferred user shell.

  Args:
    command_completion: bool, Whether or not to do command completion. If None,
      ask.
    path_update: bool, Whether or not to update PATH. If None, ask.
    rc_path: str, The path to the rc file to update. If None, ask.
    sdk_root: str, The path to the Cloud SDK root.
    host_os: str, The host os identification string.

  Returns:
    An _RcPaths() object for the preferred user shell.
  """

    # An initial guess on the preferred user shell based on the environment.
    preferred_shell = _GetPreferredShell(os.environ.get('SHELL', '/bin/sh'))
    if not command_completion and not path_update:
        rc_path = None
    elif not rc_path:
        file_name = _GetShellRcFileName(preferred_shell, host_os)
        rc_path = os.path.join(platforms.GetHomePath(), file_name)

        rc_path_update = console_io.PromptResponse((
            'The Google Cloud SDK installer will now prompt you to update an rc '
            'file to bring the Google Cloud CLIs into your environment.\n\n'
            'Enter a path to an rc file to update, or leave blank to use '
            '[{rc_path}]:  ').format(rc_path=rc_path))
        if rc_path_update:
            rc_path = os.path.expanduser(rc_path_update)

    if rc_path:
        # Check the rc_path for a better hint at the user preferred shell.
        preferred_shell = _GetPreferredShell(rc_path, default=preferred_shell)

    return _RcPaths(preferred_shell, rc_path, sdk_root)