2021-07-23 02:36:56 +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
2021-11-05 01:31:41 +01:00
Last patch : 05.11 .2021
2021-07-23 02:36:56 +02:00
License : GNU GPL v3
ToDo
2021-10-28 16:00:52 +02:00
- add routine to check for differences between remotes ( unequal commits )
2021-07-23 02:36:56 +02:00
"""
import inkex
import os
import warnings
from datetime import datetime
2021-11-05 02:08:46 +01:00
2021-07-23 02:36:56 +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! " )
exit ( 1 )
class AboutUpgradeMightyScape ( inkex . EffectExtension ) :
2021-10-28 16:00:52 +02:00
def update ( self , local_repo , remote , localCommitCount ) :
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " Chosen remote is: {} " . format ( remote ) )
2021-07-23 02:36:56 +02:00
try :
localCommit = local_repo . head . commit
2021-10-28 16:00:52 +02:00
remote_repo = git . remote . Remote ( local_repo , remote )
2021-07-23 02:36:56 +02:00
remoteCommit = remote_repo . fetch ( ) [ 0 ] . commit
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " Latest remote commit is: " + str ( remoteCommit ) [ : 7 ] )
2021-10-28 16:00:52 +02:00
remoteCommitCount = 0
for c in remote_repo . repo . iter_commits ( ' origin/master ' ) :
remoteCommitCount + = 1
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " Commits at remote: {} " . format ( remoteCommitCount ) )
2021-10-28 16:00:52 +02:00
2021-07-23 02:36:56 +02:00
if localCommit . hexsha != remoteCommit . hexsha :
ssh_executable = ' git '
with local_repo . git . custom_environment ( GIT_SSH = ssh_executable ) :
2021-10-28 16:00:52 +02:00
#origin = local_repo.remotes.origin
#origin.fetch()
#fetch_info = origin.pull() #finally pull new data
fetch_info = remote_repo . fetch ( )
remote_repo . pull ( ) #finally pull new data
2021-07-23 02:36:56 +02:00
for info in fetch_info : #should return only one line in total
2021-10-28 16:00:52 +02:00
inkex . utils . debug ( " Updated {} to commit id {} . {} commits were pulled " . format ( info . ref , str ( info . commit ) [ : 7 ] , remoteCommitCount - localCommitCount ) )
2021-07-23 02:36:56 +02:00
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 as e :
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " Error: " )
inkex . utils . debug ( e )
2021-07-23 02:36:56 +02:00
return False
return True
def add_arguments ( self , pars ) :
pars . add_argument ( " --tab " )
pars . add_argument ( " --convert_to_git " , type = inkex . Boolean , default = False , help = " If you downloaded MightyScape as .zip or .tar.gz you cannot upgrade using this extension. But you can convert your downloaded directory to a .git one by enabling this option " )
2021-11-05 01:31:41 +01:00
pars . add_argument ( " --recreate_remotes " , type = inkex . Boolean , default = False , help = " Update remotes in git config file (useful if you have an older version of MightyScape or if sth. changes) " )
2021-07-23 02:36:56 +02:00
pars . add_argument ( " --stash_untracked " , type = inkex . Boolean , default = False , help = " Stash untracked files and continue to upgrade " )
2021-11-05 01:31:41 +01:00
2021-07-23 02:36:56 +02:00
def effect ( self ) :
2021-11-05 01:31:41 +01:00
global so
so = self . options
2021-07-23 02:36:56 +02:00
warnings . simplefilter ( ' ignore ' , ResourceWarning ) #suppress "enable tracemalloc to get the object allocation traceback"
#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/
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 ( " Locally there are {} extension folders with {} .inx files! \n " . format ( totalFolders , totalInx ) )
remotes = [ ]
remotes . append ( [ " https://gitea.fablabchemnitz.de/FabLab_Chemnitz/mightyscape-1.X.git " , " origin " ] ) #main
2022-01-06 16:44:03 +01:00
remotes . append ( [ " https://github.com/eridur-de/mightyscape-1.X.git " , " github " ] ) #copy/second remote
2021-07-23 02:36:56 +02:00
gitDir = os . path . join ( main_dir , " .git " )
2021-11-05 02:18:36 +01:00
2021-07-23 02:36:56 +02:00
if not os . path . exists ( gitDir ) :
2021-11-05 01:31:41 +01:00
if so . convert_to_git is True :
2021-07-23 02:36:56 +02:00
local_repo = Repo . init ( main_dir )
local_repo . git . add ( all = True )
localRemotes = [ ]
for remote in remotes :
localRemotes . append ( local_repo . create_remote ( remote [ 1 ] , url = remote [ 0 ] ) )
2021-11-05 01:31:41 +01:00
localRemotes [ 0 ] . update ( )
2021-07-23 02:36:56 +02:00
local_repo . index . commit ( ' . ' )
local_repo . git . checkout ( ' origin/master ' )
else :
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " MightyScape .git directory was not found. It seems you installed MightyScape the traditional way (by downloading and extracting from archive). Please install MightyScape using the git clone method if you want to use the upgrade function. More details can be found in the official README. " )
2021-07-23 02:36:56 +02:00
exit ( 1 )
local_repo = Repo ( gitDir )
2021-11-05 02:18:36 +01:00
#drop local changed. update might fail if file changes are present
if so . stash_untracked is True :
local_repo . git . stash ( ' save ' )
2021-11-05 01:31:41 +01:00
existingRemotes = [ ] #check for existing remotes. if one is missing, add it (or delete and recreate)
for r in local_repo . remotes :
existingRemotes . append ( str ( r ) )
for remote in remotes :
if remote [ 1 ] not in existingRemotes :
local_repo . create_remote ( remote [ 1 ] , url = remote [ 0 ] )
if so . recreate_remotes is True : #delete and then recreate
local_repo . delete_remote ( remote [ 1 ] )
local_repo . create_remote ( remote [ 1 ] , url = remote [ 0 ] )
2021-07-23 02:36:56 +02:00
#check if it is a non-empty git repository
if local_repo . bare is False :
if local_repo . is_dirty ( untracked_files = True ) is False :
if len ( local_repo . untracked_files ) > 0 :
2021-11-05 01:31:41 +01:00
if so . stash_untracked is True :
2021-07-23 02:36:56 +02:00
local_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... " )
localLatestCommit = local_repo . head . commit
2021-10-28 16:00:52 +02:00
localCommits = list ( local_repo . iter_commits ( " origin/master " , skip = 0 ) )
localCommitCount = len ( localCommits )
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " Local commit id is: " + str ( localLatestCommit ) [ : 7 ] )
inkex . utils . debug ( " There are {} local commits at the moment. " . format ( localCommitCount ) )
2021-10-28 16:00:52 +02:00
localCommits = localCommits [ : 10 ] #get only last ten commits
2021-07-23 02:36:56 +02:00
localCommitList = [ ]
for localCommit in localCommits :
localCommitList . append ( localCommit )
#localCommitList.reverse()
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " * " * 40 )
inkex . utils . debug ( " Latest {} local commits are: " . format ( len ( localCommits ) ) )
2021-07-23 02:36:56 +02:00
for i in range ( 0 , len ( localCommits ) ) :
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " {} | {} : {} " . format (
2021-07-23 02:36:56 +02:00
datetime . utcfromtimestamp ( localCommitList [ i ] . committed_date ) . strftime ( ' % Y- % m- %d % H: % M: % S ' ) ,
localCommitList [ i ] . name_rev [ : 7 ] ,
localCommitList [ i ] . message . strip ( ) )
)
2021-11-05 01:31:41 +01:00
#inkex.utils.debug(" - {}: {}".format(localCommitList[i].newhexsha[:7], localCommitList[i].message))
inkex . utils . debug ( " * " * 40 )
2021-07-23 02:36:56 +02:00
#finally run the update
2021-10-28 16:00:52 +02:00
success = self . update ( local_repo , remotes [ 0 ] [ 1 ] , localCommitCount )
2021-07-23 02:36:56 +02:00
if success is False : #try the second remote if first failed
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " Error receiving latest remote commit from main git remote {} . Trying second remote ... " . format ( remotes [ 0 ] [ 0 ] ) )
2021-10-28 16:00:52 +02:00
success = self . update ( local_repo , remotes [ 1 ] [ 1 ] , localCommitCount )
2021-07-23 02:36:56 +02:00
if success is False : #if still false:
2021-11-05 01:31:41 +01:00
inkex . utils . debug ( " Error receiving latest remote commit from second git remote {} . \n Are you offline? Cannot continue! " . format ( remotes [ 0 ] [ 0 ] ) )
2021-07-23 02:36:56 +02:00
exit ( 1 )
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__ ' :
AboutUpgradeMightyScape ( ) . run ( )