Skip to content

Commit 47d7451

Browse files
committed
Print a custom error message if the package exists
1 parent fea4cc4 commit 47d7451

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

pyk/src/pyk/klean/__main__.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from argparse import ArgumentParser
4+
from dataclasses import dataclass
45
from pathlib import Path
56
from typing import TYPE_CHECKING
67

@@ -12,6 +13,12 @@
1213
from collections.abc import Iterable
1314

1415

16+
@dataclass(frozen=True)
17+
class CliError(Exception):
18+
message: str
19+
status: int
20+
21+
1522
def main() -> None:
1623
import logging
1724
import sys
@@ -22,7 +29,15 @@ def main() -> None:
2229
level, args = _extract_log_level(args)
2330
logging.basicConfig(level=level, format=LOG_FORMAT)
2431

25-
klean(args)
32+
try:
33+
klean(args)
34+
except CliError as err:
35+
match err:
36+
case CliError(message, status):
37+
print(f'Error: {message}', file=sys.stderr)
38+
raise SystemExit(status) from err
39+
case _:
40+
raise AssertionError() from None
2641

2742

2843
def _extract_log_level(args: list[str]) -> tuple[int, list[str]]:
@@ -39,13 +54,18 @@ def klean(args: Iterable[str]) -> None:
3954
from .generate import generate
4055

4156
ns = _parse_args(args)
57+
output_dir = ns.output_dir or Path()
58+
package_dir = output_dir / ns.package_name
59+
if package_dir.exists():
60+
raise CliError(f'Directory exists: {package_dir}', 115)
61+
4262
defn = _load_defn(ns.definition_dir)
4363
if ns.rules:
4464
defn = defn.filter_rewrites(ns.rules)
4565
defn = defn.project_to_rewrites()
4666
generate(
4767
defn=defn,
48-
output_dir=ns.output_dir,
68+
output_dir=output_dir,
4969
context={
5070
'package_name': ns.package_name,
5171
'library_name': ns.library_name,

pyk/src/pyk/klean/generate.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ class GenContext(TypedDict):
1818

1919

2020
def generate(
21+
*,
2122
defn: KoreDefn,
2223
context: GenContext,
23-
*,
24-
output_dir: str | Path | None = None,
24+
output_dir: Path,
2525
) -> Path:
26-
output_dir = Path(output_dir) if output_dir is not None else Path('.')
27-
2826
k2lean4 = K2Lean4(defn)
2927
genmodel = {
3028
'Sorts': (k2lean4.sort_module, ['Prelude']),

0 commit comments

Comments
 (0)