Merge remote-tracking branch 'origin/main'
# Conflicts: # desktop_env/controllers/setup.py # desktop_env/evaluators/metrics/utils.py
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
import json
|
||||
import logging
|
||||
import os.path
|
||||
import time
|
||||
import os.path
|
||||
import traceback
|
||||
import uuid
|
||||
from typing import Any, Union
|
||||
|
||||
from typing import Dict, List
|
||||
from typing import Any, Union, Optional
|
||||
|
||||
import requests
|
||||
from playwright.sync_api import sync_playwright
|
||||
from requests_toolbelt.multipart.encoder import MultipartEncoder
|
||||
from desktop_env.evaluators.metrics.utils import compare_urls
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger("desktopenv.setup")
|
||||
|
||||
|
||||
@@ -47,6 +48,8 @@ class SetupController:
|
||||
assert hasattr(self, setup_function)
|
||||
getattr(self, setup_function)(**parameters)
|
||||
|
||||
logger.info("SETUP: %s(%s)", setup_function, str(parameters))
|
||||
|
||||
# self._download_setup(config)
|
||||
# self._change_wallpaper(config)
|
||||
# self._tidy_desktop(config) todo: implement this
|
||||
@@ -54,31 +57,31 @@ class SetupController:
|
||||
# can add other setup steps
|
||||
|
||||
# ZDY_COMMENT: merged with launch
|
||||
# def _command_setup(self, command: str):
|
||||
# """
|
||||
# Directly send a command into the virtual machine os for setting up.
|
||||
# """
|
||||
# payload = json.dumps({"command": command})
|
||||
# headers = {
|
||||
# 'Content-Type': 'application/json'
|
||||
# }
|
||||
# timeout = 5
|
||||
# timout_whitelist = ["vlc"]
|
||||
#
|
||||
# try:
|
||||
#
|
||||
# response = requests.post(self.http_server + "/execute", headers=headers, data=payload, timeout=timeout)
|
||||
# if response.status_code == 200:
|
||||
# print("Command executed successfully:", response.text)
|
||||
# else:
|
||||
# print("Failed to execute command. Status code:", response.status_code)
|
||||
# except requests.exceptions.Timeout as e:
|
||||
# if command in timout_whitelist:
|
||||
# print("Command executed successfully:", command)
|
||||
# else:
|
||||
# print("An error occurred while trying to execute the command:", e)
|
||||
# except requests.exceptions.RequestException as e:
|
||||
# print("An error occurred while trying to execute the command:", e)
|
||||
#def _command_setup(self, command: str):
|
||||
#"""
|
||||
#Directly send a command into the virtual machine os for setting up.
|
||||
#"""
|
||||
#payload = json.dumps({"command": command})
|
||||
#headers = {
|
||||
#'Content-Type': 'application/json'
|
||||
#}
|
||||
#timeout = 5
|
||||
#timout_whitelist = ["vlc"]
|
||||
#
|
||||
#try:
|
||||
#
|
||||
#response = requests.post(self.http_server + "/execute", headers=headers, data=payload, timeout=timeout)
|
||||
#if response.status_code == 200:
|
||||
#print("Command executed successfully:", response.text)
|
||||
#else:
|
||||
#print("Failed to execute command. Status code:", response.status_code)
|
||||
#except requests.exceptions.Timeout as e:
|
||||
#if command in timout_whitelist:
|
||||
#print("Command executed successfully:", command)
|
||||
#else:
|
||||
#print("An error occurred while trying to execute the command:", e)
|
||||
#except requests.exceptions.RequestException as e:
|
||||
#print("An error occurred while trying to execute the command:", e)
|
||||
|
||||
def _download_setup(self, files: List[Dict[str, str]]):
|
||||
"""
|
||||
@@ -221,35 +224,60 @@ class SetupController:
|
||||
except requests.exceptions.RequestException as 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 = ""):
|
||||
def _execute_setup( self, command: List[str]
|
||||
, stdout: str = "", stderr: str = ""
|
||||
, shell: bool = False, until: Optional[Dict[str, Any]] = None):
|
||||
if not command:
|
||||
raise Exception("Empty comman to launch.")
|
||||
|
||||
payload = json.dumps({"command": command})
|
||||
until: Dict[str, Any] = until or {}
|
||||
terminates: bool = False
|
||||
nb_failings = 0
|
||||
|
||||
payload = json.dumps({"command": command, "shell": shell})
|
||||
headers = {"Content-Type": "application/json"}
|
||||
|
||||
try:
|
||||
response = requests.post(self.http_server + "/setup" + "/execute", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
results: Dict[str, str] = response.json()
|
||||
if stdout:
|
||||
with open(os.path.join(self.cache_dir, stdout), "w") as f:
|
||||
f.write(results["output"])
|
||||
if stderr:
|
||||
with open(os.path.join(self.cache_dir, stderr), "w") as f:
|
||||
f.write(results["error"])
|
||||
logger.info("Command executed successfully: %s -> %s"
|
||||
, " ".join(command)
|
||||
, response.text
|
||||
)
|
||||
else:
|
||||
logger.error("Failed to launch application. Status code: %s", response.text)
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error("An error occurred while trying to send the request: %s", e)
|
||||
traceback.print_exc()
|
||||
while not terminates:
|
||||
try:
|
||||
response = requests.post(self.http_server_setup_root + "/execute", headers=headers, data=payload)
|
||||
if response.status_code == 200:
|
||||
results: Dict[str, str] = response.json()
|
||||
if stdout:
|
||||
with open(os.path.join(self.cache_dir, stdout), "w") as f:
|
||||
f.write(results["output"])
|
||||
if stderr:
|
||||
with open(os.path.join(self.cache_dir, stderr), "w") as f:
|
||||
f.write(results["error"])
|
||||
logger.info( "Command executed successfully: %s -> %s"
|
||||
, " ".join(command)
|
||||
, response.text
|
||||
)
|
||||
else:
|
||||
logger.error("Failed to launch application. Status code: %s", response.text)
|
||||
results = None
|
||||
nb_failings += 1
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error("An error occurred while trying to send the request: %s", e)
|
||||
traceback.print_exc()
|
||||
|
||||
def _command_setup(self, command: List[str], stdout: str = "", stderr: str = ""):
|
||||
self._execute_setup(command, stdout, stderr)
|
||||
results = None
|
||||
nb_failings += 1
|
||||
|
||||
if len(until)==0:
|
||||
terminates = True
|
||||
elif results is not None:
|
||||
terminates = "returncode" in until and results["returncode"]==until["returncode"]\
|
||||
or "stdout" in until and until["stdout"] in results["output"]\
|
||||
or "stderr" in until and until["stderr"] in results["error"]
|
||||
terminates = terminates or nb_failings>=5
|
||||
if not terminates:
|
||||
time.sleep(0.3)
|
||||
|
||||
def _command_setup(self, command: List[str], **kwargs):
|
||||
self._execute_setup(command, **kwargs)
|
||||
|
||||
def _sleep_setup(self, seconds: float):
|
||||
time.sleep(seconds)
|
||||
|
||||
def _act_setup(self, action_seq: List[Union[Dict[str, Any], str]]):
|
||||
# TODO
|
||||
|
||||
Reference in New Issue
Block a user