|
|
|
|
|
import gradio as gr |
|
|
import subprocess |
|
|
import os |
|
|
import shlex |
|
|
|
|
|
|
|
|
config_path = "/root/.config/rclone/rclone.conf" |
|
|
if os.path.exists(config_path): |
|
|
print(f"INFO: rclone config file found at {config_path}") |
|
|
|
|
|
|
|
|
|
|
|
if os.path.getsize(config_path) == 0: |
|
|
print("WARNING: rclone config file exists but is empty. Check RCLONE_CONFIG_CONTENT secret value.") |
|
|
else: |
|
|
print(f"WARNING: rclone config file not found at {config_path}. Ensure RCLONE_CONFIG_CONTENT secret is set correctly.") |
|
|
|
|
|
def run_rclone_command(command_string): |
|
|
""" Executes an rclone command and returns its output """ |
|
|
if not command_string or not command_string.strip().lower().startswith("rclone"): |
|
|
return "Error: Invalid command. Input must start with 'rclone'." |
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
args = shlex.split(command_string) |
|
|
print(f"Executing command: {' '.join(args)}") |
|
|
|
|
|
|
|
|
result = subprocess.run( |
|
|
args, |
|
|
capture_output=True, |
|
|
text=True, |
|
|
check=False |
|
|
) |
|
|
|
|
|
|
|
|
output = f"Command: {' '.join(args)}\n" |
|
|
output += f"--- STDOUT ---\n{result.stdout}\n" |
|
|
if result.stderr: |
|
|
output += f"--- STDERR ---\n{result.stderr}\n" |
|
|
output += f"--- Exit Code: {result.returncode} ---" |
|
|
|
|
|
print(f"Command finished with exit code {result.returncode}") |
|
|
return output |
|
|
|
|
|
except FileNotFoundError: |
|
|
print(f"Error: '{args[0]}' not found.") |
|
|
return f"Error: '{args[0]}' command not found. Is rclone correctly installed in the Docker container?" |
|
|
except Exception as e: |
|
|
print(f"Error executing command: {e}") |
|
|
return f"An unexpected error occurred: {str(e)}" |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=run_rclone_command, |
|
|
inputs=gr.Textbox(lines=2, placeholder="Enter rclone command here...\ne.g., rclone version", label="Rclone Command"), |
|
|
outputs=gr.Textbox(lines=20, label="Output Log"), |
|
|
title="Rclone Command Runner", |
|
|
description="Enter an rclone command below and click 'Submit'. Ensure your rclone configuration is provided via the `RCLONE_CONFIG_CONTENT` secret in Space settings.", |
|
|
examples=[ |
|
|
["rclone version"], |
|
|
["rclone listremotes"], |
|
|
["rclone config file"], |
|
|
|
|
|
], |
|
|
allow_flagging='never' |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
iface.launch(server_name="0.0.0.0") |