|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Extract sierra_code sections from cairo e2e test data files. |
| 4 | +
|
| 5 | +Input: test_data/e2e_libfuncs_raw/ (files in //! > format) |
| 6 | +Output: test_data/e2e_sierra/ (individual .sierra files) |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import re |
| 11 | +import shutil |
| 12 | +import sys |
| 13 | + |
| 14 | +SRC_DIR = "test_data/e2e_libfuncs_raw" |
| 15 | +DST_DIR = "test_data/e2e_sierra" |
| 16 | + |
| 17 | + |
| 18 | +def sanitize_name(name): |
| 19 | + """Convert test name to a safe filename.""" |
| 20 | + return re.sub(r"[^\w\-]", "_", name.strip()).strip("_") |
| 21 | + |
| 22 | + |
| 23 | +def parse_test_cases(text): |
| 24 | + """Parse //! > format into list of test-case dicts mapping section_key -> content.""" |
| 25 | + cases = re.split(r"^//! > =+\s*$", text, flags=re.MULTILINE) |
| 26 | + results = [] |
| 27 | + for case in cases: |
| 28 | + sections = {} |
| 29 | + section_order = [] |
| 30 | + current_section = None |
| 31 | + lines = [] |
| 32 | + for line in case.splitlines(): |
| 33 | + m = re.match(r"^//! > (.+)$", line) |
| 34 | + if m: |
| 35 | + if current_section is not None: |
| 36 | + sections[current_section] = "\n".join(lines).strip() |
| 37 | + current_section = m.group(1).strip() |
| 38 | + section_order.append(current_section) |
| 39 | + lines = [] |
| 40 | + else: |
| 41 | + lines.append(line) |
| 42 | + if current_section is not None: |
| 43 | + sections[current_section] = "\n".join(lines).strip() |
| 44 | + if sections: |
| 45 | + sections["_order"] = section_order |
| 46 | + results.append(sections) |
| 47 | + return results |
| 48 | + |
| 49 | + |
| 50 | +def main(): |
| 51 | + if not os.path.isdir(SRC_DIR): |
| 52 | + print( |
| 53 | + f"Source directory {SRC_DIR} not found. " |
| 54 | + "Run 'make pull-external-projects' first." |
| 55 | + ) |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + shutil.rmtree(DST_DIR, ignore_errors=True) |
| 59 | + |
| 60 | + count = 0 |
| 61 | + for dirpath, _, filenames in sorted(os.walk(SRC_DIR)): |
| 62 | + for filename in sorted(filenames): |
| 63 | + filepath = os.path.join(dirpath, filename) |
| 64 | + |
| 65 | + with open(filepath, "r") as f: |
| 66 | + content = f.read() |
| 67 | + |
| 68 | + # Preserve subdirectory structure (e.g. starknet/syscalls) |
| 69 | + rel_dir = os.path.relpath(dirpath, SRC_DIR) |
| 70 | + test_cases = parse_test_cases(content) |
| 71 | + for case in test_cases: |
| 72 | + sierra = case.get("sierra_code", "").strip() |
| 73 | + if not sierra: |
| 74 | + continue |
| 75 | + |
| 76 | + # The test name is the first section key (not a section called "test_name") |
| 77 | + order = case.get("_order", []) |
| 78 | + if not order: |
| 79 | + continue |
| 80 | + test_name = order[0] |
| 81 | + |
| 82 | + safe_name = sanitize_name(test_name) |
| 83 | + if not safe_name: |
| 84 | + continue |
| 85 | + |
| 86 | + out_dir = os.path.join(DST_DIR, rel_dir, filename) |
| 87 | + os.makedirs(out_dir, exist_ok=True) |
| 88 | + out_path = os.path.join(out_dir, f"{safe_name}.sierra") |
| 89 | + |
| 90 | + with open(out_path, "w") as f: |
| 91 | + f.write(sierra + "\n") |
| 92 | + count += 1 |
| 93 | + |
| 94 | + print(f"Extracted {count} sierra files into {DST_DIR}/") |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments