68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Sync or verify the shared HG files bundled by a standalone plugin."""
|
||
|
|
|
||
|
|
import argparse
|
||
|
|
import hashlib
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
import re
|
||
|
|
import subprocess
|
||
|
|
|
||
|
|
|
||
|
|
SOURCE_URL = 'https://code.hosting.hippogrief.nl/hippogrief/wp-plugins'
|
||
|
|
SHARED_FILES = (
|
||
|
|
'assets/hg-admin-menu.css',
|
||
|
|
'includes/hg-admin-menu.php',
|
||
|
|
'includes/hg-plugin-updates.php',
|
||
|
|
'scripts/sync_shared.py',
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def verify_shared(root: Path) -> None:
|
||
|
|
try:
|
||
|
|
lock = json.loads((root / 'shared.lock.json').read_text())
|
||
|
|
if lock['source'] != SOURCE_URL or not re.fullmatch(r'[0-9a-f]{40}', lock['commit']):
|
||
|
|
raise ValueError('invalid source or commit')
|
||
|
|
if set(lock['sha256']) != set(SHARED_FILES):
|
||
|
|
raise ValueError('unexpected shared file list')
|
||
|
|
for name in SHARED_FILES:
|
||
|
|
checksum = hashlib.sha256((root / name).read_bytes()).hexdigest()
|
||
|
|
if checksum != lock['sha256'][name]:
|
||
|
|
raise ValueError(f'checksum differs for {name}')
|
||
|
|
except (OSError, ValueError, TypeError, KeyError) as error:
|
||
|
|
raise SystemExit(f'HG shared files failed verification: {error}') from error
|
||
|
|
|
||
|
|
|
||
|
|
def sync_shared(root: Path, source: Path, ref: str) -> None:
|
||
|
|
git = ['git', '-C', str(source)]
|
||
|
|
commit = subprocess.check_output(
|
||
|
|
git + ['rev-parse', '--verify', '--end-of-options', f'{ref}^{{commit}}'],
|
||
|
|
text=True,
|
||
|
|
).strip()
|
||
|
|
shared_paths = {name: f'shared/{name}' for name in SHARED_FILES}
|
||
|
|
shared_paths['scripts/sync_shared.py'] = 'shared/sync_shared.py'
|
||
|
|
files = {
|
||
|
|
name: subprocess.check_output(git + ['show', f'{commit}:{remote_path}'])
|
||
|
|
for name, remote_path in shared_paths.items()
|
||
|
|
}
|
||
|
|
for name, contents in files.items():
|
||
|
|
(root / name).write_bytes(contents)
|
||
|
|
lock = {
|
||
|
|
'source': SOURCE_URL,
|
||
|
|
'commit': commit,
|
||
|
|
'sha256': {name: hashlib.sha256(contents).hexdigest() for name, contents in files.items()},
|
||
|
|
}
|
||
|
|
(root / 'shared.lock.json').write_text(json.dumps(lock, indent=2) + '\n')
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
||
|
|
parser.add_argument('--source', type=Path, help='Path to a checkout of wp-plugins')
|
||
|
|
parser.add_argument('--ref', help='Tag or commit to copy from the shared checkout')
|
||
|
|
options = parser.parse_args()
|
||
|
|
if bool(options.source) != bool(options.ref):
|
||
|
|
parser.error('--source and --ref must be supplied together')
|
||
|
|
plugin_root = Path(__file__).resolve().parents[1]
|
||
|
|
if options.source:
|
||
|
|
sync_shared(plugin_root, options.source, options.ref)
|
||
|
|
verify_shared(plugin_root)
|