zabgrab.adapters.files

Helpers for writing files safely.

Functions

match_permissions(temporary, path)

Give a temporary file the permissions path should end up with.

replacing(path, *[, copy])

Write a file via a temporary file, so it's never left half-written.

zabgrab.adapters.files.replacing(path, *, copy=False)[source]

Write a file via a temporary file, so it’s never left half-written.

The temporary file is created next to path (so replacing it is atomic) and replaces path when the with block ends without an error. If it fails, the temporary file is deleted and path is left as it was.

Parameters:
  • path (Path) – The file to write. Its directory is created if needed.

  • copy (bool, optional) – Start the temporary file as a copy of path, to change it rather than write it from scratch.

Yields:

Path – The temporary file to write to.

Raises:

OSError – If the file can’t be written.

Return type:

Iterator[Path]

zabgrab.adapters.files.match_permissions(temporary, path)[source]

Give a temporary file the permissions path should end up with.

Temporary files are private (0600). When one replaces path, it should keep path’s permissions, or get the usual ones for new files (from the umask).

Parameters:
  • temporary (Path) – The temporary file, about to replace path.

  • path (Path) – The file being written (which may not exist yet).

Return type:

None