87 lines
4.5 KiB
Python
87 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Local preserved-session and typed evidence CLI (never launches originals).
|
|
|
|
Examples: stage --source ../Original --manifest docs/original-manifest.json
|
|
--output artifacts/fidelity/sessions --session-id fresh-001; identify dist/linux-x64;
|
|
import BUNDLE/receipt.json --root BUNDLE --output artifacts/fidelity/receipts;
|
|
validate RECEIPT --root IMPORTED_DIR --allow-open. Synthetic imports require a
|
|
separate output directory whose name contains 'test'. All outputs remain local.
|
|
"""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
from fidelity_receipts import (identify_tree, identify_report, import_receipt,
|
|
read_json, read_receipt, stage_original, validate_receipt,
|
|
audit_receipt_tree, canonical_bytes, MAX_JSON_BYTES)
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
sub = parser.add_subparsers(dest='command', required=True)
|
|
stage = sub.add_parser('stage')
|
|
for flag in ('source', 'manifest', 'output'):
|
|
stage.add_argument('--' + flag, type=Path, required=True)
|
|
stage.add_argument('--session-id', required=True)
|
|
identify = sub.add_parser('identify')
|
|
identify.add_argument('path', type=Path)
|
|
for flag in ('report', 'executable', 'resources', 'launcher', 'project'):
|
|
identify.add_argument('--' + flag, type=Path)
|
|
imp = sub.add_parser('import')
|
|
imp.add_argument('receipt', type=Path)
|
|
imp.add_argument('--root', type=Path, required=True)
|
|
imp.add_argument('--output', type=Path, required=True)
|
|
validate = sub.add_parser('validate')
|
|
validate.add_argument('receipt', type=Path, nargs='?')
|
|
validate.add_argument('--root', type=Path)
|
|
validate.add_argument('--scenarios', type=Path)
|
|
validate.add_argument('--receipts', type=Path)
|
|
mode = validate.add_mutually_exclusive_group()
|
|
mode.add_argument('--allow-open', action='store_true')
|
|
mode.add_argument('--require-complete', action='store_true')
|
|
args = parser.parse_args(argv)
|
|
try:
|
|
if args.command == 'stage':
|
|
result = stage_original(args.source, args.manifest, args.output, args.session_id)
|
|
elif args.command == 'identify':
|
|
result = identify_report(args.path, args.report, args.executable, args.resources,
|
|
args.launcher, args.project) if any(
|
|
getattr(args, k) is not None for k in ('report', 'executable', 'resources', 'launcher', 'project')) else identify_tree(args.path)
|
|
elif args.command == 'import':
|
|
result = import_receipt(args.receipt, args.root, args.output)
|
|
else:
|
|
if args.scenarios:
|
|
if args.receipt or args.root or not args.receipts:
|
|
raise ValueError('Scenario validation requires --scenarios PATH --receipts ROOT')
|
|
result = audit_receipt_tree(read_json(args.scenarios), args.receipts)
|
|
print('Structural readiness: READY; acceptance: ' + ('COMPLETE' if result['complete'] else 'OPEN'))
|
|
print('Accepted obligations: {accepted}/{required}'.format(**result))
|
|
print('Missing case IDs: ' + ', '.join(result['missing_case_ids']))
|
|
for key in ('missing_obligations', 'open_receipts', 'invalid'):
|
|
for item in result[key]:
|
|
print(key + ': ' + item)
|
|
if result['invalid']:
|
|
return 2
|
|
return 1 if args.require_complete and not result['complete'] else 0
|
|
if args.receipt is None or args.root is None or args.receipts:
|
|
raise ValueError('Receipt validation requires RECEIPT --root ROOT')
|
|
result = validate_receipt(read_receipt(args.receipt), args.root)
|
|
print('Receipt {receipt_id}: {evidence_kind}, status={status}'.format(**result))
|
|
print('Acceptance: ' + ('ACCEPTED' if result['accepted'] else 'OPEN'))
|
|
for missing in result['missing_evidence']:
|
|
print('Missing/open: ' + str(missing))
|
|
return 1 if args.require_complete and not result['accepted'] else 0
|
|
data = canonical_bytes(result)
|
|
if len(data) > MAX_JSON_BYTES:
|
|
raise ValueError('Result JSON exceeds 4 MiB limit')
|
|
print(data.decode(), end='')
|
|
return 0
|
|
except (ValueError, OSError, KeyError, TypeError) as error:
|
|
print('Invalid fidelity evidence: ' + str(error), file=sys.stderr)
|
|
return 2
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|