flzta commited on
Commit
6107d9f
·
verified ·
1 Parent(s): 47aad0d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ import subprocess
4
+ import os
5
+ import shlex # 用于安全地分割命令
6
+
7
+ # 检查配置文件路径(主要用于调试)
8
+ config_path = "/root/.config/rclone/rclone.conf"
9
+ if os.path.exists(config_path):
10
+ print(f"INFO: rclone config file found at {config_path}")
11
+ # 可以在这里添加读取大小或少量内容进行验证,但避免打印敏感信息
12
+ # config_size = os.path.getsize(config_path)
13
+ # print(f"INFO: rclone config file size: {config_size} bytes")
14
+ if os.path.getsize(config_path) == 0:
15
+ print("WARNING: rclone config file exists but is empty. Check RCLONE_CONFIG_CONTENT secret value.")
16
+ else:
17
+ print(f"WARNING: rclone config file not found at {config_path}. Ensure RCLONE_CONFIG_CONTENT secret is set correctly.")
18
+
19
+ def run_rclone_command(command_string):
20
+ """ Executes an rclone command and returns its output """
21
+ if not command_string or not command_string.strip().lower().startswith("rclone"):
22
+ return "Error: Invalid command. Input must start with 'rclone'."
23
+
24
+ # 使用 shlex 安全地分割命令和参数
25
+ try:
26
+ # 将命令转换为列表,以便 subprocess.run 安全处理
27
+ args = shlex.split(command_string)
28
+ print(f"Executing command: {' '.join(args)}") # Log the command being run
29
+
30
+ # 执行 rclone 命令
31
+ result = subprocess.run(
32
+ args,
33
+ capture_output=True, # 捕获标准输出和标准错误
34
+ text=True, # 以文本形式捕获输出
35
+ check=False # 不会在命令失败时抛出异常,而是检查返回码
36
+ )
37
+
38
+ # 准备输出字符串
39
+ output = f"Command: {' '.join(args)}\n"
40
+ output += f"--- STDOUT ---\n{result.stdout}\n"
41
+ if result.stderr:
42
+ output += f"--- STDERR ---\n{result.stderr}\n"
43
+ output += f"--- Exit Code: {result.returncode} ---"
44
+
45
+ print(f"Command finished with exit code {result.returncode}") # Log exit code
46
+ return output
47
+
48
+ except FileNotFoundError:
49
+ print(f"Error: '{args[0]}' not found.")
50
+ return f"Error: '{args[0]}' command not found. Is rclone correctly installed in the Docker container?"
51
+ except Exception as e:
52
+ print(f"Error executing command: {e}")
53
+ return f"An unexpected error occurred: {str(e)}"
54
+
55
+ # 创建 Gradio 用户界面
56
+ iface = gr.Interface(
57
+ fn=run_rclone_command,
58
+ inputs=gr.Textbox(lines=2, placeholder="Enter rclone command here...\ne.g., rclone version", label="Rclone Command"),
59
+ outputs=gr.Textbox(lines=20, label="Output Log"),
60
+ title="Rclone Command Runner",
61
+ description="Enter an rclone command below and click 'Submit'. Ensure your rclone configuration is provided via the `RCLONE_CONFIG_CONTENT` secret in Space settings.",
62
+ examples=[
63
+ ["rclone version"],
64
+ ["rclone listremotes"],
65
+ ["rclone config file"], # 显示 rclone 正在使用的配置文件路径
66
+ # ["rclone lsd YourRemoteName:"] # 取消注释并替换 YourRemoteName
67
+ ],
68
+ allow_flagging='never' # 禁止用户标记输出(通常用于反馈)
69
+ )
70
+
71
+ # 启动 Gradio 应用
72
+ if __name__ == "__main__":
73
+ # server_name="0.0.0.0" 使应用在 Docker 容器内监听所有接口
74
+ iface.launch(server_name="0.0.0.0")