File size: 2,083 Bytes
ae5bbef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""Shared configuration and helpers for set_option scripts."""

import re
import subprocess
from pathlib import Path

PROJECT_DIR = Path(__file__).resolve().parent.parent

DEFAULT_OPTIONS = [
    "backward.isDefEq.respectTransparency",
    "backward.whnf.reducibleClassField",
    "backward.inferInstanceAs.wrap",
]


def set_option_line(option: str, value: str = "false") -> str:
    """Return the set_option line text for an option."""
    return f"set_option {option} {value} in\n"


def removable_pattern(option: str, value: str = "false") -> re.Pattern:
    """Match bare `set_option X <value> in` lines (no trailing comment)."""
    escaped = re.escape(option)
    escaped_val = re.escape(value)
    return re.compile(rf"^\s*set_option {escaped} {escaped_val} in\s*$")


def commented_pattern(option: str, value: str = "false") -> re.Pattern:
    """Match `set_option X <value> in -- ...` lines."""
    escaped = re.escape(option)
    escaped_val = re.escape(value)
    return re.compile(rf"^\s*set_option {escaped} {escaped_val} in\s+--")


def lakefile_pattern(option: str, value: str = "false") -> re.Pattern:
    """Match lakefile entries for an option."""
    escaped = re.escape(option)
    escaped_val = re.escape(value)
    return re.compile(
        rf"^\s*⟨`{escaped},\s*{escaped_val}⟩,?\s*\n",
        re.MULTILINE,
    )


PROGRESS_RE = re.compile(r"\[(\d+)/(\d+)\]")


def lake_build_with_progress(project_dir: Path) -> tuple[int, str]:
    """Run ``lake build`` with a progress display.

    Returns (returncode, captured_output).
    """
    proc = subprocess.Popen(
        ["lake", "build"],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        cwd=project_dir,
    )
    output_lines: list[str] = []
    for line in proc.stdout:
        output_lines.append(line)
        m = PROGRESS_RE.search(line)
        if m:
            print(f"\r  [{m.group(1)}/{m.group(2)}]", end="", flush=True)
    proc.wait()
    print()  # newline after progress
    return proc.returncode, "".join(output_lines)