rwnd 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rewind/__init__.py +1 -0
- rewind/cli.py +46 -0
- rewind/core.py +76 -0
- rwnd-0.1.0.dist-info/METADATA +19 -0
- rwnd-0.1.0.dist-info/RECORD +7 -0
- rwnd-0.1.0.dist-info/WHEEL +4 -0
- rwnd-0.1.0.dist-info/entry_points.txt +2 -0
rewind/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
rewind/cli.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from rich.console import Console
|
|
4
|
+
from rich.table import Table
|
|
5
|
+
from .core import start_watcher, get_db
|
|
6
|
+
|
|
7
|
+
console = Console()
|
|
8
|
+
|
|
9
|
+
def main():
|
|
10
|
+
parser = argparse.ArgumentParser(description="rewind - A passive time machine for your filesystem.")
|
|
11
|
+
subparsers = parser.add_subparsers(dest="command")
|
|
12
|
+
|
|
13
|
+
watch_parser = subparsers.add_parser("watch", help="Watch a folder for changes")
|
|
14
|
+
watch_parser.add_argument("path", help="Path to the folder to watch")
|
|
15
|
+
|
|
16
|
+
log_parser = subparsers.add_parser("log", help="View version history of a file")
|
|
17
|
+
log_parser.add_argument("file", help="Path to the file")
|
|
18
|
+
|
|
19
|
+
args = parser.parse_args()
|
|
20
|
+
|
|
21
|
+
if args.command == "watch":
|
|
22
|
+
start_watcher(args.path)
|
|
23
|
+
|
|
24
|
+
elif args.command == "log":
|
|
25
|
+
conn = get_db()
|
|
26
|
+
filepath = str(Path(args.file).resolve())
|
|
27
|
+
rows = conn.execute("SELECT * FROM versions WHERE path = ? ORDER BY id DESC", (filepath,)).fetchall()
|
|
28
|
+
|
|
29
|
+
if not rows:
|
|
30
|
+
console.print(f"[red]No history found for {filepath}[/red]")
|
|
31
|
+
return
|
|
32
|
+
|
|
33
|
+
table = Table(title=f"History for {args.file}")
|
|
34
|
+
table.add_column("Version", style="cyan")
|
|
35
|
+
table.add_column("Hash", style="magenta")
|
|
36
|
+
table.add_column("Date", style="green")
|
|
37
|
+
|
|
38
|
+
for row in rows:
|
|
39
|
+
table.add_row(str(row["id"]), row["hash"][:8], row["created_at"])
|
|
40
|
+
console.print(table)
|
|
41
|
+
|
|
42
|
+
else:
|
|
43
|
+
parser.print_help()
|
|
44
|
+
|
|
45
|
+
if __name__ == "__main__":
|
|
46
|
+
main()
|
rewind/core.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import sqlite3
|
|
3
|
+
import os
|
|
4
|
+
import time
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from watchdog.observers import Observer
|
|
7
|
+
from watchdog.events import FileSystemEventHandler
|
|
8
|
+
|
|
9
|
+
DB_PATH = Path.home() / ".config" / "rewind" / "rewind.db"
|
|
10
|
+
|
|
11
|
+
def get_db():
|
|
12
|
+
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
13
|
+
conn = sqlite3.connect(DB_PATH)
|
|
14
|
+
conn.row_factory = sqlite3.Row
|
|
15
|
+
conn.execute('''
|
|
16
|
+
CREATE TABLE IF NOT EXISTS versions (
|
|
17
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
18
|
+
path TEXT NOT NULL,
|
|
19
|
+
hash TEXT NOT NULL,
|
|
20
|
+
created_at TEXT NOT NULL
|
|
21
|
+
)
|
|
22
|
+
''')
|
|
23
|
+
return conn
|
|
24
|
+
|
|
25
|
+
def hash_file(filepath):
|
|
26
|
+
hasher = hashlib.sha256()
|
|
27
|
+
try:
|
|
28
|
+
with open(filepath, 'rb') as f:
|
|
29
|
+
for chunk in iter(lambda: f.read(4096), b""):
|
|
30
|
+
hasher.update(chunk)
|
|
31
|
+
return hasher.hexdigest()
|
|
32
|
+
except (PermissionError, FileNotFoundError):
|
|
33
|
+
return None
|
|
34
|
+
|
|
35
|
+
def save_version(filepath):
|
|
36
|
+
filepath = str(Path(filepath).resolve())
|
|
37
|
+
file_hash = hash_file(filepath)
|
|
38
|
+
if not file_hash:
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
conn = get_db()
|
|
42
|
+
last = conn.execute("SELECT hash FROM versions WHERE path = ? ORDER BY id DESC LIMIT 1", (filepath,)).fetchone()
|
|
43
|
+
|
|
44
|
+
if last and last["hash"] == file_hash:
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
conn.execute(
|
|
48
|
+
"INSERT INTO versions (path, hash, created_at) VALUES (?, ?, datetime('now'))",
|
|
49
|
+
(filepath, file_hash)
|
|
50
|
+
)
|
|
51
|
+
conn.commit()
|
|
52
|
+
print(f"[rewind] Saved: {filepath}")
|
|
53
|
+
|
|
54
|
+
class RewindHandler(FileSystemEventHandler):
|
|
55
|
+
def on_modified(self, event):
|
|
56
|
+
if not event.is_directory:
|
|
57
|
+
save_version(event.src_path)
|
|
58
|
+
|
|
59
|
+
def start_watcher(path):
|
|
60
|
+
path = str(Path(path).resolve())
|
|
61
|
+
print(f"[rewind] Watching {path}... (Ctrl+C to stop)")
|
|
62
|
+
|
|
63
|
+
for root, _, files in os.walk(path):
|
|
64
|
+
for file in files:
|
|
65
|
+
if file.startswith('.'): continue
|
|
66
|
+
save_version(os.path.join(root, file))
|
|
67
|
+
|
|
68
|
+
observer = Observer()
|
|
69
|
+
observer.schedule(RewindHandler(), path, recursive=True)
|
|
70
|
+
observer.start()
|
|
71
|
+
try:
|
|
72
|
+
while True:
|
|
73
|
+
time.sleep(1)
|
|
74
|
+
except KeyboardInterrupt:
|
|
75
|
+
observer.stop()
|
|
76
|
+
observer.join()
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: rwnd
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A passive time machine for your filesystem.
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Requires-Dist: rich>=13.0.0
|
|
8
|
+
Requires-Dist: watchdog>=4.0.0
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# rewind
|
|
12
|
+
A passive time machine for your filesystem (Linux/Ubuntu).
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
pip install -e .
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
rewind watch ~/my-project
|
|
19
|
+
rewind log ~/my-project/test.txt
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
rewind/__init__.py,sha256=0FuwqOrARfDZDazud7BoDrtTB0XLd2VpkujEjPahxHI,26
|
|
2
|
+
rewind/cli.py,sha256=Sa3reA1gM3g6rZjUFRrBULI0pTMhtP_bHPfLu8e3XDQ,1513
|
|
3
|
+
rewind/core.py,sha256=cqJ0-H6Q7h9TpiO0iiPyNM3LbB2NU6e0gzrA2ArWzXU,2164
|
|
4
|
+
rwnd-0.1.0.dist-info/METADATA,sha256=GorNymWuXAUdrUefKGxqGEJ6iet2uQNR9Lu3jXgCRKA,406
|
|
5
|
+
rwnd-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
6
|
+
rwnd-0.1.0.dist-info/entry_points.txt,sha256=ObbXyL_jYLu8AuXi-7AA7A4u1prBqGnt_jdiRz84tQs,43
|
|
7
|
+
rwnd-0.1.0.dist-info/RECORD,,
|