def CreateObjectWithMetadata(self, apitools_object, contents=''):
    """Creates an object in the bucket according to the input metadata.

    This will create a new object version (ignoring the generation specified
    in the input object).

    Args:
      apitools_object: apitools Object.
      contents: optional object contents.

    Returns:
      apitools Object representing created object.
    """
    # This modifies the apitools_object with a generation number.
    object_name = apitools_object.name
    if (self.root_object.versioning and self.root_object.versioning.enabled and
        apitools_object.name in self.objects):
      if 'live' in self.objects[object_name]:
        # Versioning enabled and object exists, create an object with a
        # generation 1 higher.
        apitools_object.generation = (
            self.objects[object_name]['live'].root_object.generation + 1)
        # Move the live object to versioned.
        if 'versioned' not in self.objects[object_name]:
          self.objects[object_name]['versioned'] = []
        self.objects[object_name]['versioned'].append(
            self.objects[object_name]['live'])
      elif ('versioned' in self.objects[object_name] and
            self.objects[object_name]['versioned']):
        # Versioning enabled but only archived objects exist, pick a generation
        # higher than the highest versioned object (which will be at the end).
        apitools_object.generation = (
            self.objects[object_name]['versioned'][-1].root_object.generation
            + 1)
    else:
      # Versioning disabled or no objects exist yet with this name.
      apitools_object.generation = 1
      self.objects[object_name] = {}
    new_object = MockObject(apitools_object, contents=contents)
    self.objects[object_name]['live'] = new_object
    return new_object
dataset/ETHPy150Open GoogleCloudPlatform/gsutil/gslib/tests/mock_cloud_api.py/MockBucket.CreateObjectWithMetadata/original