114 lines
3.0 KiB
Python
114 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""7z-encrypt 客户端 TUI (Python input() REPL, 中文 IME 友好)
|
|
|
|
vue-tui 版输入竞争问题修复前的过渡方案 (mt-translate 同款模式:
|
|
vue-tui 的 raw mode 输入无法配合 IME, Python cooked-mode input() 可以)。
|
|
|
|
流程: 直接驱动 7z-encrypt 客户端模块 CLI, 实时透传进度输出。
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
CLIENT_DIR = "/home/lou/文档/7z-encrypt"
|
|
PYTHON = os.path.join(CLIENT_DIR, ".venv", "bin", "python")
|
|
|
|
|
|
def run(args: list[str]) -> int:
|
|
"""子进程运行 7z-encrypt CLI, 实时透传输出 (进度可见)"""
|
|
proc = subprocess.Popen(
|
|
[PYTHON] + args,
|
|
cwd=CLIENT_DIR,
|
|
text=True,
|
|
encoding="utf-8",
|
|
)
|
|
try:
|
|
return proc.wait()
|
|
except KeyboardInterrupt:
|
|
proc.kill()
|
|
print("\n[已中断]")
|
|
return 130
|
|
|
|
|
|
def upload() -> None:
|
|
print("--- 上传文件 ---")
|
|
path = input("文件路径: ").strip()
|
|
if not path or not os.path.exists(path):
|
|
print(f"[错误] 文件不存在: {path}")
|
|
return
|
|
server = (
|
|
input("服务器 [http://127.0.0.1:8000]: ").strip()
|
|
or "http://127.0.0.1:8000"
|
|
)
|
|
chunk_size = input("卷大小 MB [10]: ").strip() or "10"
|
|
|
|
print("\n[1/2] 加密 + 分卷 + 元数据 ...")
|
|
code = run(["metadata.py", path, "--chunk-size", chunk_size])
|
|
if code != 0:
|
|
print("[失败] 加密/分卷出错")
|
|
return
|
|
|
|
init_json = path + ".init.json"
|
|
manifest = path + "_manifest.json"
|
|
if not (os.path.exists(init_json) and os.path.exists(manifest)):
|
|
print("[失败] 产物缺失 (init.json / manifest)")
|
|
return
|
|
|
|
print("\n[2/2] 上传 ...")
|
|
code = run(["transfer.py", "--server", server, "--init", init_json, "--manifest", manifest])
|
|
print("[完成]" if code == 0 else "[失败] 上传出错")
|
|
|
|
|
|
def tasks() -> None:
|
|
print("--- 任务续传 ---")
|
|
run(["state.py", "pending"])
|
|
|
|
|
|
def config_show() -> None:
|
|
print("--- 配置 ---")
|
|
run(["config.py"])
|
|
|
|
|
|
def config_edit() -> None:
|
|
url = input("服务器地址: ").strip()
|
|
if not url:
|
|
return
|
|
run(["config.py", "--set-server", url])
|
|
config_show()
|
|
|
|
|
|
def main() -> int:
|
|
print("7z-encrypt 客户端 (Python TUI)")
|
|
print("安全文件传输 · 加密 / 分卷 / 上传")
|
|
while True:
|
|
print()
|
|
print("主菜单")
|
|
print(" 1. 上传文件")
|
|
print(" 2. 任务续传")
|
|
print(" 3. 配置")
|
|
print(" q. 退出")
|
|
choice = input("> ").strip().lower()
|
|
if choice == "1":
|
|
upload()
|
|
elif choice == "2":
|
|
tasks()
|
|
elif choice == "3":
|
|
config_show()
|
|
edit = input("修改服务器地址? (y/N): ").strip().lower()
|
|
if edit == "y":
|
|
config_edit()
|
|
elif choice == "q":
|
|
print("再见")
|
|
return 0
|
|
else:
|
|
print("无效输入")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
sys.exit(main())
|
|
except KeyboardInterrupt:
|
|
print("\n再见")
|
|
sys.exit(130)
|