#!/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.defeqAttrib.useBackward",
"backward.isDefEq.respectTransparency",
"backward.isDefEq.respectTransparency.types",
"backward.isDefEq.respectTransparency.instanceSearchTypes",
"backward.privateInPublic",
"backward.whnf.reducibleClassField",
"backward.inferInstanceAs.wrap",
"backward.inferInstanceAs.wrap.data",
"backward.inferInstanceAs.wrap.instances",
"linter.auxLemma",
"linter.checkUnivs",
"linter.flexible",
"linter.style.longFile",
"linter.translate.overwrite",
"linter.translate.warnInvalid",
"linter.unusedDecidableInType",
"linter.unusedVariables",
"linter.unusedTactic",
]
# Options that change what a declaration exports (its body, or the visibility of its
# auxiliary declarations), so downstream modules can depend on them.
# `rm_set_option.py` only rebuilds the modules it edits, so it cannot check that removing
# these is safe: it skips them unless one is requested explicitly with `--option`.
EXPORT_AFFECTING_OPTIONS = [
"backward.privateInPublic",
"backward.inferInstanceAs.wrap",
"backward.inferInstanceAs.wrap.data",
"backward.inferInstanceAs.wrap.instances",
]
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) -> re.Pattern:
"""Match bare `set_option X <value> in` lines (no trailing comment).
|value| can be a boolean (true or false), number or string (not containing `"`)."""
escaped = re.escape(option)
return re.compile(rf'^\s*set_option {escaped} (true|false|\d+|"[^"]*") in\s*$')
def commented_pattern(option: str) -> re.Pattern:
"""Match `set_option X <value> in -- ...` lines.
|value| can be a boolean (true or false), number or string (not containing `"`)."""
escaped = re.escape(option)
return re.compile(rf'^\s*set_option {escaped} (true|false|\d+|"[^"]*") in\s+--')
def is_annotated(lines: list[str], idx: int) -> bool:
"""Check whether line `idx` is immediately preceded by a `--` comment
or by a (possibly multi-line) `/-- ... -/` doc comment or `/- ... -/` block comment.
"""
if idx == 0:
return False
prev = lines[idx - 1]
if re.search(r"^\s*--", prev):
return True
if re.search(r"-/\s*$", prev):
return True
return False
def lakefile_pattern(option: str) -> re.Pattern:
"""Match lakefile entries for an option."""
escaped = re.escape(option)
return re.compile(
rf'^\s*⟨\s*`{escaped}\s*,\s*(true|false|\.ofNat\s+\d+|"[^"]*")\s*⟩\s*,?\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", "--iofail"],
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)
¤ Dauer der Verarbeitung: 0.3 Sekunden
¤
*© Formatika GbR, Deutschland