11import json
22import os
33import time
4+ import subprocess
5+ import getpass
46
57from ..utils .recipient_utils import parse_for_recipient
68from .languages .applescript import AppleScript
@@ -45,6 +47,29 @@ def __init__(self, computer):
4547 ]
4648 self ._active_languages = {}
4749
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+
4873 def get_language (self , language ):
4974 for lang in self .languages :
5075 if language .lower () == lang .name .lower () or (
@@ -55,6 +80,14 @@ def get_language(self, language):
5580 return None
5681
5782 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+
5891 if language == "python" :
5992 if (
6093 self .computer .import_computer_api
0 commit comments