Files
qingjian/apps/linux/scripts/files.py
T
li-chenaandLi b12218557f feat(linux): Server 接本地整句模型重排 (#214)
* feat(linux): Server 接本地整句模型重排

- 照 Windows Server 的 dispatch/rescore 平移:后台加载、停键 80 ms 防抖、20 ms 收结果、2 s 兜底,只换整句候选
- 模型在后台接上时用户正在组句,补查一次攒下整句路径再起防抖,这一轮不错过重排
- 主循环改按 next_tick 的绝对到点时间等消息,防抖与轮询才到得了点;Poll 先 tick 再回帧
- 前文用本会话历史,不读应用光标前文;[model] 只在启动时读,Linux 没有配置热加载

* feat(linux): Poll 回帧内容没变时沿用展示身份

- 插件组句期间每 80 ms Poll 一次取重排结果;若每次都推进版本号,插件就得每拍重画、每拍回报曝光,面板闪且词汇统计被刷高
- 会话记下上次发出的帧,Poll 回的帧与之相同就当没有新展示:身份不变、不清曝光记录;重排换了顺序才推进
- KeyResult 的行为不变,每次按键仍是新的展示

* feat(linux): 插件组句期间轮询取本地整句模型的重排结果

- 插件是一问一答,Server 推不了;照 Windows DLL 挂一个 80 ms 定时器,组句期间发已有的 Poll 消息
- 只在 revision 推进时重画并回报;同版本什么都不做,面板不闪、曝光不重复记
- 帧为空、Reset、失焦、停用、断线时停表;定时器建一次反复用
- 回归:mock 全模式应答 Poll,rescore 模式第一条回换序帧,断言重画一次、回报一次

* build(linux): 安装清单加本地整句模型文件

- files.py 只装 data/generated,data-fetch.sh 取回的 data/model/model.qjm 没进清单,装完 Server 找不到模型
- 非样例安装把它按 data.lock 的校验值装到 resources/data/model/,缺文件提示先跑 data-fetch.sh
- 用户文档:Linux 页去掉「暂不提供神经模型」,本地整句模型页补 Linux 的开关与自有模型路径

---------

Co-authored-by: Li <clwillsoncl@gmail.com>
2026-09-23 08:50:45 +08:00

106 lines
5.4 KiB
Python

#!/usr/bin/env python3
# //! 安装文件清单与校验;不管理桌面服务、系统文件或用户数据。
import hashlib
import json
import os
from pathlib import Path
import shutil
import sys
import tarfile
def digest(path):
with path.open('rb') as stream:
return hashlib.file_digest(stream, 'sha256').hexdigest()
def main():
action, prefix_text, *args = sys.argv[1:]
prefix = Path(prefix_text)
if not prefix.is_absolute() or prefix == Path('/'):
raise SystemExit('需要非根绝对安装路径')
prefix = prefix.resolve()
manifest = prefix / 'share/qingjian/install-manifest.json'
old = json.loads(manifest.read_text()) if manifest.is_file() else {}
if action == 'uninstall':
for filename, checksum in old.items():
path = Path(filename)
if path.is_file() and not path.is_symlink() and digest(path) == checksum:
path.unlink()
elif path.exists():
print(f'保留已修改文件:{path}')
manifest.unlink(missing_ok=True)
return
root, server, plugin, sample = args
root = Path(root)
data = Path(os.environ.get('XDG_DATA_HOME', str(Path.home() / '.local/share')))
if not data.is_absolute():
raise SystemExit('XDG_DATA_HOME 必须是绝对路径')
files = {prefix / 'share/licenses/qingjian/LICENSE': root / 'LICENSE',
data / 'icons/hicolor/128x128/apps/qingjian.png': root / 'assets/icon/logo.png',
prefix / 'bin/qingjian-linux-server': Path(server),
prefix / 'lib/fcitx5/qingjian.so': Path(plugin)}
for kind in ('addon', 'inputmethod'):
files[data / f'fcitx5/{kind}/qingjian.conf'] = root / f'apps/linux/fcitx5/data/{kind}/qingjian.conf'
resources = prefix / 'share/qingjian/resources'
for kind in ('sample', 'glossary', 'levels', 'emoji'):
for source in (root / 'assets' / kind).rglob('*'):
if source.is_file():
files[resources / source.relative_to(root)] = source
if sample != 'true':
generated = root / 'data/generated'
dictionary = generated / 'dict.qj'
archive = root / 'target/release-data/qingjian-data.tar.gz'
lock = dict(line.strip().split(' = ', 1) for line in (root / 'tools/release/data.lock').read_text().splitlines() if ' = ' in line)
if not dictionary.is_file() or not archive.is_file():
raise SystemExit('缺少产品数据,请先运行 tools/release/data-fetch.sh,或用 --sample 体验样例词库')
if digest(archive) != lock.get('qingjian-data.tar.gz'):
raise SystemExit('产品数据包与 tools/release/data.lock 校验值不符')
verified = {}
with tarfile.open(archive) as bundle:
for member in bundle.getmembers():
if not member.isfile():
continue
source = (generated / member.name).resolve()
if not source.is_relative_to(generated.resolve()):
raise SystemExit('数据包路径越界')
checksum = hashlib.file_digest(bundle.extractfile(member), 'sha256').hexdigest()
if not source.is_file() or digest(source) != checksum:
raise SystemExit(f'产品数据校验失败:{member.name}')
verified[source] = checksum
wanted = ('dict.qj', 'lm.qj', 'lm-unigram.tsv', 'lm-bigram.tsv', 'english.tsv')
for source in generated.rglob('*'):
if source.is_file() and (source.name in wanted or source.name.startswith('glossary-') or source.parent.name == 'dicts'):
if source.resolve() not in verified:
raise SystemExit(f'产品数据没有校验记录:{source}')
files[resources / source.relative_to(root)] = source
model = root / 'data/model/model.qjm'
if not model.is_file():
raise SystemExit('缺少本地整句模型 data/model/model.qjm,请先运行 tools/release/data-fetch.sh,或用 --sample 体验样例词库')
if digest(model) != lock.get('model.qjm'):
raise SystemExit('本地整句模型与 tools/release/data.lock 校验值不符')
files[resources / 'data/model/model.qjm'] = model
# 安装前先检查所有目标,避免覆盖其他来源的同名文件。
for target in files:
if target.is_symlink() or (target.exists() and (str(target) not in old or digest(target) != old[str(target)])):
raise SystemExit(f'目标已存在且不属于本次安装:{target}')
installed = {}
for target, source in files.items():
target.parent.mkdir(parents=True, exist_ok=True)
temporary = target.with_name(target.name + '.qingjian-tmp')
shutil.copy2(source, temporary)
if target == data / 'fcitx5/addon/qingjian.conf':
temporary.write_text(temporary.read_text().replace('Library=qingjian\n', f'Library={prefix}/lib/fcitx5/qingjian\n'))
temporary.replace(target)
installed[str(target)] = digest(target)
for filename, checksum in old.items():
path = Path(filename)
if filename not in installed and path.is_file() and not path.is_symlink() and digest(path) == checksum:
path.unlink()
manifest.parent.mkdir(parents=True, exist_ok=True)
manifest.write_text(json.dumps(installed, ensure_ascii=False, indent=2) + '\n')
if __name__ == '__main__':
main()