Beispiel #1
0
    def _EnsureNotDisabled(self):
        """Prints an error and raises an Exception if the updater is disabled.

    The updater is disabled for installations that come from other package
    managers like apt-get or if the current user does not have permission
    to create or delete files in the SDK root directory.

    Raises:
      UpdaterDisableError: If the updater is disabled.
      exceptions.RequiresAdminRightsError: If the caller has insufficient
        privilege.
    """
        if config.INSTALLATION_CONFIG.disable_updater:
            message = (
                'You cannot perform this action because this Cloud SDK installation '
                'is managed by an external package manager.  If you would like to get'
                ' the latest version, please see our main download page at:\n  '
                + config.INSTALLATION_CONFIG.documentation_url + '\n')
            self.__Write(log.err, message, word_wrap=True)
            raise UpdaterDisableError(
                'The component manager is disabled for this installation')
        config.EnsureSDKWriteAccess(self.__sdk_root)
Beispiel #2
0
def PersistProperty(prop, value, scope=None, properties_file=None):
    """Sets the given property in the properties file.

  This function should not generally be used as part of normal program
  execution.  The property files are user editable config files that they should
  control.  This is mostly for initial setup of properties that get set during
  SDK installation.

  Args:
    prop: properties.Property, The property to set.
    value: str, The value to set for the property. If None, the property is
      removed.
    scope: Scope, The config location to set the property in.  If given, only
      this location will be udpated and it is an error if that location does
      not exist.  If not given, it will attempt to update the property in first
      the workspace config (if it exists) but then fall back to user level
      config.  It will never fall back to installation properties; you must
      use that scope explicitly to set that value.
    properties_file: str, Path to an explicit properties file to use (instead of
      one of the known locations).  It is an error to specify a scope and an
      explicit file.

  Raises:
    ValueError: If you give both a scope and a properties file.
    MissingConfigLocationError: If there is not file for the given scope.
  """
    prop.Validate(value)
    if scope and properties_file:
        raise ValueError(
            'You cannot provide both a scope and a specific properties'
            ' file.')
    if not properties_file:
        if scope:
            if scope == Scope.INSTALLATION:
                config.EnsureSDKWriteAccess()
            properties_file = scope.get_file()
            if not properties_file:
                raise MissingConfigLocationError(scope)
        else:
            properties_file = Scope.WORKSPACE.get_file()
            if not properties_file:
                properties_file = Scope.USER.get_file()
                if not properties_file:
                    raise MissingConfigLocationError(Scope.USER)

    parsed_config = ConfigParser.ConfigParser()
    parsed_config.read(properties_file)

    if not parsed_config.has_section(prop.section):
        if value is None:
            return
        parsed_config.add_section(prop.section)

    if value is None:
        parsed_config.remove_option(prop.section, prop.name)
    else:
        parsed_config.set(prop.section, prop.name, str(value))

    properties_dir, unused_name = os.path.split(properties_file)
    files.MakeDir(properties_dir)
    with open(properties_file, 'w') as fp:
        parsed_config.write(fp)

    _PropertiesFile.Invalidate()
Beispiel #3
0
def PersistProperty(prop, value, scope=None, properties_file=None):
    """Sets the given property in the properties file.

  This function should not generally be used as part of normal program
  execution.  The property files are user editable config files that they should
  control.  This is mostly for initial setup of properties that get set during
  SDK installation.

  Args:
    prop: properties.Property, The property to set.
    value: str, The value to set for the property. If None, the property is
      removed.
    scope: Scope, The config location to set the property in.  If given, only
      this location will be udpated and it is an error if that location does
      not exist.  If not given, it will attempt to update the property in the
      first of the following places that exists:
        - the workspace config
        - the active named config
        - user level config
      It will never fall back to installation properties; you must
      use that scope explicitly to set that value.
    properties_file: str, Path to an explicit properties file to use (instead of
      one of the known locations).  It is an error to specify a scope and an
      explicit file.

  Raises:
    ValueError: If you give both a scope and a properties file.
    MissingConfigLocationError: If there is not file for the given scope.
    ReadOnlyNamedConfigNotSettableError: If the user is attempting to set
      a property in a read-only configuration.
    InternalError: If there's a programming error.
  """
    prop.Validate(value)
    if scope and properties_file:
        raise ValueError(
            'You cannot provide both a scope and a specific properties'
            ' file.')
    if not properties_file:
        if scope:
            if scope == Scope.INSTALLATION:
                config.EnsureSDKWriteAccess()
            properties_file = scope.get_file()
            if not properties_file:
                raise MissingConfigLocationError(scope)
        else:
            properties_file = Scope.WORKSPACE.get_file()
            if not properties_file:
                properties_file = named_configs.GetEffectiveNamedConfigFile()
                if properties_file is None:
                    # Should be dead code.
                    raise exceptions.InternalError(
                        'Unexpected None properties file.')
                if properties_file == os.path.devnull:
                    # Refuse to write and fail with an informative error
                    # TODO(b/22817095) Simplify control flow and update
                    # messaging when moving to automatic upgrade scenario
                    # on all release tracks.
                    if (named_configs.GetNameOfActiveNamedConfig() ==
                            named_configs.RESERVED_NAMED_CONFIG_NAME_NONE):
                        raise ReadOnlyNamedConfigNotSettableError(
                            named_configs.RESERVED_NAMED_CONFIG_NAME_NONE)
                    if not Scope.USER.get_file():
                        raise MissingConfigLocationError(Scope.USER)

    parsed_config = ConfigParser.ConfigParser()
    parsed_config.read(properties_file)

    if not parsed_config.has_section(prop.section):
        if value is None:
            return
        parsed_config.add_section(prop.section)

    if value is None:
        parsed_config.remove_option(prop.section, prop.name)
    else:
        parsed_config.set(prop.section, prop.name, str(value))

    properties_dir, unused_name = os.path.split(properties_file)
    files.MakeDir(properties_dir)
    with open(properties_file, 'w') as fp:
        parsed_config.write(fp)

    PropertiesFile.Invalidate()