ver Jan5th

debugged
This commit is contained in:
David Chang
2024-01-05 15:20:47 +08:00
parent 5fedf5b891
commit eeb8a120d6
17 changed files with 158 additions and 86 deletions

View File

@@ -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