Skip to content

Commit

Permalink
githubplugin: [wip] rename old version, disable install on rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Nov 17, 2024
1 parent 5084684 commit f4a24a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
41 changes: 37 additions & 4 deletions githubplugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'])
Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions githubplugin/webif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,18 @@ 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
plugin is None or plugin == ''):
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:
Expand Down
15 changes: 12 additions & 3 deletions githubplugin/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@
<button id='btn-plugin' disabled type="button" class="btn btn-shng btn-sm" onclick="javascript:selectPlugin(this);">Auswählen</button>
</td>
</tr>
<tr>
<td colspan=2>alte Version umbenennen:</td>
<td><input id="rename" type="checkbox" /></td>
</tr>
</table>
</div>
</form>
Expand Down Expand Up @@ -491,15 +495,16 @@
var branch = document.getElementById('branch').value;
var plugin = document.getElementById('plugin').value;
var name = document.getElementById('name').value;
doInstallPlugin(owner, branch, plugin, name);
var rename = document.getElementById('rename').checked;
doInstallPlugin(owner, branch, plugin, name, rename);
}

function doInstallPlugin(owner, branch, plugin, name) {
function doInstallPlugin(owner, branch, plugin, name, rename: false) {
// execute installation
showModal('install', owner, branch, plugin);
if (owner != '' && branch != '' && plugin != '') {
spinModal('install');
sendData("selectPlugin", {'owner': owner, 'branch': branch, 'plugin': plugin, 'name': name, 'confirm': true},
sendData("selectPlugin", {'owner': owner, 'branch': branch, 'plugin': plugin, 'name': name, 'confirm': true, 'rename': rename},
function(response) {
hideModal('install');
},
Expand Down Expand Up @@ -589,6 +594,7 @@
document.getElementById('remain').textContent = remain;
var bo = document.getElementById('backoff')
if (backoff > 0 && remain == 0) {
// backoff active, no remaining actions
var secs = backoff % 60;
var mins = ~~(backoff / 60);
var val = "" + mins + ":" + (secs < 10 ? "0" : "") + parseInt(secs) + "m";
Expand All @@ -600,6 +606,7 @@
rateInterval = setInterval(getRateLimit, 10000);
}

document.getElementById('install').disabled = true;
} else {
bo.style.color = 'black';
bo.textContent = '---';
Expand All @@ -608,6 +615,8 @@
clearInterval(rateInterval);
rateInterval = null;
}

document.getElementById('install').disabled = false;
}
}
);
Expand Down

0 comments on commit f4a24a7

Please sign in to comment.