Skip to main content

Transactional file modifications – atomic, rollback on failure

Project description

safefile

Atomic, transactional file modifications – automatic rollback on failure.

Protect files and directories from being left corrupted if your script crashes.

Installation

pip install safefile

Quick start

from safefile import transaction

# Basic protection
with transaction("config.yaml", "data.csv"):
    update_config("config.yaml")
    update_data("data.csv")
    # crash here → both files restored automatically

Strategies

copy (default)

Safe copy via shutil.copy2. Works everywhere.

with transaction("config.yaml", strategy="copy"):
    ...

hardlink

Near-instant snapshot via os.link() — no data duplication. Falls back to copy automatically if the temp directory is on a different filesystem.

with transaction("big_file.bin", strategy="hardlink"):
    ...

Directory support

Pass a directory path — the entire tree is snapshotted and restored.

with transaction("configs/", "data.csv"):
    shutil.rmtree("configs/")
    os.makedirs("configs/")
    rebuild_configs("configs/")
    update_data("data.csv")
    # any failure → configs/ and data.csv both restored

Hooks

with transaction(
    "db.sqlite",
    on_commit=lambda: logger.info("committed"),
    on_rollback=lambda: alert.send("rolled back"),
):
    modify_db("db.sqlite")

How it works

  1. On enter: each existing file/directory is backed up (using the chosen strategy).
  2. On success: backups are discarded.
  3. On exception: all originals are restored; new files/dirs created inside the block are deleted.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safefile-0.2.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safefile-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file safefile-0.2.0.tar.gz.

File metadata

  • Download URL: safefile-0.2.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for safefile-0.2.0.tar.gz
Algorithm Hash digest
SHA256 89f778d69895f5187c486788cf7a2549c6cef5697b9c1f4cdd50b0072988dc05
MD5 e5084ae3108466b1fcaaef4a6f021b7e
BLAKE2b-256 31409368f9e73c735a0289b20bfb7a8bd86022751368eae55a8c6d15aea055aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for safefile-0.2.0.tar.gz:

Publisher: publish.yml on AbirHasanSupta/safefile

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file safefile-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: safefile-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for safefile-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 feb002310d74c388457a70b6a4e50181acdfa0055d46843249ffd466aa094ff7
MD5 6852180acbf081fa238dbc303dcac652
BLAKE2b-256 173cdff1849688b87d30aba7f1dd1d27ad47757c899525cec3a14b7c97c3bfda

See more details on using hashes here.

Provenance

The following attestation bundles were made for safefile-0.2.0-py3-none-any.whl:

Publisher: publish.yml on AbirHasanSupta/safefile

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page