Files
Excersise-Tracker/scripts/testtask_source.py
T

52 lines
2.1 KiB
Python

"""Resolve a numbered task to its checked-out implementation, never a snapshot."""
import re
import subprocess
import sys
from pathlib import Path
def task_source(repository, task):
if not re.fullmatch(r'[0-9]{3}', task):
raise ValueError('task ID must contain exactly three digits')
listing = subprocess.check_output(
['git', '-C', str(repository), 'worktree', 'list', '--porcelain', '-z'],
).decode()
prefix = f'refs/heads/task/{task}'
candidates = []
missing = []
for entry in listing.split('\0\0'):
fields = dict(field.split(' ', 1) for field in entry.split('\0') if ' ' in field)
branch = fields.get('branch', '')
if branch == prefix or branch.startswith(prefix + '-'):
path = Path(fields['worktree'])
if 'prunable' in fields or not path.is_dir():
missing.append(path)
else:
candidates.append(path)
if not candidates and missing:
paths = ', '.join(map(str, missing))
raise ValueError(
f'TASK-{task} implementation is missing: {paths}. '
'Restore the source from a saved copy or rebuild the candidate in a persistent '
'worktree; its branch alone does not recover uncommitted files. '
'Refusing to test the integration baseline instead.'
)
if not candidates:
raise ValueError(f'no checked-out task/{task} branch; prepare its implementation worktree first')
if len(candidates) != 1:
raise ValueError(f'multiple worktrees match TASK-{task}; reconcile the duplicate task branches first')
source = candidates[0]
if not source.is_dir() or not (source / '.roborto/testtask.conf').is_file():
raise ValueError(f'task source is missing or has no testtask configuration: {source}')
return source
if __name__ == '__main__':
try:
if len(sys.argv) != 3:
raise ValueError('expected repository path and task ID')
print(task_source(Path(sys.argv[1]), sys.argv[2]))
except (ValueError, subprocess.CalledProcessError) as error:
print(f'testtask: {error}', file=sys.stderr)
sys.exit(2)