rwnd 0.1.0__tar.gz
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.
- rwnd-0.1.0/PKG-INFO +19 -0
- rwnd-0.1.0/README.md +9 -0
- rwnd-0.1.0/app.py +222 -0
- rwnd-0.1.0/pyproject.toml +21 -0
- rwnd-0.1.0/rewind-cli/README.md +9 -0
- rwnd-0.1.0/rewind-cli/pyproject.toml +21 -0
- rwnd-0.1.0/rewind-cli/src/rewind/__init__.py +1 -0
- rwnd-0.1.0/rewind-cli/src/rewind/cli.py +46 -0
- rwnd-0.1.0/rewind-cli/src/rewind/core.py +76 -0
- rwnd-0.1.0/src/rewind/__init__.py +1 -0
- rwnd-0.1.0/src/rewind/cli.py +46 -0
- rwnd-0.1.0/src/rewind/core.py +76 -0
rwnd-0.1.0/PKG-INFO
ADDED
|
@@ -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
|
rwnd-0.1.0/README.md
ADDED
rwnd-0.1.0/app.py
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import sys
|
|
3
|
+
import subprocess
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
def write_file(path, content):
|
|
7
|
+
path = Path(path)
|
|
8
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
9
|
+
path.write_text(content.strip() + '\n', encoding='utf-8')
|
|
10
|
+
print(f"Created: {path}")
|
|
11
|
+
|
|
12
|
+
base_dir = "rewind-cli"
|
|
13
|
+
os.makedirs(base_dir, exist_ok=True)
|
|
14
|
+
|
|
15
|
+
# 1. pyproject.toml
|
|
16
|
+
toml_content = """
|
|
17
|
+
[build-system]
|
|
18
|
+
requires = ["hatchling"]
|
|
19
|
+
build-backend = "hatchling.build"
|
|
20
|
+
|
|
21
|
+
[project]
|
|
22
|
+
name = "rewind"
|
|
23
|
+
version = "0.1.0"
|
|
24
|
+
description = "A passive time machine for your filesystem."
|
|
25
|
+
readme = "README.md"
|
|
26
|
+
requires-python = ">=3.9"
|
|
27
|
+
license = { text = "MIT" }
|
|
28
|
+
dependencies = [
|
|
29
|
+
"watchdog>=4.0.0",
|
|
30
|
+
"rich>=13.0.0"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
rewind = "rewind.cli:main"
|
|
35
|
+
|
|
36
|
+
[tool.hatch.build.targets.wheel]
|
|
37
|
+
packages = ["src/rewind"]
|
|
38
|
+
"""
|
|
39
|
+
write_file(f"{base_dir}/pyproject.toml", toml_content)
|
|
40
|
+
|
|
41
|
+
# 2. src/rewind/__init__.py
|
|
42
|
+
write_file(f"{base_dir}/src/rewind/__init__.py", '__version__ = "0.1.0"')
|
|
43
|
+
|
|
44
|
+
# 3. src/rewind/core.py
|
|
45
|
+
core_content = """
|
|
46
|
+
import hashlib
|
|
47
|
+
import sqlite3
|
|
48
|
+
import os
|
|
49
|
+
import time
|
|
50
|
+
from pathlib import Path
|
|
51
|
+
from watchdog.observers import Observer
|
|
52
|
+
from watchdog.events import FileSystemEventHandler
|
|
53
|
+
|
|
54
|
+
DB_PATH = Path.home() / ".config" / "rewind" / "rewind.db"
|
|
55
|
+
|
|
56
|
+
def get_db():
|
|
57
|
+
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
58
|
+
conn = sqlite3.connect(DB_PATH)
|
|
59
|
+
conn.row_factory = sqlite3.Row
|
|
60
|
+
conn.execute('''
|
|
61
|
+
CREATE TABLE IF NOT EXISTS versions (
|
|
62
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
63
|
+
path TEXT NOT NULL,
|
|
64
|
+
hash TEXT NOT NULL,
|
|
65
|
+
created_at TEXT NOT NULL
|
|
66
|
+
)
|
|
67
|
+
''')
|
|
68
|
+
return conn
|
|
69
|
+
|
|
70
|
+
def hash_file(filepath):
|
|
71
|
+
hasher = hashlib.sha256()
|
|
72
|
+
try:
|
|
73
|
+
with open(filepath, 'rb') as f:
|
|
74
|
+
for chunk in iter(lambda: f.read(4096), b""):
|
|
75
|
+
hasher.update(chunk)
|
|
76
|
+
return hasher.hexdigest()
|
|
77
|
+
except (PermissionError, FileNotFoundError):
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
def save_version(filepath):
|
|
81
|
+
filepath = str(Path(filepath).resolve())
|
|
82
|
+
file_hash = hash_file(filepath)
|
|
83
|
+
if not file_hash:
|
|
84
|
+
return
|
|
85
|
+
|
|
86
|
+
conn = get_db()
|
|
87
|
+
last = conn.execute("SELECT hash FROM versions WHERE path = ? ORDER BY id DESC LIMIT 1", (filepath,)).fetchone()
|
|
88
|
+
|
|
89
|
+
if last and last["hash"] == file_hash:
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
conn.execute(
|
|
93
|
+
"INSERT INTO versions (path, hash, created_at) VALUES (?, ?, datetime('now'))",
|
|
94
|
+
(filepath, file_hash)
|
|
95
|
+
)
|
|
96
|
+
conn.commit()
|
|
97
|
+
print(f"[rewind] Saved: {filepath}")
|
|
98
|
+
|
|
99
|
+
class RewindHandler(FileSystemEventHandler):
|
|
100
|
+
def on_modified(self, event):
|
|
101
|
+
if not event.is_directory:
|
|
102
|
+
save_version(event.src_path)
|
|
103
|
+
|
|
104
|
+
def start_watcher(path):
|
|
105
|
+
path = str(Path(path).resolve())
|
|
106
|
+
print(f"[rewind] Watching {path}... (Ctrl+C to stop)")
|
|
107
|
+
|
|
108
|
+
for root, _, files in os.walk(path):
|
|
109
|
+
for file in files:
|
|
110
|
+
if file.startswith('.'): continue
|
|
111
|
+
save_version(os.path.join(root, file))
|
|
112
|
+
|
|
113
|
+
observer = Observer()
|
|
114
|
+
observer.schedule(RewindHandler(), path, recursive=True)
|
|
115
|
+
observer.start()
|
|
116
|
+
try:
|
|
117
|
+
while True:
|
|
118
|
+
time.sleep(1)
|
|
119
|
+
except KeyboardInterrupt:
|
|
120
|
+
observer.stop()
|
|
121
|
+
observer.join()
|
|
122
|
+
"""
|
|
123
|
+
write_file(f"{base_dir}/src/rewind/core.py", core_content)
|
|
124
|
+
|
|
125
|
+
# 4. src/rewind/cli.py
|
|
126
|
+
cli_content = """
|
|
127
|
+
import argparse
|
|
128
|
+
from pathlib import Path
|
|
129
|
+
from rich.console import Console
|
|
130
|
+
from rich.table import Table
|
|
131
|
+
from .core import start_watcher, get_db
|
|
132
|
+
|
|
133
|
+
console = Console()
|
|
134
|
+
|
|
135
|
+
def main():
|
|
136
|
+
parser = argparse.ArgumentParser(description="rewind - A passive time machine for your filesystem.")
|
|
137
|
+
subparsers = parser.add_subparsers(dest="command")
|
|
138
|
+
|
|
139
|
+
watch_parser = subparsers.add_parser("watch", help="Watch a folder for changes")
|
|
140
|
+
watch_parser.add_argument("path", help="Path to the folder to watch")
|
|
141
|
+
|
|
142
|
+
log_parser = subparsers.add_parser("log", help="View version history of a file")
|
|
143
|
+
log_parser.add_argument("file", help="Path to the file")
|
|
144
|
+
|
|
145
|
+
args = parser.parse_args()
|
|
146
|
+
|
|
147
|
+
if args.command == "watch":
|
|
148
|
+
start_watcher(args.path)
|
|
149
|
+
|
|
150
|
+
elif args.command == "log":
|
|
151
|
+
conn = get_db()
|
|
152
|
+
filepath = str(Path(args.file).resolve())
|
|
153
|
+
rows = conn.execute("SELECT * FROM versions WHERE path = ? ORDER BY id DESC", (filepath,)).fetchall()
|
|
154
|
+
|
|
155
|
+
if not rows:
|
|
156
|
+
console.print(f"[red]No history found for {filepath}[/red]")
|
|
157
|
+
return
|
|
158
|
+
|
|
159
|
+
table = Table(title=f"History for {args.file}")
|
|
160
|
+
table.add_column("Version", style="cyan")
|
|
161
|
+
table.add_column("Hash", style="magenta")
|
|
162
|
+
table.add_column("Date", style="green")
|
|
163
|
+
|
|
164
|
+
for row in rows:
|
|
165
|
+
table.add_row(str(row["id"]), row["hash"][:8], row["created_at"])
|
|
166
|
+
console.print(table)
|
|
167
|
+
|
|
168
|
+
else:
|
|
169
|
+
parser.print_help()
|
|
170
|
+
|
|
171
|
+
if __name__ == "__main__":
|
|
172
|
+
main()
|
|
173
|
+
"""
|
|
174
|
+
write_file(f"{base_dir}/src/rewind/cli.py", cli_content)
|
|
175
|
+
|
|
176
|
+
# 5. README.md
|
|
177
|
+
readme_content = """
|
|
178
|
+
# rewind
|
|
179
|
+
A passive time machine for your filesystem.
|
|
180
|
+
|
|
181
|
+
## Install
|
|
182
|
+
pip install -e .
|
|
183
|
+
|
|
184
|
+
## Usage
|
|
185
|
+
rewind watch .
|
|
186
|
+
rewind log test.txt
|
|
187
|
+
"""
|
|
188
|
+
write_file(f"{base_dir}/README.md", readme_content)
|
|
189
|
+
|
|
190
|
+
# ---------------------------------------------------------
|
|
191
|
+
# AUTO-FIX THE WINDOWS PATH
|
|
192
|
+
# ---------------------------------------------------------
|
|
193
|
+
print("\n" + "="*50)
|
|
194
|
+
print("Attempting to add pip scripts to Windows PATH...")
|
|
195
|
+
|
|
196
|
+
try:
|
|
197
|
+
# Get the Python executable path
|
|
198
|
+
python_dir = Path(sys.executable).parent
|
|
199
|
+
# The scripts folder is usually next to the python.exe in AppData\Roaming\Python\PythonXXX\Scripts
|
|
200
|
+
scripts_dir = Path(os.environ['APPDATA']) / "Python" / f"Python{sys.version_info.major}{sys.version_info.minor}" / "Scripts"
|
|
201
|
+
|
|
202
|
+
if not scripts_dir.exists():
|
|
203
|
+
# Fallback to local Python directory
|
|
204
|
+
scripts_dir = python_dir / "Scripts"
|
|
205
|
+
|
|
206
|
+
if scripts_dir.exists():
|
|
207
|
+
# Use setx to permanently add it to the User PATH
|
|
208
|
+
subprocess.run(["setx", "PATH", f"%PATH%;{scripts_dir}"], check=True, shell=True)
|
|
209
|
+
print(f"\nSUCCESS! Added {scripts_dir} to your PATH.")
|
|
210
|
+
print("IMPORTANT: You MUST close this terminal and open a NEW one for the command to work.")
|
|
211
|
+
else:
|
|
212
|
+
print(f"\nCould not find the Scripts folder. Please manually add it to your PATH.")
|
|
213
|
+
except Exception as e:
|
|
214
|
+
print(f"\nFailed to auto-update PATH: {e}")
|
|
215
|
+
print("You may need to manually add your Python Scripts folder to Environment Variables.")
|
|
216
|
+
|
|
217
|
+
print("="*50)
|
|
218
|
+
print("\nNext steps:")
|
|
219
|
+
print(f"1. cd {base_dir}")
|
|
220
|
+
print("2. pip install -e .")
|
|
221
|
+
print("3. CLOSE THIS TERMINAL AND OPEN A NEW ONE.")
|
|
222
|
+
print("4. Run: rewind watch .")
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rwnd"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A passive time machine for your filesystem."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
dependencies = [
|
|
13
|
+
"watchdog>=4.0.0",
|
|
14
|
+
"rich>=13.0.0"
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
rewind = "rewind.cli:main"
|
|
19
|
+
|
|
20
|
+
[tool.hatch.build.targets.wheel]
|
|
21
|
+
packages = ["src/rewind"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "rewind"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A passive time machine for your filesystem."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
dependencies = [
|
|
13
|
+
"watchdog>=4.0.0",
|
|
14
|
+
"rich>=13.0.0"
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
rewind = "rewind.cli:main"
|
|
19
|
+
|
|
20
|
+
[tool.hatch.build.targets.wheel]
|
|
21
|
+
packages = ["src/rewind"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -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()
|
|
@@ -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 @@
|
|
|
1
|
+
__version__ = "0.1.0"
|
|
@@ -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()
|
|
@@ -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()
|