update_manager: refactoring

Code style and naming convention improvements.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-02-06 07:50:08 -05:00
parent c8ad69b902
commit 01977c8775
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
4 changed files with 32 additions and 47 deletions

View File

@ -28,7 +28,6 @@ if TYPE_CHECKING:
from .update_manager import CommandHelper from .update_manager import CommandHelper
from ..machine import Machine from ..machine import Machine
from ..file_manager.file_manager import FileManager from ..file_manager.file_manager import FileManager
from ..shell_command import ShellCommandFactory as ShellCmd
MIN_PIP_VERSION = (23, 0) MIN_PIP_VERSION = (23, 0)
@ -300,9 +299,9 @@ class AppDeploy(BaseDeploy):
self.log_exc("Error updating packages") self.log_exc("Error updating packages")
return return
async def _update_virtualenv(self, async def _update_python_requirements(
requirements: Union[pathlib.Path, List[str]] self, requirements: Union[pathlib.Path, List[str]]
) -> None: ) -> None:
if self.pip_cmd is None: if self.pip_cmd is None:
return return
await self._update_pip() await self._update_pip()
@ -346,9 +345,8 @@ class AppDeploy(BaseDeploy):
if self.pip_cmd is None: if self.pip_cmd is None:
return None return None
self.notify_status("Checking pip version...") self.notify_status("Checking pip version...")
scmd: ShellCmd = self.server.lookup_component("shell_command")
try: try:
data: str = await scmd.exec_cmd( data: str = await self.cmd_helper.run_cmd_with_response(
f"{self.pip_cmd} --version", timeout=30., retries=3 f"{self.pip_cmd} --version", timeout=30., retries=3
) )
match = re.match( match = re.match(

View File

@ -200,7 +200,7 @@ class GitDeploy(AppDeploy):
if packages: if packages:
await self._install_packages(packages) await self._install_packages(packages)
if modules: if modules:
await self._update_virtualenv(modules) await self._update_python_requirements(modules)
npm_hash: Optional[str] = dep_info["npm_hash"] npm_hash: Optional[str] = dep_info["npm_hash"]
ret = await self._check_need_update(npm_hash, self.npm_pkg_json) ret = await self._check_need_update(npm_hash, self.npm_pkg_json)
if force or ret: if force or ret:

View File

@ -507,6 +507,7 @@ class CommandHelper:
shell_cmd: SCMDComp = self.server.lookup_component('shell_command') shell_cmd: SCMDComp = self.server.lookup_component('shell_command')
self.scmd_error = shell_cmd.error self.scmd_error = shell_cmd.error
self.build_shell_command = shell_cmd.build_shell_command self.build_shell_command = shell_cmd.build_shell_command
self.run_cmd_with_response = shell_cmd.exec_cmd
self.pkg_updater: Optional[PackageDeploy] = None self.pkg_updater: Optional[PackageDeploy] = None
# database management # database management
@ -581,15 +582,16 @@ class CommandHelper:
def set_package_updater(self, updater: PackageDeploy) -> None: def set_package_updater(self, updater: PackageDeploy) -> None:
self.pkg_updater = updater self.pkg_updater = updater
async def run_cmd(self, async def run_cmd(
cmd: str, self,
timeout: float = 20., cmd: str,
notify: bool = False, timeout: float = 20.,
retries: int = 1, notify: bool = False,
env: Optional[Dict[str, str]] = None, retries: int = 1,
cwd: Optional[str] = None, env: Optional[Dict[str, str]] = None,
sig_idx: int = 1 cwd: Optional[str] = None,
) -> None: sig_idx: int = 1
) -> None:
cb = self.notify_update_response if notify else None cb = self.notify_update_response if notify else None
scmd = self.build_shell_command(cmd, callback=cb, env=env, cwd=cwd) scmd = self.build_shell_command(cmd, callback=cb, env=env, cwd=cwd)
for _ in range(retries): for _ in range(retries):
@ -598,20 +600,7 @@ class CommandHelper:
else: else:
raise self.server.error("Shell Command Error") raise self.server.error("Shell Command Error")
async def run_cmd_with_response(self, def notify_update_refreshed(self) -> None:
cmd: str,
timeout: float = 20.,
retries: int = 5,
env: Optional[Dict[str, str]] = None,
cwd: Optional[str] = None,
sig_idx: int = 1
) -> str:
scmd = self.build_shell_command(cmd, None, env=env, cwd=cwd)
result = await scmd.run_with_response(timeout, retries,
sig_idx=sig_idx)
return result
def notify_update_refreshed(self):
vinfo: Dict[str, Any] = {} vinfo: Dict[str, Any] = {}
for name, updater in self.get_updaters().items(): for name, updater in self.get_updaters().items():
vinfo[name] = updater.get_update_status() vinfo[name] = updater.get_update_status()
@ -620,10 +609,9 @@ class CommandHelper:
uinfo['busy'] = self.is_update_busy() uinfo['busy'] = self.is_update_busy()
self.server.send_event("update_manager:update_refreshed", uinfo) self.server.send_event("update_manager:update_refreshed", uinfo)
def notify_update_response(self, def notify_update_response(
resp: Union[str, bytes], self, resp: Union[str, bytes], is_complete: bool = False
is_complete: bool = False ) -> None:
) -> None:
if self.cur_update_app is None: if self.cur_update_app is None:
return return
resp = resp.strip() resp = resp.strip()
@ -640,30 +628,29 @@ class CommandHelper:
self.server.send_event( self.server.send_event(
"update_manager:update_response", notification) "update_manager:update_response", notification)
async def install_packages(self, async def install_packages(
package_list: List[str], self, package_list: List[str], **kwargs
**kwargs ) -> None:
) -> None:
if self.pkg_updater is None: if self.pkg_updater is None:
return return
await self.pkg_updater.install_packages(package_list, **kwargs) await self.pkg_updater.install_packages(package_list, **kwargs)
def get_rate_limit_stats(self): def get_rate_limit_stats(self) -> Dict[str, Any]:
return self.http_client.github_api_stats() return self.http_client.github_api_stats()
def on_download_progress(self, def on_download_progress(
progress: int, self, progress: int, download_size: int, downloaded: int
download_size: int, ) -> None:
downloaded: int
) -> None:
totals = ( totals = (
f"{downloaded // 1024} KiB / " f"{downloaded // 1024} KiB / "
f"{download_size// 1024} KiB" f"{download_size // 1024} KiB"
) )
self.notify_update_response( self.notify_update_response(
f"Downloading {self.cur_update_app}: {totals} [{progress}%]") f"Downloading {self.cur_update_app}: {totals} [{progress}%]")
async def create_tempdir(self, suffix=None, prefix=None): async def create_tempdir(
self, suffix: Optional[str] = None, prefix: Optional[str] = None
) -> tempfile.TemporaryDirectory[str]:
def _createdir(sfx, pfx): def _createdir(sfx, pfx):
return tempfile.TemporaryDirectory(suffix=sfx, prefix=pfx) return tempfile.TemporaryDirectory(suffix=sfx, prefix=pfx)

View File

@ -337,7 +337,7 @@ class ZipDeploy(AppDeploy):
await self._install_packages(system_pkgs) await self._install_packages(system_pkgs)
if python_pkgs: if python_pkgs:
if force or python_pkgs != self.python_pkg_list: if force or python_pkgs != self.python_pkg_list:
await self._update_virtualenv(python_pkgs) await self._update_python_requirements(python_pkgs)
ret = await self._check_need_update(npm_hash, self.npm_pkg_json) ret = await self._check_need_update(npm_hash, self.npm_pkg_json)
if force or ret: if force or ret:
if self.npm_pkg_json is not None: if self.npm_pkg_json is not None: