Ejemplo n.º 1
0
def Remove(path: str, backup: bool = True):
    """Remove a config file.

  Args:
    path: The filesystem path to the file.
    backup: Whether to make a backup of the file being removed.

  Raises:
    Error: Failure performing the filesystem operation.
  """
    if backup:
        try:
            file_util.Move(path, path + '.bak')
        except file_util.Error as e:
            raise Error('Failed to create backup file (%s)' % str(e))
    else:
        try:
            file_util.Remove(path)
        except file_util.Error as e:
            raise Error('Failed to remove file (%s)' % str(e))
Ejemplo n.º 2
0
def Dump(path: str, data: Any, mode: str = 'w'):
    """Write a config file containing some data.

  Args:
    path: The filesystem path to the destination file.
    data: Data to be written to the file as yaml.
    mode: Mode to use for writing the file (default: w)
  """
    file_util.CreateDirectories(path)
    tmp_f = path + '.tmp'
    # Write to a .tmp file to avoid corrupting the original if aborted mid-way.
    try:
        with open(tmp_f, mode) as handle:
            handle.write(yaml.dump(data))
    except IOError as e:
        raise Error('Could not save data to yaml file %s: %s' % (path, str(e)))
    # Replace the original with the tmp.
    try:
        file_util.Move(tmp_f, path)
    except file_util.Error as e:
        raise Error('Could not replace config file. (%s)' % str(e))