2012-04-17 23:03:45 +02:00
|
|
|
import xbmc
|
|
|
|
import xbmcaddon
|
2012-04-19 23:13:50 +02:00
|
|
|
import xbmcgui
|
2012-08-20 21:27:35 +02:00
|
|
|
import xbmcvfs
|
2012-04-19 23:13:50 +02:00
|
|
|
import os
|
2012-08-17 21:59:47 +02:00
|
|
|
import time
|
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-08-20 21:27:35 +02:00
|
|
|
not_dir = ['.zip','.xsp','.rar']
|
2012-07-18 17:27:00 +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-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-08-20 21:27:35 +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-08-20 21:27:35 +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
|
2012-08-20 21:27:35 +02:00
|
|
|
dirs,configFiles = xbmcvfs.listdir(self.walk_path + "userdata/")
|
2012-04-22 00:18:41 +02:00
|
|
|
for aFile in configFiles:
|
2012-08-20 21:27:35 +02:00
|
|
|
if(aFile.endswith(".xml")):
|
|
|
|
self.addFile("userdata/" + aFile)
|
2012-04-22 00:18:41 +02:00
|
|
|
|
|
|
|
def walkTree(self,directory):
|
2012-08-20 21:27:35 +02:00
|
|
|
dirs,files = xbmcvfs.listdir(directory)
|
|
|
|
|
|
|
|
#create all the subdirs first
|
|
|
|
for aDir in dirs:
|
|
|
|
dirPath = xbmc.translatePath(directory + "/" + aDir)
|
|
|
|
file_ext = aDir.split('.')[-1]
|
|
|
|
|
|
|
|
self.addFile("-" + dirPath[len(self.walk_path):].decode("UTF-8"))
|
|
|
|
#catch for "non directory" type files
|
|
|
|
if (not any(file_ext in s for s in self.not_dir)):
|
|
|
|
self.walkTree(dirPath)
|
2012-04-22 00:18:41 +02:00
|
|
|
|
2012-08-20 21:27:35 +02:00
|
|
|
#copy all the files
|
|
|
|
for aFile in files:
|
|
|
|
filePath = xbmc.translatePath(directory + "/" + aFile)
|
|
|
|
self.addFile(filePath[len(self.walk_path):].decode("UTF-8"))
|
2012-04-22 00:18:41 +02:00
|
|
|
|
2012-04-20 22:48:18 +02:00
|
|
|
def addFile(self,filename):
|
|
|
|
#write the full remote path name of this file
|
2012-08-17 21:18:08 +02:00
|
|
|
log("Add File: " + filename,xbmc.LOGDEBUG)
|
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-17 21:18:08 +02:00
|
|
|
self.local_path = xbmc.makeLegalFilename(xbmc.translatePath("special://home"),False);
|
2012-04-28 21:46:42 +02:00
|
|
|
|
2012-08-17 21:18:08 +02:00
|
|
|
if(self.addon.getSetting('remote_selection') == '1'):
|
2012-08-03 22:44:02 +02:00
|
|
|
self.remote_path = self.addon.getSetting('remote_path_2')
|
|
|
|
self.addon.setSetting("remote_path","")
|
2012-08-17 21:18:08 +02:00
|
|
|
elif(self.addon.getSetting('remote_selection') == '0'):
|
2012-08-03 22:44:02 +02:00
|
|
|
self.remote_path = self.addon.getSetting("remote_path")
|
2012-08-17 21:59:47 +02:00
|
|
|
|
|
|
|
#check if trailing slash is included
|
|
|
|
if(self.remote_path[-1:] != "/"):
|
|
|
|
self.remote_path = self.remote_path + "/"
|
|
|
|
|
|
|
|
#append backup folder name
|
|
|
|
if(int(self.addon.getSetting('addon_mode')) == 0 and self.remote_path != ''):
|
|
|
|
date_today = time.localtime(time.time())
|
|
|
|
self.remote_path = self.remote_path + str(date_today[1]) + str(date_today[2]) + str(date_today[0]) + "/"
|
|
|
|
elif(int(self.addon.getSetting('addon_mode')) == 1 and self.addon.getSetting("backup_name") != '' and self.remote_path != ''):
|
2012-08-03 22:44:02 +02:00
|
|
|
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-20 21:27:35 +02:00
|
|
|
|
|
|
|
self.local_path = self.local_path.decode("UTF-8")
|
|
|
|
self.local_path = self.local_path.decode("UTF-8")
|
2012-04-20 22:48:18 +02:00
|
|
|
|
2012-08-17 21:18:08 +02:00
|
|
|
log(self.addon.getLocalizedString(30046))
|
|
|
|
log(self.addon.getLocalizedString(30047) + ": " + self.local_path)
|
|
|
|
log(self.addon.getLocalizedString(30048) + ": " + 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()
|
2012-08-07 16:32:21 +02:00
|
|
|
self.progressBar.create(self.addon.getLocalizedString(30010),self.addon.getLocalizedString(30049) + "......")
|
2012-04-25 04:01:12 +02:00
|
|
|
|
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-08-17 21:59:47 +02:00
|
|
|
|
|
|
|
#for backups check if remote path exists
|
2012-08-20 21:27:35 +02:00
|
|
|
if(xbmcvfs.exists(self.remote_path)):
|
2012-08-17 21:59:47 +02:00
|
|
|
#this will fail - need a disclaimer here
|
|
|
|
log(self.addon.getLocalizedString(30050))
|
|
|
|
|
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-08-17 21:59:47 +02:00
|
|
|
|
|
|
|
#for restores remote path must exist
|
2012-08-20 21:27:35 +02:00
|
|
|
if(xbmcvfs.exists(self.remote_path)):
|
2012-08-17 21:59:47 +02:00
|
|
|
self.restoreFiles()
|
|
|
|
else:
|
|
|
|
xbmcgui.Dialog().ok(self.addon.getLocalizedString(30010),self.addon.getLocalizedString(30045))
|
2012-04-19 23:13:50 +02:00
|
|
|
|
|
|
|
def syncFiles(self):
|
2012-08-17 21:59:47 +02:00
|
|
|
|
2012-04-22 00:18:41 +02:00
|
|
|
#make the remote directory
|
2012-08-20 21:27:35 +02:00
|
|
|
xbmcvfs.mkdir(self.remote_path)
|
2012-05-28 20:54:08 +02:00
|
|
|
|
2012-08-17 21:18:08 +02:00
|
|
|
log(self.addon.getLocalizedString(30051))
|
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
|
|
|
|
2012-08-17 21:18:08 +02:00
|
|
|
log(self.addon.getLocalizedString(30051))
|
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-08-17 21:18:08 +02:00
|
|
|
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-08-17 21:18:08 +02:00
|
|
|
log('Writing file: ' + source + aFile,xbmc.LOGDEBUG)
|
2012-04-22 00:18:41 +02:00
|
|
|
self.updateProgress(aFile)
|
|
|
|
if (aFile.startswith("-")):
|
2012-08-20 21:27:35 +02:00
|
|
|
xbmcvfs.mkdir(xbmc.makeLegalFilename(dest + aFile[1:],False))
|
2012-04-22 00:18:41 +02:00
|
|
|
else:
|
2012-08-20 21:27:35 +02:00
|
|
|
xbmcvfs.copy(xbmc.makeLegalFilename(source + aFile),xbmc.makeLegalFilename(dest + aFile,False))
|
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 isReady(self):
|
|
|
|
return True if self.remote_path != '' else False
|
2012-04-20 20:32:25 +02:00
|
|
|
|
2012-08-17 21:18:08 +02:00
|
|
|
#global functions for logging and encoding
|
|
|
|
def log(message,loglevel=xbmc.LOGNOTICE):
|
2012-08-20 21:27:35 +02:00
|
|
|
logM = encode(__Addon.getLocalizedString(30010) + ": " + message)
|
|
|
|
xbmc.log(logM,level=loglevel)
|
2012-08-17 21:18:08 +02:00
|
|
|
|
|
|
|
def encode(string):
|
|
|
|
return string.encode('UTF-8','replace')
|
|
|
|
|
|
|
|
|
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))
|