team-ljyzipelo

공개
150줄4.8 KB
1"""고정 크기 버퍼로 합성 압축파일과 현재 main의 기대값을 생성함."""
2
3import argparse
4import gzip
5import hashlib
6import io
7import json
8from pathlib import Path
9import tarfile
10import zipfile
11
12
13ROOT = Path(__file__).resolve().parents[2]
14GIB = 1024 ** 3
15CHUNK = 64 * 1024
16INPUT_LIMIT = 64 * 1024 ** 2
17OUTPUT_LIMIT = 128 * 1024 ** 2
18BASELINE = "d6b1a45"
19NAME = "payload.bin"
20
21
22class ZeroStream(io.RawIOBase):
23 def __init__(self, size):
24 self.remaining = size
25
26 def read(self, size=-1):
27 if size < 0 or size > CHUNK:
28 raise ValueError("표본 읽기는 64 KiB 이하로 제한함")
29 count = min(size, self.remaining)
30 self.remaining -= count
31 return bytes(count)
32
33
34def write_zip(path, size, compressed):
35 info = zipfile.ZipInfo(NAME, (1980, 1, 1, 0, 0, 0))
36 info.compress_type = zipfile.ZIP_DEFLATED if compressed else zipfile.ZIP_STORED
37 info.create_system = 3
38 info.external_attr = 0o100600 << 16
39 info.file_size = size
40 with zipfile.ZipFile(path, "x", compression=info.compress_type) as archive:
41 with archive.open(info, "w") as output:
42 source = ZeroStream(size)
43 while chunk := source.read(CHUNK):
44 output.write(chunk)
45
46
47def tar_size(size):
48 return 512 + ((size + 511) // 512) * 512 + 1024
49
50
51def write_targz(path, size, compressed):
52 info = tarfile.TarInfo(NAME)
53 info.size = size
54 info.mode = 0o600
55 info.mtime = 0
56 with path.open("xb") as raw:
57 with gzip.GzipFile(
58 filename="", mode="wb", fileobj=raw,
59 compresslevel=6 if compressed else 0, mtime=0,
60 ) as output:
61 output.write(info.tobuf(format=tarfile.USTAR_FORMAT))
62 source = ZeroStream(size)
63 while chunk := source.read(CHUNK):
64 output.write(chunk)
65 output.write(bytes((-size) % 512 + 1024))
66
67
68def digest(path):
69 value = hashlib.sha256()
70 with path.open("rb") as source:
71 while chunk := source.read(CHUNK):
72 value.update(chunk)
73 return value.hexdigest()
74
75
76def build(directory, large=False):
77 directory = Path(directory).resolve()
78 directory.relative_to(ROOT / "tests")
79 directory.mkdir(mode=0o700, parents=True, exist_ok=False)
80 cases = []
81 artifacts = {}
82
83 def add(name, size, compressed, category, limits):
84 path = directory / name
85 if name.endswith(".zip"):
86 write_zip(path, size, compressed)
87 expanded = size
88 else:
89 write_targz(path, size, compressed)
90 expanded = tar_size(size)
91 artifacts[name] = {
92 "archive_bytes": path.stat().st_size,
93 "payload_bytes": size,
94 "decoded_bytes": expanded,
95 "sha256": digest(path),
96 }
97 for suffix, output_limit, failed in limits:
98 cases.append({
99 "id": name + "-" + suffix,
100 "file": name,
101 "category": category,
102 "limits": {"input": INPUT_LIMIT, "output": output_limit, "entries": 10000},
103 "expected": {
104 "outcome": "Failed" if failed else "Completed",
105 "error": "Limit" if failed else "None",
106 "publication": "None" if failed else "Complete",
107 "created_count": 0 if failed else 1,
108 "retained_partial_count": 0,
109 "output_events": 0 if failed else 1,
110 },
111 })
112
113 for name, size in (("boundary.zip", CHUNK), ("boundary.tar.gz", CHUNK - 1536)):
114 add(name, size, True, "output-boundary", [
115 ("below", CHUNK + 1, False),
116 ("equal", CHUNK, False),
117 ("above", CHUNK - 1, True),
118 ])
119 if large:
120 for extension in ("zip", "tar.gz"):
121 add("large-input." + extension, GIB, False, "input-limit", [
122 ("default", OUTPUT_LIMIT, True),
123 ])
124 add("large-expansion." + extension, GIB, True, "output-limit", [
125 ("default", OUTPUT_LIMIT, True),
126 ])
127 manifest = {
128 "baseline_commit": BASELINE,
129 "worker_execution_verified": False,
130 "generator_chunk_bytes": CHUNK,
131 "artifacts": artifacts,
132 "cases": cases,
133 }
134 (directory / "expectations.json").write_text(
135 json.dumps(manifest, ensure_ascii=False, indent=4) + "\n", encoding="utf-8"
136 )
137 return manifest
138
139
140def main():
141 parser = argparse.ArgumentParser()
142 parser.add_argument("--output", required=True, type=Path, help="tests 아래의 새 디렉터리")
143 parser.add_argument("--large", action="store_true", help="1 GiB 표본 4개도 생성함")
144 args = parser.parse_args()
145 manifest = build(args.output, args.large)
146 print(f"{len(manifest['cases'])} cases: {args.output / 'expectations.json'}")
147
148
149if __name__ == "__main__":
150 main()