1
1
import json
2
2
import os
3
3
import time
4
+ import subprocess
5
+ import getpass
4
6
5
7
from ..utils .recipient_utils import parse_for_recipient
6
8
from .languages .applescript import AppleScript
@@ -45,6 +47,29 @@ def __init__(self, computer):
45
47
]
46
48
self ._active_languages = {}
47
49
50
+ def sudo_install (self , package ):
51
+ try :
52
+ # First, try to install without sudo
53
+ subprocess .run (['apt' , 'install' , '-y' , package ], check = True )
54
+ except subprocess .CalledProcessError :
55
+ # If it fails, try with sudo
56
+ print (f"Installation of { package } requires sudo privileges." )
57
+ sudo_password = getpass .getpass ("Enter sudo password: " )
58
+
59
+ try :
60
+ # Use sudo with password
61
+ subprocess .run (
62
+ ['sudo' , '-S' , 'apt' , 'install' , '-y' , package ],
63
+ input = sudo_password .encode (),
64
+ check = True
65
+ )
66
+ print (f"Successfully installed { package } " )
67
+ except subprocess .CalledProcessError as e :
68
+ print (f"Failed to install { package } . Error: { e } " )
69
+ return False
70
+
71
+ return True
72
+
48
73
def get_language (self , language ):
49
74
for lang in self .languages :
50
75
if language .lower () == lang .name .lower () or (
@@ -55,6 +80,14 @@ def get_language(self, language):
55
80
return None
56
81
57
82
def run (self , language , code , stream = False , display = False ):
83
+ # Check if this is an apt install command
84
+ if language == "shell" and code .strip ().startswith ("apt install" ):
85
+ package = code .split ()[- 1 ]
86
+ if self .sudo_install (package ):
87
+ return [{"type" : "console" , "format" : "output" , "content" : f"Package { package } installed successfully." }]
88
+ else :
89
+ return [{"type" : "console" , "format" : "output" , "content" : f"Failed to install package { package } ." }]
90
+
58
91
if language == "python" :
59
92
if (
60
93
self .computer .import_computer_api
0 commit comments