-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauto_archive.py
More file actions
61 lines (47 loc) · 1.71 KB
/
Copy pathauto_archive.py
File metadata and controls
61 lines (47 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
"""auto_archive.py — Archive done tasks older than N days.
Usage:
python3 auto_archive.py [--db PATH] [--days N] [--dry-run]
"""
import argparse
import sqlite3
from db_utils import DB_PATH, TaskDAO, get_conn
def dry_run(conn: sqlite3.Connection, days: int) -> None:
rows = conn.execute(
"SELECT id, title, status, updated_at FROM tasks "
"WHERE status = 'done' AND type = 'task' "
"AND updated_at < datetime('now', ? || ' days')",
(f"-{days}",),
).fetchall()
if not rows:
print(f"[dry-run] No tasks would be archived (threshold: {days} days).")
return
print(f"[dry-run] {len(rows)} task(s) would be archived (older than {days} days):")
for row in rows:
print(
f" id={row['id']} status={row['status']} updated_at={row['updated_at']} title={row['title']}"
)
def archive(conn: sqlite3.Connection, days: int) -> None:
archived_ids = TaskDAO.archive_done(conn, days)
print(f"Archived {len(archived_ids)} tasks (older than {days} days).")
def main() -> None:
parser = argparse.ArgumentParser(
description="Archive done tasks older than N days."
)
parser.add_argument("--db", default=DB_PATH, help="Path to the SQLite DB")
parser.add_argument(
"--days", type=int, default=7, help="Threshold in days (default: 7)"
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Show what would be archived without modifying",
)
args = parser.parse_args()
with get_conn(args.db) as conn:
if args.dry_run:
dry_run(conn, args.days)
else:
archive(conn, args.days)
if __name__ == "__main__":
main()