#!/usr/bin/env python3 """Refresh accepted source-only package inventory; never restore/build/test or inspect private state.""" import argparse from datetime import datetime, timezone import hashlib import json from pathlib import Path import subprocess import xml.etree.ElementTree as ET HERE = Path(__file__).resolve().parent BASELINE = HERE / 'package-status.json' EXACT = {'ZB.MOM.WW.Theme': '1.0.1', 'ZB.MOM.WW.Secrets.Ui': '1.0.1', 'ZB.MOM.WW.Secrets.Abstractions': '0.6.6', 'bunit': '2.7.2', 'AngleSharp': '1.5.2'} def git(root, *args): return subprocess.check_output(['git', '-C', str(root), *args]) def tracked(root): return [p.decode() for p in git(root, 'ls-files', '-z').split(b'\0') if p] def elements(path, tag): return [e for e in ET.parse(path).getroot().iter() if e.tag.rsplit('}', 1)[-1] == tag] def attr(e, name): return e.get(name) or next((c.text for c in e if c.tag.rsplit('}', 1)[-1] == name), None) def package_id(e): return e.get('Include') or e.get('Update') def reference(root, project, package): entries = [e for e in elements(root / project, 'PackageReference') if package_id(e) == package] if len(entries) != 1: raise ValueError(f'{project}: expected one PackageReference for {package}; found {len(entries)}') value = attr(entries[0], 'VersionOverride') or attr(entries[0], 'Version') if value: return {'version': value, 'authority': project, 'kind': 'direct PackageReference'} # Match the currently accepted nearest-authority source declarations, not an MSBuild evaluation. for parent in (root / project).parents: central = parent / 'Directory.Packages.props' if central.is_file(): pins = [e for e in elements(central, 'PackageVersion') if package_id(e) == package] if len(pins) != 1: raise ValueError(f'{central.relative_to(root)}: ambiguous/missing central pin for {package}') return {'version': attr(pins[0], 'Version'), 'authority': str(central.relative_to(root)), 'kind': 'nearest Directory.Packages.props'} if parent == root: break raise ValueError(f'{project}: no direct or nearest central pin for {package}') def is_core(package): return package == 'ZB.MOM.WW.Secrets' or package.startswith('ZB.MOM.WW.Secrets.Replicator.') def application_declaration(path): return not any(part.lower() in {'docs', 'components', 'validation', 'artifacts', 'bin', 'obj'} for part in Path(path).parts) def production_core_declaration(path): return application_declaration(path) and not any(part.lower() in {'test', 'tests'} for part in Path(path).parts) def core_pins(root, paths): rows = [] for path in paths: if not path.endswith(('.csproj', '.props')) or not production_core_declaration(path): continue for tag in ('PackageReference', 'PackageVersion'): for e in elements(root / path, tag): package = package_id(e) or '' if is_core(package): rows.append({'file': path, 'kind': tag, 'package': package, 'version': attr(e, 'Version')}) return sorted(rows, key=lambda x: (x['file'], x['kind'], x['package'], x['version'] or '')) def statuses(root): # Git metadata only: do not open untracked files (including unrelated OEE evidence). return git(root, 'status', '--porcelain=v1', '--untracked-files=all').decode().splitlines() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--output', type=Path, required=True, help='New JSON artifact; refuses overwrite') parser.add_argument('--ot-source-frozen', action='store_true', help='Coordinator declaration only; never marks G3 complete') args = parser.parse_args() if args.output.exists() or args.output.resolve() == BASELINE: parser.error('Output must be a new path; accepted snapshot is immutable') baseline = json.loads(BASELINE.read_text()) mismatches, repositories, bunit = [], [], [] archived = {(x['sourceRoot'], x['project']) for x in baseline['bunitDiscovery']['archivedProbes']} expected_bunit = {(x['sourceRoot'], x['project']) for x in baseline['activeBunitProjects']} core_versions = {p['package']: p['version'] for r in baseline['repositories'] for p in r['secretsCoreAndBackendPins']['declaredPins'] if p['version']} discovered = set() for old in baseline['repositories']: root = Path(old['sourceRoot']) head = git(root, 'rev-parse', 'HEAD').decode().strip() paths = tracked(root) refs = [] for path in paths: if not path.endswith('.csproj'): continue packages = [package_id(e) for e in elements(root / path, 'PackageReference')] if 'bunit' in packages and (str(root), path) not in archived: discovered.add((str(root), path)) for package in packages: if (package in EXACT or is_core(package or '')) and package not in ('bunit', 'AngleSharp') and application_declaration(path): # Audit active application/test declarations; archived validation fixture graphs are separate evidence. resolved = reference(root, path, package) refs.append({'project': path, 'package': package, **resolved}) expected_version = EXACT.get(package, core_versions.get(package)) if resolved['version'] != expected_version: mismatches.append(f'{old["name"]}/{path}: {package}={resolved["version"]}, expected {expected_version}') current_core = core_pins(root, paths) accepted_core = sorted(old['secretsCoreAndBackendPins']['declaredPins'], key=lambda x: (x['file'], x['kind'], x['package'], x['version'] or '')) if current_core != accepted_core: mismatches.append(f'{old["name"]}: Secrets Core/replicator source declarations differ from accepted planning-baseline comparison') expected_refs = {(x['project'], x['package']) for x in old['productionSharedReferences']} actual_refs = {(x['project'], x['package']) for x in refs} # Core packages are separately checked in core_pins; require every previously accepted UI/Abstraction reference. missing = sorted((p, n) for p, n in expected_refs if n in EXACT and (p, n) not in actual_refs) if missing: mismatches.append(f'{old["name"]}: removed accepted shared package references {missing}') repositories.append({'name': old['name'], 'sourceRoot': str(root), 'branch': git(root, 'branch', '--show-current').decode().strip(), 'head': head, 'workingStatus': statuses(root), 'applicationSharedReferences': refs, 'excludedValidationFixtureProjects': [p for p in paths if p.endswith('.csproj') and not application_declaration(p)], 'secretsCoreAndBackendPins': {'planningBaselineCommit': old['secretsCoreAndBackendPins']['baselineCommit'], 'declaredPins': current_core, 'unchanged': current_core == accepted_core}, 'headAfterRead': git(root, 'rev-parse', 'HEAD').decode().strip()}) if repositories[-1]['headAfterRead'] != head: mismatches.append(f'{old["name"]}: HEAD changed during snapshot; rerun after coordinator freeze') if discovered != expected_bunit: mismatches.append(f'Active bUnit discovery differs: missing={sorted(expected_bunit-discovered)} unexpected={sorted(discovered-expected_bunit)}') for source_root, project in sorted(expected_bunit): root = Path(source_root) values = {package: reference(root, project, package) for package in ('bunit', 'AngleSharp')} for package, value in values.items(): if value['version'] != EXACT[package]: mismatches.append(f'{root.name}/{project}: {package} declaration mismatch') bunit.append({'sourceRoot': source_root, 'project': project, **values}) producers = {} umbrella = Path(baseline['repositories'][0]['sourceRoot']) for name, old in baseline['producerVersions'].items(): values = [e.text for e in elements(umbrella / old['authority'], 'Version')] if values != [old['version']]: mismatches.append(f'Producer {name}: expected exact Version {old["version"]}, found {values}') producers[name] = {'versionDeclarations': values, 'authority': old['authority']} oee = next(Path(r['sourceRoot']) for r in baseline['repositories'] if r['name'] == 'ignitionoee') prior = baseline['nonNugetThemeConsumers'] manifest_path = oee / prior['registry']['manifest'] manifest = json.loads(manifest_path.read_text()) files = [] for name, expected in manifest['files'].items(): target = manifest_path.parent / name if not target.resolve().is_relative_to(manifest_path.parent.resolve()): raise ValueError('Registry manifest payload escapes its directory') actual = hashlib.sha256(target.read_bytes()).hexdigest() files.append({'file': name, 'sha256': actual, 'matchesManifest': actual == expected}) if not all(x['matchesManifest'] for x in files) or manifest['files'] != {x['file']: x['expectedSha256'] for x in prior['registry']['files']}: mismatches.append('Registry static export payload hashes differ from accepted exact export') for key in ('themeVersion', 'sourcePackageSha256'): if manifest[key] != prior['registry'][key]: mismatches.append(f'Registry manifest {key} changed') perspective = json.loads((oee / prior['perspective']['map']).read_text()) if hashlib.sha256(manifest_path.read_bytes()).hexdigest() != prior['perspective']['release']['manifestSha256']: mismatches.append('Registry manifest bytes differ from accepted Perspective/export provenance') if manifest['sourceRevision'] != prior['perspective']['release']['sourceRevision']: mismatches.append('Registry source revision differs from accepted release') if perspective['release'] != prior['perspective']['release']: mismatches.append('Native Perspective release mapping changed') result = {'schemaVersion': 2, 'capturedAtUtc': datetime.now(timezone.utc).isoformat(), 'purpose': 'Read-only source-declaration refresh against accepted G3 support inventory; not G3 acceptance', 'g3Complete': False, 'otSourceFrozenDeclaredByCoordinator': args.ot_source_frozen, 'noRestoreTestsNetworkOrPrivateStateRead': True, 'limitations': ['Declared XML/nearest central authority only, not an evaluated MSBuild or newly resolved runtime graph.', 'Duplicate/missing literal package declarations fail; MSBuild conditions/import expressions are not evaluated.', 'Core/backend unchanged comparison concerns production declarations; test references are version-checked and separately discoverable in applicationSharedReferences.', 'AngleSharp.Css/Diffing resolved versions remain proven by prior GB evidence, not this source-only check.', 'Untracked paths are Git metadata only; their contents are not opened.'], 'repositories': repositories, 'activeBunitProjects': bunit, 'producerVersions': producers, 'nonNugetThemeConsumers': {'registryManifest': str(manifest_path.relative_to(oee)), 'registryFiles': files, 'perspectiveRelease': perspective['release']}, 'mismatches': mismatches, 'summary': {'activeBunitProjects': len(bunit), 'mismatchCount': len(mismatches), 'sourceOnlyNoRuntimeClaim': True}} args.output.parent.mkdir(parents=True, exist_ok=True) with args.output.open('x') as stream: json.dump(result, stream, indent=2); stream.write('\n') print(json.dumps({'output': str(args.output), **result['summary']})) return 1 if mismatches else 0 if __name__ == '__main__': raise SystemExit(main())