2021-05-14 01:09:15 +02:00
#!/usr/bin/env python3
"""
Upgrade MightyScape from Inkscape Extension Dialog . Made for end users
Extension for InkScape 1. X
Author : Mario Voigt / FabLab Chemnitz
Mail : mario . voigt @stadtfabrikanten.org
Date : 14.05 .2021
Last patch : 14.05 .2021
License : GNU GPL v3
"""
import inkex
import os
2021-05-14 01:33:26 +02:00
import warnings
2021-05-24 21:09:44 +02:00
2021-05-14 01:09:15 +02:00
class Upgrade ( inkex . EffectExtension ) :
def add_arguments ( self , pars ) :
2021-05-17 11:56:17 +02:00
pars . add_argument ( " --tab " )
2021-05-14 01:09:15 +02:00
pars . add_argument ( " --stash_untracked " , type = inkex . Boolean , default = False , help = " Stash untracked files and continue to upgrade " )
def effect ( self ) :
2021-05-14 01:33:26 +02:00
warnings . simplefilter ( ' ignore ' , ResourceWarning ) #suppress "enable tracemalloc to get the object allocation traceback"
2021-05-24 21:09:44 +02:00
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
2021-05-14 01:09:15 +02:00
#get the directory of mightyscape
2021-05-17 11:56:17 +02:00
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/
#create some statistics
totalFolders = 0
for root , folders , files in os . walk ( extension_dir ) :
totalFolders + = len ( folders )
break #prevent descending into subfolders
totalInx = 0
for root , folders , files in os . walk ( extension_dir ) :
for file in files :
if file . endswith ( ' .inx ' ) :
totalInx + = 1
inkex . utils . debug ( " There are {} extension folders with {} .inx files! " . format ( totalFolders , totalInx ) )
2021-05-14 01:09:15 +02:00
2021-05-17 11:56:17 +02:00
repo = Repo ( os . path . join ( main_dir , " .git " ) )
2021-05-14 01:09:15 +02:00
#check if it is a non-empty git repository
if repo . bare is False :
if repo . is_dirty ( untracked_files = True ) is False :
if len ( repo . untracked_files ) > 0 :
if self . options . stash_untracked is True :
repo . git . stash ( ' save ' )
else :
inkex . utils . debug ( " There are some untracked files in your MightyScape directory. Still trying to pull recent files from git... " )
2021-05-21 10:30:49 +02:00
origin = repo . remotes . origin
2021-05-14 01:09:15 +02:00
2021-05-21 10:30:49 +02:00
latestRemoteCommit = git . cmd . Git ( ) . ls_remote ( " https://gitea.fablabchemnitz.de/MarioVoigt/mightyscape-1.X.git " , heads = True ) . replace ( ' refs/heads/master ' , ' ' ) . strip ( )
localCommit = str ( repo . head . commit )
2021-05-27 23:22:12 +02:00
ref_logs = repo . head . reference . log ( )
#commits = list(repo.iter_commits("master", max_count=5))
commits = list ( repo . iter_commits ( " master " ) )
2021-05-24 21:09:44 +02:00
self . msg ( " Local commit id is: " + localCommit [ : 7 ] )
self . msg ( " Latest remote commit is: " + latestRemoteCommit [ : 7 ] )
2021-05-27 23:22:12 +02:00
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 ) ) )
2021-05-24 21:09:44 +02:00
logList = [ ]
2021-05-27 23:22:12 +02:00
for log in ref_logs :
2021-05-24 21:09:44 +02:00
logList . append ( log )
logList . reverse ( )
#show last 10 entries
self . msg ( " * " * 40 )
self . msg ( " Latest 10 log entries are: " )
for i in range ( 0 , 10 ) :
self . msg ( " - {} : {} " . format ( logList [ i ] . newhexsha [ : 7 ] , logList [ i ] . message ) )
self . msg ( " * " * 40 )
2021-05-21 10:30:49 +02:00
if localCommit != latestRemoteCommit :
2021-05-14 01:33:26 +02:00
ssh_executable = ' git '
with repo . git . custom_environment ( GIT_SSH = ssh_executable ) :
origin . fetch ( )
2021-05-21 10:30:49 +02:00
2021-05-14 01:33:26 +02:00
fetch_info = origin . pull ( ) #finally pull new data
2021-05-24 21:09:44 +02:00
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 ] ) )
2021-05-14 12:24:17 +02:00
inkex . utils . debug ( " Please restart Inkscape to let the changes take effect. " )
2021-05-14 01:09:15 +02:00
else :
2021-05-14 01:33:26 +02:00
inkex . utils . debug ( " Nothing to do! MightyScape is already up to date! " )
2021-05-14 01:09:15 +02:00
exit ( 0 )
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. " )
exit ( 1 )
if __name__ == ' __main__ ' :
Upgrade ( ) . run ( )