启动并读取lftp输出

This commit is contained in:
zhbaor 2021-11-03 20:28:40 +08:00
parent b012ae991c
commit 11d745b3e0

View file

@ -1,13 +1,17 @@
from configparser import ConfigParser from configparser import ConfigParser
from pathlib import Path from pathlib import Path
from json import dump, load from json import dump, dumps
import subprocess from subprocess import STDOUT, Popen, PIPE
from threading import Thread
from queue import Empty, SimpleQueue
from tkinter import * from tkinter import *
from tkinter import filedialog from tkinter import filedialog
from tkinter.ttk import * from tkinter.ttk import *
config = ConfigParser() config = ConfigParser()
options = {"ftp": ["host", "user", "pass", "lftp"], "file": ["local", "remote"]} options = {"ftp": ["host", "user", "pass", "lftp"], "file": ["local", "remote"]}
output_queue = SimpleQueue()
finished = False
def check_config_valid(config: ConfigParser, options: dict[str, list[str]]) -> bool: def check_config_valid(config: ConfigParser, options: dict[str, list[str]]) -> bool:
@ -30,7 +34,7 @@ def set_default_config(config: ConfigParser, options: dict[str, list[str]]) -> N
config.write(f) config.write(f)
def generate_db(config: ConfigParser) -> None: def generate_db(config: ConfigParser) -> str:
local_dir = Path(config["file"]["local"]) local_dir = Path(config["file"]["local"])
categories = Path(local_dir, "categories") categories = Path(local_dir, "categories")
output = [] output = []
@ -43,9 +47,7 @@ def generate_db(config: ConfigParser) -> None:
output.append(output_category) output.append(output_category)
with open(Path(local_dir, "db.json"), "w") as f: with open(Path(local_dir, "db.json"), "w") as f:
dump(output, f) dump(output, f)
return dumps(output)
# generate_db(config)
def sync_all_command() -> list[str]: def sync_all_command() -> list[str]:
@ -58,7 +60,7 @@ def sync_all_command() -> list[str]:
] ]
def sync_files_command() -> list[str]: def sync_pdf_command() -> list[str]:
return [ return [
config["ftp"]["lftp"], config["ftp"]["lftp"],
f'-e mirror -R -e --parallel=10 {config["file"]["local"]}/categories {config["file"]["remote"]}/categories; exit', f'-e mirror -R -e --parallel=10 {config["file"]["local"]}/categories {config["file"]["remote"]}/categories; exit',
@ -68,6 +70,47 @@ def sync_files_command() -> list[str]:
] ]
def sync_func(command: list[str]) -> None:
global finished
finished = False
db_log = generate_db(config)
output_queue.put_nowait(f"db.json: {db_log}\n")
output_queue.put_nowait(f"command: {command}\n")
output_queue.put_nowait("Started lftp...\n")
proc = Popen(command, stdout=PIPE, stderr=STDOUT)
ret_val = None
while ret_val is None:
ret_val = proc.poll()
line = proc.stdout.readline()
if line:
output_queue.put_nowait(line)
output_queue.put_nowait(f"lftp exited with code {ret_val}.\n")
finished = True
def display_output():
try:
while True:
line = output_queue.get_nowait()
lftp_output.insert(END, line)
except Empty:
pass
if not finished:
root.after(200, display_output)
def sync_all_files(_: Event) -> None:
lftp_output.delete("1.0", END)
Thread(target=sync_func, args=(sync_all_command(),), daemon=True).start()
root.after(500, display_output)
def sync_pdf_files(_: Event) -> None:
lftp_output.delete("1.0", END)
Thread(target=sync_func, args=(sync_pdf_command(),), daemon=True).start()
root.after(500, display_output)
def select_lftp(_: Event) -> None: def select_lftp(_: Event) -> None:
file_path = filedialog.askopenfilename() file_path = filedialog.askopenfilename()
config["ftp"]["lftp"] = file_path config["ftp"]["lftp"] = file_path
@ -115,8 +158,6 @@ def save_config(_: Event) -> None:
config.write(f) config.write(f)
# print(subprocess.run(sync_files_command))
config.read("config.ini") config.read("config.ini")
if not check_config_valid(config, options): if not check_config_valid(config, options):
set_default_config(config, options) set_default_config(config, options)
@ -128,7 +169,7 @@ ftp_frm.grid(row=0, column=0, pady=10, padx=10)
Label(ftp_frm, text="FTP Settings").grid(row=0, column=0, columnspan=3) Label(ftp_frm, text="FTP Settings").grid(row=0, column=0, columnspan=3)
Label(ftp_frm, text="Host", width=10).grid(row=1, column=0) Label(ftp_frm, text="Hostname", width=10).grid(row=1, column=0)
host_entry = Entry(ftp_frm, width=40) host_entry = Entry(ftp_frm, width=40)
host_entry.insert(0, config["ftp"]["host"]) host_entry.insert(0, config["ftp"]["host"])
host_entry.grid(row=1, column=1, columnspan=2) host_entry.grid(row=1, column=1, columnspan=2)
@ -180,7 +221,15 @@ save_btn = Button(actions_frm, text="Save Config", width=12)
save_btn.grid(row=0, column=1) save_btn.grid(row=0, column=1)
save_btn.bind("<Button-1>", save_config) save_btn.bind("<Button-1>", save_config)
Button(actions_frm, text="Sync all Files", width=12).grid(row=0, column=2) sync_all_btn = Button(actions_frm, text="Sync all Files", width=12)
Button(actions_frm, text="Sync PDF Files", width=12).grid(row=0, column=3) sync_all_btn.grid(row=0, column=2)
sync_all_btn.bind("<Button-1>", sync_all_files)
sync_pdf_btn = Button(actions_frm, text="Sync PDF Files", width=12)
sync_pdf_btn.grid(row=0, column=3)
sync_pdf_btn.bind("<Button-1>", sync_pdf_files)
lftp_output = Text(actions_frm, width=51, height=12)
lftp_output.grid(row=1, column=0, columnspan=4)
root.mainloop() root.mainloop()