Merge branch 'dropbox' of https://github.com/robweber/xbmcbackup into dropbox

This commit is contained in:
robweber 2012-11-05 10:57:21 -06:00
commit 140610db5a
4 changed files with 96 additions and 77 deletions

View File

@ -4,17 +4,15 @@ import xbmcvfs
import utils as utils import utils as utils
import os import os
import time import time
from vfs import XBMCFilesystem,DropboxFilesystem from vfs import XBMCFileSystem,DropboxFileSystem
class FileManager: class FileManager:
walk_path = ''
fileArray = None fileArray = None
verbose_log = False verbose_log = False
not_dir = ['.zip','.xsp','.rar'] not_dir = ['.zip','.xsp','.rar']
vfs = None vfs = None
def __init__(self,path,vfs): def __init__(self,vfs):
self.walk_path = path
self.vfs = vfs self.vfs = vfs
def createFileList(self): def createFileList(self):
@ -24,35 +22,35 @@ class FileManager:
#figure out which syncing options to run #figure out which syncing options to run
if(utils.getSetting('backup_addons') == 'true'): if(utils.getSetting('backup_addons') == 'true'):
self.addFile("-addons") self.addFile("-addons")
self.walkTree(self.walk_path + "addons/") self.walkTree(self.vfs.root_path + "addons/")
self.addFile("-userdata") self.addFile("-userdata")
if(utils.getSetting('backup_addon_data') == 'true'): if(utils.getSetting('backup_addon_data') == 'true'):
self.addFile("-userdata/addon_data") self.addFile("-userdata/addon_data")
self.walkTree(self.walk_path + "userdata/addon_data/") self.walkTree(self.vfs.root_path + "userdata/addon_data/")
if(utils.getSetting('backup_database') == 'true'): if(utils.getSetting('backup_database') == 'true'):
self.addFile("-userdata/Database") self.addFile("-userdata/Database")
self.walkTree(self.walk_path + "userdata/Database") self.walkTree(self.vfs.root_path + "userdata/Database")
if(utils.getSetting("backup_playlists") == 'true'): if(utils.getSetting("backup_playlists") == 'true'):
self.addFile("-userdata/playlists") self.addFile("-userdata/playlists")
self.walkTree(self.walk_path + "userdata/playlists") self.walkTree(self.vfs.root_path + "userdata/playlists")
if(utils.getSetting("backup_thumbnails") == "true"): if(utils.getSetting("backup_thumbnails") == "true"):
self.addFile("-userdata/Thumbnails") self.addFile("-userdata/Thumbnails")
self.walkTree(self.walk_path + "userdata/Thumbnails") self.walkTree(self.vfs.root_path + "userdata/Thumbnails")
if(utils.getSetting("backup_config") == "true"): if(utils.getSetting("backup_config") == "true"):
self.addFile("-userdata/keymaps") self.addFile("-userdata/keymaps")
self.walkTree(self.walk_path + "userdata/keymaps") self.walkTree(self.vfs.root_path + "userdata/keymaps")
self.addFile("-userdata/peripheral_data") self.addFile("-userdata/peripheral_data")
self.walkTree(self.walk_path + "userdata/peripheral_data") self.walkTree(self.vfs.root_path + "userdata/peripheral_data")
#this part is an oddity #this part is an oddity
dirs,configFiles = self.vfs.listdir(self.walk_path + "userdata/") dirs,configFiles = self.vfs.listdir(self.vfs.root_path + "userdata/")
for aFile in configFiles: for aFile in configFiles:
if(aFile.endswith(".xml")): if(aFile.endswith(".xml")):
self.addFile("userdata/" + aFile) self.addFile("userdata/" + aFile)
@ -65,7 +63,7 @@ class FileManager:
dirPath = xbmc.translatePath(directory + "/" + aDir) dirPath = xbmc.translatePath(directory + "/" + aDir)
file_ext = aDir.split('.')[-1] file_ext = aDir.split('.')[-1]
self.addFile("-" + dirPath[len(self.walk_path):].decode("UTF-8")) self.addFile("-" + dirPath[len(self.vfs.root_path):].decode("UTF-8"))
#catch for "non directory" type files #catch for "non directory" type files
if (not any(file_ext in s for s in self.not_dir)): if (not any(file_ext in s for s in self.not_dir)):
self.walkTree(dirPath) self.walkTree(dirPath)
@ -73,7 +71,7 @@ class FileManager:
#copy all the files #copy all the files
for aFile in files: for aFile in files:
filePath = xbmc.translatePath(directory + "/" + aFile) filePath = xbmc.translatePath(directory + "/" + aFile)
self.addFile(filePath[len(self.walk_path):].decode("UTF-8")) self.addFile(filePath[len(self.vfs.root_path):].decode("UTF-8"))
def addFile(self,filename): def addFile(self,filename):
#write the full remote path name of this file #write the full remote path name of this file
@ -91,9 +89,8 @@ class XbmcBackup:
#remote file system #remote file system
vfs = None vfs = None
local_path = '' local_vfs = None
remote_root = '' remote_vfs = None
remote_path = ''
restoreFile = None restoreFile = None
#for the progress bar #for the progress bar
@ -104,7 +101,8 @@ class XbmcBackup:
fileManager = None fileManager = None
def __init__(self): def __init__(self):
self.local_path = xbmc.makeLegalFilename(xbmc.translatePath("special://home"),False); self.local_vfs = XBMCFileSystem()
self.local_vfs.set_root(xbmc.translatePath("special://home"))
self.configureVFS() self.configureVFS()
@ -112,22 +110,15 @@ class XbmcBackup:
def configureVFS(self): def configureVFS(self):
if(utils.getSetting('remote_selection') == '1'): if(utils.getSetting('remote_selection') == '1'):
self.remote_root = utils.getSetting('remote_path_2') self.remote_vfs = XBMCFileSystem()
self.remote_vfs.set_root(utils.getSetting('remote_path_2'))
utils.setSetting("remote_path","") utils.setSetting("remote_path","")
self.vfs = XBMCFilesystem()
elif(utils.getSetting('remote_selection') == '0'): elif(utils.getSetting('remote_selection') == '0'):
self.remote_root = utils.getSetting("remote_path") self.remote_vfs = XBMCFileSystem()
self.vfs = XBMCFilesystem() self.remote_vfs.set_root(utils.getSetting("remote_path"))
elif(utils.getSetting('remote_selection') == '2'): elif(utils.getSetting('remote_selection') == '2'):
self.remote_root = '/xbmc' self.remote_vfs = DropboxFileSystem()
self.remote_vfs.set_root(utils.getSetting("dropbox_path"))
#fix slashes
self.remote_root = self.remote_root.replace("\\","/")
#check if trailing slash is included
if(self.remote_root[-1:] != "/"):
self.remote_root = self.remote_root + "/"
def run(self,mode=-1,runSilent=False): def run(self,mode=-1,runSilent=False):
#check if we should use the progress bar #check if we should use the progress bar
@ -140,25 +131,23 @@ class XbmcBackup:
mode = int(utils.getSetting('addon_mode')) mode = int(utils.getSetting('addon_mode'))
#append backup folder name #append backup folder name
if(mode == self.Backup and self.remote_root != ''): if(mode == self.Backup and self.remote_vfs.root_path != ''):
self.remote_path = self.remote_root + time.strftime("%Y%m%d") + "/" self.remote_vfs.set_root(self.remote_vfs.root_path + time.strftime("%Y%m%d") + "/")
elif(mode == self.Restore and utils.getSetting("backup_name") != '' and self.remote_root != ''): elif(mode == self.Restore and utils.getSetting("backup_name") != '' and self.remote_vfs.root_path != ''):
self.remote_path = self.remote_root + utils.getSetting("backup_name") + "/" self.remote_vfs.set_root(self.remote_vfs.root_path + utils.getSetting("backup_name") + "/")
else: else:
self.remote_path = "" self.remote_vfs = None
utils.log(utils.getString(30047) + ": " + self.local_path) utils.log(utils.getString(30047) + ": " + self.local_vfs.root_path)
utils.log(utils.getString(30048) + ": " + self.remote_path) utils.log(utils.getString(30048) + ": " + self.remote_vfs.root_path)
#run the correct mode #run the correct mode
if(mode == self.Backup): if(mode == self.Backup):
utils.log(utils.getString(30023) + " - " + utils.getString(30016)) utils.log(utils.getString(30023) + " - " + utils.getString(30016))
self.fileManager = FileManager(self.local_path,XBMCFileSystem()) self.fileManager = FileManager(self.local_vfs)
#for backups check if remote path exists #for backups check if remote path exists
if(self.vfs.exists(self.remote_path)): if(self.remote_vfs.exists(self.remote_vfs.root_path)):
#this will fail - need a disclaimer here #this will fail - need a disclaimer here
utils.log(utils.getString(30050)) utils.log(utils.getString(30050))
@ -168,7 +157,7 @@ class XbmcBackup:
total_backups = int(utils.getSetting('backup_rotation')) total_backups = int(utils.getSetting('backup_rotation'))
if(total_backups > 0): if(total_backups > 0):
dirs,files = self.vfs.listdir(self.remote_root) dirs,files = self.remote_vfs.listdir(self.remote_vfs.root_path)
if(len(dirs) > total_backups): if(len(dirs) > total_backups):
#remove backups to equal total wanted #remove backups to equal total wanted
dirs.sort() dirs.sort()
@ -179,19 +168,19 @@ class XbmcBackup:
while(remove_num >= 0 and not self.checkCancel()): while(remove_num >= 0 and not self.checkCancel()):
self.updateProgress(utils.getString(30054) + " " + dirs[remove_num]) self.updateProgress(utils.getString(30054) + " " + dirs[remove_num])
utils.log("Removing backup " + dirs[remove_num]) utils.log("Removing backup " + dirs[remove_num])
self.vfs.rmdir(self.remote_root + dirs[remove_num] + "/") self.remote_vfs.rmdir(self.remote_vfs.root_path + dirs[remove_num] + "/")
remove_num = remove_num - 1 remove_num = remove_num - 1
else: else:
utils.log(utils.getString(30023) + " - " + utils.getString(30017)) utils.log(utils.getString(30023) + " - " + utils.getString(30017))
self.fileManager = FileManager(self.remote_path,self.vfs) self.fileManager = FileManager(self.remote_vfs)
#for restores remote path must exist #for restores remote path must exist
if(self.vfs.exists(self.remote_path)): if(self.remote_vfs.exists(self.remote_vfs.root_path)):
self.restoreFiles() self.restoreFiles()
else: else:
xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_path) xbmcgui.Dialog().ok(utils.getString(30010),utils.getString(30045),self.remote_vfs.root_path)
if(utils.getSetting('run_silent') == 'false' and not runSilent): if(utils.getSetting('run_silent') == 'false' and not runSilent):
self.progressBar.close() self.progressBar.close()
@ -199,7 +188,7 @@ class XbmcBackup:
def syncFiles(self): def syncFiles(self):
#make the remote directory #make the remote directory
self.vfs.mkdir(self.remote_path) self.remote_vfs.mkdir(self.remote_vfs.root_path)
utils.log(utils.getString(30051)) utils.log(utils.getString(30051))
self.fileManager.createFileList() self.fileManager.createFileList()
@ -207,7 +196,7 @@ class XbmcBackup:
allFiles = self.fileManager.getFileList() allFiles = self.fileManager.getFileList()
#write list from local to remote #write list from local to remote
self.writeFiles(allFiles,self.local_path,self.remote_path) self.writeFiles(allFiles,self.local_vfs,self.remote_vfs)
def restoreFiles(self): def restoreFiles(self):
self.fileManager.createFileList() self.fileManager.createFileList()
@ -216,25 +205,25 @@ class XbmcBackup:
allFiles = self.fileManager.getFileList() allFiles = self.fileManager.getFileList()
#write list from remote to local #write list from remote to local
self.writeFiles(allFiles,self.remote_path,self.local_path) self.writeFiles(allFiles,self.remote_vfs,self.local_vfs)
#call update addons to refresh everything #call update addons to refresh everything
xbmc.executebuiltin('UpdateLocalAddons') xbmc.executebuiltin('UpdateLocalAddons')
def writeFiles(self,fileList,source,dest): def writeFiles(self,fileList,source,dest):
utils.log("Writing files to: " + dest) utils.log("Writing files to: " + dest.root_path)
self.filesTotal = len(fileList) self.filesTotal = len(fileList)
self.filesLeft = self.filesTotal self.filesLeft = self.filesTotal
#write each file from source to destination #write each file from source to destination
for aFile in fileList: for aFile in fileList:
if(not self.checkCancel()): if(not self.checkCancel()):
utils.log('Writing file: ' + source + aFile,xbmc.LOGDEBUG) utils.log('Writing file: ' + source.root_path + aFile,xbmc.LOGDEBUG)
self.updateProgress(aFile) self.updateProgress(aFile)
if (aFile.startswith("-")): if (aFile.startswith("-")):
xbmcvfs.mkdir(xbmc.makeLegalFilename(dest + aFile[1:],False)) dest.mkdir(dest.root_path + aFile[1:])
else: else:
xbmcvfs.copy(xbmc.makeLegalFilename(source + aFile),xbmc.makeLegalFilename(dest + aFile,False)) dest.copy(source.root_path + aFile,dest.root_path + aFile)
def updateProgress(self,message=''): def updateProgress(self,message=''):
self.filesLeft = self.filesLeft - 1 self.filesLeft = self.filesLeft - 1
@ -252,4 +241,4 @@ class XbmcBackup:
return result return result
def isReady(self): def isReady(self):
return True if self.remote_root != '' else False return True if self.remote_vfs != None else False

View File

@ -1,4 +1,4 @@
import resources.lib.utils as utils import resources.lib.utils as utils
def resource_filename(*args): def resource_filename(*args):
return utils.addon_dir() + "resources/lib/dropbox/trusted-certs.crt" return utils.addon_dir() + "/resources/lib/dropbox/trusted-certs.crt"

View File

@ -1,45 +1,77 @@
import utils as utils import utils as utils
import xbmcvfs import xbmcvfs
import xbmcgui
from dropbox import client, rest, session from dropbox import client, rest, session
APP_KEY = 'f5wlmek6aoriqax' APP_KEY = 'f5wlmek6aoriqax'
APP_SECRET = 'b1461sje1kxgzet' APP_SECRET = 'b1461sje1kxgzet'
class Vfs: class Vfs:
type = 'none' root_path = None
def listdir(directory): def set_root(self,rootString):
self.root_path = rootString
#fix slashes
self.root_path = self.root_path.replace("\\","/")
#check if trailing slash is included
if(self.root_path[-1:] != "/"):
self.root_path = self.root_path + "/"
def listdir(self,directory):
return {} return {}
def mkdir(directory): def mkdir(self,directory):
return True return True
def copy(source,dest): def copy(self,source,dest):
return True return True
def rmdir(directory): def rmdir(self,directory):
return True return True
def exists(aFile): def exists(self,aFile):
return True return True
class XBMCFileSystem(Vfs): class XBMCFileSystem(Vfs):
self.root_path
def listdir(directory): def listdir(self,directory):
return xbmcvfs.listdir(directory) return xbmcvfs.listdir(directory)
def mkdir(directory): def mkdir(self,directory):
return xbmcvfs.mkdir(directory) return xbmcvfs.mkdir(directory)
def rmdir(directory): def copy(self,source,dest):
return xbmcvfs.copy(source,dest)
def rmdir(self,directory):
return xbmcvfs.rmdir(directory,True) return xbmcvfs.rmdir(directory,True)
def exists(aFile): def exists(self,aFile):
return xbmcvfs.exists(aFile) return xbmcvfs.exists(aFile)
class DropboxFilesystem(Vfs): class DropboxFileSystem(Vfs):
user_token = None
def __init__(self): def __init__(self):
session = session.DropboxSession(APP_KEY,APP_SECRET,"app_folder") self.user_token = utils.getSetting('dropbox_token')
token = session.obtain_request_token() sess = session.DropboxSession(APP_KEY,APP_SECRET,"app_folder")
access_token = session.obtain_access_token(token)
if(self.user_token == ''):
token = sess.obtain_request_token()
url = sess.build_authorize_url(token)
try:
self.user_token = sess.obtain_access_token(token)
utils.setSetting("dropbox_token",self.user_token)
except:
xbmcgui.Dialog().ok(utils.getString(30010),"Authorize Dropbox URL, also in log",url)
utils.log("Authorize URL: " + url)
self.client = client.DropboxClient(sess)
utils.log(self.client.account_info())

View File

@ -5,10 +5,8 @@
<setting id="remote_selection" type="enum" lvalues="30018|30019|30027" default="0" label="30025"/> <setting id="remote_selection" type="enum" lvalues="30018|30019|30027" default="0" label="30025"/>
<setting id="remote_path_2" type="text" label="30024" default="" visible="eq(-1,1)" /> <setting id="remote_path_2" type="text" label="30024" default="" visible="eq(-1,1)" />
<setting id="remote_path" type="folder" label="30020" visible="eq(-2,0)" /> <setting id="remote_path" type="folder" label="30020" visible="eq(-2,0)" />
<setting id="dropbox_username" type="text" label="Dropbox Username" visible="eq(-3,2)" /> <setting id="dropbox_path" type="text" label="Dropbox Path" visible="eq(-3,2)" default="/xbmc" />
<setting id="dropbox_password" type="text" label="Dropbox Password" visible="eq(-4,2)" /> <setting id="backup_name" type="text" label="30021" default="backup_date" visible="eq(-5,1)"/>
<setting id="dropbox_path" type="text" label="Dropbox Path" visible="eq(-5,2)" default="/xbmc" />
<setting id="backup_name" type="text" label="30021" default="backup_date" visible="eq(-7,1)"/>
<setting id="backup_rotation" type="number" label="30026" default="0" /> <setting id="backup_rotation" type="number" label="30026" default="0" />
<setting id="run_silent" type="bool" label="30022" default="false" /> <setting id="run_silent" type="bool" label="30022" default="false" />
</category> </category>