Answer stability round 1: 1 🔴, 3 🟡 (0 misread); sort the new files' imports, sync the gzip before removing the file, read the plain file then its gzip without a check between, and age a file by the later of its mtime and ctime
This commit is contained in:
@@ -1,29 +1,32 @@
|
||||
"""Compress every file of loop-claude's archive and the review folders that no tool writes to any more, keeping each one.
|
||||
"""Gzip each file of loop-claude's archive and the review folders that no tool changes any more, keeping all of them.
|
||||
|
||||
usage: compress-archives
|
||||
|
||||
It gzips each file last written over 30 days ago, beside its plain name as <name>.gz with the same
|
||||
times: in loop-claude's archive, $LOOP_CLAUDE_TRANSCRIPTS (default ~/.claude/loop-claude-transcripts),
|
||||
each transcript (*.jsonl); in the review folders, ~/.local/state/larv-review, every file. It writes
|
||||
the gzip, reads it back, and removes the plain file only once the gzip reads back its bytes. Every
|
||||
tool that reads these folders reads a gzipped file by its plain name. Install's crontab line runs it
|
||||
daily.
|
||||
It gzips each file that last changed over 30 days ago: in loop-claude's archive,
|
||||
$LOOP_CLAUDE_TRANSCRIPTS (default ~/.claude/loop-claude-transcripts), each transcript, *.jsonl;
|
||||
in the review folders, ~/.local/state/larv-review, every file. A file's last change is the later
|
||||
of its mtime and ctime, so a copied or unpacked file counts from when it landed.
|
||||
|
||||
It prints how many files it compressed. Exit 1, saying why, at the first file it cannot compress,
|
||||
which it keeps plain with every file after it, or on any argument but --help.
|
||||
The gzip lands beside the file as <name>.gz, with the same times. The file is removed only once
|
||||
its gzip reads back the same bytes. The archive's readers and review-replay read a gzipped file by
|
||||
its plain name. README.md → Install has the crontab line that runs it daily.
|
||||
|
||||
It prints how many files it compressed. It exits 1, saying why, on any argument but --help or -h,
|
||||
and at the first file it cannot compress, leaving that file and every one after it plain.
|
||||
"""
|
||||
import os
|
||||
import pathlib
|
||||
import stat
|
||||
import sys
|
||||
import time
|
||||
import typing
|
||||
|
||||
from transcript_archive.location import archive_root
|
||||
from text_files.compressed import compress, SUFFIX
|
||||
from failure import Failure
|
||||
from review_rules.review_folder import REPORTS
|
||||
from text_files.compressed import compress, SUFFIX
|
||||
from transcript_archive.location import archive_root
|
||||
|
||||
# Why 30 days: docs/decisions.md → `archives: loop-claude's archive and the review folders keep every file, and gzip each one 30 days after it was last written`.
|
||||
# Why 30 days: docs/decisions.md → `archives: gzip each transcript in loop-claude's archive and each file in the review folders 30 days after it last changed, keeping all of them`.
|
||||
AGE_DAYS = 30
|
||||
|
||||
|
||||
@@ -35,9 +38,9 @@ def main(argv: list[str], env: typing.Mapping[str, str] = os.environ, home: path
|
||||
print(f"unknown arguments {' '.join(argv)}; usage: compress-archives", file=sys.stderr)
|
||||
return 1
|
||||
home = home or pathlib.Path.home()
|
||||
before = (time.time() if now is None else now) - AGE_DAYS * 24 * 60 * 60
|
||||
cutoff = (time.time() if now is None else now) - AGE_DAYS * 24 * 60 * 60
|
||||
count = 0
|
||||
for path in [*old_files(archive_root(home, env), "*.jsonl", before), *old_files(home / REPORTS, "*", before)]:
|
||||
for path in [*old_files(archive_root(home, env), "*.jsonl", cutoff), *old_files(home / REPORTS, "*", cutoff)]:
|
||||
if failed := compress(path):
|
||||
print(failed.reason, file=sys.stderr)
|
||||
return 1
|
||||
@@ -46,14 +49,17 @@ def main(argv: list[str], env: typing.Mapping[str, str] = os.environ, home: path
|
||||
return 0
|
||||
|
||||
|
||||
def old_files(root: pathlib.Path, pattern: str, before: float) -> list[pathlib.Path]:
|
||||
"""Each plain file under root that pattern matches, last written before the time before, sorted; a link is left alone, since its target lies elsewhere."""
|
||||
def old_files(root: pathlib.Path, pattern: str, cutoff: float) -> list[pathlib.Path]:
|
||||
"""Each plain file under root that pattern matches and that last changed before the cutoff, sorted; a link is left alone, since its target lies elsewhere."""
|
||||
found = []
|
||||
for path in root.rglob(pattern):
|
||||
if path.name.endswith(SUFFIX) or path.is_symlink():
|
||||
continue
|
||||
try:
|
||||
if not path.name.endswith(SUFFIX) and path.is_file() and not path.is_symlink() and path.stat().st_mtime < before:
|
||||
found.append(path)
|
||||
status = path.stat()
|
||||
# Removed while the walk ran, such as a file of review-replay's copy of a review folder.
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
if stat.S_ISREG(status.st_mode) and max(status.st_mtime, status.st_ctime) < cutoff:
|
||||
found.append(path)
|
||||
return sorted(found)
|
||||
|
||||
Reference in New Issue
Block a user