"""Generate the access.log fixture for the access-log-report task. Deterministic: same bytes every run. The fixture is deliberately awkward -- one user-agent carries escaped quotes, two byte counts are "-", four lines are malformed in four different ways, and four request paths carry query strings. Run with: python tools/mk_access_log.py """ from pathlib import Path OUT = ( Path(__file__).resolve().parent.parent / "tasks" / "access-log-report" / "environment" / "logs" / "access.log" ) MS = [ 3, 7, 11, 12, 14, 18, 21, 25, 28, 31, 35, 40, 44, 47, 52, 55, 58, 61, 66, 70, 74, 78, 82, 85, 90, 95, 99, 104, 110, 118, 126, 133, 141, 150, 162, 175, ] PATHS = ( ["/index.html"] * 12 + ["/api/users"] * 8 + ["/api/users?id=42"] * 4 + ["/static/app.js"] * 6 + ["/health"] * 3 + ["/api/orders"] * 3 ) STATUS = [200] * 28 + [304] * 4 + [404] * 3 + [500] MALFORMED = { 7: '10.0.0.9 - - [10/Oct/2026:13:02:11 +0000] "GET /index.html HTTP/1.1" 200 512 "-" "curl/8.0"', 19: '10.0.0.3 - - [10/Oct/2026:13:05:44 +0000] "POST /api/users HTTP/1.1" abc 300 "-" "curl/8.0" 44', 28: '10.0.0.4 - - [10/Oct/2026:13:09:02 +0000] "GET /health HTTP/1.1" 200 0', 37: "# rotated by logrotate at 2026-10-10T14:00:00Z", } UA_WEIRD = 'Mozilla/5.0 (\\"compatible\\"; bot) Gecko/20100101' def build(): valid = [] for index in range(36): path = PATHS[index] method = "GET" if path == "/api/users" and index % 5 == 0: method = "POST" status = STATUS[index] if index in (9, 26): size = "-" else: size = str(200 + index * 13) ms = MS[(index * 7) % 36] referer = "-" if index % 4 == 0 else "https://example.com/" ua = UA_WEIRD if index in (5, 17) else "curl/8.5.0" line = ( '10.0.0.%d - - [10/Oct/2026:13:%02d:%02d +0000] "%s %s HTTP/1.1" %d %s "%s" "%s" %d' % (index % 9 + 1, index // 2, (index * 17) % 60, method, path, status, size, referer, ua, ms) ) valid.append(line) return valid def main(): lines = build() out = [] source = iter(lines) for position in range(1, 41): if position in MALFORMED: out.append(MALFORMED[position]) else: out.append(next(source)) OUT.parent.mkdir(parents=True, exist_ok=True) with open(OUT, "w", encoding="utf-8", newline="\n") as handle: handle.write("\n".join(out) + "\n") print("wrote %s (%d lines, %d valid)" % (OUT, len(out), len(lines))) if __name__ == "__main__": main()