Files
kefu/deploy/perm-button-20261008/head-build/pbtn_reverse.py
T
2026-11-16 09:39:42 +08:00

147 lines
7.8 KiB
Python

"""Reconstruct the pre-release (= knowledge-view, live) versions of the admin-web files touched by perm-button.
Replays every edit of this release in reverse order (new -> old, each must match exactly once) on copies of the
current files and writes them under OUT. New files of this release are listed in REMOVED. The result is verified
by building it: its page graph must equal production's.
python pbtn_reverse.py OUT_DIR
"""
import runpy, sys, types
from pathlib import Path
SCRATCH = Path(__file__).resolve().parent
WEB = 'C:/wechat_rpa/admin-web/apps/web-antd/'
OUT = Path(sys.argv[1])
steps = [] # (path, pairs) in the order they were applied
def recorder(path, pairs):
path = str(path).replace('\\', '/')
if path.startswith(WEB) or not path.startswith('C:/'):
steps.append((path, list(pairs)))
fake = types.ModuleType('edhelp')
fake.edit = recorder
sys.modules['edhelp'] = fake
def script(name):
runpy.run_path(str(SCRATCH / name), run_name='__main__')
def inline(path, pairs):
steps.append((WEB + path, pairs))
script('ed_time.py')
inline('src/bootstrap.ts', [("import '@vben/styles/antd';\n", "import '@vben/styles/antd';\nimport './styles/layout-fixes.css';\n")])
script('ed_front_btn1.py')
script('ed_front_btn2.py')
script('ed_front_btn3.py')
script('ed_front_btn4.py')
script('ed_front_btn5.py')
inline('src/views/console/permission-menu.test.ts', [(" pageMissing,\n routePermissions,\n", " pageMissing,\n REQUIRES,\n routePermissions,\n")])
inline('src/router/access.ts', [
("import { mergeCustomMenus } from './custom-menus';",
"import { mergeCustomMenus } from './custom-menus';\nimport { parentPaths, pruneEmptyMenus } from './menu-access';"),
(" return await generateAccessible(preferences.app.accessMode, {", " const access = await generateAccessible(preferences.app.accessMode, {"),
(" layoutMap,\n pageMap,\n });\n}",
" layoutMap,\n pageMap,\n });\n return { ...access, accessibleMenus: pruneEmptyMenus(access.accessibleMenus, parentPaths(routes)) };\n}"),
])
inline('src/router/guard.ts', [
("import { generateAccess } from './access';", "import { generateAccess } from './access';\nimport { firstMenuPath } from './menu-access';"),
(""" const redirectPath = (from.query.redirect ??
(to.path === preferences.app.defaultHomePath
? userInfo.homePath || preferences.app.defaultHomePath
: to.fullPath)) as string;
return {
...router.resolve(decodeURIComponent(redirectPath)),
replace: true,
};""", """ const redirectPath = (from.query.redirect ??
(to.path === preferences.app.defaultHomePath
? userInfo.homePath || preferences.app.defaultHomePath
: to.fullPath)) as string;
const target = router.resolve(decodeURIComponent(redirectPath));
// 默认首页没权限(比如只管知识审核的角色看不了调用统计):去左侧第一个能打开的页面,别落在 404
const home = [preferences.app.defaultHomePath, userInfo.homePath].includes(target.path);
const fallback = home && target.name === 'FallbackNotFound' ? firstMenuPath(accessibleMenus) : undefined;
return {
...(fallback ? router.resolve(fallback) : target),
replace: true,
};"""),
])
inline('src/router/guard.ts', [(""" // 是否已经生成过动态路由
if (accessStore.isAccessChecked) {
return true;
}""", """ // 是否已经生成过动态路由
if (accessStore.isAccessChecked) {
// 点 Logo / 回首页时同理:首页没权限就去第一个能打开的页面
if (to.name === 'FallbackNotFound' && to.path === preferences.app.defaultHomePath) {
const fallback = firstMenuPath(accessStore.accessMenus);
if (fallback) return { path: fallback, replace: true };
}
return true;
}""")])
inline('src/views/console/model-catalog.vue', [
(""" { title: '名称', dataIndex: 'name', key: 'name' },
{ title: '接口类型', dataIndex: 'kind', key: 'kind', width: 110 },
{ title: '实际请求地址', dataIndex: 'endpoint', key: 'endpoint', ellipsis: true },
{ title: '模型', dataIndex: 'model', key: 'model' },""", """ // 「实际请求地址」省略显示会让表格按固定列宽排版:没写宽度的列在窄窗口里会被挤成 0,
// 所以每列都给宽度,放不下时表格横向滚动
{ title: '名称', dataIndex: 'name', key: 'name', width: 160 },
{ title: '接口类型', dataIndex: 'kind', key: 'kind', width: 110 },
{ title: '实际请求地址', dataIndex: 'endpoint', key: 'endpoint', ellipsis: true, width: 260 },
{ title: '模型', dataIndex: 'model', key: 'model', width: 160 },"""),
(""" :columns="columns" :data-source="models" :loading="loading"
row-key="id" size="small" :pagination="false"
>""", """ :columns="columns" :data-source="models" :loading="loading"
row-key="id" size="small" :pagination="false" :scroll="{ x: 1350 }"
>"""),
])
inline('src/views/knowledge/center.vue', [
(""" <Table :row-selection="rowSelection" :data-source="items" row-key="id" :loading="loading" :pagination="{ current: page, pageSize: 30, total, showSizeChanger: false }" @change="(p) => { page = p.current || 1; listChanged(); }"
:columns="[{ title: '问题 / 标题', dataIndex: 'title', key: 'title' }, ...(single ? [] : [{ title: '账号', dataIndex: 'account_label', width: 160 }]), { title: '分类', dataIndex: 'category' },""",
""" <!-- 固定列宽 + 最小 1000px:窗口窄时整表横向滚动,标题列不会把账号、状态挤成一个字一行 -->
<Table :row-selection="rowSelection" :data-source="items" row-key="id" :loading="loading" :pagination="{ current: page, pageSize: 30, total, showSizeChanger: false }" @change="(p) => { page = p.current || 1; listChanged(); }"
:scroll="{ x: 1000 }"
:columns="[{ title: '问题 / 标题', dataIndex: 'title', key: 'title' }, ...(single ? [] : [{ title: '账号', dataIndex: 'account_label', width: 160 }]), { title: '分类', dataIndex: 'category', width: 120 },"""),
])
inline('src/views/console/permission-menu.ts', [(""" action: '操作',
link: '菜单链接',""", """ action: '按钮',
link: '菜单链接',""")])
inline('src/views/console/roles.vue', [
("勾了操作但没勾「页面」,左侧菜单里看不到这个页面。", "勾了按钮但没勾「页面」,左侧菜单里看不到这个页面。"),
("提交的是最终状态:取消勾选就是收回该权限。勾选操作时会自动带上它所在的页面和必需的权限。",
"提交的是最终状态:取消勾选就是收回该权限。每个按钮单独授权;勾选按钮时会自动带上它所在的页面和必需的权限。"),
('message="权限按左侧菜单组织:菜单 → 页面 → 能做的操作"', 'message="权限按左侧菜单组织:菜单 → 页面 → 页面里的每个按钮"'),
])
REMOVED = ['src/styles/layout-fixes.css', 'src/router/menu-access.ts', 'src/router/menu-access.test.ts']
texts = {}
for path, pairs in steps:
if path.startswith(WEB) and path not in texts:
texts[path] = Path(path).read_bytes().decode('utf-8')
for path, pairs in reversed(steps):
if not path.startswith(WEB):
continue
crlf = '\r\n' in texts[path]
text = texts[path].replace('\r\n', '\n')
for old, new in reversed(pairs):
assert text.count(new) == 1, (path, new[:80], text.count(new))
text = text.replace(new, old)
texts[path] = text.replace('\n', '\r\n') if crlf else text
for path, text in texts.items():
rel = path[len(WEB):]
target = OUT / rel
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(text.encode('utf-8'))
(OUT / 'REMOVED.txt').write_text('\n'.join(REMOVED) + '\n')
print(f'{len(texts)} files reconstructed, {len(REMOVED)} new files to remove')
for path in sorted(texts):
print(' ', path[len(WEB):])