|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Generate test files with various filenames for HTTPDirFS integration tests. |
| 3 | +
|
| 4 | +Creates a set of files with known content and checksums to verify filesystem |
| 5 | +correctness after mounting with httpdirfs. Filenames include |
| 6 | +non-alphanumeric characters, spaces, and other edge cases. |
| 7 | +
|
| 8 | +File sizes are randomised between 1 KB and 10 MB using a deterministic seed |
| 9 | +so that the manifest is reproducible across runs. |
| 10 | +""" |
| 11 | + |
| 12 | +import hashlib |
| 13 | +import json |
| 14 | +import os |
| 15 | +import random |
| 16 | +import sys |
| 17 | + |
| 18 | +# Fixed seed so that file sizes are reproducible across runs, while still |
| 19 | +# being "random" in the sense that they cover a wide range. |
| 20 | +RNG_SEED = 20260518 |
| 21 | + |
| 22 | +# 64 KB write chunk — balances memory usage and I/O throughput |
| 23 | +CHUNK_SIZE = 64 * 1024 |
| 24 | + |
| 25 | + |
| 26 | +def write_deterministic_file(filepath, name, size): |
| 27 | + """Write a file with deterministic content and return its SHA-256. |
| 28 | +
|
| 29 | + Uses a name-derived seed with Python's Mersenne Twister PRNG. |
| 30 | + Streams to disk in 64 KB chunks so even multi-GB files use constant |
| 31 | + memory. |
| 32 | + """ |
| 33 | + if size == 0: |
| 34 | + with open(filepath, "wb"): |
| 35 | + pass |
| 36 | + return hashlib.sha256(b"").hexdigest() |
| 37 | + |
| 38 | + seed_hash = hashlib.sha256(name.encode()).digest() |
| 39 | + file_rng = random.Random(int.from_bytes(seed_hash[:8], "big")) |
| 40 | + h = hashlib.sha256() |
| 41 | + written = 0 |
| 42 | + |
| 43 | + with open(filepath, "wb") as f: |
| 44 | + while written < size: |
| 45 | + n = min(CHUNK_SIZE, size - written) |
| 46 | + chunk = file_rng.randbytes(n) |
| 47 | + f.write(chunk) |
| 48 | + h.update(chunk) |
| 49 | + written += n |
| 50 | + |
| 51 | + return h.hexdigest() |
| 52 | + |
| 53 | + |
| 54 | +def random_size(rng): |
| 55 | + """Return a random file size between 1 KB and 10 MB.""" |
| 56 | + return rng.randint(1024, 10 * 1024 * 1024) |
| 57 | + |
| 58 | + |
| 59 | +def format_size(size): |
| 60 | + """Human-readable size string.""" |
| 61 | + if size >= 1024 * 1024 * 1024: |
| 62 | + return f"{size / (1024**3):.1f} GB" |
| 63 | + if size >= 1024 * 1024: |
| 64 | + return f"{size / (1024**2):.1f} MB" |
| 65 | + if size >= 1024: |
| 66 | + return f"{size / 1024:.1f} KB" |
| 67 | + return f"{size} bytes" |
| 68 | + |
| 69 | + |
| 70 | +def main(): |
| 71 | + if len(sys.argv) < 2: |
| 72 | + print(f"Usage: {sys.argv[0]} <output_directory> [--large]", |
| 73 | + file=sys.stderr) |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + output_dir = sys.argv[1] |
| 77 | + include_large = "--large" in sys.argv |
| 78 | + os.makedirs(output_dir, exist_ok=True) |
| 79 | + |
| 80 | + rng = random.Random(RNG_SEED) |
| 81 | + |
| 82 | + # Filenames that exercise interesting edge cases. Each file gets a |
| 83 | + # random size between 1 KB and 10 MB. |
| 84 | + filenames = [ |
| 85 | + # Basic ASCII filenames |
| 86 | + "simple.txt", |
| 87 | + "UPPERCASE.DAT", |
| 88 | + |
| 89 | + # Filenames with spaces |
| 90 | + "file with spaces.txt", |
| 91 | + "multiple spaces here.bin", |
| 92 | + |
| 93 | + # Non-alphanumeric characters that are valid in URLs |
| 94 | + "file-with-dashes.txt", |
| 95 | + "file_with_underscores.txt", |
| 96 | + "file.multiple.dots.txt", |
| 97 | + "file~tilde.txt", |
| 98 | + |
| 99 | + # Percent-encoded characters in HTTP (common edge cases) |
| 100 | + "parens(1).txt", |
| 101 | + "brackets[2].txt", |
| 102 | + "curly{3}.txt", |
| 103 | + "hash#tag.txt", |
| 104 | + "at@sign.txt", |
| 105 | + "plus+plus.txt", |
| 106 | + "equals=value.txt", |
| 107 | + "comma,separated.txt", |
| 108 | + "semi;colon.txt", |
| 109 | + "exclaim!mark.txt", |
| 110 | + "single'quote.txt", |
| 111 | + |
| 112 | + # Mixed case and numbers |
| 113 | + "CamelCase123.txt", |
| 114 | + "MiXeD_cAsE-456.dat", |
| 115 | + |
| 116 | + # Longer filenames |
| 117 | + "a" * 200 + ".txt", |
| 118 | + ] |
| 119 | + |
| 120 | + # Build (filename, size) pairs with random sizes |
| 121 | + test_files = [(name, random_size(rng)) for name in filenames] |
| 122 | + |
| 123 | + # Keep a few deterministic edge-case sizes |
| 124 | + test_files.append(("empty_file.txt", 0)) |
| 125 | + test_files.append(("tiny.txt", 1)) |
| 126 | + |
| 127 | + # Add the 1 GB file for cache system testing when --large is given |
| 128 | + if include_large: |
| 129 | + test_files.append(("large_1g.bin", 1024 * 1024 * 1024)) |
| 130 | + |
| 131 | + manifest = {} |
| 132 | + |
| 133 | + for filename, size in test_files: |
| 134 | + filepath = os.path.join(output_dir, filename) |
| 135 | + checksum = write_deterministic_file(filepath, filename, size) |
| 136 | + |
| 137 | + manifest[filename] = { |
| 138 | + "size": size, |
| 139 | + "sha256": checksum, |
| 140 | + } |
| 141 | + print(f" Created: {filename} ({format_size(size)}," |
| 142 | + f" sha256={checksum[:16]}...)") |
| 143 | + |
| 144 | + # Also create a subdirectory with files (random sizes too) |
| 145 | + subdir = os.path.join(output_dir, "subdir with spaces") |
| 146 | + os.makedirs(subdir, exist_ok=True) |
| 147 | + |
| 148 | + subdir_filenames = [ |
| 149 | + "nested file.txt", |
| 150 | + "deep-data.bin", |
| 151 | + ] |
| 152 | + |
| 153 | + for filename in subdir_filenames: |
| 154 | + size = random_size(rng) |
| 155 | + filepath = os.path.join(subdir, filename) |
| 156 | + seed_name = f"subdir/{filename}" |
| 157 | + checksum = write_deterministic_file(filepath, seed_name, size) |
| 158 | + |
| 159 | + relative = os.path.join("subdir with spaces", filename) |
| 160 | + manifest[relative] = { |
| 161 | + "size": size, |
| 162 | + "sha256": checksum, |
| 163 | + } |
| 164 | + print(f" Created: {relative} ({format_size(size)}," |
| 165 | + f" sha256={checksum[:16]}...)") |
| 166 | + |
| 167 | + # Write manifest |
| 168 | + manifest_path = os.path.join(output_dir, "manifest.json") |
| 169 | + with open(manifest_path, "w") as f: |
| 170 | + json.dump(manifest, f, indent=2, sort_keys=True) |
| 171 | + print(f"\nManifest written to {manifest_path}") |
| 172 | + print(f"Total files: {len(manifest)}") |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + main() |
0 commit comments