2012-09-12 22:03:46 +02:00
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
2012-09-12 22:30:54 +02:00
|
|
|
import xbmcvfs
|
2012-09-12 22:34:13 +02:00
|
|
|
import utils as utils
|
2012-11-05 18:13:48 +01:00
|
|
|
import os.path
|
2012-09-12 22:03:46 +02:00
|
|
|
import time
|
2012-11-04 21:39:17 +01:00
|
|
|
from vfs import XBMCFileSystem,DropboxFileSystem
|
2012-10-22 22:02:30 +02:00
|
|
|
|
2012-09-12 22:03:46 +02:00
|
|
|
class FileManager:
|
|
|
|
fileArray = None
|
|
|
|
verbose_log = False
|
|
|
|
not_dir = ['.zip','.xsp','.rar']
|
2012-11-02 21:59:40 +01:00
|
|
|
vfs = None
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
def __init__(self,vfs):
|
2012-11-02 21:59:40 +01:00
|
|
|
self.vfs = vfs
|
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
def createFileList(self):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.fileArray = []
|
2012-09-05 21:28:43 +02:00
|
|
|
self.verbose_log = utils.getSetting("verbose_log") == 'true'
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
#figure out which syncing options to run
|
2012-09-05 21:28:43 +02:00
|
|
|
if(utils.getSetting('backup_addons') == 'true'):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.addFile("-addons")
|
2012-11-04 21:39:17 +01:00
|
|
|
self.walkTree(self.vfs.root_path + "addons/")
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
self.addFile("-userdata")
|
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
if(utils.getSetting('backup_addon_data') == 'true'):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.addFile("-userdata/addon_data")
|
2012-11-04 21:39:17 +01:00
|
|
|
self.walkTree(self.vfs.root_path + "userdata/addon_data/")
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
if(utils.getSetting('backup_database') == 'true'):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.addFile("-userdata/Database")
|
2012-11-04 21:39:17 +01:00
|
|
|
self.walkTree(self.vfs.root_path + "userdata/Database")
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
if(utils.getSetting("backup_playlists") == 'true'):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.addFile("-userdata/playlists")
|
2012-11-04 21:39:17 +01:00
|
|
|
self.walkTree(self.vfs.root_path + "userdata/playlists")
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
if(utils.getSetting("backup_thumbnails") == "true"):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.addFile("-userdata/Thumbnails")
|
2012-11-04 21:39:17 +01:00
|
|
|
self.walkTree(self.vfs.root_path + "userdata/Thumbnails")
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
if(utils.getSetting("backup_config") == "true"):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.addFile("-userdata/keymaps")
|
2012-11-04 21:39:17 +01:00
|
|
|
self.walkTree(self.vfs.root_path + "userdata/keymaps")
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
self.addFile("-userdata/peripheral_data")
|
2012-11-04 21:39:17 +01:00
|
|
|
self.walkTree(self.vfs.root_path + "userdata/peripheral_data")
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
#this part is an oddity
|
2012-11-04 21:39:17 +01:00
|
|
|
dirs,configFiles = self.vfs.listdir(self.vfs.root_path + "userdata/")
|
2012-09-12 22:03:46 +02:00
|
|
|
for aFile in configFiles:
|
|
|
|
if(aFile.endswith(".xml")):
|
|
|
|
self.addFile("userdata/" + aFile)
|
|
|
|
|
|
|
|
def walkTree(self,directory):
|
2012-11-02 21:59:40 +01:00
|
|
|
dirs,files = self.vfs.listdir(directory)
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
#create all the subdirs first
|
|
|
|
for aDir in dirs:
|
|
|
|
dirPath = xbmc.translatePath(directory + "/" + aDir)
|
|
|
|
file_ext = aDir.split('.')[-1]
|
2012-12-11 20:39:13 +01:00
|
|
|
self.addFile("-" + dirPath[len(self.vfs.root_path):])
|
2012-09-12 22:03:46 +02:00
|
|
|
#catch for "non directory" type files
|
|
|
|
if (not any(file_ext in s for s in self.not_dir)):
|
|
|
|
self.walkTree(dirPath)
|
|
|
|
|
|
|
|
#copy all the files
|
|
|
|
for aFile in files:
|
|
|
|
filePath = xbmc.translatePath(directory + "/" + aFile)
|
2012-12-11 20:39:13 +01:00
|
|
|
self.addFile(filePath[len(self.vfs.root_path):])
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
def addFile(self,filename):
|
2012-12-11 20:39:13 +01:00
|
|
|
try:
|
|
|
|
filename = filename.decode('UTF-8')
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
filename = filename.decode('ISO-8859-2')
|
|
|
|
|
2012-09-12 22:03:46 +02:00
|
|
|
#write the full remote path name of this file
|
2012-09-05 21:28:43 +02:00
|
|
|
utils.log("Add File: " + filename,xbmc.LOGDEBUG)
|
2012-09-12 22:03:46 +02:00
|
|
|
self.fileArray.append(filename)
|
|
|
|
|
|
|
|
def getFileList(self):
|
|
|
|
return self.fileArray
|
|
|
|
|
|
|
|
class XbmcBackup:
|
2012-09-05 21:28:43 +02:00
|
|
|
#constants for initiating a back or restore
|
|
|
|
Backup = 0
|
|
|
|
Restore = 1
|
2012-11-02 21:59:40 +01:00
|
|
|
|
|
|
|
#remote file system
|
2012-11-04 21:39:17 +01:00
|
|
|
local_vfs = None
|
|
|
|
remote_vfs = None
|
2012-09-12 22:03:46 +02:00
|
|
|
restoreFile = None
|
|
|
|
|
|
|
|
#for the progress bar
|
|
|
|
progressBar = None
|
|
|
|
filesLeft = 0
|
|
|
|
filesTotal = 1
|
|
|
|
|
|
|
|
fileManager = None
|
2012-11-28 17:19:03 +01:00
|
|
|
restore_point = None
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
def __init__(self):
|
2012-11-04 21:39:17 +01:00
|
|
|
self.local_vfs = XBMCFileSystem()
|
|
|
|
self.local_vfs.set_root(xbmc.translatePath("special://home"))
|
2012-11-02 21:59:40 +01:00
|
|
|
|
|
|
|
self.configureVFS()
|
|
|
|
|
|
|
|
utils.log(utils.getString(30046))
|
|
|
|
|
|
|
|
def configureVFS(self):
|
|
|
|
if(utils.getSetting('remote_selection') == '1'):
|
2012-11-04 21:39:17 +01:00
|
|
|
self.remote_vfs = XBMCFileSystem()
|
|
|
|
self.remote_vfs.set_root(utils.getSetting('remote_path_2'))
|
2012-09-05 21:28:43 +02:00
|
|
|
utils.setSetting("remote_path","")
|
|
|
|
elif(utils.getSetting('remote_selection') == '0'):
|
2012-11-04 21:39:17 +01:00
|
|
|
self.remote_vfs = XBMCFileSystem()
|
|
|
|
self.remote_vfs.set_root(utils.getSetting("remote_path"))
|
2012-11-02 21:59:40 +01:00
|
|
|
elif(utils.getSetting('remote_selection') == '2'):
|
2012-11-04 21:39:17 +01:00
|
|
|
self.remote_vfs = DropboxFileSystem()
|
2012-11-05 19:25:25 +01:00
|
|
|
self.remote_vfs.set_root('/')
|
2012-09-05 21:28:43 +02:00
|
|
|
|
2012-11-28 17:19:03 +01:00
|
|
|
def listBackups(self):
|
|
|
|
result = list()
|
|
|
|
|
|
|
|
#get all the folders in the current root path
|
|
|
|
dirs,files = self.remote_vfs.listdir(self.remote_vfs.root_path)
|
|
|
|
|
|
|
|
for aDir in dirs:
|
|
|
|
result.append(aDir)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def selectRestore(self,restore_point):
|
|
|
|
self.restore_point = restore_point
|
|
|
|
|
2012-09-06 22:28:41 +02:00
|
|
|
def run(self,mode=-1,runSilent=False):
|
2012-09-05 21:28:43 +02:00
|
|
|
|
|
|
|
#append backup folder name
|
2012-11-05 18:13:48 +01:00
|
|
|
remote_base_path = ""
|
2012-11-28 16:37:37 +01:00
|
|
|
progressBarTitle = utils.getString(30010) + " - "
|
2012-11-04 21:39:17 +01:00
|
|
|
if(mode == self.Backup and self.remote_vfs.root_path != ''):
|
2012-11-05 18:13:48 +01:00
|
|
|
#capture base path for backup rotation
|
|
|
|
remote_base_path = self.remote_vfs.set_root(self.remote_vfs.root_path + time.strftime("%Y%m%d") + "/")
|
2012-11-28 16:37:37 +01:00
|
|
|
progressBarTitle = progressBarTitle + utils.getString(30016)
|
2012-11-28 17:19:03 +01:00
|
|
|
elif(mode == self.Restore and self.restore_point != None and self.remote_vfs.root_path != ''):
|
|
|
|
self.remote_vfs.set_root(self.remote_vfs.root_path + self.restore_point + "/")
|
2012-11-28 16:37:37 +01:00
|
|
|
progressBarTitle = progressBarTitle + utils.getString(30017)
|
2012-09-12 22:03:46 +02:00
|
|
|
else:
|
2012-11-28 17:19:03 +01:00
|
|
|
#kill the program here
|
2012-11-04 21:39:17 +01:00
|
|
|
self.remote_vfs = None
|
2012-11-28 17:19:03 +01:00
|
|
|
return
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-11-04 21:39:17 +01:00
|
|
|
utils.log(utils.getString(30047) + ": " + self.local_vfs.root_path)
|
|
|
|
utils.log(utils.getString(30048) + ": " + self.remote_vfs.root_path)
|
2012-11-02 21:59:40 +01:00
|
|
|
|
2012-11-28 16:37:37 +01:00
|
|
|
#check if we should use the progress bar
|
|
|
|
if(utils.getSetting('run_silent') == 'false' and not runSilent):
|
|
|
|
self.progressBar = xbmcgui.DialogProgress()
|
|
|
|
self.progressBar.create(progressBarTitle,utils.getString(30049) + "......")
|
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
#run the correct mode
|
|
|
|
if(mode == self.Backup):
|
|
|
|
utils.log(utils.getString(30023) + " - " + utils.getString(30016))
|
2012-11-04 21:39:17 +01:00
|
|
|
self.fileManager = FileManager(self.local_vfs)
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
#for backups check if remote path exists
|
2012-11-04 21:39:17 +01:00
|
|
|
if(self.remote_vfs.exists(self.remote_vfs.root_path)):
|
2012-09-12 22:03:46 +02:00
|
|
|
#this will fail - need a disclaimer here
|
2012-09-05 21:28:43 +02:00
|
|
|
utils.log(utils.getString(30050))
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
self.syncFiles()
|
2012-09-19 18:43:36 +02:00
|
|
|
|
|
|
|
#remove old backups
|
|
|
|
total_backups = int(utils.getSetting('backup_rotation'))
|
|
|
|
if(total_backups > 0):
|
|
|
|
|
2012-11-05 18:13:48 +01:00
|
|
|
dirs,files = self.remote_vfs.listdir(remote_base_path)
|
2012-09-19 18:43:36 +02:00
|
|
|
if(len(dirs) > total_backups):
|
|
|
|
#remove backups to equal total wanted
|
2012-11-04 21:39:17 +01:00
|
|
|
dirs.sort()
|
2012-09-19 18:43:36 +02:00
|
|
|
remove_num = len(dirs) - total_backups - 1
|
|
|
|
self.filesTotal = self.filesTotal + remove_num + 1
|
|
|
|
|
|
|
|
#update the progress bar if it is available
|
|
|
|
while(remove_num >= 0 and not self.checkCancel()):
|
|
|
|
self.updateProgress(utils.getString(30054) + " " + dirs[remove_num])
|
|
|
|
utils.log("Removing backup " + dirs[remove_num])
|
2012-11-05 18:13:48 +01:00
|
|
|
self.remote_vfs.rmdir(remote_base_path + dirs[remove_num] + "/")
|
2012-09-19 18:43:36 +02:00
|
|
|
remove_num = remove_num - 1
|
|
|
|
|
|
|
|
|
2012-09-12 22:03:46 +02:00
|
|
|
else:
|
2012-09-05 21:28:43 +02:00
|
|
|
utils.log(utils.getString(30023) + " - " + utils.getString(30017))
|
2012-11-04 21:39:17 +01:00
|
|
|
self.fileManager = FileManager(self.remote_vfs)
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
#for restores remote path must exist
|
2012-11-04 21:39:17 +01:00
|
|
|
if(self.remote_vfs.exists(self.remote_vfs.root_path)):
|
2012-09-12 22:03:46 +02:00
|
|
|
self.restoreFiles()
|
|
|
|
else:
|
2012-11-04 21:39:17 +01:00
|
|
|
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_vfs.root_path)
|
2012-09-06 22:28:41 +02:00
|
|
|
|
|
|
|
if(utils.getSetting('run_silent') == 'false' and not runSilent):
|
|
|
|
self.progressBar.close()
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
def syncFiles(self):
|
|
|
|
|
|
|
|
#make the remote directory
|
2012-11-04 21:39:17 +01:00
|
|
|
self.remote_vfs.mkdir(self.remote_vfs.root_path)
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
utils.log(utils.getString(30051))
|
|
|
|
self.fileManager.createFileList()
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
allFiles = self.fileManager.getFileList()
|
|
|
|
|
|
|
|
#write list from local to remote
|
2012-11-04 21:39:17 +01:00
|
|
|
self.writeFiles(allFiles,self.local_vfs,self.remote_vfs)
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
def restoreFiles(self):
|
2012-09-05 21:28:43 +02:00
|
|
|
self.fileManager.createFileList()
|
2012-09-12 22:03:46 +02:00
|
|
|
|
2012-09-05 21:28:43 +02:00
|
|
|
utils.log(utils.getString(30051))
|
2012-09-12 22:03:46 +02:00
|
|
|
allFiles = self.fileManager.getFileList()
|
|
|
|
|
|
|
|
#write list from remote to local
|
2012-11-04 21:39:17 +01:00
|
|
|
self.writeFiles(allFiles,self.remote_vfs,self.local_vfs)
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
#call update addons to refresh everything
|
|
|
|
xbmc.executebuiltin('UpdateLocalAddons')
|
|
|
|
|
|
|
|
def writeFiles(self,fileList,source,dest):
|
2012-11-04 21:39:17 +01:00
|
|
|
utils.log("Writing files to: " + dest.root_path)
|
2012-09-12 22:03:46 +02:00
|
|
|
self.filesTotal = len(fileList)
|
|
|
|
self.filesLeft = self.filesTotal
|
|
|
|
|
|
|
|
#write each file from source to destination
|
|
|
|
for aFile in fileList:
|
|
|
|
if(not self.checkCancel()):
|
2012-11-04 21:39:17 +01:00
|
|
|
utils.log('Writing file: ' + source.root_path + aFile,xbmc.LOGDEBUG)
|
2012-09-12 22:03:46 +02:00
|
|
|
self.updateProgress(aFile)
|
|
|
|
if (aFile.startswith("-")):
|
2012-11-04 21:39:17 +01:00
|
|
|
dest.mkdir(dest.root_path + aFile[1:])
|
2012-09-12 22:03:46 +02:00
|
|
|
else:
|
2012-11-06 18:37:39 +01:00
|
|
|
if(isinstance(source,DropboxFileSystem)):
|
|
|
|
#if copying from dropbox we need the file handle, use get_file
|
|
|
|
source.get_file(source.root_path + aFile,dest.root_path + aFile)
|
|
|
|
else:
|
|
|
|
#copy using normal method
|
|
|
|
dest.put(source.root_path + aFile,dest.root_path + aFile)
|
2012-09-12 22:03:46 +02:00
|
|
|
|
|
|
|
def updateProgress(self,message=''):
|
|
|
|
self.filesLeft = self.filesLeft - 1
|
|
|
|
|
|
|
|
#update the progress bar
|
|
|
|
if(self.progressBar != None):
|
|
|
|
self.progressBar.update(int((float(self.filesTotal - self.filesLeft)/float(self.filesTotal)) * 100),message)
|
|
|
|
|
|
|
|
def checkCancel(self):
|
|
|
|
result = False
|
|
|
|
|
|
|
|
if(self.progressBar != None):
|
|
|
|
result = self.progressBar.iscanceled()
|
|
|
|
|
|
|
|
return result
|