update_manager: use pathlib to represent file system paths

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2021-07-02 17:13:47 -04:00
parent d3ec2dbae2
commit 88ee83c7d8
1 changed files with 18 additions and 22 deletions

View File

@ -6,6 +6,7 @@
from __future__ import annotations from __future__ import annotations
import os import os
import pathlib
import logging import logging
import json import json
import sys import sys
@ -113,16 +114,15 @@ class UpdateManager:
cfg = config[section] cfg = config[section]
name = section.split()[-1] name = section.split()[-1]
if name in self.updaters: if name in self.updaters:
raise config.error("Client repo named %s already added" raise config.error(f"Client repo {name} already added")
% (name,))
client_type = cfg.get("type") client_type = cfg.get("type")
if client_type == "git_repo": if client_type == "git_repo":
self.updaters[name] = GitDeploy(cfg, self.cmd_helper) self.updaters[name] = GitDeploy(cfg, self.cmd_helper)
elif client_type == "web": elif client_type == "web":
self.updaters[name] = WebClientDeploy(cfg, self.cmd_helper) self.updaters[name] = WebClientDeploy(cfg, self.cmd_helper)
else: else:
raise config.error("Invalid type '%s' for section [%s]" raise config.error(
% (client_type, section)) f"Invalid type '{client_type}' for section [{section}]")
self.cmd_request_lock = Lock() self.cmd_request_lock = Lock()
self.initialized_lock = Event() self.initialized_lock = Event()
@ -630,8 +630,7 @@ class WebClientDeploy(BaseDeploy):
super().__init__(config, cmd_helper) super().__init__(config, cmd_helper)
self.repo = config.get('repo').strip().strip("/") self.repo = config.get('repo').strip().strip("/")
self.owner = self.repo.split("/", 1)[0] self.owner = self.repo.split("/", 1)[0]
self.path: str = os.path.realpath(os.path.expanduser( self.path = pathlib.Path(config.get("path")).expanduser().resolve()
config.get("path")))
self.persistent_files: List[str] = [] self.persistent_files: List[str] = []
pfiles = config.get('persistent_files', None) pfiles = config.get('persistent_files', None)
if pfiles is not None: if pfiles is not None:
@ -653,11 +652,9 @@ class WebClientDeploy(BaseDeploy):
f"\npath: {self.path}") f"\npath: {self.path}")
def _get_local_version(self) -> None: def _get_local_version(self) -> None:
version_path = os.path.join(self.path, ".version") version_path = self.path.joinpath(".version")
if os.path.isfile(os.path.join(self.path, ".version")): if version_path.is_file():
with open(version_path, "r") as f: self.version = version_path.read_text().strip()
v = f.read()
self.version = v.strip()
async def refresh(self) -> None: async def refresh(self) -> None:
if self.refresh_condition is None: if self.refresh_condition is None:
@ -714,14 +711,14 @@ class WebClientDeploy(BaseDeploy):
f"Downloading Client: {self.name}") f"Downloading Client: {self.name}")
archive = await self.cmd_helper.http_download_request(self.dl_url) archive = await self.cmd_helper.http_download_request(self.dl_url)
with tempfile.TemporaryDirectory( with tempfile.TemporaryDirectory(
suffix=self.name, prefix="client") as tempdir: suffix=self.name, prefix="client") as tempdirname:
if os.path.isdir(self.path): tempdir = pathlib.Path(tempdirname)
if self.path.is_dir():
# find and move persistent files # find and move persistent files
for fname in os.listdir(self.path): for fname in os.listdir(self.path):
src_path = os.path.join(self.path, fname) src_path = self.path.joinpath(fname)
if fname in self.persistent_files: if fname in self.persistent_files:
dest_dir = os.path.dirname( dest_dir = tempdir.joinpath(fname).parent
os.path.join(tempdir, fname))
os.makedirs(dest_dir, exist_ok=True) os.makedirs(dest_dir, exist_ok=True)
shutil.move(src_path, dest_dir) shutil.move(src_path, dest_dir)
shutil.rmtree(self.path) shutil.rmtree(self.path)
@ -730,15 +727,14 @@ class WebClientDeploy(BaseDeploy):
zf.extractall(self.path) zf.extractall(self.path)
# Move temporary files back into # Move temporary files back into
for fname in os.listdir(tempdir): for fname in os.listdir(tempdir):
src_path = os.path.join(tempdir, fname) src_path = tempdir.joinpath(fname)
dest_dir = os.path.dirname(os.path.join(self.path, fname)) dest_dir = self.path.joinpath(fname).parent
os.makedirs(dest_dir, exist_ok=True) os.makedirs(dest_dir, exist_ok=True)
shutil.move(src_path, dest_dir) shutil.move(src_path, dest_dir)
self.version = self.remote_version self.version = self.remote_version
version_path = os.path.join(self.path, ".version") version_path = self.path.joinpath(".version")
if not os.path.exists(version_path): if not version_path.exists():
with open(version_path, "w") as f: version_path.write_text(self.version)
f.write(self.version)
self.cmd_helper.notify_update_response( self.cmd_helper.notify_update_response(
f"Client Update Finished: {self.name}", is_complete=True) f"Client Update Finished: {self.name}", is_complete=True)