2012-04-17 23:03:45 +02:00
|
|
|
import xbmc
|
|
|
|
import xbmcaddon
|
2012-04-19 23:13:50 +02:00
|
|
|
import xbmcgui
|
2012-04-28 21:29:59 +02:00
|
|
|
import resources.lib.vfs as vfs
|
2012-04-19 23:13:50 +02:00
|
|
|
import os
|
2012-04-17 23:03:45 +02:00
|
|
|
|
2012-08-03 22:44:02 +02:00
|
|
|
__addon_id__ = 'script.xbmcbackup'
|
|
|
|
__Addon = xbmcaddon.Addon(__addon_id__)
|
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
class FileManager:
|
2012-04-28 21:29:59 +02:00
|
|
|
walk_path = ''
|
2012-04-20 22:48:18 +02:00
|
|
|
addonDir = ''
|
2012-05-22 04:12:37 +02:00
|
|
|
fileArray = None
|
2012-07-18 17:27:00 +02:00
|
|
|
verbose_log = False
|
|
|
|
|
2012-04-28 21:29:59 +02:00
|
|
|
def __init__(self,path,addon_dir):
|
|
|
|
self.walk_path = path
|
2012-04-20 22:48:18 +02:00
|
|
|
self.addonDir = addon_dir
|
|
|
|
|
|
|
|
#create the addon folder if it doesn't exist
|
|
|
|
if(not os.path.exists(unicode(xbmc.translatePath(self.addonDir),'utf-8'))):
|
|
|
|
os.makedirs(unicode(xbmc.translatePath(self.addonDir),'utf-8'))
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
def createFileList(self,Addon):
|
2012-05-22 04:12:37 +02:00
|
|
|
self.fileArray = []
|
2012-07-18 17:27:00 +02:00
|
|
|
self.verbose_log = Addon.getSetting("verbose_log") == 'true'
|
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
#figure out which syncing options to run
|
|
|
|
if(Addon.getSetting('backup_addons') == 'true'):
|
|
|
|
self.addFile("-addons")
|
2012-04-28 21:29:59 +02:00
|
|
|
self.walkTree(self.walk_path + "addons/")
|
2012-04-20 22:48:18 +02:00
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
self.addFile("-userdata")
|
|
|
|
|
|
|
|
if(Addon.getSetting('backup_addon_data') == 'true'):
|
|
|
|
self.addFile("-userdata/addon_data")
|
2012-04-28 21:29:59 +02:00
|
|
|
self.walkTree(self.walk_path + "userdata/addon_data/")
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
if(Addon.getSetting('backup_database') == 'true'):
|
|
|
|
self.addFile("-userdata/Database")
|
2012-04-28 21:29:59 +02:00
|
|
|
self.walkTree(self.walk_path + "userdata/Database")
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
if(Addon.getSetting("backup_playlists") == 'true'):
|
|
|
|
self.addFile("-userdata/playlists")
|
2012-04-28 21:29:59 +02:00
|
|
|
self.walkTree(self.walk_path + "userdata/playlists")
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
if(Addon.getSetting("backup_thumbnails") == "true"):
|
|
|
|
self.addFile("-userdata/Thumbnails")
|
2012-04-28 21:29:59 +02:00
|
|
|
self.walkTree(self.walk_path + "userdata/Thumbnails")
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
if(Addon.getSetting("backup_config") == "true"):
|
2012-05-25 22:57:48 +02:00
|
|
|
self.addFile("-userdata/keymaps")
|
|
|
|
self.walkTree(self.walk_path + "userdata/keymaps")
|
|
|
|
|
|
|
|
self.addFile("-userdata/peripheral_data")
|
|
|
|
self.walkTree(self.walk_path + "userdata/peripheral_data")
|
|
|
|
|
|
|
|
#this part is an oddity
|
|
|
|
configFiles = vfs.listdir(self.walk_path + "userdata/",extra_metadata=True)
|
2012-04-22 00:18:41 +02:00
|
|
|
for aFile in configFiles:
|
2012-05-25 22:57:48 +02:00
|
|
|
if(aFile['file'].endswith(".xml")):
|
2012-05-25 23:02:23 +02:00
|
|
|
self.addFile(aFile['file'][len(self.walk_path):])
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
def walkTree(self,directory):
|
2012-04-28 21:29:59 +02:00
|
|
|
for (path, dirs, files) in vfs.walk(directory):
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
#create all the subdirs first
|
|
|
|
for aDir in dirs:
|
2012-04-28 21:29:59 +02:00
|
|
|
self.addFile("-" + aDir[len(self.walk_path):])
|
2012-04-22 00:18:41 +02:00
|
|
|
#copy all the files
|
|
|
|
for aFile in files:
|
2012-04-28 21:29:59 +02:00
|
|
|
filePath = aFile[len(self.walk_path):]
|
2012-04-22 00:18:41 +02:00
|
|
|
self.addFile(filePath)
|
|
|
|
|
2012-04-20 22:48:18 +02:00
|
|
|
def addFile(self,filename):
|
|
|
|
#write the full remote path name of this file
|
2012-07-18 17:27:00 +02:00
|
|
|
if(self.verbose_log):
|
2012-08-01 21:50:07 +02:00
|
|
|
xbmc.log("Add File: " + filename.encode("utf-8"))
|
2012-05-22 04:12:37 +02:00
|
|
|
self.fileArray.append(filename)
|
2012-04-22 00:18:41 +02:00
|
|
|
|
2012-05-22 04:12:37 +02:00
|
|
|
def getFileList(self):
|
|
|
|
return self.fileArray
|
2012-04-20 22:48:18 +02:00
|
|
|
|
2012-04-19 23:13:50 +02:00
|
|
|
class XbmcBackup:
|
2012-08-03 22:44:02 +02:00
|
|
|
addon = None
|
2012-04-19 23:13:50 +02:00
|
|
|
local_path = ''
|
|
|
|
remote_path = ''
|
2012-04-20 22:48:18 +02:00
|
|
|
restoreFile = None
|
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
#for the progress bar
|
|
|
|
progressBar = None
|
2012-04-22 00:18:41 +02:00
|
|
|
filesLeft = 0
|
2012-04-25 04:01:12 +02:00
|
|
|
filesTotal = 1
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
fileManager = None
|
2012-04-20 20:32:25 +02:00
|
|
|
|
2012-08-03 22:44:02 +02:00
|
|
|
def __init__(self,__Addon):
|
|
|
|
self.addon = __Addon
|
2012-08-01 21:50:07 +02:00
|
|
|
self.local_path = xbmc.translatePath("special://home").encode("utf-8");
|
2012-04-28 21:46:42 +02:00
|
|
|
|
2012-08-03 22:44:02 +02:00
|
|
|
if(self.addon.getSetting('remote_selection') == '1' and vfs.exists(self.addon.getSetting('remote_path_2'))):
|
|
|
|
self.remote_path = self.addon.getSetting('remote_path_2')
|
|
|
|
self.addon.setSetting("remote_path","")
|
|
|
|
elif(self.addon.getSetting('remote_selection') == '0' and vfs.exists(self.addon.getSetting("remote_path"))):
|
|
|
|
self.remote_path = self.addon.getSetting("remote_path")
|
2012-04-24 04:04:03 +02:00
|
|
|
|
2012-08-03 22:44:02 +02:00
|
|
|
if(self.addon.getSetting("backup_name") != '' and self.remote_path != ''):
|
|
|
|
self.remote_path = self.remote_path + self.addon.getSetting("backup_name") + "/"
|
2012-04-24 04:04:03 +02:00
|
|
|
else:
|
|
|
|
self.remote_path = ""
|
2012-08-01 21:50:07 +02:00
|
|
|
|
|
|
|
self.remote_path = self.remote_path.encode("utf-8");
|
2012-04-20 22:48:18 +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)
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
def run(self):
|
2012-04-25 04:01:12 +02:00
|
|
|
#check if we should use the progress bar
|
2012-08-03 22:44:02 +02:00
|
|
|
if(self.addon.getSetting('run_silent') == 'false'):
|
2012-04-25 04:01:12 +02:00
|
|
|
self.progressBar = xbmcgui.DialogProgress()
|
|
|
|
self.progressBar.create('XBMC Backup','Gathering file list.....')
|
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
#check what mode were are in
|
2012-08-03 22:44:02 +02:00
|
|
|
if(int(self.addon.getSetting('addon_mode')) == 0):
|
|
|
|
self.fileManager = FileManager(self.local_path,self.addon.getAddonInfo('profile'))
|
2012-04-22 00:18:41 +02:00
|
|
|
self.syncFiles()
|
|
|
|
else:
|
2012-08-03 22:44:02 +02:00
|
|
|
self.fileManager = FileManager(self.remote_path,self.addon.getAddonInfo('profile'))
|
2012-04-22 00:18:41 +02:00
|
|
|
self.restoreFiles()
|
2012-04-19 23:13:50 +02:00
|
|
|
|
|
|
|
def syncFiles(self):
|
2012-04-28 21:29:59 +02:00
|
|
|
if(vfs.exists(self.remote_path)):
|
2012-04-19 23:13:50 +02:00
|
|
|
#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-22 00:18:41 +02:00
|
|
|
#make the remote directory
|
2012-04-28 21:29:59 +02:00
|
|
|
vfs.mkdir(self.remote_path)
|
2012-05-28 20:54:08 +02:00
|
|
|
|
|
|
|
self.log("Creating Files List")
|
2012-08-03 22:44:02 +02:00
|
|
|
self.fileManager.createFileList(self.addon)
|
2012-04-22 00:18:41 +02:00
|
|
|
|
2012-05-22 04:12:37 +02:00
|
|
|
allFiles = self.fileManager.getFileList()
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
#write list from local to remote
|
|
|
|
self.writeFiles(allFiles,self.local_path,self.remote_path)
|
|
|
|
|
|
|
|
def restoreFiles(self):
|
2012-08-03 22:44:02 +02:00
|
|
|
self.fileManager.createFileList(self.addon)
|
2012-05-28 20:54:08 +02:00
|
|
|
|
|
|
|
self.log("Creating Files List")
|
2012-05-22 04:12:37 +02:00
|
|
|
allFiles = self.fileManager.getFileList()
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
#write list from remote to local
|
|
|
|
self.writeFiles(allFiles,self.remote_path,self.local_path)
|
2012-05-04 21:54:26 +02:00
|
|
|
|
|
|
|
#call update addons to refresh everything
|
|
|
|
xbmc.executebuiltin('UpdateLocalAddons')
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
def writeFiles(self,fileList,source,dest):
|
2012-05-28 20:54:08 +02:00
|
|
|
self.log("Writing files to: " + dest)
|
2012-04-22 00:18:41 +02:00
|
|
|
self.filesTotal = len(fileList)
|
|
|
|
self.filesLeft = self.filesTotal
|
2012-04-19 23:13:50 +02:00
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
#write each file from source to destination
|
|
|
|
for aFile in fileList:
|
|
|
|
if(not self.checkCancel()):
|
2012-05-28 20:54:08 +02:00
|
|
|
xbmc.log('Writing file: ' + source + aFile, level=xbmc.LOGDEBUG)
|
2012-04-22 00:18:41 +02:00
|
|
|
self.updateProgress(aFile)
|
|
|
|
if (aFile.startswith("-")):
|
2012-04-28 21:29:59 +02:00
|
|
|
vfs.mkdir(dest + aFile[1:])
|
2012-04-22 00:18:41 +02:00
|
|
|
else:
|
2012-04-28 21:29:59 +02:00
|
|
|
vfs.copy(source + aFile,dest + aFile)
|
2012-04-19 23:13:50 +02:00
|
|
|
|
2012-08-03 22:44:02 +02:00
|
|
|
if(self.addon.getSetting('run_silent') == 'false'):
|
2012-04-20 20:32:25 +02:00
|
|
|
self.progressBar.close()
|
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
def updateProgress(self,message=''):
|
|
|
|
self.filesLeft = self.filesLeft - 1
|
2012-04-19 23:13:50 +02:00
|
|
|
|
2012-04-20 20:32:25 +02:00
|
|
|
#update the progress bar
|
|
|
|
if(self.progressBar != None):
|
2012-04-22 22:58:39 +02:00
|
|
|
self.progressBar.update(int((float(self.filesTotal - self.filesLeft)/float(self.filesTotal)) * 100),message)
|
2012-04-20 20:32:25 +02:00
|
|
|
|
|
|
|
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):
|
2012-08-03 22:44:02 +02:00
|
|
|
xbmc.log(self.addon.getLocalizedString(30010) + ": " + message)
|
2012-04-19 23:13:50 +02:00
|
|
|
|
|
|
|
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
|
2012-08-03 22:44:02 +02:00
|
|
|
backup = XbmcBackup(__Addon)
|
2012-04-19 23:13:50 +02:00
|
|
|
|
|
|
|
if(backup.isReady()):
|
2012-04-22 00:18:41 +02:00
|
|
|
backup.run()
|
2012-04-19 23:13:50 +02:00
|
|
|
else:
|
2012-08-03 22:44:02 +02:00
|
|
|
xbmcgui.Dialog().ok(__Addon.getLocalizedString(30010),__Addon.getLocalizedString(30045))
|