From f4a24a7df0475a4ae005de147f7e93f58782bac6 Mon Sep 17 00:00:00 2001 From: Morg42 <43153739+Morg42@users.noreply.github.com> Date: Sun, 17 Nov 2024 22:24:17 +0100 Subject: [PATCH] githubplugin: [wip] rename old version, disable install on rate limit --- githubplugin/__init__.py | 41 ++++++++++++++++++++++--- githubplugin/webif/__init__.py | 5 +-- githubplugin/webif/templates/index.html | 15 +++++++-- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/githubplugin/__init__.py b/githubplugin/__init__.py index d7b317e31..67ae2adbc 100644 --- a/githubplugin/__init__.py +++ b/githubplugin/__init__.py @@ -30,6 +30,7 @@ from pathlib import Path from lib.model.smartplugin import SmartPlugin +from lib.shyaml import yaml_load from .webif import WebInterface from .gperror import GPError @@ -391,19 +392,19 @@ def read_repos_from_dir(self, exc=False): } self.repos[name]['clean'] = self.is_repo_clean(name, exc) - def check_for_repo_name(self, name) -> bool: + def check_for_repo_name(self, name, rename=False) -> bool: """ check if name exists in repos or link exists """ - if name in self.repos or os.path.exists(os.path.join(self.plg_path, 'priv_' + name)): + if name in self.repos or (not rename and os.path.exists(os.path.join(self.plg_path, 'priv_' + name))): self.loggerr(f'name {name} already taken, delete old plugin first or choose a different name.') return False return True - def create_repo(self, name, owner, plugin, branch=None) -> bool: + def create_repo(self, name, owner, plugin, branch=None, rename=False) -> bool: """ create repo from given parameters """ try: - self.check_for_repo_name(name) + self.check_for_repo_name(name, rename=rename) except Exception as e: self.loggerr(e) return False @@ -510,6 +511,11 @@ def create_repo(self, name, owner, plugin, branch=None) -> bool: repo['clean'] = True + if rename: + self.logger.debug(f'renaming old link priv_{name}') + if not self._move_old_link(name, repo): + self.logger.warning(f'unable to move old link priv_{name}, installation needs to be repaired manually') + self.logger.debug(f'creating link {repo["link"]} to {repo["rel_link_path"]}...') try: os.symlink(repo['rel_link_path'], repo['link']) @@ -520,6 +526,33 @@ def create_repo(self, name, owner, plugin, branch=None) -> bool: return True + def _move_old_link(self, name) -> bool: + """ rename old plugin link or folder """ + link = os.path.join(self.plg_path, f'priv_{name}') + if not os.path.exists(link): + self.logger.debug(f'old link/folder not found: {link}') + return True + + self.logger.debug(f'found old link/folder {link}') + + # try plugin.yaml + plgyml = os.path.join(link, 'plugin.yaml') + if not os.path.exists(plgyml): + self.logger.debug(f'plugin.yaml not found for {link}, aborting') + return False + + try: + yaml = yaml_load(plgyml, ignore_notfound=True) + ver = yaml['plugin']['version'] + self.logger.debug(f'found old version {ver}') + newlink = f'{link}_{ver}' + os.rename(link, newlink) + self.logger.debug(f'renamed {link} to {newlink}') + return True + except Exception as e: + self.loggerr(f'error renaming old plugin: {e}') + return False + def remove_plugin(self, name) -> bool: """ remove plugin link, worktree and if not longer needed, local repo """ if name not in self.repos: diff --git a/githubplugin/webif/__init__.py b/githubplugin/webif/__init__.py index d7235335d..bf7d2a346 100644 --- a/githubplugin/webif/__init__.py +++ b/githubplugin/webif/__init__.py @@ -315,6 +315,7 @@ def selectPlugin(self): plugin = json.get("plugin") name = json.get("name") confirm = json.get("confirm") + rename = json.get("rename") if (owner is None or owner == '' or branch is None or branch == '' or @@ -322,10 +323,10 @@ def selectPlugin(self): raise Exception(f'Fehlerhafte Daten für Repo {owner}/plugins, Branch {branch} oder Plugin {plugin} übergeben.') if confirm: - res = self.plugin.create_repo(name, owner, plugin, branch) + res = self.plugin.create_repo(name, owner, plugin, branch, rename=rename) msg = f'Fehler beim Erstellen des Repos "{owner}/plugins", Branch {branch}, Plugin {plugin}' else: - res = self.plugin.check_for_repo_name(name) + res = self.plugin.check_for_repo_name(name, rename=rename) msg = f'Repo {name} oder Plugin-Link "priv_{name}" schon vorhanden' if res: diff --git a/githubplugin/webif/templates/index.html b/githubplugin/webif/templates/index.html index babcc1d6c..b2073fe23 100644 --- a/githubplugin/webif/templates/index.html +++ b/githubplugin/webif/templates/index.html @@ -151,6 +151,10 @@ +