44 lines
2.7 KiB
Python
44 lines
2.7 KiB
Python
"""Apply the exact replayed shared client source to its existing owner."""
|
|
from pathlib import Path
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import subprocess
|
|
|
|
HERE = Path(__file__).resolve().parent
|
|
ROOT = Path('/home/worsch/key-cape')
|
|
manifest = json.loads((HERE / 'key-cape-source/source-manifest.json').read_text())
|
|
sha = lambda raw: 'sha256:' + hashlib.sha256(raw).hexdigest()
|
|
def git(*args):
|
|
return subprocess.check_output(['git', '-C', str(ROOT), *args], text=True, timeout=40).strip()
|
|
base = manifest['base_revision'].removeprefix('git:')
|
|
assert git('rev-parse', 'HEAD') == base
|
|
assert git('ls-remote', 'origin', 'refs/heads/main').split()[0] == base
|
|
assert not git('diff', '--name-only', 'HEAD'), 'preserve preexisting tracked changes'
|
|
paths = {row['path'] for row in manifest['files']}
|
|
protected = {name: {'sha256': sha((ROOT / name).read_bytes()), 'index': git('ls-files', '--stage', '--', name)}
|
|
for name in git('ls-files').splitlines() if name not in paths and (ROOT / name).is_file()}
|
|
plan = 'workplans/KEY-WP-0042-online-access-token-status.md'
|
|
before = re.findall(r'```task\n.*?```', (ROOT / plan).read_text(), re.S)
|
|
for row in manifest['files']:
|
|
file = ROOT / row['path']
|
|
assert (not file.exists()) if row['before_sha256'] is None else sha(file.read_bytes()) == row['before_sha256'], row['path']
|
|
assert sha((HERE / 'key-cape-source/postimages' / row['path']).read_bytes()) == row['sha256'], row['path']
|
|
for row in manifest['files']:
|
|
file = ROOT / row['path']; file.parent.mkdir(parents=True, exist_ok=True)
|
|
file.write_bytes((HERE / 'key-cape-source/postimages' / row['path']).read_bytes())
|
|
assert re.findall(r'```task\n.*?```', (ROOT / plan).read_text(), re.S)[:len(before)] == before
|
|
for name, row in protected.items():
|
|
assert sha((ROOT / name).read_bytes()) == row['sha256']
|
|
assert git('ls-files', '--stage', '--', name) == row['index']
|
|
out = HERE / 'keycape-application'; out.mkdir(exist_ok=False)
|
|
(out / 'protected.json').write_text(json.dumps(protected, indent=2) + '\n')
|
|
receipt = {'status': 'source-applied', 'owner': 'key-cape', 'files': len(paths),
|
|
'existing_task_blocks_preserved': len(before), 'new_source_only_task': 'KEY-WP-0042-T04',
|
|
'all_other_tracked_files_and_index_preserved': True, 'native_admission': False}
|
|
(out / 'application.json').write_text(json.dumps(receipt, indent=2) + '\n')
|
|
publication = {'owner': 'key-cape', 'base_revision': base,
|
|
'message': 'Share current-token verification and scoped cleanup through one producer client',
|
|
'files': [{'path': row['path'], 'sha256': row['sha256'].removeprefix('sha256:')} for row in manifest['files']]}
|
|
(out / 'publication-inputs.json').write_text(json.dumps(publication, indent=2) + '\n')
|
|
print(json.dumps(receipt))
|