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
- On enter: each existing file/directory is backed up (using the chosen strategy).
- On success: backups are discarded.
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89f778d69895f5187c486788cf7a2549c6cef5697b9c1f4cdd50b0072988dc05
|
|
| MD5 |
e5084ae3108466b1fcaaef4a6f021b7e
|
|
| BLAKE2b-256 |
31409368f9e73c735a0289b20bfb7a8bd86022751368eae55a8c6d15aea055aa
|
Provenance
The following attestation bundles were made for safefile-0.2.0.tar.gz:
Publisher:
publish.yml on AbirHasanSupta/safefile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
safefile-0.2.0.tar.gz -
Subject digest:
89f778d69895f5187c486788cf7a2549c6cef5697b9c1f4cdd50b0072988dc05 - Sigstore transparency entry: 1667668571
- Sigstore integration time:
-
Permalink:
AbirHasanSupta/safefile@aab492dfaaabac59528507801aa5f0bbc648f3e0 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/AbirHasanSupta
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@aab492dfaaabac59528507801aa5f0bbc648f3e0 -
Trigger Event:
release
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
feb002310d74c388457a70b6a4e50181acdfa0055d46843249ffd466aa094ff7
|
|
| MD5 |
6852180acbf081fa238dbc303dcac652
|
|
| BLAKE2b-256 |
173cdff1849688b87d30aba7f1dd1d27ad47757c899525cec3a14b7c97c3bfda
|
Provenance
The following attestation bundles were made for safefile-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on AbirHasanSupta/safefile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
safefile-0.2.0-py3-none-any.whl -
Subject digest:
feb002310d74c388457a70b6a4e50181acdfa0055d46843249ffd466aa094ff7 - Sigstore transparency entry: 1667668728
- Sigstore integration time:
-
Permalink:
AbirHasanSupta/safefile@aab492dfaaabac59528507801aa5f0bbc648f3e0 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/AbirHasanSupta
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@aab492dfaaabac59528507801aa5f0bbc648f3e0 -
Trigger Event:
release
-
Statement type: