|
| 1 | +""" |
| 2 | +Validate the job numbering in a CCP-EM Pipeliner project. |
| 3 | +
|
| 4 | +SPA live processing allocates Pipeliner job numbers from several processes at |
| 5 | +once (Murfey reserving them at schedule time, the node creator registering them |
| 6 | +once compute finishes). When that allocation goes wrong then it shows up in |
| 7 | +default_pipeline.star as two job types sharing a number, or as a block of |
| 8 | +numbers unused, or jobs falling out of order. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import argparse |
| 14 | +import re |
| 15 | +import sys |
| 16 | +from collections import defaultdict |
| 17 | +from pathlib import Path |
| 18 | +from typing import Dict, List |
| 19 | + |
| 20 | +from gemmi import cif |
| 21 | +from pipeliner.star_keys import ( |
| 22 | + GENERAL_BLOCK, |
| 23 | + JOB_COUNTER, |
| 24 | + PROCESS_BLOCK, |
| 25 | + PROCESS_PREFIX, |
| 26 | + PROCESS_SUFFIXES, |
| 27 | +) |
| 28 | + |
| 29 | +JOB_NUMBER = re.compile(r"/job(\d+)/?$") |
| 30 | + |
| 31 | + |
| 32 | +def _job_number(process_name: str) -> int | None: |
| 33 | + match = JOB_NUMBER.search(process_name.rstrip("/") + "/") |
| 34 | + return int(match.group(1)) if match else None |
| 35 | + |
| 36 | + |
| 37 | +def check_project(project_dir: Path) -> List[str]: |
| 38 | + """Return a list of problems found in the project's job numbering.""" |
| 39 | + pipeline_file = project_dir / "default_pipeline.star" |
| 40 | + if not pipeline_file.is_file(): |
| 41 | + return [f"No default_pipeline.star in {project_dir}"] |
| 42 | + |
| 43 | + doc = cif.read_file(str(pipeline_file)) |
| 44 | + general = doc.find_block(GENERAL_BLOCK) |
| 45 | + if general is None: |
| 46 | + return [f"{pipeline_file} has no {GENERAL_BLOCK} block"] |
| 47 | + job_counter = int(general.find_value(JOB_COUNTER)) |
| 48 | + |
| 49 | + process_block = doc.find_block(PROCESS_BLOCK) |
| 50 | + if process_block is None: |
| 51 | + return [f"{pipeline_file} has no {PROCESS_BLOCK} block"] |
| 52 | + |
| 53 | + # Process name and type are the first and third columns of the process table. |
| 54 | + by_number: Dict[int, List[str]] = defaultdict(list) |
| 55 | + unnumbered: List[str] = [] |
| 56 | + for row in process_block.find(PROCESS_PREFIX, PROCESS_SUFFIXES): |
| 57 | + name = cif.as_string(row[0]) |
| 58 | + job_type = cif.as_string(row[2]) |
| 59 | + number = _job_number(name) |
| 60 | + if number is None: |
| 61 | + unnumbered.append(name) |
| 62 | + else: |
| 63 | + by_number[number].append(f"{name} ({job_type})") |
| 64 | + |
| 65 | + problems: List[str] = [] |
| 66 | + for name in unnumbered: |
| 67 | + problems.append(f"Process {name} has no jobNNN number") |
| 68 | + |
| 69 | + for number in sorted(n for n, procs in by_number.items() if len(procs) > 1): |
| 70 | + problems.append( |
| 71 | + f"job{number:03} is used by {len(by_number[number])} processes: " |
| 72 | + + ", ".join(sorted(by_number[number])) |
| 73 | + ) |
| 74 | + |
| 75 | + if by_number: |
| 76 | + highest = max(by_number) |
| 77 | + gaps = sorted(set(range(1, highest + 1)) - set(by_number)) |
| 78 | + if gaps: |
| 79 | + problems.append( |
| 80 | + "Job numbers reserved but never used: " |
| 81 | + + ", ".join(f"job{n:03}" for n in gaps) |
| 82 | + ) |
| 83 | + if job_counter <= highest: |
| 84 | + problems.append( |
| 85 | + f"{JOB_COUNTER} is {job_counter} but job{highest:03} is already " |
| 86 | + "registered — the next job would reuse an existing number" |
| 87 | + ) |
| 88 | + |
| 89 | + for number, procs in sorted(by_number.items()): |
| 90 | + for proc in procs: |
| 91 | + job_dir = project_dir / proc.split(" (")[0] |
| 92 | + if not job_dir.is_dir(): |
| 93 | + problems.append(f"Process {proc} has no directory at {job_dir}") |
| 94 | + |
| 95 | + return problems |
| 96 | + |
| 97 | + |
| 98 | +def run() -> int: |
| 99 | + parser = argparse.ArgumentParser(description=__doc__) |
| 100 | + parser.add_argument( |
| 101 | + "project_dir", |
| 102 | + type=Path, |
| 103 | + nargs="?", |
| 104 | + default=Path.cwd(), |
| 105 | + help="Pipeliner project directory (default: the current directory)", |
| 106 | + ) |
| 107 | + args = parser.parse_args() |
| 108 | + |
| 109 | + problems = check_project(args.project_dir) |
| 110 | + if not problems: |
| 111 | + print(f"{args.project_dir}: job numbering is consistent") |
| 112 | + return 0 |
| 113 | + print(f"{args.project_dir}: {len(problems)} problem(s) found") |
| 114 | + for problem in problems: |
| 115 | + print(f" - {problem}") |
| 116 | + return 1 |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + sys.exit(run()) |
0 commit comments