2012-04-17 23:03:45 +02:00
|
|
|
import xbmc
|
|
|
|
import xbmcaddon
|
2012-04-19 23:13:50 +02:00
|
|
|
import xbmcgui
|
2012-04-17 23:03:45 +02:00
|
|
|
import xbmcvfs
|
2012-04-19 23:13:50 +02:00
|
|
|
import os
|
2012-04-17 23:03:45 +02:00
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
class XbmcBackup:
|
|
|
|
__addon_id__ = 'script.xbmcbackup'
|
|
|
|
Addon = xbmcaddon.Addon(__addon_id__)
|
|
|
|
local_path = ''
|
|
|
|
remote_path = ''
|
2012-04-17 23:03:45 +02:00
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
#for the progress bar
|
|
|
|
progressBar = None
|
|
|
|
dirsLeft = 0
|
|
|
|
dirsTotal = 0
|
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.local_path = xbmc.translatePath("special://home")
|
2012-04-17 23:03:45 +02:00
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
if(self.Addon.getSetting('remote_path') != '' and self.Addon.getSetting("backup_name") != ''):
|
|
|
|
self.remote_path = self.Addon.getSetting("remote_path") + self.Addon.getSetting('backup_name') + "/"
|
2012-04-17 23:03:45 +02:00
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
self.log("Starting")
|
|
|
|
self.log('Local Dir: ' + self.local_path)
|
|
|
|
self.log('Remote Dir: ' + self.remote_path)
|
|
|
|
|
|
|
|
def syncFiles(self):
|
|
|
|
|
|
|
|
if(xbmcvfs.exists(self.remote_path)):
|
|
|
|
#this will fail - need a disclaimer here
|
|
|
|
self.log("Remote Path exists - may have old files in it!")
|
2012-04-17 23:03:45 +02:00
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
xbmcvfs.mkdir(self.remote_path)
|
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting('run_silent') == 'false'):
|
|
|
|
self.progressBar = xbmcgui.DialogProgress()
|
|
|
|
self.progressBar.create('XBMC Backup','Running......')
|
|
|
|
self.updateProgress(1)
|
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
#figure out which syncing options to run
|
2012-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting('backup_addons') == 'true' and not self.checkCancel()):
|
2012-04-19 23:13:50 +02:00
|
|
|
xbmcvfs.mkdir(self.remote_path + "addons")
|
|
|
|
self.walkTree(self.local_path + "addons/")
|
|
|
|
|
|
|
|
xbmcvfs.mkdir(self.remote_path + "userdata")
|
2012-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting('backup_addon_data') == 'true' and not self.checkCancel()):
|
2012-04-19 23:13:50 +02:00
|
|
|
xbmcvfs.mkdir(self.remote_path + "userdata/addon_data")
|
|
|
|
self.walkTree(self.local_path + "userdata/addon_data/")
|
2012-04-20 01:00:20 +02:00
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting('backup_database') == 'true' and not self.checkCancel()):
|
2012-04-20 01:00:20 +02:00
|
|
|
xbmcvfs.mkdir(self.remote_path + "userdata/Database")
|
|
|
|
self.walkTree(self.local_path + "userdata/Database")
|
2012-04-19 23:13:50 +02:00
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting("backup_playlists") == 'true' and not self.checkCancel()):
|
2012-04-20 01:00:20 +02:00
|
|
|
xbmcvfs.mkdir(self.remote_path + "userdata/playlists")
|
|
|
|
self.walkTree(self.local_path + "userdata/playlists")
|
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting("backup_thumbnails") == "true" and not self.checkCancel()):
|
2012-04-20 01:00:20 +02:00
|
|
|
xbmcvfs.mkdir(self.remote_path + "userdata/Thumbnails")
|
|
|
|
self.walkTree(self.local_path + "userdata/Thumbnails")
|
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting("backup_config") == "true" and not self.checkCancel()):
|
|
|
|
#this one is an oddity
|
2012-04-20 01:00:20 +02:00
|
|
|
configFiles = os.listdir(self.local_path + "userdata/")
|
|
|
|
for aFile in configFiles:
|
|
|
|
if(aFile.endswith(".xml")):
|
|
|
|
self.log("Copying: " + self.local_path + "userdata/" + aFile)
|
|
|
|
xbmcvfs.copy(self.local_path + "userdata/" + aFile,self.remote_path + "userdata/" + aFile)
|
2012-04-20 20:32:25 +02:00
|
|
|
|
|
|
|
if(self.Addon.getSetting('run_silent') == 'false'):
|
|
|
|
self.progressBar.close()
|
|
|
|
|
2012-04-20 01:00:20 +02:00
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
def walkTree(self,directory):
|
|
|
|
for (path, dirs, files) in os.walk(directory):
|
2012-04-20 20:32:25 +02:00
|
|
|
if(not self.checkCancel()):
|
|
|
|
#get the relative part of this path
|
|
|
|
path = path[len(self.local_path):]
|
|
|
|
|
|
|
|
#subtract one from total
|
|
|
|
self.dirsLeft = self.dirsLeft - 1
|
|
|
|
self.updateProgress(len(dirs),"Copying: " + path)
|
|
|
|
|
|
|
|
#create all the subdirs first
|
|
|
|
for aDir in dirs:
|
|
|
|
xbmcvfs.mkdir(self.remote_path + os.sep + path + os.sep + aDir)
|
|
|
|
#copy all the files
|
|
|
|
for aFile in files:
|
|
|
|
filePath = path + os.sep + aFile
|
|
|
|
self.log("copying: " + self.local_path + filePath)
|
|
|
|
xbmcvfs.copy(self.local_path + filePath,self.remote_path + filePath)
|
|
|
|
|
|
|
|
def updateProgress(self,dirCount,message=''):
|
|
|
|
#add to the total
|
|
|
|
self.dirsTotal = self.dirsTotal + dirCount
|
|
|
|
self.dirsLeft = self.dirsLeft + dirCount
|
2012-04-19 23:13:50 +02:00
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
#update the progress bar
|
|
|
|
if(self.progressBar != None):
|
|
|
|
self.progressBar.update((float(self.dirsTotal - self.dirsLeft)/float(self.dirsTotal)) * 100,message)
|
|
|
|
|
|
|
|
def checkCancel(self):
|
|
|
|
result = False
|
|
|
|
|
|
|
|
if(self.progressBar != None):
|
|
|
|
result = self.progressBar.iscanceled()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
def log(self,message):
|
|
|
|
xbmc.log(self.__addon_id__ + ": " + message)
|
|
|
|
|
|
|
|
def isReady(self):
|
|
|
|
return True if self.remote_path != '' else False
|
2012-04-20 20:32:25 +02:00
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
#run the profile backup
|
|
|
|
backup = XbmcBackup()
|
|
|
|
|
|
|
|
if(backup.isReady()):
|
|
|
|
backup.syncFiles()
|
|
|
|
else:
|
|
|
|
xbmcgui.Dialog().ok('XBMC Backup','Error: Remote path cannot be empty')
|