ver Jan5th
debugged
This commit is contained in:
@@ -3,6 +3,8 @@ from typing import Any, Dict
|
||||
import requests
|
||||
from desktop_env.envs.actions import KEYBOARD_KEYS
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger("desktopenv.pycontroller")
|
||||
|
||||
class PythonController:
|
||||
def __init__(self, http_server: str, pkgs_prefix: str = "python -c \"import pyautogui; {command}\""):
|
||||
@@ -17,7 +19,7 @@ class PythonController:
|
||||
if response.status_code == 200:
|
||||
return response.content
|
||||
else:
|
||||
print("Failed to get screenshot. Status code:", response.status_code)
|
||||
logger.error("Failed to get screenshot. Status code: %d", response.status_code)
|
||||
return None
|
||||
|
||||
def get_file(self, file_path: str):
|
||||
@@ -26,10 +28,10 @@ class PythonController:
|
||||
"""
|
||||
response = requests.post(self.http_server + "/file", data={"file_path": file_path})
|
||||
if response.status_code == 200:
|
||||
print("File downloaded successfully")
|
||||
logger.info("File downloaded successfully")
|
||||
return response.content
|
||||
else:
|
||||
print("Failed to get file. Status code:", response.status_code)
|
||||
logger.error("Failed to get file. Status code: %d", response.status_code)
|
||||
return None
|
||||
|
||||
def execute_python_command(self, command: str) -> None:
|
||||
@@ -38,7 +40,7 @@ class PythonController:
|
||||
It can be used to execute the pyautogui commands, or... any other python command. who knows?
|
||||
"""
|
||||
command = self.pkgs_prefix.format(command=command)
|
||||
payload = json.dumps({"command": command})
|
||||
payload = json.dumps({"command": command, "shell": True})
|
||||
headers = {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
@@ -46,12 +48,12 @@ class PythonController:
|
||||
try:
|
||||
response = requests.post(self.http_server + "/execute", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
logger.info("Command executed successfully: %s", response.text)
|
||||
else:
|
||||
print("Failed to execute command. Status code:", response.status_code)
|
||||
logger.error("Failed to execute command. Status code: %d", response.status_code)
|
||||
return response.json()
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to execute the command:", e)
|
||||
logger.error("An error occurred while trying to execute the command: %s", e)
|
||||
|
||||
def execute_action(self, action: Dict[str, Any]):
|
||||
"""
|
||||
|
||||
@@ -8,6 +8,10 @@ import os.path
|
||||
from typing import Dict, List
|
||||
from typing import Any, Union
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger("desktopenv.setup")
|
||||
|
||||
import traceback
|
||||
|
||||
class SetupController:
|
||||
def __init__( self
|
||||
@@ -88,12 +92,12 @@ class SetupController:
|
||||
for chunk in response.iter_content(chunk_size=8192):
|
||||
if chunk:
|
||||
f.write(chunk)
|
||||
print("File downloaded successfully")
|
||||
logger.info("File downloaded successfully")
|
||||
downloaded = True
|
||||
break
|
||||
|
||||
except requests.RequestException as e:
|
||||
print(f"Failed to download {url}. Retrying... ({max_retries - i - 1} attempts left)")
|
||||
logger.error(f"Failed to download {url}. Retrying... ({max_retries - i - 1} attempts left)")
|
||||
if not downloaded:
|
||||
raise requests.RequestException(f"Failed to download {url}. No retries left. Error: {e}")
|
||||
|
||||
@@ -107,17 +111,18 @@ class SetupController:
|
||||
}
|
||||
)
|
||||
headers = {"Content-Type": form.content_type}
|
||||
print(form.content_type)
|
||||
logger.debug(form.content_type)
|
||||
|
||||
# send request to server to upload file
|
||||
try:
|
||||
logger.debug("REQUEST ADDRESS: %s", self.http_server + "/upload")
|
||||
response = requests.post(self.http_server + "/upload", headers=headers, data=form)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
logger.info("Command executed successfully: %s", response.text)
|
||||
else:
|
||||
print("Failed to upload file. Status code:", response.text)
|
||||
logger.error("Failed to upload file. Status code: %s", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
logger.error("An error occurred while trying to send the request: %s", e)
|
||||
|
||||
def _change_wallpaper_setup(self, path: str):
|
||||
# if not config:
|
||||
@@ -138,11 +143,11 @@ class SetupController:
|
||||
try:
|
||||
response = requests.post(self.http_server + "/change_wallpaper", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
logger.info("Command executed successfully: %s", response.text)
|
||||
else:
|
||||
print("Failed to change wallpaper. Status code:", response.text)
|
||||
logger.error("Failed to change wallpaper. Status code: %s", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
logger.error("An error occurred while trying to send the request: %s", e)
|
||||
|
||||
def _tidy_desktop_setup(self, **config):
|
||||
raise NotImplementedError()
|
||||
@@ -165,11 +170,11 @@ class SetupController:
|
||||
try:
|
||||
response = requests.post(self.http_server + "/open_file", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
logger.info("Command executed successfully: %s", response.text)
|
||||
else:
|
||||
print("Failed to open file. Status code:", response.text)
|
||||
logger.error("Failed to open file. Status code: %s", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
logger.error("An error occurred while trying to send the request: %s", e)
|
||||
|
||||
def _launch_setup(self, command: List[str]):
|
||||
if not command:
|
||||
@@ -181,11 +186,11 @@ class SetupController:
|
||||
try:
|
||||
response = requests.post(self.http_server + "/launch", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
print("Command executed successfully:", response.text)
|
||||
logger.info("Command executed successfully: %s", response.text)
|
||||
else:
|
||||
print("Failed to launch application. Status code:", response.text)
|
||||
logger.error("Failed to launch application. Status code: %s", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
logger.error("An error occurred while trying to send the request: %s", e)
|
||||
|
||||
def _execute_setup(self, command: List[str], stdout: str = "", stderr: str = ""):
|
||||
if not command:
|
||||
@@ -195,7 +200,7 @@ class SetupController:
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
try:
|
||||
response = requests.post(self.http_server + "/launch", headers=headers, data=payload)
|
||||
response = requests.post(self.http_server + "/execute", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
results: Dict[str, str] = response.json()
|
||||
if stdout:
|
||||
@@ -204,13 +209,15 @@ class SetupController:
|
||||
if stderr:
|
||||
with open(os.path.join(self.cache_dir, stderr), "w") as f:
|
||||
f.write(results["error"])
|
||||
print( "Command executed successfully: {:} ->".format(" ".join(command))
|
||||
, response.text
|
||||
)
|
||||
logger.info( "Command executed successfully: %s -> %s"
|
||||
, " ".join(command)
|
||||
, response.text
|
||||
)
|
||||
else:
|
||||
print("Failed to launch application. Status code:", response.text)
|
||||
logger.error("Failed to launch application. Status code: %s", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
print("An error occurred while trying to send the request:", e)
|
||||
logger.error("An error occurred while trying to send the request: %s", e)
|
||||
traceback.print_exc()
|
||||
|
||||
def _act_setup(self, action_seq: List[Union[Dict[str, Any], str]]):
|
||||
# TODO
|
||||
|
||||
Reference in New Issue
Block a user