167 lines
8.0 KiB
Python
167 lines
8.0 KiB
Python
import pathlib
|
|
import typing
|
|
|
|
from larv_review.agreement import agreement_report
|
|
from review_rules.comprehension_history import appended, DIMENSIONS, heading, Heading, latest_places, recorded, ruling_entry, scoring_entry, ScoringRun, vote_text
|
|
from larv_review.plan import checkout_plan, plan_reasons
|
|
from git_cli.git_repo import commit_exists
|
|
from larv_review.ruling import counted, overall, ruling, split, surplus, votes
|
|
from failure import Failure
|
|
from plan_store.stores import file_plan, store as plan_store
|
|
from larv_review.seats import FIRST_SEATS, scores, seats_file, SeatsFile
|
|
from larv_review.scoring import floor_step, means, under_floor
|
|
from project_config.main import located
|
|
from record_store.stores import RecordStore, store
|
|
from larv_review.scoring_setup import removed_setup
|
|
|
|
Say = typing.Callable[[str], None]
|
|
|
|
|
|
def record_ruling(votes_file: pathlib.Path, home: pathlib.Path, now: float) -> str | Failure:
|
|
"""Records the ruling the votes file's seats reach, and returns what the tool prints: the ruling and where it went, or which seats to launch where they split."""
|
|
read = read_seats(votes_file, True, home, now)
|
|
if isinstance(read, Failure):
|
|
return read
|
|
heading_read, seats_read = read
|
|
written = votes(seats_read.seats)
|
|
if isinstance(written, Failure):
|
|
return written
|
|
counts = counted(seats_read.seats, written)
|
|
overall_votes = [overall(each) for each in counts.values()]
|
|
if surplus(overall_votes):
|
|
return Failure(f"{' and '.join(FIRST_SEATS)} both count as {overall_votes[0]} overall, which settles the ruling; drop the other seats' lines")
|
|
tally = ", ".join(f"{seat.name} {vote_text(DIMENSIONS[-1], overall(written[seat.name]), overall(counts[seat.name]))}" for seat in seats_read.seats)
|
|
if split(overall_votes):
|
|
return f"Split: {tally}. Launch the maintainability senior and Junior A together, add their lines to the votes file, and run this again."
|
|
ruled = ruling(overall_votes)
|
|
appended_to = append(seats_read.read, ruling_entry(heading_read.title, seats_read.seats, written, counts, ruled), home)
|
|
return appended_to if isinstance(appended_to, Failure) else f"Ruling: {ruled}. Overall votes as counted: {tally}.\n{appended_to}"
|
|
|
|
|
|
def record_scoring(default: str, scores_file: pathlib.Path, ids: list[int], home: pathlib.Path, now: float, say: Say, warn: Say) -> Failure | None:
|
|
"""Records the scoring run the scores file holds in the comprehension history, and ranks the plan again. It says each result as soon as it has it, since a later step may fail after the run is recorded."""
|
|
read = read_seats(scores_file, False, home, now)
|
|
if isinstance(read, Failure):
|
|
return read
|
|
named, given = read
|
|
scoring = scored_seats(given)
|
|
if isinstance(scoring, Failure):
|
|
return scoring
|
|
reasons = plan_reasons(default, ids, home)
|
|
if isinstance(reasons, Failure):
|
|
return reasons
|
|
if not reasons:
|
|
return Failure("no plan item or release calls this scoring run; name the item IDs the chunk ships, as needs-scoring-run took them")
|
|
config = located(None, home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
checkout = str(config.top)
|
|
todo = checkout_plan(plan_store(config))
|
|
if isinstance(todo, Failure):
|
|
return todo
|
|
plan = todo.items
|
|
appended_to = append(given.read, scoring_entry(named.title, scoring), home)
|
|
if isinstance(appended_to, Failure):
|
|
return appended_to
|
|
say(appended_to)
|
|
say(f"Means: {', '.join(f'{name} {value}' for name, value in zip(DIMENSIONS, scoring.means))}.")
|
|
if under := under_floor(scoring.means, config.readability_floor):
|
|
say(f"Floor: {', '.join(under)} under the floor of {config.readability_floor}.")
|
|
if step := floor_step(scoring.means, plan, ids, config.readability_floor):
|
|
say(step)
|
|
reranked = rerank(home, warn)
|
|
removed = removed_setup(checkout, home)
|
|
return reranked or (removed and Failure(f"recorded the run, but {removed.reason}"))
|
|
|
|
|
|
def rerank(home: pathlib.Path, warn: Say) -> Failure | None:
|
|
"""Rank the project's todo.md again as rank_todo.py does, since the overall sets code health's weight, and fail where rank_todo.py would. The run stays recorded whatever happens, so those failures say to settle todo.md and run rank_todo.py on it, not to record the run again."""
|
|
config = located(None, home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
todo = config.plan_file
|
|
if todo is None:
|
|
# A plan in Jira is ranked by Jira's backlog, which no score moves.
|
|
return None
|
|
plan = file_plan(config, todo)
|
|
ranked = plan.rank()
|
|
if isinstance(ranked, Failure):
|
|
return Failure(f"recorded the run, but rank_todo.py cannot rank {todo}: {ranked.reason}. Fix what that names, then run rank_todo.py on {todo}, not record-scoring again")
|
|
for line in plan.reported(ranked):
|
|
warn(line)
|
|
if ranked.under_bar:
|
|
return Failure(f"recorded the run and ranked {todo}, but the lines above name items now under the bar. Settle each as its line says, then run rank_todo.py on {todo}, not record-scoring again")
|
|
return None
|
|
|
|
|
|
def scored_seats(given: SeatsFile) -> ScoringRun | Failure:
|
|
found = scores(given.seats)
|
|
if isinstance(found, Failure):
|
|
return found
|
|
return ScoringRun(given.seats, found, means(list(found.values())))
|
|
|
|
|
|
def read_seats(path: pathlib.Path, paired: bool, home: pathlib.Path, now: float) -> tuple[Heading, SeatsFile] | Failure:
|
|
"""The heading the seats file's entry goes under, and the file, as decided says. Its callers run it first, so a file it refuses fails before anything is printed."""
|
|
try:
|
|
text = path.read_text(encoding="utf-8")
|
|
except (OSError, UnicodeDecodeError) as failure:
|
|
return Failure(f"could not read {path}: {failure}")
|
|
history = history_file(home)
|
|
if isinstance(history, Failure):
|
|
return history
|
|
return decided(text, now, paired, commit_exists, history)
|
|
|
|
|
|
def decided(text: str, now: float, paired: bool, commit_known: typing.Callable[[str], bool], history: tuple[RecordStore, str]) -> tuple[Heading, SeatsFile] | Failure:
|
|
"""The heading the seats file's entry goes under, stamped with now, and the file parsed from text. Fails when text names a commit that commit_known denies, or when the history already holds an entry on what the seats read."""
|
|
given = seats_file(text)
|
|
if isinstance(given, Failure):
|
|
return given
|
|
named = heading(now, given.read, paired)
|
|
if isinstance(named, Failure):
|
|
return named
|
|
if unknown := [commit for commit in (named.commit, named.against) if commit and not commit_known(commit)]:
|
|
return Failure(f"the seats file names {', '.join(unknown)}, which is no commit in this checkout")
|
|
if recorded(history[1], given.read):
|
|
return Failure(f"{history[0].label} already holds an entry on {given.read}; record each ruling or run once")
|
|
return named, given
|
|
|
|
|
|
def read_places(home: pathlib.Path) -> str | Failure:
|
|
history = history_file(home)
|
|
return history if isinstance(history, Failure) else places_report(history[1])
|
|
|
|
|
|
def places_report(history: str) -> str:
|
|
"""What `larv-review places` prints for history."""
|
|
return (latest_places(history) or "Seats found no places hard.").rstrip("\n")
|
|
|
|
|
|
def read_agreement(home: pathlib.Path) -> str | Failure:
|
|
history = history_file(home)
|
|
return history if isinstance(history, Failure) else agreement_report(history[1])
|
|
|
|
|
|
def history_file(home: pathlib.Path) -> tuple[RecordStore, str] | Failure:
|
|
"""The store the project's config names for its comprehension history, and the history's text, empty where there is none yet."""
|
|
config = located(None, home)
|
|
if isinstance(config, Failure):
|
|
return config
|
|
history = store(config, "comprehension-history")
|
|
text = history.read()
|
|
return text if isinstance(text, Failure) else (history, text)
|
|
|
|
|
|
def append(read: str, entry: str, home: pathlib.Path) -> str | Failure:
|
|
"""Appends entry and returns the line the tool prints; a Failure where the history already holds an entry on what the seats read."""
|
|
read_history = history_file(home)
|
|
if isinstance(read_history, Failure):
|
|
return read_history
|
|
history, text = read_history
|
|
if recorded(text, read):
|
|
return Failure(f"{history.label} already holds an entry on {read}; record each ruling or run once")
|
|
if unwritten := history.write(appended(text, entry)):
|
|
return unwritten
|
|
return f"Appended to {history.label}."
|