重新按英文命名,修改成相对路径

This commit is contained in:
lianjianwei2024
2026-10-11 10:52:01 +08:00
parent eea5d511dc
commit 9bb23ac13b
64 changed files with 913 additions and 1427 deletions

View File

@@ -0,0 +1,173 @@
# -*- coding: utf-8 -*-
"""提交前体检门禁(2026-10-06)。
为什么要有这道闸:
10-06 那天 app/ui/common.js 被整段写成随机二进制,工具却报告"写入成功"。
它当时**根本没进 git**,于是唯一的副本就是那份坏文件,最后只能靠重建。
更危险的是:如果当时已经配了"每天自动 git commit",坏文件会被**自动提交**,
把服务器上一份好版本也顶掉 —— 备份就变成了销毁。
所以体检必须跑在 `git add` **之前**,且只要有一个文件坏,就中止提交。
查三件事(语法没错 ≠ 没坏):
① 编码:必须能按 UTF-8 解开(随机二进制在这一步就露馅)
② 语法:.js 走 node --check,.py 走 py_compile
③ 锚点:唯一来源文件必须含关键符号 —— 防"语法合法但内容被截短/换成别的"
另加④ 体积骤降:比上次体检小一半以上且掉超 2KB,多半是被截断,同样拦下
用法:
python health_check.py # 体检,坏了 exit 1(备份脚本据此中止)
python health_check.py --init # 只建基线不判体积(首次/大改后跑一次)
"""
import os, sys, json, subprocess, shutil
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, '..', '..', '..')) # 项目根(D:/Pricing review)
APP = os.path.join(ROOT, '造价审核', 'app')
BASE = os.path.join(ROOT, '.workbuddy', 'health_baseline.json') # .workbuddy 已被 gitignore
EXT = ('.py', '.js', '.html', '.css', '.sql')
SKIP_DIR = {'__pycache__', 'node_modules', '.git', 'release', '_build', 'data', 'uploads'}
# 顶层也要查的(根目录脚本/文档不查,只查 app 下的代码)
# 唯一来源文件 → 必须出现的关键串(缺一个就是内容不对)
ANCHORS = {
os.path.join('ui', 'common.js'): [
'function esc(', 'function ell(', 'const COLS', 'function RawView',
'function regFill', 'function saveAs', 'function tgNorm', 'function renderLme'],
os.path.join('ui', 'common.css'): ['.ell{', '.kind', '.brow'],
os.path.join('store', 'schema.sql'): ['CREATE TABLE'],
os.path.join('store', 'session.sql'): ['CREATE TABLE'],
}
def find_node():
base = os.path.join(os.path.expanduser('~'), '.workbuddy', 'binaries', 'node', 'versions')
if os.path.isdir(base):
for d in sorted(os.listdir(base), reverse=True):
for cand in (os.path.join(base, d, 'node.exe'), os.path.join(base, d, 'bin', 'node.exe')):
if os.path.exists(cand):
return cand
return None
NODE = find_node()
PY = sys.executable
def check_utf8(p):
try:
with open(p, 'rb') as f:
f.read().decode('utf-8')
return None
except Exception as e:
return 'UTF-8 解不开(多半被写成二进制):%s' % e
def _as_ext(p, ext):
"""校验用的路径:node / py_compile 都是**按后缀**认文件类型的,
给它们一个 .tmp 会直接报 Unknown file extension,所以先复制成正确后缀的副本。"""
if p.endswith(ext):
return p, None
t = p + ext
shutil.copyfile(p, t)
return t, t
def check_syntax(p, ext=None):
# ext 可显式传入:校验 .tmp 临时文件时,后缀是 .tmp,必须按**目标文件**的类型判
ext = ext or os.path.splitext(p)[1].lower()
if ext == '.js':
if not NODE:
return None, '(未找到 node,跳过语法检查)'
tgt, tmp = _as_ext(p, ext)
try:
r = subprocess.run([NODE, '--check', tgt], capture_output=True)
finally:
if tmp and os.path.exists(tmp):
os.remove(tmp)
return (None if r.returncode == 0 else r.stderr.decode('utf-8', 'ignore').strip()[:160]), None
if ext == '.py':
tgt, tmp = _as_ext(p, ext)
try:
r = subprocess.run([PY, '-m', 'py_compile', tgt], capture_output=True)
finally:
if tmp and os.path.exists(tmp):
os.remove(tmp)
return (None if r.returncode == 0 else r.stderr.decode('utf-8', 'ignore').strip()[:160]), None
return None, None
def check_anchor(rel):
keys = ANCHORS.get(rel)
if not keys:
return None
try:
s = open(os.path.join(APP, rel), encoding='utf-8').read()
except Exception as e:
return '读不出来:%s' % e
miss = [k for k in keys if k not in s]
return ('关键符号缺失:%s —— 文件可能被截短或换成了别的版本' % ', '.join(miss)) if miss else None
def main():
init_only = '--init' in sys.argv
files = []
for dirpath, dirnames, filenames in os.walk(APP):
dirnames[:] = [d for d in dirnames if d not in SKIP_DIR]
for fn in filenames:
if fn.endswith(EXT):
files.append(os.path.join(dirpath, fn))
old = {}
if os.path.exists(BASE):
try:
old = json.load(open(BASE, encoding='utf-8'))
except Exception:
old = {}
new = {}
bad, warn = [], []
for p in sorted(files):
rel = os.path.relpath(p, APP).replace('\\', '/')
sz = os.path.getsize(p)
new[rel] = sz
e = check_utf8(p)
if e:
bad.append((rel, e)); continue
e, note = check_syntax(p)
if e:
bad.append((rel, '语法错误:' + e)); continue
if note:
warn.append((rel, note))
e = check_anchor(rel)
if e:
bad.append((rel, e)); continue
if (not init_only) and rel in old:
o = old[rel]
if sz < o * 0.5 and (o - sz) > 2000:
bad.append((rel, '体积骤降 %d → %d 字节,疑似被截断' % (o, sz)))
os.makedirs(os.path.dirname(BASE), exist_ok=True)
json.dump(new, open(BASE, 'w', encoding='utf-8'), ensure_ascii=False, indent=1)
print('=' * 62)
print(' 提交前体检:共 %d 个文件' % len(files))
print('=' * 62)
for r, m in bad:
print(' [坏] %s' % r)
print(' %s' % m)
for r, m in warn:
print(' [提醒] %s %s' % (r, m))
if not bad and not warn:
print(' 全部通过:编码 OK / 语法 OK / 关键符号全在 / 体积无异常')
print('=' * 62)
if bad:
print(' 体检不通过 —— 已中止,本次不会提交任何东西。')
print(' 请先把上面标[坏]的文件修好,再重新备份。')
return 1
return 0
if __name__ == '__main__':
sys.exit(main())