Skip to content

Commit

Permalink
Format Python under tla/ (#6515)
Browse files Browse the repository at this point in the history
  • Loading branch information
achamayou authored Oct 1, 2024
1 parent 0182242 commit df6d004
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 24 deletions.
4 changes: 2 additions & 2 deletions scripts/ci-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ endgroup

group "Python format"
if [ $FIX -ne 0 ]; then
git ls-files tests/ python/ scripts/ .cmake-format.py | grep -e '\.py$' | xargs black
git ls-files tests/ python/ scripts/ tla/ .cmake-format.py | grep -e '\.py$' | xargs black
else
git ls-files tests/ python/ scripts/ .cmake-format.py | grep -e '\.py$' | xargs black --check
git ls-files tests/ python/ scripts/ tla/ .cmake-format.py | grep -e '\.py$' | xargs black --check
fi
endgroup

Expand Down
8 changes: 5 additions & 3 deletions tla/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"""

TOP=5
TOP = 5

if __name__ == "__main__":
ts = defaultdict(list)
Expand All @@ -40,8 +40,10 @@
top_TOP = [x[0] for x in col_max[-TOP:]]
df_TOP = df[top_TOP]
print(df_TOP)
plot = df_TOP.plot.line(y=df_TOP.columns, width=1200, height=600, title=f"Top {TOP} distinct actions")
plot = df_TOP.plot.line(
y=df_TOP.columns, width=1200, height=600, title=f"Top {TOP} distinct actions"
)
if len(sys.argv) > 1:
hvplot.save(plot, sys.argv[1])
else:
hvplot.show(plot)
hvplot.show(plot)
4 changes: 3 additions & 1 deletion tla/install_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def _parse_args() -> argparse.Namespace:

def install_tlc():
java = "java"
tlaplus_path = "~/.vscode-remote/extensions/tlaplus.vscode-ide-*/tools/tla2tools.jar"
tlaplus_path = (
"~/.vscode-remote/extensions/tlaplus.vscode-ide-*/tools/tla2tools.jar"
)
copy_tlaplus = f"-cp {tlaplus_path} tlc2.TLC"

set_alias("tlcrepl", f"{java} -cp {tlaplus_path} tlc2.REPL")
Expand Down
17 changes: 14 additions & 3 deletions tla/loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dataclasses
import sys


@dataclasses.dataclass
class LOC:
code: int = 0
Expand All @@ -21,9 +22,10 @@ def __add__(self, other):
return LOC(
self.code + other.code,
self.comment + other.comment,
self.blank + other.blank
self.blank + other.blank,
)


def is_comment(line: str) -> bool:
if line.startswith(r"\*"):
return True
Expand All @@ -33,15 +35,19 @@ def is_comment(line: str) -> bool:
return True
return False


def is_multiline_comment_start(line: str) -> bool:
return line.startswith("(*")


def is_footer(line: str) -> bool:
return all(c == "=" for c in line)


def is_multiline_comment_end(line: str) -> bool:
return line.endswith("*)")


def count_loc(lines) -> LOC:
loc = LOC()
in_comment = False
Expand All @@ -68,6 +74,7 @@ def count_loc(lines) -> LOC:
loc.code += 1
return loc


JUST_CODE = r"""
---------- MODULE MCccfraft ----------
EXTENDS ccfraft, StatsFile, MCAliases
Expand Down Expand Up @@ -127,6 +134,7 @@ def count_loc(lines) -> LOC:
/\ msg.prevLogIndex = logline.msg.packet.prev_idx
"""


def test_loc():
"""
Run with py.test loc.py
Expand All @@ -135,7 +143,10 @@ def test_loc():
assert count_loc(CODE_AND_COMMENTS.splitlines()) == LOC(code=2, comment=2, blank=1)
assert count_loc(FOOTER.splitlines()) == LOC(code=0, comment=9, blank=1)
assert count_loc(MULTILINE_COMMENT.splitlines()) == LOC(code=2, comment=4, blank=2)
assert count_loc(MULTILINE_COMMENT_2.splitlines()) == LOC(code=7, comment=12, blank=1)
assert count_loc(MULTILINE_COMMENT_2.splitlines()) == LOC(
code=7, comment=12, blank=1
)


if __name__ == "__main__":
locs = []
Expand All @@ -144,4 +155,4 @@ def test_loc():
lines = f.readlines()
locs.append(count_loc(lines))
print(f"{arg} {locs[-1]}")
print(f"Total {sum(locs, LOC())}")
print(f"Total {sum(locs, LOC())}")
59 changes: 44 additions & 15 deletions tla/trace2scen.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,41 @@
import json
import os


def comment(action):
return f"# {action['name']} {action['location']['module']}:{action['location']['beginLine']}"


def term(ctx, pre):
return str(pre["currentTerm"][ctx['i']])
return str(pre["currentTerm"][ctx["i"]])


def noop(ctx, pre, post):
return ["# Noop"]


MAP = {
"ClientRequest": lambda ctx, pre, post: ["replicate", term(ctx, pre), "42"],
"MCClientRequest": lambda ctx, pre, post: ["replicate", term(ctx, pre), "42"],
"CheckQuorum": lambda ctx, pre, post: ["periodic_one", ctx['i'], "110"],
"Timeout": lambda ctx, pre, post: ["periodic_one", ctx['i'], "110"],
"MCTimeout": lambda ctx, pre, post: ["periodic_one", ctx['i'], "110"],
"CheckQuorum": lambda ctx, pre, post: ["periodic_one", ctx["i"], "110"],
"Timeout": lambda ctx, pre, post: ["periodic_one", ctx["i"], "110"],
"MCTimeout": lambda ctx, pre, post: ["periodic_one", ctx["i"], "110"],
"RequestVote": noop,
"AppendEntries": lambda _, __, ___: ["dispatch_all"],
"BecomeLeader": noop,
"SignCommittableMessages": lambda ctx, pre, post: ["emit_signature", term(ctx, pre)],
"MCSignCommittableMessages": lambda ctx, pre, post: ["emit_signature", term(ctx, pre)],
"ChangeConfigurationInt": lambda ctx, pre, post: ["replicate_new_configuration", term(ctx, pre), *ctx["newConfiguration"]],
"SignCommittableMessages": lambda ctx, pre, post: [
"emit_signature",
term(ctx, pre),
],
"MCSignCommittableMessages": lambda ctx, pre, post: [
"emit_signature",
term(ctx, pre),
],
"ChangeConfigurationInt": lambda ctx, pre, post: [
"replicate_new_configuration",
term(ctx, pre),
*ctx["newConfiguration"],
],
"AdvanceCommitIndex": noop,
"HandleRequestVoteRequest": lambda _, __, ___: ["dispatch_all"],
"HandleRequestVoteResponse": noop,
Expand All @@ -38,31 +52,46 @@ def noop(ctx, pre, post):
"RcvRequestVoteResponse": noop,
}


def post_commit(post):
return [["assert_commit_idx", node, str(idx)] for node, idx in post["commitIndex"].items()]
return [
["assert_commit_idx", node, str(idx)]
for node, idx in post["commitIndex"].items()
]


def post_state(post):
entries = []
for node, state in post["state"].items():
entries.append(["assert_detail", "leadership_state", node, state])
entries.append(["assert_detail", "leadership_state", node, state])
return entries


def step_to_action(pre_state, action, post_state):
return os.linesep.join([
comment(action),
','.join(MAP[action['name']](action['context'], pre_state[1], post_state[1]))])
return os.linesep.join(
[
comment(action),
",".join(
MAP[action["name"]](action["context"], pre_state[1], post_state[1])
),
]
)


def asserts(pre_state, action, post_state, assert_gen):
return os.linesep.join([','.join(assertion) for assertion in assert_gen(post_state[1])])
return os.linesep.join(
[",".join(assertion) for assertion in assert_gen(post_state[1])]
)


if __name__ == "__main__":
with open(sys.argv[1]) as trace:
steps = json.load(trace)["action"]
initial_state = steps[0][0][1]
initial_node, = [node for node, log in initial_state["log"].items() if log]
(initial_node,) = [node for node, log in initial_state["log"].items() if log]
print(f"start_node,{initial_node}")
print(f"emit_signature,2")
for step in steps:
print(step_to_action(*step))
print(asserts(*step, post_state))
print(asserts(*steps[-1], post_commit))
print(asserts(*steps[-1], post_commit))

0 comments on commit df6d004

Please sign in to comment.