# -*- coding: utf-8 -*- """xq0085 团购数据核对 打包脚本:生成 dist-plugin/xq0085_v<版本>.zip。 排除:ui-src(前端源码,发布包只要构建产物 ui/)、node_modules、__pycache__、 debug、.sessions、本脚本、*.pyc/*.pyo。 声明了 ``"ui": true`` 时必须先构建出 ``ui/index.html``,否则直接报错。 用法:.venv\\Scripts\\python.exe plugins\\XQ0001-0100\\XQ0085 团购数据核对\\_pack.py """ import json import os import sys import zipfile HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.abspath(os.path.join(HERE, '..', '..', '..')) OUT = os.path.join(ROOT, 'dist-plugin') EXCLUDE_DIRS = {'ui-src', 'node_modules', '__pycache__', 'debug', '.sessions'} EXCLUDE_FILES = {'_pack.py'} def main(): with open(os.path.join(HERE, 'plugin.json'), encoding='utf-8') as fp: manifest = json.load(fp) if manifest.get('ui') and not os.path.isfile(os.path.join(HERE, 'ui', 'index.html')): sys.exit('插件声明了 ui: true,但缺少 ui/index.html;请先在 ui-src 下执行 npm run build') name = 'xq%s_v%s.zip' % (manifest['id'].replace('xq', ''), manifest['version']) os.makedirs(OUT, exist_ok=True) target = os.path.join(OUT, name) with zipfile.ZipFile(target, 'w', zipfile.ZIP_DEFLATED) as zf: for dirpath, dirnames, files in os.walk(HERE): dirnames[:] = [d for d in dirnames if d not in EXCLUDE_DIRS] for f in files: if f in EXCLUDE_FILES or f.endswith(('.pyc', '.pyo')): continue full = os.path.join(dirpath, f) rel = os.path.relpath(full, HERE) zf.write(full, rel) size = os.path.getsize(target) / 1024 print(f'{target}({size:.0f} KB)') if __name__ == '__main__': main()