Metadata-Version: 2.4
Name: clonetree
Version: 1.4.2
Summary: Copy-on-write snapshots of a project directory, as the clonetree CLI makes them, on macOS (APFS) and Linux (XFS, Btrfs)
Author-email: Gabriel Meola <banter@gabe.mx>
License-Expression: MPL-2.0
Keywords: apfs,btrfs,clone,copy-on-write,reflink,snapshot,xfs
Project-URL: Homepage, https://github.com/sprisa/clonetree/tree/main/lib/python/clonetree
Project-URL: Source, https://github.com/sprisa/clonetree
Project-URL: Issues, https://github.com/sprisa/clonetree/issues
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: System :: Filesystems
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# clonetree

Copy-on-write snapshots of a project directory, on macOS (APFS) and Linux (XFS
and Btrfs). A snapshot is a full working copy that shares the project's data on
disk, so it's fast to make, and takes no new space until files change.

It's the library behind the [clonetree](https://github.com/sprisa/clonetree)
CLI, and makes the same snapshots as `clonetree new`. The snapshot `feature` of
a project is the directory `clonetree/feature.clonetree` inside it. To clone a
single file or directory, see [`clonefile`](https://pypi.org/project/clonefile/).

```sh
pip install clonetree
```

```python
import clonetree

# Snapshot the current directory into clonetree/feature.clonetree.
created = clonetree.new(".", "feature")

# List, measure and delete snapshots.
snapshots = clonetree.list(".")
infos = clonetree.info(".")
clonetree.delete(".", "feature")
```

| Function | What it does | CLI command |
| --- | --- | --- |
| `new` | Clones the project, except other snapshots, into a new snapshot. If it fails or you cancel it, it removes the partial snapshot. | `clonetree new` |
| `list` | Returns the snapshots. It doesn't read them, so it's instant. | `clonetree ls` |
| `info` | Measures each snapshot: the size of its files, and the disk space that only it uses. | `clonetree info` |
| `delete` | Removes a snapshot. If it stops partway, deleting it again removes the rest. | `clonetree delete` |

None of them follow symlinks out of a project or a snapshot. They return
dataclasses, such as `Created` and `SnapshotInfo`.

## Progress and canceling

`new`, `info` and `delete` use several threads, and let other Python threads
run meanwhile. They take a `progress` callback, which runs on your thread about
20 times a second and once more at the end, and a `Cancel`, which you can call
from any thread:

```python
cancel = clonetree.Cancel()  # cancel.cancel() stops it.
clonetree.new(
    ".",
    "feature",
    progress=lambda p: print(f"{p.cloned} of {p.total} entries cloned"),
    cancel=cancel,
)
```

Canceling makes the function raise `clonetree.Error` with code `"Canceled"`,
after `new` has removed the partial snapshot. If `progress` raises, the
function stops the same way, and raises that. Ctrl-C stops them too, though
`new` only notices it when it has a `progress` callback.

## Errors

The functions raise `clonetree.Error`, an `OSError` whose `code` says why,
such as `"InvalidId"` or `"NoSpaceLeft"`. Its `filename` is the path that
failed, if there is one, and `left_behind` says whether part of a snapshot
remains. Its `errno` is the closest errno, where there is one. The type hints
list every code.

```python
try:
    clonetree.new(".", "feature")
except clonetree.Error as err:
    if err.code != "PathAlreadyExists":
        raise
    print("feature already exists")
```

## Requirements

- macOS with APFS, or Linux with XFS (with reflinks) or Btrfs. Snapshots must be
  on the project's filesystem.
- x86_64 or arm64. On Linux, glibc 2.28 or later, or musl.
- Python 3.11 or later.

pip installs the wheel for your platform, which includes the compiled library,
so it needs no build tools, and the package has no dependencies. On other
platforms, the functions raise `clonetree.Error` with code
`"OperationUnsupported"`.

## Documentation

- [The docstrings](https://github.com/sprisa/clonetree/blob/main/lib/python/clonetree/clonetree/__init__.py)
  document every function, dataclass and error.
- [The library from Go, Zig, Rust and Node](https://github.com/sprisa/clonetree/tree/main/lib)

<img src="https://pl.sprisa.com/Asts1wWV5" alt="" width="1" height="1">
