reworked updater to use a fallback remote

This commit is contained in:
Mario Voigt 2021-06-11 19:02:11 +02:00
parent c96b05d0bb
commit 2045bc400c

View File

@ -15,23 +15,70 @@ import inkex
import os import os
import warnings import warnings
from datetime import datetime from datetime import datetime
try:
import git
from git import Repo #requires GitPython lib
except:
inkex.utils.debug("Error. GitPython was not installed but is required to run the upgrade process!")
exit(1)
class AboutUpgradeMightyScape(inkex.EffectExtension): class AboutUpgradeMightyScape(inkex.EffectExtension):
def update(self, local_repo, remote):
try:
latestRemoteCommit = git.cmd.Git().ls_remote(remote, heads=True).replace('refs/heads/master','').strip()
localCommit = str(local_repo.head.commit)
#ref_logs = repo.head.reference.log()
#commits = list(local_repo.iter_commits("master", max_count=5))
commits = list(local_repo.iter_commits("master"))
self.msg("Local commit id is: " + localCommit[:7])
self.msg("Latest remote commit is: " + latestRemoteCommit[:7])
self.msg("There are {} remote commits at the moment.".format(len(commits)))
#self.msg("There are {} remote ref logs at the moment.".format(len(ref_logs)))
commitList = []
for commit in commits:
commitList.append(commit)
#commitList.reverse()
#show last 10 entries
self.msg("*"*40)
self.msg("Latest 10 commits are:")
for i in range(0, 10):
self.msg("{} | {} : {}".format(
datetime.utcfromtimestamp(commitList[i].committed_date).strftime('%Y-%m-%d %H:%M:%S'),
commitList[i].name_rev[:7],
commitList[i].message.strip())
)
#self.msg(" - {}: {}".format(commitList[i].newhexsha[:7], commitList[i].message))
self.msg("*"*40)
if localCommit != latestRemoteCommit:
ssh_executable = 'git'
with repo.git.custom_environment(GIT_SSH=ssh_executable):
origin.fetch()
fetch_info = origin.pull() #finally pull new data
for info in fetch_info: #should return only one line in total
inkex.utils.debug("Updated %s to commit id %s" % (info.ref, str(info.commit)[:7]))
inkex.utils.debug("Please restart Inkscape to let the changes take effect.")
else:
inkex.utils.debug("Nothing to do! MightyScape is already up to date!")
except git.exc.GitCommandError:
return False
return True
def add_arguments(self, pars): def add_arguments(self, pars):
pars.add_argument("--tab") pars.add_argument("--tab")
pars.add_argument("--stash_untracked", type=inkex.Boolean, default=False, help="Stash untracked files and continue to upgrade") pars.add_argument("--stash_untracked", type=inkex.Boolean, default=False, help="Stash untracked files and continue to upgrade")
def effect(self): def effect(self):
warnings.simplefilter('ignore', ResourceWarning) #suppress "enable tracemalloc to get the object allocation traceback" warnings.simplefilter('ignore', ResourceWarning) #suppress "enable tracemalloc to get the object allocation traceback"
try:
import git
from git import Repo #requires GitPython lib
except:
inkex.utils.debug("Error. GitPython was not installed but is required to run the upgrade process!")
return
#get the directory of mightyscape #get the directory of mightyscape
extension_dir = os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../')) #go up to main dir /home/<user>/.config/inkscape/extensions/mightyscape-1.X/ extension_dir = os.path.abspath(os.path.join(os.path.abspath(os.path.dirname(__file__)), '../')) #go up to main dir /home/<user>/.config/inkscape/extensions/mightyscape-1.X/
main_dir = os.path.abspath(os.path.join(extension_dir, '../../')) #go up to main dir /home/<user>/.config/inkscape/extensions/mightyscape-1.X/ main_dir = os.path.abspath(os.path.join(extension_dir, '../../')) #go up to main dir /home/<user>/.config/inkscape/extensions/mightyscape-1.X/
@ -48,66 +95,32 @@ class AboutUpgradeMightyScape(inkex.EffectExtension):
if file.endswith('.inx'): if file.endswith('.inx'):
totalInx += 1 totalInx += 1
inkex.utils.debug("Locally there are {} extension folders with {} .inx files!".format(totalFolders, totalInx)) inkex.utils.debug("Locally there are {} extension folders with {} .inx files!\n".format(totalFolders, totalInx))
repo = Repo(os.path.join(main_dir, ".git")) local_repo = Repo(os.path.join(main_dir, ".git"))
#check if it is a non-empty git repository #check if it is a non-empty git repository
if repo.bare is False: if local_repo.bare is False:
if repo.is_dirty(untracked_files=True) is False: if local_repo.is_dirty(untracked_files=True) is False:
if len(repo.untracked_files) > 0: if len(local_repo.untracked_files) > 0:
if self.options.stash_untracked is True: if self.options.stash_untracked is True:
repo.git.stash('save') local_repo.git.stash('save')
else: else:
inkex.utils.debug("There are some untracked files in your MightyScape directory. Still trying to pull recent files from git...") inkex.utils.debug("There are some untracked files in your MightyScape directory. Still trying to pull recent files from git...")
origin = repo.remotes.origin origin = local_repo.remotes.origin
try: remotes = []
latestRemoteCommit = git.cmd.Git().ls_remote("https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X.git", heads=True).replace('refs/heads/master','').strip() remotes.append("https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X.git") #main
localCommit = str(repo.head.commit) remotes.append("https://github.com/vmario89/mightyscape-1.X.git") #copy/second remote
#ref_logs = repo.head.reference.log()
#commits = list(repo.iter_commits("master", max_count=5))
commits = list(repo.iter_commits("master"))
self.msg("Local commit id is: " + localCommit[:7])
self.msg("Latest remote commit is: " + latestRemoteCommit[:7])
self.msg("There are {} remote commits at the moment.".format(len(commits)))
#self.msg("There are {} remote ref logs at the moment.".format(len(ref_logs)))
commitList = []
for commit in commits:
commitList.append(commit)
#commitList.reverse()
#show last 10 entries
self.msg("*"*40)
self.msg("Latest 10 commits are:")
for i in range(0, 10):
self.msg("{} | {} : {}".format(
datetime.utcfromtimestamp(commitList[i].committed_date).strftime('%Y-%m-%d %H:%M:%S'),
commitList[i].name_rev[:7],
commitList[i].message.strip())
)
#self.msg(" - {}: {}".format(commitList[i].newhexsha[:7], commitList[i].message))
self.msg("*"*40)
if localCommit != latestRemoteCommit:
ssh_executable = 'git'
with repo.git.custom_environment(GIT_SSH=ssh_executable):
origin.fetch()
fetch_info = origin.pull() #finally pull new data
for info in fetch_info: #should return only one line in total
inkex.utils.debug("Updated %s to commit id %s" % (info.ref, str(info.commit)[:7]))
inkex.utils.debug("Please restart Inkscape to let the changes take effect.")
else:
inkex.utils.debug("Nothing to do! MightyScape is already up to date!")
exit(0)
except git.exc.GitCommandError:
self.msg("Error receiving latest remote commit from git. Are you offline? Cannot continue!")
return
#finally run the update
success = self.update(local_repo, remotes[0])
if success is False: #try the second remote if first failed
self.msg("Error receiving latest remote commit from main git remote {}. Trying second remote ...".format(remotes[0]))
success = self.update(local_repo, remotes[1])
if success is False: #if still false:
self.msg("Error receiving latest remote commit from second git remote {}.\nAre you offline? Cannot continue!".format(remotes[0]))
exit(1)
else: else:
inkex.utils.debug("No \".git\" directory found. Seems your MightyScape was not installed with git clone. Please see documentation on how to do that.") inkex.utils.debug("No \".git\" directory found. Seems your MightyScape was not installed with git clone. Please see documentation on how to do that.")
exit(1) exit(1)