"""Export all public working files, including new files; exclude local secrets/state.""" import hashlib import json import os import tarfile from pathlib import Path import subprocess import sys ROOT=Path(__file__).resolve().parents[1] subprocess.run([sys.executable,str(ROOT/'scripts/check-course-files.py')],check=True) paths=subprocess.check_output(['git','ls-files','--cached','--others','--exclude-standard','-z'],cwd=ROOT).split(b'\0') files={} for raw in paths: if not raw:continue name=os.fsdecode(raw) if name=='SOURCE_MANIFEST.json':continue path=ROOT/name if path.is_file():files[name]=hashlib.sha256(path.read_bytes()).hexdigest() # Actual non-example credentials must not occur in exported source/evidence. from dotenv import dotenv_values example=dotenv_values(ROOT/'student.env.example') private=[v.encode() for k,v in dotenv_values(ROOT/'.env').items() if v and len(v)>=12 and v!=example.get(k) and any(word in k for word in ('PASSWORD','SECRET','TOKEN','API_KEY','DATABASE_URL'))] unsafe=[] for name in files: path=ROOT/name if name.startswith(('.env','secrets/','backups/','data/')) and name!='.env.example': unsafe.append(name) elif any(secret in path.read_bytes() for secret in private):unsafe.append(name) if unsafe: raise SystemExit('Не экспортированы файлы с приватными данными: '+', '.join(unsafe)) proof=json.loads((ROOT/'evidence/lab4-clean-start.json').read_text()) changed=[name for name,digest in proof['code_manifest'].items() if files.get(name)!=digest] if changed:raise SystemExit('После clean-start изменился код: '+', '.join(changed)) required=('Dockerfile','compose.yaml','src/core/request_deadline.py','src/maintenance/reconcile.py', 'student.mk','student.env.example','requirements-student.txt','STUDENT_README.md', 'STUDENT_REPORT.md','course-files.sha256.json','Dockerfile.dockerignore', 'src/core/storage_bootstrap.py','infra/s3/api-policy.json','infra/s3/reconciler-policy.json', 'migrations/versions/008_reconciliation.py', 'scripts/setup-env.py','scripts/check-course-files.py','scripts/test-reconciliation.sh','tests/reconciliation.py', 'tests/lab4_review.py','tests/fixtures/sample.png') assert all(name in files for name in required),'Required source missing from export' manifest={'format':1,'lab':4,'author':'Каменский Андрей, БПИ2301', 'clean_start_code_matches':True,'private_credentials_scan':'PASS','files':files} (ROOT/'SOURCE_MANIFEST.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2)+'\n') destination=ROOT/'dist';destination.mkdir(exist_ok=True) archive=destination/'mtusi2026lab4-submission.tar.gz' with tarfile.open(archive,'w:gz') as output: for name in sorted([*files,'SOURCE_MANIFEST.json']): output.add(ROOT/name,arcname='mtusi2026lab4/'+name,recursive=False) with tarfile.open(archive,'r:gz') as source: bundled={member.name.removeprefix('mtusi2026lab4/') for member in source.getmembers()} assert set(files).issubset(bundled) and 'SOURCE_MANIFEST.json' in bundled assert '.env' not in bundled and not any(name.startswith(('.git/','.venv/')) for name in bundled) receipt={'result':'PASS','archive':str(archive.relative_to(ROOT)), 'sha256':hashlib.sha256(archive.read_bytes()).hexdigest(), 'source_files':len(files),'required_files_present':True,'clean_start_code_matches':True, 'private_credentials_scan':'PASS','local_env_excluded':True,'course_files_unchanged':True} # Receipt is separate to avoid a circular archive hash in its own contents. (destination/'submission-receipt.json').write_text(json.dumps(receipt,ensure_ascii=False,indent=2)+'\n') print(json.dumps(receipt,ensure_ascii=False,indent=2))