From f488e985abd82c70fac696090e101abc67d8d1b2 Mon Sep 17 00:00:00 2001 From: Arksine Date: Sun, 29 Nov 2020 06:52:08 -0500 Subject: [PATCH] update_manager: implement ability to rebuild the python env This implementation adds a delay that allows for pip to function correclty after the venv is built. Signed-off-by: Eric Callahan --- moonraker/plugins/update_manager.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/moonraker/plugins/update_manager.py b/moonraker/plugins/update_manager.py index 5f6d4ba..edb9da7 100644 --- a/moonraker/plugins/update_manager.py +++ b/moonraker/plugins/update_manager.py @@ -123,11 +123,16 @@ class UpdateManager: 'version_info': vinfo, 'busy': self.current_update is not None} - async def execute_cmd(self, cmd, timeout=10., notify=False): + async def execute_cmd(self, cmd, timeout=10., notify=False, retries=1): shell_command = self.server.lookup_plugin('shell_command') cb = self.notify_update_response if notify else None scmd = shell_command.build_shell_command(cmd, callback=cb) - await scmd.run(timeout=timeout, verbose=notify) + while retries: + if await scmd.run(timeout=timeout, verbose=notify): + break + retries -= 1 + if not retries: + raise self.server.error("Shell Command Error") async def execute_cmd_with_response(self, cmd, timeout=10.): shell_command = self.server.lookup_plugin('shell_command') @@ -342,6 +347,21 @@ class GitUpdater: async def _update_virtualenv(self, rebuild_env=False): # Update python dependencies bin_dir = os.path.dirname(self.env) + if rebuild_env: + env_path = os.path.normpath(os.path.join(bin_dir, "..")) + env_args = REPO_DATA[self.name]['venv_args'] + self._notify_status(f"Creating virtualenv at: {env_path}...") + if os.path.exists(env_path): + shutil.rmtree(env_path) + try: + await self.execute_cmd( + f"virtualenv {env_args} {env_path}") + except Exception: + self._log_exc(f"Error creating virtualenv") + return + await tornado.gen.sleep(.5) + if not os.path.expanduser(self.env): + raise self._log_exc("Failed to create new virtualenv", False) reqs = os.path.join( self.repo_path, REPO_DATA[self.name]['requirements']) if not os.path.isfile(reqs): @@ -351,7 +371,8 @@ class GitUpdater: self._notify_status("Updating python packages...") try: await self.execute_cmd( - f"{pip} install -r {reqs}", timeout=1200., notify=True) + f"{pip} install -r {reqs}", timeout=1200., notify=True, + retries=3) except Exception: self._log_exc("Error updating python requirements")