Skip to content

Commit db4b053

Browse files
Add options classes and use in functions (#349)
* Add options classes and use in functions * Set Version: 0.2.47 * Remove default values from argparse calls * Fix test * Fix output format in test * Fix test * Fix test * Wrap llvm and haskell dir in Path * Fix formatting * Try fixing compiletest-rs tests --------- Co-authored-by: devops <devops@runtimeverification.com>
1 parent c97b91c commit db4b053

File tree

11 files changed

+321
-239
lines changed

11 files changed

+321
-239
lines changed

kmir/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
44

55
[tool.poetry]
66
name = "kmir"
7-
version = "0.2.46"
7+
version = "0.2.47"
88
description = ""
99
authors = [
1010
"Runtime Verification, Inc. <contact@runtimeverification.com>",

kmir/src/kmir/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from .kmir import KMIR
44

5-
VERSION: Final = '0.2.46'
5+
VERSION: Final = '0.2.47'

kmir/src/kmir/__main__.py

Lines changed: 85 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import logging
22
import sys
33
from argparse import Namespace
4-
from pathlib import Path
5-
from typing import Any, Final, Iterable, Optional
4+
from typing import Any, Final, Iterable
65

6+
from pyk.cli.args import LoggingOptions
77
from pyk.proof import APRProof
88
from pyk.proof.reachability import APRFailureInfo
99
from pyk.utils import BugReport, check_dir_path, check_file_path
1010

1111
from . import VERSION
1212
from .cli import create_argument_parser
1313
from .kmir import KMIR
14-
from .parse import parse
15-
from .prove import get_claim_labels, prove, show_proof, view_proof
16-
from .run import run
17-
from .utils import NodeIdLike
14+
from .parse import ParseOptions, parse
15+
from .prove import ProveOptions, ShowProofOptions, ViewProofOptions, get_claim_labels, prove, show_proof, view_proof
16+
from .run import RunOptions, run
1817

1918
_LOGGER: Final = logging.getLogger(__name__)
2019
_LOG_FORMAT: Final = '%(levelname)s %(asctime)s %(name)s - %(message)s'
@@ -25,128 +24,120 @@ def main() -> None:
2524
args = parser.parse_args()
2625
logging.basicConfig(level=_loglevel(args), format=_LOG_FORMAT)
2726

27+
stripped_args = {
28+
key: val for (key, val) in vars(args).items() if val is not None and not (isinstance(val, Iterable) and not val)
29+
}
30+
options = generate_options(stripped_args)
31+
2832
executor_name = 'exec_' + args.command.lower().replace('-', '_')
2933
if executor_name not in globals():
3034
raise AssertionError(f'Unimplemented command: {args.command}')
3135

3236
execute = globals()[executor_name]
33-
execute(**vars(args))
37+
execute(options)
38+
39+
40+
def generate_options(args: dict[str, Any]) -> LoggingOptions:
41+
command = args['command']
42+
match command:
43+
case 'version':
44+
return VersionOptions(args)
45+
case 'parse':
46+
return ParseOptions(args)
47+
case 'run':
48+
return RunOptions(args)
49+
case 'prove':
50+
return ProveOptions(args)
51+
case 'show-proof':
52+
return ShowProofOptions(args)
53+
case 'view-proof':
54+
return ViewProofOptions(args)
55+
case _:
56+
raise ValueError(f'Unrecognized command: {command}')
57+
3458

59+
class VersionOptions(LoggingOptions): ...
3560

36-
def exec_version(**kwargs: Any) -> None:
61+
62+
def exec_version(options: VersionOptions) -> None:
3763
print(f'KMIR Version: {VERSION}')
3864

3965

40-
def exec_parse(
41-
mir_file: Path,
42-
input: str,
43-
output: str,
44-
definition_dir: Optional[Path] = None,
45-
**kwargs: Any,
46-
) -> None:
47-
check_file_path(mir_file)
66+
def exec_parse(options: ParseOptions) -> None:
67+
check_file_path(options.mir_file)
4868

49-
if definition_dir is None:
69+
if options.definition_dir is None:
5070
raise RuntimeError('Cannot find KMIR LLVM definition, please specify --definition-dir, or KMIR_LLVM_DIR')
5171
else:
5272
# llvm_dir = Path(definition_dir)
53-
check_dir_path(definition_dir)
73+
check_dir_path(options.definition_dir)
5474

55-
kmir = KMIR(definition_dir)
75+
kmir = KMIR(options.definition_dir)
5676
# _LOGGER.log( 'Call parser at {definition_dir}')
57-
parse(kmir, mir_file, output=output)
77+
parse(kmir, options)
5878
# print(proc_res.stdout) if output != 'none' else None
5979

6080

61-
def exec_run(
62-
mir_file: Path,
63-
output: str,
64-
definition_dir: Optional[Path] = None,
65-
depth: Optional[int] = None,
66-
bug_report: bool = False,
67-
# ignore_return_code: bool = False,
68-
**kwargs: Any,
69-
) -> None:
81+
def exec_run(options: RunOptions) -> None:
7082
# mir_file = Path(input_file)
71-
check_file_path(mir_file)
83+
check_file_path(options.mir_file)
7284

73-
if definition_dir is None:
85+
if options.definition_dir is None:
7486
raise RuntimeError('Cannot find KMIR LLVM definition, please specify --definition-dir, or KMIR_LLVM_DIR')
7587
else:
7688
# llvm_dir = Path(definition_dir)
77-
check_dir_path(definition_dir)
89+
check_dir_path(options.definition_dir)
7890

79-
if depth is not None:
80-
assert depth < 0, ValueError(f'Argument "depth" must be non-negative, got: {depth}')
91+
if options.depth is not None:
92+
assert options.depth < 0, ValueError(f'Argument "depth" must be non-negative, got: {options.depth}')
8193

82-
if bug_report:
83-
br = BugReport(mir_file.with_suffix('.bug_report.tar'))
84-
kmir = KMIR(definition_dir, bug_report=br)
94+
if options.bug_report:
95+
br = BugReport(options.mir_file.with_suffix('.bug_report.tar'))
96+
kmir = KMIR(options.definition_dir, bug_report=br)
8597
else:
86-
kmir = KMIR(definition_dir)
87-
88-
run(kmir, mir_file, depth=depth, output=output)
89-
90-
91-
def exec_prove(
92-
spec_file: Path,
93-
smt_timeout: int,
94-
smt_retry_limit: int,
95-
claim_list: bool = False,
96-
claim: Optional[str] = None,
97-
definition_dir: Optional[Path] = None,
98-
haskell_dir: Optional[Path] = None,
99-
use_booster: bool = True,
100-
bug_report: bool = False,
101-
save_directory: Optional[Path] = None,
102-
reinit: bool = False,
103-
depth: Optional[int] = None,
104-
trace_rewrites: bool = False,
105-
**kwargs: Any,
106-
) -> None:
98+
kmir = KMIR(options.definition_dir)
99+
100+
run(kmir, options)
101+
102+
103+
def exec_prove(options: ProveOptions) -> None:
107104
# TODO: workers
108105
# TODO: md_selector doesn't work
109106

110-
check_file_path(spec_file)
107+
check_file_path(options.spec_file)
111108

112-
if definition_dir is None:
109+
if options.definition_dir is None:
113110
raise RuntimeError('Cannot find KMIR LLVM definition, please specify --definition-dir, or KMIR_LLVM_DIR')
114111
else:
115112
# llvm_dir = Path(definition_dir)
116-
check_dir_path(definition_dir)
113+
check_dir_path(options.definition_dir)
117114

118-
if haskell_dir is None:
115+
if options.haskell_dir is None:
119116
raise RuntimeError('Cannot find KMIR Haskell definition, please specify --haskell-dir, or KMIR_HASKELL_DIR')
120117
else:
121118
# haskell_dir = Path(haskell_dir)
122-
check_dir_path(haskell_dir)
119+
check_dir_path(options.haskell_dir)
123120

124121
# if the save_directory is not provided, the proofs are saved to the same directory of spec_file
122+
save_directory = options.save_directory
125123
if save_directory is None:
126-
save_directory = spec_file.parent
124+
save_directory = options.spec_file.parent
127125
else:
128126
check_dir_path(save_directory)
129127

130-
br = BugReport(spec_file.with_suffix('.bug_report.tar')) if bug_report else None
131-
kmir = KMIR(definition_dir, haskell_dir=haskell_dir, use_booster=use_booster, bug_report=br)
128+
br = BugReport(options.spec_file.with_suffix('.bug_report.tar')) if options.bug_report else None
129+
kmir = KMIR(options.definition_dir, haskell_dir=options.haskell_dir, use_booster=options.use_booster, bug_report=br)
132130
# We provide configuration of which backend to use in a KMIR object.
133131
# `use_booster` is by default True, where Booster Backend is always used unless turned off
134132

135-
if claim_list:
136-
claim_labels = get_claim_labels(kmir, spec_file)
133+
if options.claim_list:
134+
claim_labels = get_claim_labels(kmir, options.spec_file)
137135
print(*claim_labels, sep='\n')
138136
sys.exit(0)
139137

140138
(passed, failed) = prove(
141139
kmir,
142-
spec_file,
143-
claim_label=claim,
144-
save_directory=save_directory,
145-
reinit=reinit,
146-
depth=depth,
147-
smt_timeout=smt_timeout,
148-
smt_retry_limit=smt_retry_limit,
149-
trace_rewrites=trace_rewrites,
140+
options,
150141
)
151142

152143
for proof in passed:
@@ -166,82 +157,53 @@ def exec_prove(
166157
sys.exit(1)
167158

168159

169-
def exec_show_proof(
170-
claim_label: str,
171-
proof_dir: Optional[Path] = None,
172-
definition_dir: Optional[Path] = None,
173-
haskell_dir: Optional[Path] = None,
174-
nodes: Iterable[NodeIdLike] = (),
175-
node_deltas: Iterable[tuple[NodeIdLike, NodeIdLike]] = (),
176-
failure_info: bool = False,
177-
to_module: bool = False,
178-
# minimize: bool = True,
179-
pending: bool = False,
180-
failing: bool = False,
181-
counterexample_info: bool = False,
182-
**kwargs: Any,
183-
) -> None:
184-
if proof_dir is None:
160+
def exec_show_proof(options: ShowProofOptions) -> None:
161+
if options.proof_dir is None:
185162
raise RuntimeError('Proof directory is not specified, please provide the directory to all the proofs')
186163
else:
187-
check_dir_path(proof_dir)
164+
check_dir_path(options.proof_dir)
188165

189-
if definition_dir is None:
166+
if options.definition_dir is None:
190167
raise RuntimeError('Cannot find KMIR LLVM definition, please specify --definition-dir, or KMIR_LLVM_DIR')
191168
else:
192-
check_dir_path(definition_dir)
169+
check_dir_path(options.definition_dir)
193170

194-
if haskell_dir is None:
171+
if options.haskell_dir is None:
195172
raise RuntimeError('Cannot find KMIR Haskell definition, please specify --haskell-dir, or KMIR_HASKELL_DIR')
196173
else:
197-
check_dir_path(haskell_dir)
174+
check_dir_path(options.haskell_dir)
198175

199-
kmir = KMIR(definition_dir, haskell_dir=haskell_dir)
176+
kmir = KMIR(options.definition_dir, haskell_dir=options.haskell_dir)
200177

201178
show_output = show_proof(
202179
kmir,
203-
claim_label,
204-
proof_dir,
205-
nodes,
206-
node_deltas,
207-
to_module,
208-
failure_info,
209-
pending,
210-
failing,
211-
counterexample_info,
180+
options,
212181
)
213182

214183
print(show_output)
215184

216185

217-
def exec_view_proof(
218-
claim_label: str,
219-
proof_dir: Optional[Path] = None,
220-
definition_dir: Optional[Path] = None,
221-
haskell_dir: Optional[Path] = None,
222-
**kwargs: Any,
223-
) -> None:
186+
def exec_view_proof(options: ViewProofOptions) -> None:
224187
# TODO: include dirs
225188

226-
if proof_dir is None:
189+
if options.proof_dir is None:
227190
raise RuntimeError('Proof directory is not specified, please provide the directory to all the proofs')
228191
else:
229-
check_dir_path(proof_dir)
230-
if definition_dir is None:
192+
check_dir_path(options.proof_dir)
193+
if options.definition_dir is None:
231194
raise RuntimeError('Cannot find KMIR LLVM definition, please specify --definition-dir, or KMIR_LLVM_DIR')
232195
else:
233-
check_dir_path(definition_dir)
234-
if haskell_dir is None:
196+
check_dir_path(options.definition_dir)
197+
if options.haskell_dir is None:
235198
raise RuntimeError('Cannot find KMIR Haskell definition, please specify --haskell-dir, or KMIR_HASKELL_DIR')
236199
else:
237-
check_dir_path(haskell_dir)
200+
check_dir_path(options.haskell_dir)
238201

239-
kmir = KMIR(definition_dir, haskell_dir=haskell_dir)
202+
kmir = KMIR(options.definition_dir, haskell_dir=options.haskell_dir)
240203

241204
view_proof(
242205
kmir,
243-
claim_label,
244-
proof_dir,
206+
options,
245207
)
246208

247209

0 commit comments

Comments
 (0)