Spaces:
Sleeping
Sleeping
File size: 1,585 Bytes
dead840 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | """创建可复现源码包;排除密钥、缓存、工作目录和浏览器截图。"""
from pathlib import Path
import argparse,hashlib,zipfile
ROOT=Path(__file__).resolve().parents[1]
EXCLUDE={'.venv','.git','__pycache__','.pytest_cache','test-results','artifacts','node_modules'}
def main():
p=argparse.ArgumentParser();p.add_argument('output');args=p.parse_args();out=Path(args.output).resolve()
out.parent.mkdir(parents=True,exist_ok=True)
with zipfile.ZipFile(out,'w',zipfile.ZIP_DEFLATED,compresslevel=9) as z:
for f in sorted(ROOT.rglob('*')):
if not f.is_file() or f.resolve()==out or any(part in EXCLUDE for part in f.relative_to(ROOT).parts): continue
if f.name.startswith('.env') or f.suffix in ('.pyc','.log'): continue
raw=f.read_bytes()
# 防止误将 Hugging Face token 或私钥写入公开源码。
import re
if re.search(rb'hf_[A-Za-z0-9]{25,}',raw) or (b'-----BEGIN '+b'OPENSSH PRIVATE KEY-----') in raw: raise RuntimeError('疑似密钥文件,拒绝打包:'+str(f))
info=zipfile.ZipInfo('MinecraftFileConverter/'+f.relative_to(ROOT).as_posix(),(2026,10,12,0,0,0))
info.compress_type=zipfile.ZIP_DEFLATED;info.external_attr=0o644<<16;z.writestr(info,raw)
with zipfile.ZipFile(out) as z:
bad=z.testzip()
if bad: raise RuntimeError('压缩包校验失败:'+bad)
print('文件数:',len(z.namelist()))
print('SHA-256:',hashlib.sha256(out.read_bytes()).hexdigest());print(out)
if __name__=='__main__':main()
|