krystal7's picture
Add files using upload-large-folder tool
2eee48c verified
Raw History Blame Contribute Delete
2.03 kB
"""Normalize native service launch guards inside the local runtime container."""
from __future__ import annotations
import hashlib
import json
from pathlib import Path
import re
def normalize(source):
"""Change only the legacy environment guard; retain native service commands."""
previous = 'BENIGN_SMOKE_RUNTIME'
lines = source.splitlines(keepends=True)
changed = []
for number, line in enumerate(lines):
if previous not in line:
continue
if not re.fullmatch(
r'\[\[ "\$\{BENIGN_SMOKE_RUNTIME:-\}" == opensandbox'
r'(?: && "\$\(uname -s\)" == Linux)? \]\] \|\| '
r'\{ echo \'Run inside the authorized (?:Linux )?OpenSandbox\.\' >&2; exit 2; \}\n?', line):
raise ValueError('Unrecognized native service startup guard')
lines[number] = line.replace(previous, 'MANIPULATIONARENA_RUNTIME').replace(
'opensandbox', 'local-docker').replace('OpenSandbox', 'Docker runtime')
changed.append(number + 1)
return ''.join(lines), changed
def prepare(directory=Path('/usr/local/bin'), receipt=Path('/run/manipulationarena/startup.json')):
records = []
for path in sorted(directory.iterdir()):
if not path.name.startswith(('benign-med-', 'benign-health-')) or not path.is_file():
continue
before = path.read_bytes()
if b'BENIGN_SMOKE_RUNTIME' not in before:
continue
after, lines = normalize(before.decode('utf-8'))
raw = after.encode('utf-8')
path.write_bytes(raw)
records.append({'path': str(path), 'lines': lines,
'before_sha256': hashlib.sha256(before).hexdigest(),
'after_sha256': hashlib.sha256(raw).hexdigest()})
receipt.parent.mkdir(parents=True, exist_ok=True)
receipt.write_text(json.dumps({'runtime': 'local-docker', 'updated': records}, indent=2) + '\n')
receipt.chmod(0o600)
return records
if __name__ == '__main__':
prepare()