-
Notifications
You must be signed in to change notification settings - Fork 10
/
__init__.py
179 lines (144 loc) · 4.55 KB
/
__init__.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import sys
from pathlib import Path
from typing import Callable, Dict, Optional, Type
import modelator
import pytest
import typer
from atomkraft import __version__
from atomkraft.utils.project import NoProjectError, init_project, project_root
from rich import print
from .. import chain, test
from ..reactor.reactor import generate_reactor
class ErrorHandlingTyper(typer.Typer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.error_handlers: Dict[Type[Exception], Callable[[Exception], int]] = {}
def error_handler(self, exc: Type[Exception]):
def decorator(f: Callable[[Exception], int]):
self.error_handlers[exc] = f
return f
return decorator
def __call__(self, *args, **kwargs):
try:
super(ErrorHandlingTyper, self).__call__(*args, **kwargs)
except Exception as e:
try:
callback = self.error_handlers[type(e)]
exit_code = callback(e)
raise typer.Exit(code=exit_code)
except typer.Exit as e:
sys.exit(e.exit_code)
except KeyError:
print(e)
raise typer.Exit(1)
app = ErrorHandlingTyper(
add_completion=False,
name="atomkraft",
no_args_is_help=True,
rich_markup_mode="rich",
)
def debug_callback(flag: bool):
if not flag:
app.pretty_exceptions_enable = False
def exception_handler(exception_type, exception, _):
print(f"{exception_type.__name__}: {exception}")
sys.excepthook = exception_handler
@app.callback()
def main(
ctx: typer.Context,
debug: bool = typer.Option(None, callback=debug_callback),
):
pass
@app.error_handler(NoProjectError)
def noproject_error_handler(e) -> int:
print("You are outside of Atomkraft project")
print("You can create an Atomkraft project using `atomkraft init <PROJECT-NAME>`")
return 1
@app.command()
def version():
"""Print version of the atomkraft executable"""
print(f"atomkraft {__version__}")
@app.command(
no_args_is_help=True,
)
def init(
name: str = typer.Argument(..., help="Name of new directory", show_default=False),
):
"""
Initialize new Atomkraft project in the given directory
"""
dir_path = Path.cwd() / name
if (dir_path).exists():
print(
f"[red bold]{'Error'.rjust(12)}[/red bold] A directory `{name}` already exists!"
)
raise typer.Exit(1)
init_project(name, Path.cwd() / name)
print(
f"[green bold]{'Created'.rjust(12)}[/green bold] Atomkraft project `{name}`."
)
app.add_typer(
modelator.cli.app,
name="model",
short_help="Work with TLA+ models: load, typecheck, check invariants, or sample traces",
no_args_is_help=True,
)
app.add_typer(
chain.app,
name="chain",
help="Modify or control Cosmos-SDK chain",
no_args_is_help=True,
)
app.add_typer(
test.app,
name="test",
short_help="Test the blockchain based on abstract traces, either explicitly given or produced from models",
no_args_is_help=True,
)
@app.command(
no_args_is_help=True,
)
def smoke_test():
"""
Run smoke tests for a Cosmos-SDK chain
"""
pytest.main([".atomkraft/smoke_tests"])
# run with:
# atomkraft reactor --actions-list=act1,act2,act3 --variables-list=x,y,z --reactor-stub-file=path/to/the/file
# or
# atomkraft reactor --actions-list="act1, act2, act3" --variables-list="x, y, z" --reactor-stub-file="path/to/the/file"
@app.command(
no_args_is_help=True,
)
def reactor(
actions: str = typer.Option(
...,
help="trace actions for which to create reactor stub functions",
show_default=False,
),
variables: str = typer.Option(
...,
help="state variables to use as parameters for reactor stub functions.",
show_default=False,
),
path: Optional[Path] = typer.Option(
None,
file_okay=True,
dir_okay=False,
help="path where to create the reactor stub",
),
):
"""
Generate a reactor stub used to interpret test traces
"""
if path is None:
path = project_root() / "reactors" / "reactor.py"
actions = [act.strip() for act in actions.split(",")]
variables = [var.strip() for var in variables.split(",")]
if path.is_file():
typer.confirm(
"The stub file already exists and it will be overwritten. "
"Are you sure you want to continue?",
abort=True,
)
generate_reactor(actions, variables, stub_file_path=path)