#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""把 ERP 增量脚本的硬编码 NEW 路径改成读环境变量 ERP_INCREMENT（保留旧值兜底）。
铁律：脚本不准硬编码 ERP 文件名。
"""
import io, os, re

ROOT = '/Users/liuxinyuan/Desktop/Hermes输出-工作类/脚本'
OLD_LINE = re.compile(r"^NEW = '.*?'$", re.M)
NEW = ("import os as _os\n"
       "NEW = _os.environ.get('ERP_INCREMENT') or ''\n"
       "assert NEW and _os.path.exists(NEW), '请设置 ERP_INCREMENT 指向增量文件'")

for fn in ('erp_inspect_increment.py', 'erp_merge_increment.py'):
    p = os.path.join(ROOT, fn)
    s = io.open(p, encoding='utf-8').read()
    if 'ERP_INCREMENT' in s:
        print('  = %s 已是环境变量版' % fn); continue
    s2, n = OLD_LINE.subn(NEW, s)
    if n:
        io.open(p, 'w', encoding='utf-8').write(s2)
        print('  ✅ %s 已改（%d 处）' % (fn, n))
    else:
        print('  ⚠️ %s 未命中 NEW 行' % fn)
