Update dependencies and refactor DesktopEnv initialization

- Removed specific versioning for the 'requests' library in requirements.txt and setup.py to allow for more flexible updates.
- Refactored the DesktopEnv class to streamline the emulator initialization process, enhancing error handling and logging during startup.
- Improved retry logic for file uploads in SetupController, ensuring robust handling of network issues and providing clearer error messages.
- Maintained existing code logic while enhancing clarity and reliability in the DesktopEnv and SetupController classes.
This commit is contained in:
Timothyxxx
2025-08-21 09:38:28 +00:00
parent 492c910e94
commit ead564c92b
4 changed files with 96 additions and 62 deletions

View File

@@ -199,26 +199,62 @@ class SetupController:
path: str = f["path"]
if not os.path.exists(local_path):
logger.error(f"Setup Upload - Invalid local path ({local_path}).")
return
raise Exception(f"Setup Upload - Invalid local path ({local_path}).")
form = MultipartEncoder({
"file_path": path,
"file_data": (os.path.basename(path), open(local_path, "rb"))
})
headers = {"Content-Type": form.content_type}
logger.debug(form.content_type)
# send request to server to upload file
file_size = None
try:
logger.debug("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/upload")
response = requests.post(self.http_server + "/setup" + "/upload", headers=headers, data=form)
if response.status_code == 200:
logger.info("Command executed successfully: %s", response.text)
else:
logger.error("Failed to upload file. Status code: %s", response.text)
except requests.exceptions.RequestException as e:
logger.error("An error occurred while trying to send the request: %s", e)
file_size = os.path.getsize(local_path)
except Exception:
pass
max_retries = 3
last_error: Optional[Exception] = None
for attempt in range(max_retries):
try:
logger.info(
f"Uploading {os.path.basename(local_path)}{f' ({file_size} bytes)' if file_size is not None else ''} "
f"to VM at {path} (attempt {attempt + 1}/{max_retries})"
)
logger.debug("REQUEST ADDRESS: %s", self.http_server + "/setup" + "/upload")
# Open the file inside each attempt to ensure fresh stream position
with open(local_path, "rb") as fp:
form = MultipartEncoder({
"file_path": path,
"file_data": (os.path.basename(path), fp)
})
headers = {"Content-Type": form.content_type}
logger.debug(form.content_type)
# Explicit connect/read timeout to avoid hanging forever
response = requests.post(
self.http_server + "/setup" + "/upload",
headers=headers,
data=form,
timeout=(10, 600)
)
if response.status_code == 200:
logger.info(f"File uploaded successfully: {path}")
logger.debug("Upload response: %s", response.text)
last_error = None
break
else:
msg = f"Failed to upload file {path}. Status code: {response.status_code}, Response: {response.text}"
logger.error(msg)
last_error = requests.RequestException(msg)
except requests.exceptions.RequestException as e:
last_error = e
logger.error(f"Upload attempt {attempt + 1} failed for {path}: {e}")
# Exponential backoff between retries
if attempt < max_retries - 1:
time.sleep(2 ** attempt)
if last_error is not None:
raise last_error
def _change_wallpaper_setup(self, path: str):
if not path: