02 / FILES, ACCOUNTS AND SERVICES · LESSON 05

Plan log retention before deleting anything

Create a precisely scoped retention proposal from a supplied file manifest.

15 min · learn at your pace

Separate selection from deletion

A retention script can affect many files at once. Start with a proposal that a person can review. This exercise reads file metadata supplied as dictionaries and returns actions; it never deletes or changes a file.

The scope is files below the exact logs path component whose final suffix is .log, case-insensitively. logs-old/example.log is outside the scope, and logs/example.log.bak has a different final suffix. Use PurePosixPath to reason about the manifest's path components without querying a real filesystem.

Reject ambiguous paths and times

Accept only relative POSIX paths without parent traversal or backslashes. An absolute path or .. component should fail the plan rather than escape its declared root. This is validation of supplied path syntax, not a complete policy for symlinks in a live filesystem.

The cutoff is an aware UTC-comparable timestamp. Remove only records strictly older than it; keep a file exactly at the boundary. Missing, malformed or naive modification times become review actions for in-scope files. Unknown dates do not justify removal.

Normalise paths for the report and sort them so a second run produces the same proposal. A production deletion tool would need a separate apply step, fresh path checks and operational safeguards. Here the useful skill is choosing and explaining the candidate set before any mutation happens.

Try an example.py
from pathlib import PurePosixPath
for value in ["logs/app.log", "logs-old/app.log", "logs/app.log.bak"]:
    path = PurePosixPath(value)
    print(path.parts, path.suffix)
YOUR TURN

Put it into practice.

Define retention_plan(files, cutoff). Each record has path and modified_at. Validate all paths as relative POSIX paths with no .. components or backslashes; invalid paths raise ValueError. Select only nested files under the exact logs component with final .log suffix, case-insensitively. Use aware timestamps: modified_at strictly before cutoff means remove, otherwise keep; invalid/missing/naive modified_at means review. Invalid cutoff raises ValueError. Return normalized path/action rows sorted by path. No filesystem changes.

See an explained solution
One way to solve it.py
from datetime import datetime, timezone

def parse_utc(value):
    moment = datetime.fromisoformat(value)
    if moment.tzinfo is None or moment.utcoffset() is None:
        raise ValueError("Timestamp needs an offset")
    return moment.astimezone(timezone.utc)

from pathlib import PurePosixPath

def retention_plan(files, cutoff):
    boundary = parse_utc(cutoff)
    plan = []
    for record in files:
        raw = record["path"]
        path = PurePosixPath(raw)
        if not raw or path.is_absolute() or ".." in path.parts or "\\" in raw:
            raise ValueError("Use relative POSIX paths without traversal")
        if len(path.parts) < 2 or path.parts[0] != "logs" or path.suffix.lower() != ".log":
            continue
        try:
            modified = parse_utc(record.get("modified_at"))
            action = "remove" if modified < boundary else "keep"
        except (ValueError, TypeError):
            action = "review"
        plan.append({"path": path.as_posix(), "action": action})
    return sorted(plan, key=lambda row: row["path"])

if __name__ == "__main__":
    print(retention_plan([{"path":"logs/app.log", "modified_at":"2026-03-01T00:00:00Z"}], "2026-04-01T00:00:00Z"))

Component-based scope avoids misleading path prefixes. Explicit review actions keep unknown dates out of the proposed removal set.