Files
mtusi2026lab2/scripts/clean_check.py

50 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Check committed sources in a new local clone with fresh secrets and new volumes."""
import os
from pathlib import Path
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
clone = ROOT / 'output' / 'clean-clone'
project = 'mtusi-lab2-osokin-clean'
env = dict(os.environ, PYTHONIOENCODING='utf-8', BASE_URL='http://127.0.0.1:18082')
def main():
if clone.exists():
raise SystemExit('output/clean-clone already exists; preserve or move it before repeating this check.')
clone.parent.mkdir(exist_ok=True)
log = []
def run(command, cwd):
log.append('$ ' + ' '.join(command).replace(str(ROOT), '<workspace>').replace(sys.executable, 'python'))
process = subprocess.run(command, cwd=cwd, env=env, text=True, encoding='utf-8', errors='replace',
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
log.append(process.stdout)
cleaned = '\n'.join(line.rstrip() for line in '\n'.join(log).splitlines()) + '\n'
(ROOT / 'evidence' / 'clean-clone.txt').write_text(cleaned, encoding='utf-8')
if process.returncode:
raise SystemExit('Clean clone check failed; see evidence/clean-clone.txt')
run(['git', 'clone', '--no-hardlinks', str(ROOT), str(clone)], ROOT)
assert not (clone / '.env').exists()
assert not (clone / 'secrets').exists()
run([sys.executable, 'scripts/bootstrap.py'], clone)
local_env = clone / '.env'
local_env.write_text(local_env.read_text(encoding='utf-8').replace('APP_PORT=8080', 'APP_PORT=18082'), encoding='utf-8')
compose = ['docker', 'compose', '-p', project, '--env-file', '.env']
try:
run(compose + ['build', '--no-cache', '--quiet'], clone)
run(compose + ['up', '-d', '--wait'], clone)
run([sys.executable, 'scripts/check_contract.py'], clone)
run([sys.executable, 'tests/smoke.py', '--base-url', env['BASE_URL']], clone)
run(compose + ['exec', '-T', 'api', 'python', 'student-tests/test_database.py'], clone)
run(compose + ['logs', '--no-log-prefix', 'migrate'], clone)
finally:
# Keep even the disposable check's volumes; only stop its isolated containers.
run(compose + ['down'], clone)
print('PASS: clean local clone; no copied secrets, fresh volumes, no-cache build, smoke/database checks.')
if __name__ == '__main__':
main()