reqtable 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
reqtable/cli.py ADDED
@@ -0,0 +1,631 @@
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import json
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ from . import __version__
9
+ from .baselines import (
10
+ BaselineStore,
11
+ build_controls,
12
+ create_baseline,
13
+ current_user,
14
+ find_gate,
15
+ load_baselines,
16
+ open_store,
17
+ require_scope_args,
18
+ require_system_record,
19
+ run_git,
20
+ sort_manifests,
21
+ )
22
+ from .core import (
23
+ DEFAULT_CONFIG,
24
+ ConfigError,
25
+ CsvSource,
26
+ CsvTable,
27
+ InputError,
28
+ OutputError,
29
+ ReqtableError,
30
+ atomic_write_text,
31
+ build_report_data,
32
+ prepare_trace_table,
33
+ resolve_gate_enforcement,
34
+ extract_comments_csv,
35
+ generation_time,
36
+ load_config,
37
+ read_csv_source,
38
+ )
39
+ from .gates import evaluate_gate, format_result, table_findings
40
+ from .html_report import render_report
41
+ from .review import build_package, import_decisions, sign_off, verify_baseline
42
+ from .waivers import describe_failure, plan_waivers, records, split_ids
43
+ from .xlsx import (
44
+ XlsxSelection,
45
+ block_to_csv,
46
+ describe,
47
+ import_workbook,
48
+ is_xlsx,
49
+ mapping_template,
50
+ )
51
+
52
+
53
+ def parser() -> argparse.ArgumentParser:
54
+ result = argparse.ArgumentParser(
55
+ description="Generate an offline requirements table or diff report."
56
+ )
57
+ result.add_argument("input", nargs="?", help="CSV or .xlsx input for snapshot mode")
58
+ result.add_argument(
59
+ "--old",
60
+ help="old/baseline CSV or .xlsx for diff mode, or git:REV[:PATH] to read a CSV from a git revision",
61
+ )
62
+ result.add_argument("--new", help="new/current CSV or .xlsx for diff mode")
63
+ result.add_argument(
64
+ "--extract-comments",
65
+ metavar="REPORT.html",
66
+ help="extract comments from a generated report",
67
+ )
68
+ result.add_argument(
69
+ "-o", "--output", help="output HTML path, or CSV path to export an .xlsx input"
70
+ )
71
+ result.add_argument("-c", "--config", help="JSON configuration path")
72
+ result.add_argument("--id-column")
73
+ result.add_argument("--title")
74
+ result.add_argument("--document-mode", choices=("review", "develop"))
75
+ result.add_argument(
76
+ "--traceability",
77
+ action=argparse.BooleanOptionalAction,
78
+ default=None,
79
+ help="interpret the CSV as a systems and traceability document",
80
+ )
81
+ result.add_argument("--encoding")
82
+ result.add_argument("--delimiter")
83
+ result.add_argument("--initial-diff-view", choices=("unified", "side-by-side"))
84
+ result.add_argument("--changes-only", action="store_true", default=None)
85
+ result.add_argument(
86
+ "--include-unchanged", action=argparse.BooleanOptionalAction, default=None
87
+ )
88
+ result.add_argument("--reviewer-initials")
89
+ result.add_argument("--comments-column", default=None)
90
+ result.add_argument("--comments-format", choices=("json", "text"), default=None)
91
+ result.add_argument("--csv-base", choices=("new", "old", "union"), default="new")
92
+ result.add_argument("--allow-comment-loss", action="store_true")
93
+ result.add_argument("--table", help="read this table from each .xlsx input")
94
+ result.add_argument("--sheet", help="read this sheet from each .xlsx input")
95
+ result.add_argument(
96
+ "--header-row",
97
+ type=int,
98
+ metavar="N",
99
+ help="header row number on --sheet; default is the first non-empty row",
100
+ )
101
+ result.add_argument(
102
+ "--list-tables",
103
+ metavar="WORKBOOK.xlsx",
104
+ help="list the sheets and tables of a workbook and exit",
105
+ )
106
+ result.add_argument(
107
+ "--xlsx-mapping-template",
108
+ metavar="WORKBOOK.xlsx",
109
+ help="print a JSON configuration that maps every table and sheet of a workbook, and exit",
110
+ )
111
+ result.add_argument(
112
+ "--create-baseline",
113
+ metavar="GATE",
114
+ help="freeze the input as a baseline for this review gate, and exit",
115
+ )
116
+ result.add_argument(
117
+ "--system",
118
+ metavar="ID",
119
+ help="System ID for --create-baseline or --check-gate on a system-scoped gate",
120
+ )
121
+ result.add_argument(
122
+ "--waive",
123
+ metavar="CHECK_ID",
124
+ action="append",
125
+ default=[],
126
+ help="with --create-baseline, waive this failing strict check (repeat, or comma-separate); needs --reason",
127
+ )
128
+ result.add_argument(
129
+ "--reason",
130
+ action="append",
131
+ default=[],
132
+ help="why the --waive checks are waived; give one for all, or repeat it once per check in the same order",
133
+ )
134
+ result.add_argument(
135
+ "--list-baselines",
136
+ action="store_true",
137
+ help="list the baselines of the input (or --new), and exit",
138
+ )
139
+ result.add_argument(
140
+ "--baselines",
141
+ metavar="NAME[,NAME...]",
142
+ help="embed these gate baselines in the report so it can compare against them",
143
+ )
144
+ result.add_argument(
145
+ "--check-gate",
146
+ metavar="GATE",
147
+ help="evaluate this review gate's readiness checks on the input; exit 0 when ready, 1 when not",
148
+ )
149
+ result.add_argument(
150
+ "--sign-off",
151
+ metavar="BASELINE",
152
+ help="record a decision on this gate baseline (needs --role and --decision), and exit",
153
+ )
154
+ result.add_argument(
155
+ "--role", help="sign-off role for --sign-off, one of the gate's roles"
156
+ )
157
+ result.add_argument(
158
+ "--decision",
159
+ help="sign-off decision for --sign-off: Reviewed, Changes requested, Approved or Rejected",
160
+ )
161
+ result.add_argument(
162
+ "--user",
163
+ help="who made the decision for --sign-off, or who waives for --waive; default is git user.name or the login name",
164
+ )
165
+ result.add_argument("--note", default="", help="free-text note for --sign-off")
166
+ result.add_argument(
167
+ "--evidence",
168
+ metavar="PATH",
169
+ help="file that backs the decision for --sign-off, such as a signed PDF",
170
+ )
171
+ result.add_argument(
172
+ "--verify-baseline",
173
+ metavar="BASELINE",
174
+ help="recheck a baseline's content hash and sign-offs; exit 0 when intact and approved, 1 otherwise",
175
+ )
176
+ result.add_argument(
177
+ "--gate-package",
178
+ metavar="BASELINE",
179
+ help="write the review package for a gate baseline to -o OUT.html, and exit",
180
+ )
181
+ result.add_argument(
182
+ "--import-decisions",
183
+ metavar="PACKAGE.html",
184
+ help="record the decisions saved in a gate package copy as sign-offs, and exit",
185
+ )
186
+ result.add_argument("--print-default-config", action="store_true")
187
+ result.add_argument(
188
+ "--gui",
189
+ action="store_true",
190
+ help="open the point-and-click interface in your browser; it runs on this computer only",
191
+ )
192
+ result.add_argument(
193
+ "--no-browser",
194
+ action="store_true",
195
+ help="with --gui, print the address instead of opening a browser",
196
+ )
197
+ result.add_argument(
198
+ "--version", action="version", version=f"%(prog)s {__version__}"
199
+ )
200
+ return result
201
+
202
+
203
+ def _overrides(args: argparse.Namespace) -> dict[str, object]:
204
+ return {
205
+ "id_column": args.id_column,
206
+ "title": args.title,
207
+ "document_mode": args.document_mode,
208
+ "traceability": args.traceability,
209
+ "csv.encoding": args.encoding,
210
+ "csv.delimiter": args.delimiter,
211
+ "diff.initial_view": args.initial_diff_view,
212
+ "diff.initial_changes_only": args.changes_only,
213
+ "diff.include_unchanged": args.include_unchanged,
214
+ "comments.reviewer_initials": args.reviewer_initials,
215
+ "comments.csv_column": args.comments_column,
216
+ "comments.csv_format": args.comments_format,
217
+ }
218
+
219
+
220
+ def git_source(spec: str, default_path: str, config: dict[str, object]) -> CsvSource:
221
+ """Resolve git:REV[:PATH]; PATH defaults to the --new file's path."""
222
+ rev, _, given = spec[len("git:") :].partition(":")
223
+ if not rev:
224
+ raise ConfigError("--old git: needs a revision, as git:REV or git:REV:PATH")
225
+ path = Path(given or default_path)
226
+ try:
227
+ # --filters applies the repo's eol/attribute rules, as for a checked-out file.
228
+ blob = run_git(path.parent, "cat-file", "--filters", f"{rev}:./{path.name}")
229
+ short = (
230
+ run_git(path.parent, "rev-parse", "--short", f"{rev}^{{commit}}")
231
+ .decode()
232
+ .strip()
233
+ )
234
+ except InputError as exc:
235
+ raise InputError(f"cannot read {path} at {rev}: {exc}") from exc
236
+ encoding = config["csv"]["encoding"] # type: ignore[index]
237
+ label = (
238
+ f"{path.name} @ {rev}"
239
+ if rev.startswith(short)
240
+ else f"{path.name} @ {rev} ({short})"
241
+ )
242
+ try:
243
+ return CsvSource(label, blob.decode(encoding), path)
244
+ except (UnicodeError, LookupError) as exc:
245
+ raise InputError(f"{label}: cannot decode as {encoding}: {exc}") from exc
246
+
247
+
248
+ def file_source(path: str) -> CsvSource:
249
+ return CsvSource(str(Path(path)), None, Path(path))
250
+
251
+
252
+ def xlsx_source(
253
+ path: str, selection: XlsxSelection, config: dict[str, object]
254
+ ) -> CsvSource:
255
+ imported = import_workbook(
256
+ path,
257
+ selection,
258
+ config["xlsx"]["sources"],
259
+ config["id_column"], # type: ignore[index,arg-type]
260
+ config["traceability"],
261
+ config["comments"]["enabled"], # type: ignore[index,arg-type]
262
+ )
263
+ block = imported.block
264
+ text = block_to_csv(block.rows, config["csv"]["delimiter"]) # type: ignore[index]
265
+ # Name only: the label ends up in the report, which gets shared.
266
+ return CsvSource(
267
+ f"{Path(path).name} [{block.name}]",
268
+ text,
269
+ Path(path),
270
+ imported.warnings,
271
+ imported.comments,
272
+ )
273
+
274
+
275
+ def input_source(
276
+ path: str, selection: XlsxSelection, config: dict[str, object]
277
+ ) -> CsvSource:
278
+ return xlsx_source(path, selection, config) if is_xlsx(path) else file_source(path)
279
+
280
+
281
+ def table_store(
282
+ path: str, selection: XlsxSelection, config: dict[str, object]
283
+ ) -> BaselineStore:
284
+ return open_store(
285
+ path,
286
+ config,
287
+ lambda found: read_csv_source(
288
+ input_source(str(found), selection, config), config
289
+ ),
290
+ )
291
+
292
+
293
+ def run_baseline_command(
294
+ args: argparse.Namespace, selection: XlsxSelection, config: dict[str, object]
295
+ ) -> int:
296
+ if args.create_baseline and args.list_baselines:
297
+ raise ConfigError("--create-baseline and --list-baselines cannot be combined")
298
+ if (
299
+ args.old
300
+ or args.extract_comments
301
+ or args.baselines
302
+ or (args.create_baseline and args.new)
303
+ ):
304
+ raise ConfigError(
305
+ "--create-baseline and --list-baselines cannot be combined with diff options, --baselines or --extract-comments"
306
+ )
307
+ if args.input and args.new:
308
+ raise ConfigError(
309
+ "give the input as a positional argument or as --new, not both"
310
+ )
311
+ path = args.input or (args.new if args.list_baselines else None)
312
+ if not path:
313
+ raise ConfigError(
314
+ "--create-baseline and --list-baselines need an input CSV or .xlsx"
315
+ )
316
+ store = table_store(path, selection, config)
317
+ if args.list_baselines:
318
+ manifests = sort_manifests(store.list(), config)
319
+ for manifest in manifests:
320
+ digest = manifest.content_hash.removeprefix("sha256:")[:12]
321
+ print(
322
+ "\t".join(
323
+ (
324
+ manifest.name,
325
+ manifest.gate,
326
+ manifest.system or "-",
327
+ manifest.created,
328
+ manifest.created_by,
329
+ digest,
330
+ )
331
+ )
332
+ )
333
+ if not manifests:
334
+ print("No baselines.")
335
+ return 0
336
+ result = evaluate_input(
337
+ store.current(), store, config, args.create_baseline, args.system
338
+ )
339
+ ids = split_ids(args.waive)
340
+ user = args.user or current_user(store.input_path.resolve().parent)
341
+ waivers = plan_waivers(result, ids, args.reason, user, generation_time())
342
+ waived = set(ids)
343
+ for check in result["checks"]: # type: ignore[attr-defined]
344
+ if check["error"] is None and not check["failing"] or check["id"] in waived:
345
+ continue
346
+ if check["enforcement"] == "strict":
347
+ continue
348
+ print(f"warning: {describe_failure(check)}", file=sys.stderr)
349
+ blocked = [
350
+ check
351
+ for check in result["checks"]
352
+ if check["id"] in result["blocking"] and check["id"] not in waived
353
+ ] # type: ignore[attr-defined,index]
354
+ if blocked:
355
+ for check in blocked:
356
+ print(f"blocked: {describe_failure(check)}", file=sys.stderr)
357
+ hint = ",".join(check["id"] for check in blocked)
358
+ print(
359
+ f'Fix the data, or record a waiver: --waive {hint} --reason "..."',
360
+ file=sys.stderr,
361
+ )
362
+ return 1
363
+ made = create_baseline(store, config, args.create_baseline, args.system, waivers)
364
+ for waiver in waivers:
365
+ print(
366
+ f"Waived {waiver.check} ({records(len(waiver.failing))}): {waiver.reason}"
367
+ )
368
+ print(f"Created baseline {made.name} ({made.content_hash}) {store.where(made)}")
369
+ return 0
370
+
371
+
372
+ REVIEW_FLAGS = ("sign_off", "verify_baseline", "gate_package", "import_decisions")
373
+
374
+
375
+ def run_review_command(
376
+ args: argparse.Namespace, selection: XlsxSelection, config: dict[str, object]
377
+ ) -> int:
378
+ if not args.input:
379
+ raise ConfigError(
380
+ "--sign-off, --verify-baseline, --gate-package and --import-decisions need an input CSV or .xlsx"
381
+ )
382
+ store = table_store(args.input, selection, config)
383
+ if args.sign_off:
384
+ if not args.role or not args.decision:
385
+ raise ConfigError("--sign-off needs --role and --decision")
386
+ print(
387
+ sign_off(
388
+ store,
389
+ config,
390
+ args.sign_off,
391
+ args.role,
392
+ args.decision,
393
+ args.user,
394
+ args.note,
395
+ args.evidence,
396
+ )
397
+ )
398
+ return 0
399
+ if args.gate_package:
400
+ if not args.output:
401
+ raise ConfigError("--gate-package needs --output")
402
+ data = build_package(store, config, args.gate_package, Path(args.output).name)
403
+ for warning in data["meta"].get("warnings", []):
404
+ print(f"warning: {warning}", file=sys.stderr)
405
+ atomic_write_text(args.output, render_report(data))
406
+ print(
407
+ f"Wrote {data['package']['baseline']['name']} gate package to {args.output}"
408
+ )
409
+ return 0
410
+ if args.import_decisions:
411
+ print("\n".join(import_decisions(store, config, args.import_decisions)))
412
+ return 0
413
+ lines, approved = verify_baseline(store, config, args.verify_baseline)
414
+ print("\n".join(lines))
415
+ return 0 if approved else 1
416
+
417
+
418
+ def evaluate_input(
419
+ table: CsvTable,
420
+ store: BaselineStore,
421
+ config: dict[str, object],
422
+ gate_id: str,
423
+ system: str | None,
424
+ ) -> dict[str, object]:
425
+ gate = find_gate(config, gate_id)
426
+ require_scope_args(gate, system, config)
427
+ if config["traceability"]:
428
+ table, _ = prepare_trace_table(table, config)
429
+ require_system_record(table, system)
430
+ controls = build_controls(store, config)
431
+ findings = table_findings(table.rows, table.headers, config["id_column"], controls) # type: ignore[arg-type]
432
+ configured = next(entry for entry in config["gates"] if entry["id"] == gate.id) # type: ignore[attr-defined]
433
+ resolved = resolve_gate_enforcement(configured, config["baselines"]["enforcement"]) # type: ignore[index]
434
+ return evaluate_gate(
435
+ resolved,
436
+ table.rows,
437
+ table.headers,
438
+ config["id_column"],
439
+ system,
440
+ findings,
441
+ controls,
442
+ ) # type: ignore[arg-type]
443
+
444
+
445
+ def run_check_gate(
446
+ args: argparse.Namespace, selection: XlsxSelection, config: dict[str, object]
447
+ ) -> int:
448
+ if (
449
+ args.create_baseline
450
+ or args.list_baselines
451
+ or args.baselines
452
+ or args.old
453
+ or args.new
454
+ or args.extract_comments
455
+ ):
456
+ raise ConfigError(
457
+ "--check-gate cannot be combined with --create-baseline, --list-baselines, --baselines, --old, --new or --extract-comments"
458
+ )
459
+ if not args.input:
460
+ raise ConfigError("--check-gate needs an input CSV or .xlsx")
461
+ table = read_csv_source(input_source(args.input, selection, config), config)
462
+ result = evaluate_input(
463
+ table,
464
+ table_store(args.input, selection, config),
465
+ config,
466
+ args.check_gate,
467
+ args.system,
468
+ )
469
+ print("\n".join(format_result(result)))
470
+ return 0 if result["ready"] else 1
471
+
472
+
473
+ def run(argv: list[str] | None = None) -> int:
474
+ args = parser().parse_args(argv)
475
+ if args.no_browser and not args.gui:
476
+ raise ConfigError("--no-browser applies only to --gui")
477
+ if args.gui:
478
+ defaults = vars(parser().parse_args([]))
479
+ if any(
480
+ value != defaults[name]
481
+ for name, value in vars(args).items()
482
+ if name not in ("gui", "no_browser")
483
+ ):
484
+ raise ConfigError(
485
+ "--gui takes no other arguments except --no-browser; choose files and settings in the page"
486
+ )
487
+ from .gui_server import serve
488
+
489
+ return serve(Path.cwd(), not args.no_browser)
490
+ if args.print_default_config:
491
+ print(json.dumps(DEFAULT_CONFIG, indent=2))
492
+ return 0
493
+ if args.list_tables:
494
+ print(describe(args.list_tables))
495
+ return 0
496
+ if args.xlsx_mapping_template:
497
+ print(json.dumps(mapping_template(args.xlsx_mapping_template), indent=2))
498
+ return 0
499
+ baseline_command = bool(args.create_baseline or args.list_baselines)
500
+ review_command = [flag for flag in REVIEW_FLAGS if getattr(args, flag)]
501
+ if review_command:
502
+ names = ", ".join("--" + flag.replace("_", "-") for flag in REVIEW_FLAGS)
503
+ if (
504
+ len(review_command) > 1
505
+ or baseline_command
506
+ or args.check_gate
507
+ or args.old
508
+ or args.new
509
+ or args.extract_comments
510
+ or args.baselines
511
+ ):
512
+ raise ConfigError(
513
+ f"{names} cannot be combined with each other, --create-baseline, --list-baselines, --check-gate, "
514
+ "--baselines, --old, --new or --extract-comments"
515
+ )
516
+ if (args.role or args.decision or args.note or args.evidence) and not args.sign_off:
517
+ raise ConfigError(
518
+ "--role, --decision, --note and --evidence apply only to --sign-off"
519
+ )
520
+ if args.user and not (args.sign_off or args.waive):
521
+ raise ConfigError("--user applies only to --sign-off and --waive")
522
+ if (args.waive or args.reason) and not args.create_baseline:
523
+ raise ConfigError("--waive and --reason apply only to --create-baseline")
524
+ if args.reason and not args.waive:
525
+ raise ConfigError("--reason applies only with --waive")
526
+ if args.system and not (args.create_baseline or args.check_gate):
527
+ raise ConfigError("--system applies only to --create-baseline and --check-gate")
528
+ if (
529
+ not args.output
530
+ and not baseline_command
531
+ and not args.check_gate
532
+ and not review_command
533
+ ):
534
+ raise ConfigError("--output is required")
535
+ selection = XlsxSelection(args.table, args.sheet, args.header_row)
536
+ inputs = [path for path in (args.input, args.old, args.new) if path]
537
+ if selection.given and not any(is_xlsx(path) for path in inputs):
538
+ raise ConfigError(
539
+ "--table, --sheet and --header-row apply only to .xlsx inputs"
540
+ )
541
+ config = load_config(args.config, _overrides(args))
542
+ if selection.given and config["xlsx"]["sources"]:
543
+ raise ConfigError(
544
+ "--table, --sheet and --header-row cannot be combined with xlsx.sources in the configuration"
545
+ )
546
+ if review_command:
547
+ return run_review_command(args, selection, config)
548
+ if args.check_gate:
549
+ return run_check_gate(args, selection, config)
550
+ if baseline_command:
551
+ return run_baseline_command(args, selection, config)
552
+ if args.extract_comments:
553
+ if args.input or args.old or args.new:
554
+ raise ConfigError("--extract-comments cannot be combined with CSV inputs")
555
+ extract_comments_csv(
556
+ args.extract_comments,
557
+ args.output,
558
+ config["comments"]["csv_column"],
559
+ config["comments"]["csv_format"],
560
+ args.csv_base,
561
+ args.allow_comment_loss,
562
+ config["csv"]["delimiter"],
563
+ )
564
+ return 0
565
+ diff_mode = bool(args.old or args.new)
566
+ if diff_mode:
567
+ if not args.old or not args.new or args.input:
568
+ raise ConfigError(
569
+ "diff mode requires --old and --new and no positional input"
570
+ )
571
+ if args.old.startswith("git:"):
572
+ if is_xlsx(args.old) or is_xlsx(args.new):
573
+ raise InputError(
574
+ "git baselines need CSV for now; export the .xlsx to CSV and commit that"
575
+ )
576
+ old_source = git_source(args.old, args.new, config)
577
+ else:
578
+ old_source = input_source(args.old, selection, config)
579
+ old = read_csv_source(old_source, config)
580
+ new = read_csv_source(input_source(args.new, selection, config), config)
581
+ else:
582
+ if not args.input:
583
+ raise ConfigError("snapshot mode requires an input CSV")
584
+ old = None
585
+ source = input_source(args.input, selection, config)
586
+ new = read_csv_source(source, config)
587
+ if is_xlsx(args.input) and args.output.lower().endswith(".csv"):
588
+ # Same bytes the browser's Save CSV writes: BOM and CRLF.
589
+ assert source.text is not None
590
+ for warning in new.warnings:
591
+ print(f"warning: {warning}", file=sys.stderr)
592
+ atomic_write_text(args.output, "\ufeff" + source.text)
593
+ return 0
594
+ store = table_store(args.new if diff_mode else args.input, selection, config)
595
+ embedded = []
596
+ if args.baselines:
597
+ names = [name.strip() for name in args.baselines.split(",") if name.strip()]
598
+ embedded = load_baselines(store, config, names)
599
+ controls = build_controls(store, config)
600
+ data = build_report_data(
601
+ new,
602
+ config,
603
+ Path(args.output).name,
604
+ old=old,
605
+ baselines=embedded,
606
+ controls=controls,
607
+ )
608
+ for warning in data["meta"].get("warnings", []):
609
+ print(f"warning: {warning}", file=sys.stderr)
610
+ atomic_write_text(args.output, render_report(data))
611
+ return 0
612
+
613
+
614
+ def main(argv: list[str] | None = None) -> int:
615
+ try:
616
+ return run(argv)
617
+ except (ConfigError, argparse.ArgumentError) as exc:
618
+ print(f"error: {exc}", file=sys.stderr)
619
+ return 2
620
+ except InputError as exc:
621
+ print(f"error: {exc}", file=sys.stderr)
622
+ return 3
623
+ except OutputError as exc:
624
+ print(f"error: {exc}", file=sys.stderr)
625
+ return 4
626
+ except ReqtableError as exc:
627
+ print(f"error: {exc}", file=sys.stderr)
628
+ return 5
629
+ except Exception as exc:
630
+ print(f"error: unexpected internal error: {exc}", file=sys.stderr)
631
+ return 5
reqtable/comparison.py ADDED
@@ -0,0 +1,63 @@
1
+ """Pure comparison model shared by the report and its Node tests."""
2
+
3
+ COMPARISON_JS = r"""
4
+ function rowStatus(oldRow,newRow,columnNames,ignored,normalize){
5
+ if(!oldRow)return "added";
6
+ if(!newRow)return "removed";
7
+ const changed=columnNames.some(name=>!ignored.has(name)
8
+ &&normalize(String(oldRow[name]??""))!==normalize(String(newRow[name]??"")));
9
+ return changed?"changed":"unchanged";
10
+ }
11
+ function baselineScopeIds(rows,idColumn,system,includes){
12
+ const cell=(row,column)=>String(row[column]??"").trim(),type=row=>cell(row,"Type")||"Requirement",id=row=>cell(row,idColumn),
13
+ links=(row,column)=>cell(row,column).split(";").map(part=>part.trim()).filter(Boolean),
14
+ included=new Set(includes),candidates=rows.filter(row=>included.has(type(row)));
15
+ if(system===null)return new Set(candidates.map(id));
16
+ const subtree=new Set([system]),systems=rows.filter(row=>type(row)==="System");
17
+ for(let grew=true;grew;){
18
+ grew=false;
19
+ for(const row of systems){
20
+ if(!subtree.has(id(row))&&links(row,"Part of").some(parent=>subtree.has(parent))){subtree.add(id(row));grew=true;}
21
+ }
22
+ }
23
+ const scope=new Set(),add=(kind,test)=>{for(const row of candidates)if(type(row)===kind&&test(row))scope.add(id(row));},
24
+ cited=(kind,column)=>new Set(rows.filter(row=>type(row)===kind&&scope.has(id(row))).flatMap(row=>links(row,column)));
25
+ add("System",row=>subtree.has(id(row)));
26
+ add("Requirement",row=>links(row,"Allocated to").some(target=>subtree.has(target)));
27
+ const tests=cited("Requirement","Verified by");
28
+ add("Test",row=>tests.has(id(row)));
29
+ const plans=cited("Test","Test plan"),gated=new Set(["System","Requirement","Test","Test plan"]);
30
+ add("Test plan",row=>plans.has(id(row)));
31
+ return new Set(candidates.filter(row=>scope.has(id(row))||!gated.has(type(row))).map(id));
32
+ }
33
+ function baselineSource(baseline,currentRows,idColumn,ignoredColumns){
34
+ const scope=baselineScopeIds(currentRows,idColumn,baseline.system,baseline.includes),
35
+ ignored=new Set(ignoredColumns);
36
+ if(!baseline.capturesStatus)for(const column of baseline.statusColumns)ignored.add(column);
37
+ return{kind:"baseline",label:baseline.name,rows:baseline.rows,inScope:row=>scope.has(String(row[idColumn]??"").trim()),
38
+ ignoredColumns:ignored,moves:false};
39
+ }
40
+ function comparisonPairs(currentRows,source,idColumn){
41
+ const identify=row=>String(row[idColumn]??"").trim(),oldMap=new Map((source?.rows??[]).map(row=>[identify(row),row])),
42
+ newMap=new Map(currentRows.map(row=>[identify(row),row])),ids=currentRows.map(identify);
43
+ if(source){
44
+ let following=null;
45
+ for(let index=source.rows.length-1;index>=0;index--){
46
+ const id=identify(source.rows[index]);
47
+ if(!newMap.has(id)){
48
+ if(following===null)ids.push(id);
49
+ else ids.splice(ids.indexOf(following),0,id);
50
+ }
51
+ following=id;
52
+ }
53
+ }
54
+ return ids.map(id=>{
55
+ const newRow=newMap.get(id);
56
+ return{id,newRow,oldRow:newRow&&source&&!source.inScope(newRow)?newRow:oldMap.get(id)};
57
+ });
58
+ }
59
+ function comparisonStatuses(currentRows,source,idColumn,columnNames,normalize){
60
+ return Object.fromEntries(comparisonPairs(currentRows,source,idColumn).map(({id,oldRow,newRow})=>
61
+ [id,source?rowStatus(oldRow,newRow,columnNames,source.ignoredColumns,normalize):"unchanged"]));
62
+ }
63
+ """