Spaces:
Sleeping

File size: 2,284 Bytes
0d7c98f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e67962
 
 
 
 
 
 
0d7c98f
5e67962
0d7c98f
5e67962
0d7c98f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e67962
d4c4fb5
0d7c98f
 
 
 
 
 
 
5e67962
 
0d7c98f
 
 
 
 
5e67962
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import gradio as gr
import subprocess
import os

def check_package_status():
    status_report = ""
    # 1. Read your packages.txt automatically
    if os.path.exists("packages.txt"):
        with open("packages.txt", "r") as f:
            packages = [line.strip() for line in f if line.strip()]
    else:
        return "⚠️ packages.txt not found!"

    # 2. Loop through each package and check version
    for pkg in packages:
        try:
            # Handle specific command names for certain packages
            if pkg == "default-jdk": 
                cmd = "java -version"
            elif pkg == "python3-pip": 
                cmd = "pip --version"
            else:
                cmd = f"{pkg} --version"
            
            # Execute and capture output
            output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
            v_info = output.decode('utf-8').split('\n')[0] 
            status_report += f"✅ **{pkg}**: {v_info}\n\n"
        except Exception:
            status_report += f"❌ **{pkg}**: Not found or check failed.\n\n"
    
    return status_report

def run_universal_command(command):
    try:
        # Runs any code: Python, Node, Java, Git, FFmpeg...
        result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
        return result.decode('utf-8')
    except subprocess.CalledProcessError as e:
        return f"Error Output:\n{e.output.decode('utf-8')}"

# --- Build the UI ---
with gr.Blocks() as demo:
    gr.Markdown("# Virtual Terminal and Universal Script Executor")
    
    with gr.Accordion("📦 System Package Status", open=True):
        status_box = gr.Markdown("Checking system packages...")
    
    with gr.Tab("Terminal"):
        cmd_input = gr.Textbox(label="Enter Command", placeholder="node -v OR python my_script.py")
        run_btn = gr.Button("Execute", variant="primary")
        # Changed 'bash' to 'shell' to fix the Gradio 6.0 error
        output_box = gr.Code(label="Console Output", language="shell")

    # Connect the logic
    demo.load(fn=check_package_status, outputs=status_box)
    run_btn.click(fn=run_universal_command, inputs=cmd_input, outputs=output_box)

# Moved theme here to fix the UserWarning
demo.launch(theme=gr.themes.Soft())