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-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 = ''
|
|
|
|
fHandle = None
|
2012-04-22 00:18:41 +02:00
|
|
|
|
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-04-20 22:48:18 +02:00
|
|
|
self.fHandle = open(unicode(xbmc.translatePath(self.addonDir + "restore.txt"),'utf-8'),"w")
|
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"):
|
|
|
|
#this one is an oddity
|
2012-04-28 21:29:59 +02:00
|
|
|
configFiles = os.listdir(self.walk_path + "userdata/")
|
2012-04-22 00:18:41 +02:00
|
|
|
for aFile in configFiles:
|
|
|
|
if(aFile.endswith(".xml")):
|
|
|
|
self.addFile("userdata/" + aFile)
|
|
|
|
|
|
|
|
if(self.fHandle != None):
|
|
|
|
self.fHandle.close()
|
|
|
|
|
|
|
|
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
|
|
|
|
if(self.fHandle != None):
|
|
|
|
self.fHandle.write(str(filename) + "\n")
|
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
def readFileList(self):
|
|
|
|
allFiles = open(unicode(xbmc.translatePath(self.addonDir + "restore.txt"),'utf-8'),"r").read().splitlines()
|
|
|
|
|
|
|
|
return allFiles
|
2012-04-20 22:48:18 +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-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-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-28 21:29:59 +02:00
|
|
|
if(self.Addon.getSetting('remote_path_2') != '' and vfs.exists(self.Addon.getSetting('remote_path_2'))):
|
2012-04-24 04:04:03 +02:00
|
|
|
self.remote_path = self.Addon.getSetting('remote_path_2')
|
|
|
|
self.Addon.setSetting("remote_path","")
|
2012-04-28 21:29:59 +02:00
|
|
|
elif(self.Addon.getSetting('remote_path') != '' and vfs.exists(self.Addon.getSetting("remote_path"))):
|
2012-04-24 04:04:03 +02:00
|
|
|
self.remote_path = self.Addon.getSetting("remote_path")
|
|
|
|
|
|
|
|
if(self.Addon.getSetting("backup_name") != ''):
|
|
|
|
self.remote_path = self.remote_path + self.Addon.getSetting("backup_name") + "/"
|
|
|
|
else:
|
|
|
|
self.remote_path = ""
|
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
|
|
|
|
if(self.Addon.getSetting('run_silent') == 'false'):
|
|
|
|
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
|
|
|
|
if(int(self.Addon.getSetting('addon_mode')) == 0):
|
2012-04-28 21:29:59 +02:00
|
|
|
self.fileManager = FileManager(self.local_path,self.Addon.getAddonInfo('profile'))
|
2012-04-22 00:18:41 +02:00
|
|
|
self.syncFiles()
|
|
|
|
else:
|
2012-04-28 21:29:59 +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-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
self.fileManager.createFileList(self.Addon)
|
|
|
|
|
|
|
|
allFiles = self.fileManager.readFileList()
|
|
|
|
|
|
|
|
#write list from local to remote
|
|
|
|
self.writeFiles(allFiles,self.local_path,self.remote_path)
|
|
|
|
|
|
|
|
def restoreFiles(self):
|
2012-04-28 21:29:59 +02:00
|
|
|
self.fileManager.createFileList(self.Addon)
|
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
allFiles = self.fileManager.readFileList()
|
|
|
|
|
|
|
|
#write list from remote to local
|
|
|
|
self.writeFiles(allFiles,self.remote_path,self.local_path)
|
|
|
|
|
|
|
|
def writeFiles(self,fileList,source,dest):
|
|
|
|
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()):
|
|
|
|
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-04-20 20:32:25 +02:00
|
|
|
if(self.Addon.getSetting('run_silent') == 'false'):
|
|
|
|
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):
|
|
|
|
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()):
|
2012-04-22 00:18:41 +02:00
|
|
|
backup.run()
|
2012-04-19 23:13:50 +02:00
|
|
|
else:
|
|
|
|
xbmcgui.Dialog().ok('XBMC Backup','Error: Remote path cannot be empty')
|