Updated kodi settings on Lenovo

This commit is contained in:
2026-03-22 22:28:43 +01:00
parent 725dfa7157
commit 32b5a81da6
10925 changed files with 575678 additions and 5511 deletions

View File

@@ -0,0 +1,232 @@
import json
import xbmcgui
import xbmcvfs
import os.path
from . import utils as utils
class BackupSetManager:
jsonFile = xbmcvfs.translatePath(utils.data_dir() + "custom_paths.json")
paths = None
def __init__(self):
self.paths = {}
# try and read in the custom file
self._readFile()
def addSet(self, aSet):
self.paths[aSet['name']] = {'root': aSet['root'], 'dirs': [{"type": "include", "path": aSet['root'], 'recurse': True}]}
# save the file
self._writeFile()
def updateSet(self, name, aSet):
self.paths[name] = aSet
# save the file
self._writeFile()
def deleteSet(self, index):
# match the index to a key
keys = self.getSets()
# delete this set
del self.paths[keys[index]]
# save the file
self._writeFile()
def getSets(self):
# list all current sets by name
keys = list(self.paths.keys())
keys.sort()
return keys
def getSet(self, index):
keys = self.getSets()
# return the set at this index
return {'name': keys[index], 'set': self.paths[keys[index]]}
def validateSetName(self, name):
return (name not in self.getSets())
def _writeFile(self):
# create the custom file
aFile = xbmcvfs.File(self.jsonFile, 'w')
aFile.write(json.dumps(self.paths))
aFile.close()
def _readFile(self):
if(xbmcvfs.exists(self.jsonFile)):
# read in the custom file
aFile = xbmcvfs.File(self.jsonFile)
# load custom dirs
self.paths = json.loads(aFile.read())
aFile.close()
else:
# write a blank file
self._writeFile()
class AdvancedBackupEditor:
dialog = None
def __init__(self):
self.dialog = xbmcgui.Dialog()
def _cleanPath(self, root, path):
return path[len(root) - 1:]
def _validatePath(self, root, path):
return path.startswith(root)
def createSet(self):
backupSet = None
name = self.dialog.input(utils.getString(30110), defaultt='Backup Set')
if(name is not None):
# give a choice to start in home or enter a root path
enterHome = self.dialog.yesno(utils.getString(30111), message=utils.getString(30112) + " - " + utils.getString(30114) + "\n" + utils.getString(30113) + " - " + utils.getString(30115), nolabel=utils.getString(30112), yeslabel=utils.getString(30113))
rootFolder = 'special://home'
if(enterHome):
rootFolder = self.dialog.input(utils.getString(30116), defaultt=rootFolder)
# direcotry has to end in slash
if(rootFolder[:-1] != '/'):
rootFolder = rootFolder + '/'
# check that this path even exists
if(not xbmcvfs.exists(xbmcvfs.translatePath(rootFolder))):
self.dialog.ok(utils.getString(30117), utils.getString(30118), rootFolder)
return None
else:
# select path to start set
rootFolder = self.dialog.browse(type=0, heading=utils.getString(30119), shares='files', defaultt=rootFolder)
backupSet = {'name': name, 'root': rootFolder}
return backupSet
def editSet(self, name, backupSet):
optionSelected = ''
rootPath = backupSet['root']
while(optionSelected != -1):
options = [xbmcgui.ListItem(utils.getString(30120), utils.getString(30143)), xbmcgui.ListItem(utils.getString(30135), utils.getString(30144)), xbmcgui.ListItem(rootPath, utils.getString(30121))]
for aDir in backupSet['dirs']:
if(aDir['type'] == 'exclude'):
options.append(xbmcgui.ListItem(self._cleanPath(rootPath, aDir['path']), "%s: %s" % (utils.getString(30145), utils.getString(30129))))
elif(aDir['type'] == 'include'):
options.append(xbmcgui.ListItem(self._cleanPath(rootPath, aDir['path']), "%s: %s | %s: %s" % (utils.getString(30145), utils.getString(30134), utils.getString(30146), str(aDir['recurse']))))
optionSelected = self.dialog.select(utils.getString(30122) + ' ' + name, options, useDetails=True)
if(optionSelected == 0 or optionSelected == 1):
# add a folder, will equal root if cancel is hit
addFolder = self.dialog.browse(type=0, heading=utils.getString(30120), shares='files', defaultt=backupSet['root'])
if(addFolder.startswith(rootPath)):
if(not any(addFolder == aDir['path'] for aDir in backupSet['dirs'])):
# cannot add root as an exclusion
if(optionSelected == 0 and addFolder != backupSet['root']):
backupSet['dirs'].append({"path": addFolder, "type": "exclude"})
elif(optionSelected == 1):
# can add root as inclusion
backupSet['dirs'].append({"path": addFolder, "type": "include", "recurse": True})
else:
# this path is already part of another include/exclude rule
self.dialog.ok(utils.getString(30117), utils.getString(30137), addFolder)
else:
# folder must be under root folder
self.dialog.ok(utils.getString(30117), utils.getString(30136), rootPath)
elif(optionSelected == 2):
self.dialog.ok(utils.getString(30121), utils.getString(30130), backupSet['root'])
elif(optionSelected > 2):
cOptions = ['Delete']
if(backupSet['dirs'][optionSelected - 3]['type'] == 'include'):
cOptions.append(utils.getString(30147))
contextOption = self.dialog.contextmenu(cOptions)
if(contextOption == 0):
if(self.dialog.yesno(heading=utils.getString(30123), message=utils.getString(30128))):
# remove folder
del backupSet['dirs'][optionSelected - 3]
elif(contextOption == 1 and backupSet['dirs'][optionSelected - 3]['type'] == 'include'):
# toggle if this folder should be recursive
backupSet['dirs'][optionSelected - 3]['recurse'] = not backupSet['dirs'][optionSelected - 3]['recurse']
return backupSet
def showMainScreen(self):
exitCondition = ""
customPaths = BackupSetManager()
# show this every time
self.dialog.ok(utils.getString(30036), utils.getString(30037))
while(exitCondition != -1):
# load the custom paths
listItem = xbmcgui.ListItem(utils.getString(30126), '')
listItem.setArt({'icon': os.path.join(utils.addon_dir(), 'resources', 'images', 'plus-icon.png')})
options = [listItem]
for index in range(0, len(customPaths.getSets())):
aSet = customPaths.getSet(index)
listItem = xbmcgui.ListItem(aSet['name'], utils.getString(30121) + ': ' + aSet['set']['root'])
listItem.setArt({'icon': os.path.join(utils.addon_dir(), 'resources', 'images', 'folder-icon.png')})
options.append(listItem)
# show the gui
exitCondition = self.dialog.select(utils.getString(30125), options, useDetails=True)
if(exitCondition >= 0):
if(exitCondition == 0):
newSet = self.createSet()
# check that the name is unique
if(customPaths.validateSetName(newSet['name'])):
customPaths.addSet(newSet)
else:
self.dialog.ok(utils.getString(30117), utils.getString(30138), newSet['name'])
else:
# bring up a context menu
menuOption = self.dialog.contextmenu([utils.getString(30122), utils.getString(30123)])
if(menuOption == 0):
# get the set
aSet = customPaths.getSet(exitCondition - 1)
# edit the set
updatedSet = self.editSet(aSet['name'], aSet['set'])
# save it
customPaths.updateSet(aSet['name'], updatedSet)
elif(menuOption == 1):
if(self.dialog.yesno(heading=utils.getString(30127), message=utils.getString(30128))):
# delete this path - subtract one because of "add" item
customPaths.deleteSet(exitCondition - 1)
def copySimpleConfig(self):
# disclaimer in case the user hit this on accident
shouldContinue = self.dialog.yesno(heading=utils.getString(30139), message=utils.getString(30140) + "\n" + utils.getString(30141))
if(shouldContinue):
source = xbmcvfs.translatePath(os.path.join(utils.addon_dir(), 'resources', 'data', 'default_files.json'))
dest = xbmcvfs.translatePath(os.path.join(utils.data_dir(), 'custom_paths.json'))
xbmcvfs.copy(source, dest)

View File

@@ -0,0 +1,165 @@
import xbmcgui
import xbmcvfs
import json
import pyqrcode
import time
import resources.lib.tinyurl as tinyurl
import resources.lib.utils as utils
import datetime
# don't die on import error yet, these might not even get used
try:
from dropbox import dropbox
from dropbox import oauth
except ImportError:
pass
# fix for datetime.strptime bug https://kodi.wiki/view/Python_Problems#datetime.strptime
class proxydt(datetime.datetime):
@classmethod
def strptime(cls, date_string, format):
return datetime.datetime(*(time.strptime(date_string, format)[:6]))
datetime.datetime = proxydt
class QRCode(xbmcgui.WindowXMLDialog):
def __init__(self, *args, **kwargs):
self.image = kwargs["image"]
self.text = kwargs["text"]
self.url = kwargs['url']
def onInit(self):
self.imagecontrol = 501
self.textbox1 = 502
self.textbox2 = 504
self.okbutton = 503
self.showdialog()
def showdialog(self):
self.getControl(self.imagecontrol).setImage(self.image)
self.getControl(self.textbox1).setText(self.text)
self.getControl(self.textbox2).setText(self.url)
self.setFocus(self.getControl(self.okbutton))
def onClick(self, controlId):
if (controlId == self.okbutton):
self.close()
class DropboxAuthorizer:
TOKEN_FILE = "tokens.json"
APP_KEY = ""
APP_SECRET = ""
def __init__(self):
self.APP_KEY = utils.getSettingStringStripped('dropbox_key')
self.APP_SECRET = utils.getSettingStringStripped('dropbox_secret')
def setup(self):
result = True
if(self.APP_KEY == '' and self.APP_SECRET == ''):
# we can't go any farther, need these for sure
xbmcgui.Dialog().ok(utils.getString(30010), '%s %s\n%s' % (utils.getString(30027), utils.getString(30058), utils.getString(30059)))
result = False
return result
def isAuthorized(self):
user_token = self._getToken()
return 'access_token' in user_token
def authorize(self):
result = True
if(not self.setup()):
return False
if(self.isAuthorized()):
# delete the token to start over
self._deleteToken()
# copied flow from http://dropbox-sdk-python.readthedocs.io/en/latest/moduledoc.html#dropbox.oauth.DropboxOAuth2FlowNoRedirect
flow = oauth.DropboxOAuth2FlowNoRedirect(consumer_key=self.APP_KEY, consumer_secret=self.APP_SECRET, token_access_type="offline")
url = flow.start()
# print url in log
utils.log("Authorize URL: " + url)
# create a QR Code
shortUrl = str(tinyurl.shorten(url), 'utf-8')
imageFile = xbmcvfs.translatePath(utils.data_dir() + '/qrcode.png')
qrIMG = pyqrcode.create(shortUrl)
qrIMG.png(imageFile, scale=10)
# show the dialog prompt to authorize
qr = QRCode("script-backup-qrcode.xml", utils.addon_dir(), "default", image=imageFile, text=utils.getString(30056), url=shortUrl)
qr.doModal()
# cleanup
del qr
xbmcvfs.delete(imageFile)
# get the auth code
code = xbmcgui.Dialog().input(utils.getString(30027) + ' ' + utils.getString(30103))
# if user authorized this will work
try:
user_token = flow.finish(code)
self._setToken(user_token)
except Exception as e:
utils.log("Error: %s" % (e,))
result = False
return result
# return the DropboxClient, or None if can't be created
def getClient(self):
result = None
user_token = self._getToken()
if(user_token != ''):
# create the client
result = dropbox.Dropbox(oauth2_access_token=user_token['access_token'], oauth2_refresh_token=user_token['refresh_token'],
oauth2_access_token_expiration=user_token['expiration'], app_key=self.APP_KEY, app_secret=self.APP_SECRET)
try:
result.users_get_current_account()
except:
# this didn't work, delete the token file
self._deleteToken()
result = None
return result
def _setToken(self, token):
# write the token files
token_file = open(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE), 'w')
token_file.write(json.dumps({"access_token": token.access_token, "refresh_token": token.refresh_token, "expiration": str(token.expires_at)}))
token_file.close()
def _getToken(self):
result = {}
# get token, if it exists
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))):
token_file = open(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))
token = token_file.read()
if(token.strip() != ""):
result = json.loads(token)
# convert expiration back to a datetime object
result['expiration'] = datetime.datetime.strptime(result['expiration'], "%Y-%m-%d %H:%M:%S.%f")
token_file.close()
return result
def _deleteToken(self):
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))):
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + self.TOKEN_FILE))

View File

@@ -0,0 +1,673 @@
from __future__ import unicode_literals
import time
import json
import xbmc
import xbmcgui
import xbmcvfs
import os.path
from . import utils as utils
from datetime import datetime
from . vfs import XBMCFileSystem, DropboxFileSystem, ZipFileSystem
from . progressbar import BackupProgressBar
from resources.lib.guisettings import GuiSettingsManager
from resources.lib.extractor import ZipExtractor
def folderSort(aKey):
result = aKey[0]
if(len(result) < 8):
result = result + "0000"
return result
class XbmcBackup:
# constants for initiating a back or restore
Backup = 0
Restore = 1
ZIP_TEMP_PATH = None
# list of dirs for the "simple" file selection
simple_directory_list = ['addons', 'addon_data', 'database', 'game_saves', 'playlists', 'profiles', 'thumbnails', 'config']
# file systems
xbmc_vfs = None
remote_vfs = None
saved_remote_vfs = None
restoreFile = None
remote_base_path = None
# for the progress bar
progressBar = None
transferSize = 0
transferLeft = 0
restore_point = None
skip_advanced = False # if we should check for the existance of advancedsettings in the restore
def __init__(self):
self.xbmc_vfs = XBMCFileSystem(xbmcvfs.translatePath('special://home'))
self.ZIP_TEMP_PATH = xbmcvfs.translatePath(utils.getSetting('zip_temp_path'))
self.configureRemote()
utils.log(utils.getString(30046))
def configureRemote(self):
if(utils.getSetting('remote_selection') == '1'):
self.remote_vfs = XBMCFileSystem(utils.getSetting('remote_path_2'))
utils.setSetting("remote_path", "")
elif(utils.getSetting('remote_selection') == '0'):
self.remote_vfs = XBMCFileSystem(utils.getSetting("remote_path"))
elif(utils.getSetting('remote_selection') == '2'):
self.remote_vfs = DropboxFileSystem("/")
self.remote_base_path = self.remote_vfs.root_path
def remoteConfigured(self):
result = True
if(self.remote_base_path == "" or not xbmcvfs.exists(self.ZIP_TEMP_PATH)):
result = False
return result
# reverse - should reverse the resulting, default is true - newest to oldest
def listBackups(self, reverse=True):
result = []
# get all the folders in the current root path
dirs, files = self.remote_vfs.listdir(self.remote_base_path)
for aDir in dirs:
if(self.remote_vfs.exists(self.remote_base_path + aDir + "/xbmcbackup.val")):
# format the name according to regional settings
folderName = self._dateFormat(aDir)
result.append((aDir, folderName))
for aFile in files:
file_ext = aFile.split('.')[-1]
folderName = aFile.split('.')[0]
if(file_ext == 'zip' and len(folderName) >= 12 and folderName[0:12].isdigit()):
# format the name according to regional settings and display the file size
folderName = "%s - %s" % (self._dateFormat(folderName), utils.diskString(self.remote_vfs.fileSize(self.remote_base_path + aFile)))
result.append((aFile, folderName))
result.sort(key=folderSort, reverse=reverse)
return result
def selectRestore(self, restore_point):
self.restore_point = restore_point
def skipAdvanced(self):
self.skip_advanced = True
def backup(self, progressOverride=False):
shouldContinue = self._setupVFS(self.Backup, progressOverride)
if(shouldContinue):
utils.log(utils.getString(30023) + " - " + utils.getString(30016))
# check if remote path exists
if(self.remote_vfs.exists(self.remote_vfs.root_path)):
# may be data in here already
utils.log(utils.getString(30050))
else:
# make the remote directory
self.remote_vfs.mkdir(self.remote_vfs.root_path)
utils.log(utils.getString(30051))
utils.log('File Selection Type: ' + str(utils.getSetting('backup_selection_type')))
allFiles = []
if(utils.getSettingInt('backup_selection_type') == 0):
# read in a list of the directories to backup
selectedDirs = self._readBackupConfig(utils.addon_dir() + "/resources/data/default_files.json")
# simple mode - get file listings for all enabled directories
for aDir in self.simple_directory_list:
# if this dir enabled
if(utils.getSettingBool('backup_' + aDir)):
# get a file listing and append it to the allfiles array
allFiles.append(self._addBackupDir(aDir, selectedDirs[aDir]['root'], selectedDirs[aDir]['dirs']))
else:
# advanced mode - load custom paths
selectedDirs = self._readBackupConfig(utils.data_dir() + "/custom_paths.json")
# get the set names
keys = list(selectedDirs.keys())
# go through the custom sets
for aKey in keys:
# get the set
aSet = selectedDirs[aKey]
# get file listing and append
allFiles.append(self._addBackupDir(aKey, aSet['root'], aSet['dirs']))
# create a validation file for backup rotation
writeCheck = self._createValidationFile(allFiles)
if(not writeCheck):
# we may not be able to write to this destination for some reason
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30089), "%s\n%s" % (utils.getString(30090), utils.getString(30044)), autoclose=25000)
if(not shouldContinue):
return
orig_base_path = self.remote_vfs.root_path
# backup all the files
self.transferLeft = self.transferSize
for fileGroup in allFiles:
self.xbmc_vfs.set_root(xbmcvfs.translatePath(fileGroup['source']))
self.remote_vfs.set_root(fileGroup['dest'] + fileGroup['name'])
filesCopied = self._copyFiles(fileGroup['files'], self.xbmc_vfs, self.remote_vfs)
if(not filesCopied):
utils.showNotification(utils.getString(30092))
utils.log(utils.getString(30092))
# reset remote and xbmc vfs
self.xbmc_vfs.set_root("special://home/")
self.remote_vfs.set_root(orig_base_path)
if(utils.getSettingBool("compress_backups")):
fileManager = FileManager(self.xbmc_vfs)
# send the zip file to the real remote vfs
zip_name = os.path.join(self.ZIP_TEMP_PATH, self.remote_vfs.root_path[:-1] + ".zip")
self.remote_vfs.cleanup()
self.xbmc_vfs.rename(os.path.join(self.ZIP_TEMP_PATH, "xbmc_backup_temp.zip"), zip_name)
fileManager.addFile(zip_name)
# set root to data dir home and reset remote
self.xbmc_vfs.set_root(self.ZIP_TEMP_PATH)
self.remote_vfs = self.saved_remote_vfs
# update the amount to transfer
self.transferSize = fileManager.fileSize()
self.transferLeft = self.transferSize
fileCopied = self._copyFiles(fileManager.getFiles(), self.xbmc_vfs, self.remote_vfs)
if(not fileCopied):
# zip archive copy filed, inform the user
shouldContinue = xbmcgui.Dialog().ok(utils.getString(30089), '%s\n%s' % (utils.getString(30090), utils.getString(30091)))
# delete the temp zip file
self.xbmc_vfs.rmfile(zip_name)
# remove old backups
self._rotateBackups()
# close any files
self._closeVFS()
def restore(self, progressOverride=False, selectedSets=None):
shouldContinue = self._setupVFS(self.Restore, progressOverride)
if(shouldContinue):
utils.log(utils.getString(30023) + " - " + utils.getString(30017))
# catch for if the restore point is actually a zip file
if(self.restore_point.split('.')[-1] == 'zip'):
self.progressBar.updateProgress(2, utils.getString(30088))
utils.log("copying zip file: " + self.restore_point)
# set root to data dir home
self.xbmc_vfs.set_root(self.ZIP_TEMP_PATH)
restore_path = os.path.join(self.ZIP_TEMP_PATH, self.restore_point)
if(not self.xbmc_vfs.exists(restore_path)):
# copy just this file from the remote vfs
self.transferSize = self.remote_vfs.fileSize(self.remote_base_path + self.restore_point)
zipFile = []
zipFile.append({'file': self.remote_base_path + self.restore_point, 'size': self.transferSize, 'is_dir': False})
# set transfer size
self.transferLeft = self.transferSize
self._copyFiles(zipFile, self.remote_vfs, self.xbmc_vfs)
else:
utils.log("zip file exists already")
# extract the zip file
zip_vfs = ZipFileSystem(restore_path, 'r')
extractor = ZipExtractor()
if(not extractor.extract(zip_vfs, self.ZIP_TEMP_PATH, self.progressBar)):
# we had a problem extracting the archive, delete everything
zip_vfs.cleanup()
self.xbmc_vfs.rmfile(restore_path)
xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30101))
return
zip_vfs.cleanup()
self.progressBar.updateProgress(0, utils.getString(30049) + "......")
# set the new remote vfs and fix xbmc path
self.remote_vfs = XBMCFileSystem(os.path.join(self.ZIP_TEMP_PATH, self.restore_point.split(".")[0]))
self.xbmc_vfs.set_root(xbmcvfs.translatePath("special://home/"))
# for restores remote path must exist
if(not self.remote_vfs.exists(self.remote_vfs.root_path)):
xbmcgui.Dialog().ok(utils.getString(30010), '%s\n%s' % (utils.getString(30045), self.remote_vfs.root_path))
return
valFile = self._checkValidationFile(self.remote_vfs.root_path)
if(valFile is None):
# don't continue
return
utils.log(utils.getString(30051))
allFiles = []
fileManager = FileManager(self.remote_vfs)
# check for the existance of an advancedsettings file
if(self.remote_vfs.exists(self.remote_vfs.root_path + "config/advancedsettings.xml") and not self.skip_advanced):
# let the user know there is an advanced settings file present
restartXbmc = xbmcgui.Dialog().yesno(utils.getString(30038), "%s\n%s\n%s" % (utils.getString(30039), utils.getString(30040), utils.getString(30041)))
if(restartXbmc):
# add only this file to the file list
self.transferSize = 1
self.transferLeft = 1
fileManager.addFile(self.remote_vfs.root_path + "config/advancedsettings.xml")
self._copyFiles(fileManager.getFiles(), self.remote_vfs, self.xbmc_vfs)
# let the service know to resume this backup on startup
self._createResumeBackupFile()
# do not continue running
if(xbmcgui.Dialog().yesno(utils.getString(30077), utils.getString(30078), autoclose=15000)):
xbmc.executebuiltin('Quit')
return
# check if settings should be restored from this backup
restoreSettings = not utils.getSettingBool('always_prompt_restore_settings')
if(not restoreSettings and 'system_settings' in valFile):
# prompt the user to restore settings yes/no
restoreSettings = xbmcgui.Dialog().yesno(utils.getString(30149), utils.getString(30150))
# use a multiselect dialog to select sets to restore
restoreSets = [n['name'] for n in valFile['directories']]
# if passed in list, skip selection
if(selectedSets is None):
selectedSets = xbmcgui.Dialog().multiselect(utils.getString(30131), restoreSets)
else:
selectedSets = [restoreSets.index(n) for n in selectedSets if n in restoreSets] # if set name not found just skip it
if(selectedSets is not None):
# go through each of the directories in the backup and write them to the correct location
for index in selectedSets:
# add this directory
aDir = valFile['directories'][index]
self.xbmc_vfs.set_root(xbmcvfs.translatePath(aDir['path']))
if(self.remote_vfs.exists(self.remote_vfs.root_path + aDir['name'] + '/')):
# walk the directory
self.progressBar.updateProgress(0, f"{utils.getString(30049)}....{utils.getString(30162)}\n{utils.getString(30163)}: {aDir['name']}")
fileManager.walkTree(self.remote_vfs.root_path + aDir['name'] + '/')
self.transferSize = self.transferSize + fileManager.fileSize()
allFiles.append({"source": self.remote_vfs.root_path + aDir['name'], "dest": self.xbmc_vfs.root_path, "files": fileManager.getFiles()})
else:
utils.log("error path not found: " + self.remote_vfs.root_path + aDir['name'])
xbmcgui.Dialog().ok(utils.getString(30010), '%s\n%s' % (utils.getString(30045), self.remote_vfs.root_path + aDir['name']))
# restore all the files
self.transferLeft = self.transferSize
for fileGroup in allFiles:
self.remote_vfs.set_root(fileGroup['source'])
self.xbmc_vfs.set_root(fileGroup['dest'])
self._copyFiles(fileGroup['files'], self.remote_vfs, self.xbmc_vfs)
# update the Kodi settings - if we can
if('system_settings' in valFile and restoreSettings):
self.progressBar.updateProgress(98, "Restoring Kodi settings")
gui_settings = GuiSettingsManager()
gui_settings.restore(valFile['system_settings'])
self.progressBar.updateProgress(99, "Clean up operations .....")
if(self.restore_point.split('.')[-1] == 'zip'):
# delete the zip file and the extracted directory
self.xbmc_vfs.rmfile(os.path.join(self.ZIP_TEMP_PATH, self.restore_point))
xbmc.sleep(1000)
self.xbmc_vfs.rmdir(self.remote_vfs.clean_path(os.path.join(self.ZIP_TEMP_PATH, self.restore_point.split(".")[0])))
xbmc.sleep(1000)
# call update addons to refresh everything
xbmc.executebuiltin('UpdateLocalAddons')
# notify user that restart is recommended
if(xbmcgui.Dialog().yesno(utils.getString(30077), utils.getString(30078), autoclose=15000)):
xbmc.executebuiltin('Quit')
def _setupVFS(self, mode=-1, progressOverride=False):
# set windows setting to true
window = xbmcgui.Window(10000)
window.setProperty(utils.__addon_id__ + ".running", "true")
# append backup folder name
progressBarTitle = utils.getString(30010) + " - "
if(mode == self.Backup and self.remote_vfs.root_path != ''):
if(utils.getSettingBool("compress_backups")):
# delete old temp file
zip_path = os.path.join(self.ZIP_TEMP_PATH, 'xbmc_backup_temp.zip')
if(self.xbmc_vfs.exists(zip_path)):
if(not self.xbmc_vfs.rmfile(zip_path)):
# we had some kind of error deleting the old file
xbmcgui.Dialog().ok(utils.getString(30010), '%s\n%s' % (utils.getString(30096), utils.getString(30097)))
return False
# save the remote file system and use the zip vfs
self.saved_remote_vfs = self.remote_vfs
self.remote_vfs = ZipFileSystem(zip_path, "w")
self.remote_vfs.set_root(self.remote_vfs.root_path + time.strftime("%Y%m%d%H%M") + utils.getSetting('backup_suffix').strip() + "/")
progressBarTitle = progressBarTitle + utils.getString(30023) + ": " + utils.getString(30016)
elif(mode == self.Restore and self.restore_point is not None and self.remote_vfs.root_path != ''):
if(self.restore_point.split('.')[-1] != 'zip'):
self.remote_vfs.set_root(self.remote_vfs.root_path + self.restore_point + "/")
progressBarTitle = progressBarTitle + utils.getString(30023) + ": " + utils.getString(30017)
else:
# kill the program here
self.remote_vfs = None
return False
utils.log(utils.getString(30047) + ": " + self.xbmc_vfs.root_path)
utils.log(utils.getString(30048) + ": " + self.remote_vfs.root_path)
utils.log(utils.getString(30152) + ": " + utils.getSetting('zip_temp_path'))
# setup the progress bar
self.progressBar = BackupProgressBar(progressOverride)
self.progressBar.create(progressBarTitle, utils.getString(30049) + "......")
# if we made it this far we're good
return True
def _closeVFS(self):
self.xbmc_vfs.cleanup()
self.remote_vfs.cleanup()
self.progressBar.close()
# reset the window setting
window = xbmcgui.Window(10000)
window.setProperty(utils.__addon_id__ + ".running", "")
def _copyFiles(self, fileList, source, dest):
result = True
utils.log("Source: " + source.root_path)
utils.log("Destination: " + dest.root_path)
# make sure the dest folder exists - can cause write errors if the full path doesn't exist
if(not dest.exists(dest.root_path)):
dest.mkdir(dest.root_path)
for aFile in fileList:
if(not self.progressBar.checkCancel()):
if(utils.getSettingBool('verbose_logging')):
utils.log('Writing file: ' + aFile['file'])
if(aFile['is_dir']):
self._updateProgress('%s remaining\nwriting %s' % (utils.diskString(self.transferLeft), os.path.basename(aFile['file'][len(source.root_path):]) + "/"))
dest.mkdir(dest.root_path + aFile['file'][len(source.root_path):])
else:
self._updateProgress('%s remaining\nwriting %s' % (utils.diskString(self.transferLeft), os.path.basename(aFile['file'][len(source.root_path):])))
self.transferLeft = self.transferLeft - aFile['size']
# copy the file
wroteFile = self._copyFile(source, dest, aFile['file'], dest.root_path + aFile['file'][len(source.root_path):])
# if result is still true but this file failed
if(not wroteFile and result):
utils.log("Failed to write " + aFile['file'])
result = False
return result
def _copyFile(self, source, dest, sourceFile, destFile):
result = True
if(isinstance(source, DropboxFileSystem)):
# if copying from cloud storage we need the file handle, use get_file
result = source.get_file(sourceFile, destFile)
else:
# copy using normal method
result = dest.put(sourceFile, destFile)
return result
def _addBackupDir(self, folder_name, root_path, dirList):
utils.log('Backup set: ' + folder_name)
fileManager = FileManager(self.xbmc_vfs)
self.xbmc_vfs.set_root(xbmcvfs.translatePath(root_path))
for aDir in dirList:
fileManager.addDir(aDir)
# walk all the root trees
fileManager.walk()
# update total size
self.transferSize = self.transferSize + fileManager.fileSize()
return {"name": folder_name, "source": root_path, "dest": self.remote_vfs.root_path, "files": fileManager.getFiles()}
def _dateFormat(self, dirName):
# create date_time object from foldername YYYYMMDDHHmm
date_time = datetime(int(dirName[0:4]), int(dirName[4:6]), int(dirName[6:8]), int(dirName[8:10]), int(dirName[10:12]))
# format the string based on region settings
result = utils.getRegionalTimestamp(date_time, ['dateshort', 'time'])
return result
def _updateProgress(self, message=None):
self.progressBar.updateProgress(int((float(self.transferSize - self.transferLeft) / float(self.transferSize)) * 100), message)
def _rotateBackups(self):
total_backups = utils.getSettingInt('backup_rotation')
if(total_backups > 0):
# get a list of valid backup folders
dirs = self.listBackups(reverse=False)
if(len(dirs) > total_backups):
# remove backups to equal total wanted
remove_num = 0
# update the progress bar if it is available
while(remove_num < (len(dirs) - total_backups) and not self.progressBar.checkCancel()):
self._updateProgress(utils.getString(30054) + " " + dirs[remove_num][1])
utils.log("Removing backup " + dirs[remove_num][0])
if(dirs[remove_num][0].split('.')[-1] == 'zip'):
# this is a file, remove it that way
self.remote_vfs.rmfile(self.remote_vfs.clean_path(self.remote_base_path) + dirs[remove_num][0])
else:
self.remote_vfs.rmdir(self.remote_vfs.clean_path(self.remote_base_path) + dirs[remove_num][0] + "/")
remove_num = remove_num + 1
def _createValidationFile(self, dirList):
valInfo = {"name": "XBMC Backup Validation File", "xbmc_version": xbmc.getInfoLabel('System.BuildVersion'), "type": 0, "system_settings": [], "addons": []}
valDirs = []
# save list of file sets
for aDir in dirList:
valDirs.append({"name": aDir['name'], "path": aDir['source']})
valInfo['directories'] = valDirs
# dump all current Kodi settings
gui_settings = GuiSettingsManager()
valInfo['system_settings'] = gui_settings.backup()
# save all currently installed addons
valInfo['addons'] = gui_settings.list_addons()
vFile = xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup.val"), 'w')
vFile.write(json.dumps(valInfo))
vFile.write("")
vFile.close()
success = self._copyFile(self.xbmc_vfs, self.remote_vfs, xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup.val"), self.remote_vfs.root_path + "xbmcbackup.val")
# remove the validation file
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup.val"))
if(success):
# android requires a .nomedia file to not index the directory as media
if(not xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + ".nomedia"))):
nmFile = xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + ".nomedia"), 'w')
nmFile.close()
success = self._copyFile(self.xbmc_vfs, self.remote_vfs, xbmcvfs.translatePath(utils.data_dir() + ".nomedia"), self.remote_vfs.root_path + ".nomedia")
return success
def _checkValidationFile(self, path):
result = None
# copy the file and open it
self._copyFile(self.remote_vfs, self.xbmc_vfs, path + "xbmcbackup.val", xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
with xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup_restore.val"), 'r') as vFile:
jsonString = vFile.read()
# delete after checking
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "xbmcbackup_restore.val"))
try:
result = json.loads(jsonString)
if(xbmc.getInfoLabel('System.BuildVersion') != result['xbmc_version']):
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30085), "%s\n%s" % (utils.getString(30086), utils.getString(30044)))
if(not shouldContinue):
result = None
except ValueError:
# may fail on older archives
result = None
return result
def _createResumeBackupFile(self):
with xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"), 'w') as f:
f.write(self.restore_point)
def _readBackupConfig(self, aFile):
with xbmcvfs.File(xbmcvfs.translatePath(aFile), 'r') as f:
jsonString = f.read()
return json.loads(jsonString)
class FileManager:
not_dir = ['.zip', '.xsp', '.rar']
exclude_dir = []
root_dirs = []
pathSep = '/'
totalSize = 1
def __init__(self, vfs):
self.vfs = vfs
self.fileArray = []
self.exclude_dir = []
self.root_dirs = []
def walk(self):
for aDir in self.root_dirs:
self.addFile(xbmcvfs.translatePath(aDir['path']), True)
self.walkTree(xbmcvfs.translatePath(aDir['path']), aDir['recurse'])
def walkTree(self, directory, recurse=True):
if(utils.getSettingBool('verbose_logging')):
utils.log('walking ' + directory + ', recurse: ' + str(recurse))
if(directory[-1:] == '/' or directory[-1:] == '\\'):
directory = directory[:-1]
if(self.vfs.exists(directory + self.pathSep)):
dirs, files = self.vfs.listdir(directory)
if(recurse):
# create all the subdirs first
for aDir in dirs:
dirPath = xbmcvfs.validatePath(xbmcvfs.translatePath(directory + self.pathSep + aDir))
file_ext = aDir.split('.')[-1]
# check if directory is excluded
if(not any(dirPath.startswith(exDir) for exDir in self.exclude_dir)):
self.addFile(dirPath, True)
# catch for "non directory" type files
shouldWalk = True
for s in file_ext:
if(s in self.not_dir):
shouldWalk = False
if(shouldWalk):
self.walkTree(dirPath)
# copy all the files
for aFile in files:
filePath = xbmcvfs.translatePath(directory + self.pathSep + aFile)
self.addFile(filePath)
def addDir(self, dirMeta):
if(dirMeta['type'] == 'include'):
self.root_dirs.append({'path': dirMeta['path'], 'recurse': dirMeta['recurse']})
else:
self.excludeFile(xbmcvfs.translatePath(dirMeta['path']))
def addFile(self, filename, is_dir = False):
# write the full remote path name of this file
if(utils.getSettingBool('verbose_logging')):
utils.log("Add File: " + filename)
# get the file size
fSize = self.vfs.fileSize(filename)
self.totalSize = self.totalSize + fSize
self.fileArray.append({'file': filename, 'size': fSize, 'is_dir': is_dir})
def excludeFile(self, filename):
# remove trailing slash
if(filename[-1] == '/' or filename[-1] == '\\'):
filename = filename[:-1]
# write the full remote path name of this file
utils.log("Exclude File: " + filename)
self.exclude_dir.append(filename)
def getFiles(self):
result = self.fileArray
self.fileArray = []
self.root_dirs = []
self.exclude_dir = []
self.totalSize = 0
return result
def totalFiles(self):
return len(self.fileArray)
def fileSize(self):
return self.totalSize

View File

@@ -0,0 +1,301 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
from time import time, mktime
from datetime import datetime
from dateutil.relativedelta import relativedelta
search_re = re.compile(r'^([^-]+)-([^-/]+)(/(.*))?$')
only_int_re = re.compile(r'^\d+$')
any_int_re = re.compile(r'^\d+')
star_or_int_re = re.compile(r'^(\d+|\*)$')
__all__ = ('croniter',)
class croniter(object):
RANGES = (
(0, 59),
(0, 23),
(1, 31),
(1, 12),
(0, 6),
(0, 59)
)
DAYS = (
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
)
ALPHACONV = (
{ },
{ },
{ },
{ 'jan':1, 'feb':2, 'mar':3, 'apr':4, 'may':5, 'jun':6,
'jul':7, 'aug':8, 'sep':9, 'oct':10, 'nov':11, 'dec':12 },
{ 'sun':0, 'mon':1, 'tue':2, 'wed':3, 'thu':4, 'fri':5, 'sat':6 },
{ }
)
LOWMAP = (
{},
{},
{0: 1},
{0: 1},
{7: 0},
{},
)
bad_length = 'Exactly 5 or 6 columns has to be specified for iterator' \
'expression.'
def __init__(self, expr_format, start_time=time()):
if isinstance(start_time, datetime):
start_time = mktime(start_time.timetuple())
self.cur = start_time
self.exprs = expr_format.split()
if len(self.exprs) != 5 and len(self.exprs) != 6:
raise ValueError(self.bad_length)
expanded = []
for i, expr in enumerate(self.exprs):
e_list = expr.split(',')
res = []
while len(e_list) > 0:
e = e_list.pop()
t = re.sub(r'^\*(/.+)$', r'%d-%d\1' % (self.RANGES[i][0],
self.RANGES[i][1]),
str(e))
m = search_re.search(t)
if m:
(low, high, step) = m.group(1), m.group(2), m.group(4) or 1
if not any_int_re.search(low):
low = self.ALPHACONV[i][low.lower()]
if not any_int_re.search(high):
high = self.ALPHACONV[i][high.lower()]
if (not low or not high or int(low) > int(high)
or not only_int_re.search(str(step))):
raise ValueError("[%s] is not acceptable" %expr_format)
for j in range(int(low), int(high)+1):
if j % int(step) == 0:
e_list.append(j)
else:
if not star_or_int_re.search(t):
t = self.ALPHACONV[i][t.lower()]
try:
t = int(t)
except:
pass
if t in self.LOWMAP[i]:
t = self.LOWMAP[i][t]
if t != '*' and (int(t) < self.RANGES[i][0] or
int(t) > self.RANGES[i][1]):
raise ValueError("[%s] is not acceptable, out of range" % expr_format)
res.append(t)
res.sort()
expanded.append(['*'] if (len(res) == 1 and res[0] == '*') else res)
self.expanded = expanded
def get_next(self, ret_type=float):
return self._get_next(ret_type, is_prev=False)
def get_prev(self, ret_type=float):
return self._get_next(ret_type, is_prev=True)
def _get_next(self, ret_type=float, is_prev=False):
expanded = self.expanded[:]
if ret_type not in (float, datetime):
raise TypeError("Invalid ret_type, only 'float' or 'datetime' " \
"is acceptable.")
if expanded[2][0] != '*' and expanded[4][0] != '*':
bak = expanded[4]
expanded[4] = ['*']
t1 = self._calc(self.cur, expanded, is_prev)
expanded[4] = bak
expanded[2] = ['*']
t2 = self._calc(self.cur, expanded, is_prev)
if not is_prev:
result = t1 if t1 < t2 else t2
else:
result = t1 if t1 > t2 else t2
else:
result = self._calc(self.cur, expanded, is_prev)
self.cur = result
if ret_type == datetime:
result = datetime.fromtimestamp(result)
return result
def _calc(self, now, expanded, is_prev):
if is_prev:
nearest_method = self._get_prev_nearest
nearest_diff_method = self._get_prev_nearest_diff
sign = -1
else:
nearest_method = self._get_next_nearest
nearest_diff_method = self._get_next_nearest_diff
sign = 1
offset = len(expanded) == 6 and 1 or 60
dst = now = datetime.fromtimestamp(now + sign * offset)
day, month, year = dst.day, dst.month, dst.year
current_year = now.year
DAYS = self.DAYS
def proc_month(d):
if expanded[3][0] != '*':
diff_month = nearest_diff_method(month, expanded[3], 12)
days = DAYS[month - 1]
if month == 2 and self.is_leap(year) == True:
days += 1
reset_day = days if is_prev else 1
if diff_month != None and diff_month != 0:
if is_prev:
d += relativedelta(months=diff_month)
else:
d += relativedelta(months=diff_month, day=reset_day,
hour=0, minute=0, second=0)
return True, d
return False, d
def proc_day_of_month(d):
if expanded[2][0] != '*':
days = DAYS[month - 1]
if month == 2 and self.is_leap(year) == True:
days += 1
diff_day = nearest_diff_method(d.day, expanded[2], days)
if diff_day != None and diff_day != 0:
if is_prev:
d += relativedelta(days=diff_day)
else:
d += relativedelta(days=diff_day, hour=0, minute=0, second=0)
return True, d
return False, d
def proc_day_of_week(d):
if expanded[4][0] != '*':
diff_day_of_week = nearest_diff_method(d.isoweekday() % 7, expanded[4], 7)
if diff_day_of_week != None and diff_day_of_week != 0:
if is_prev:
d += relativedelta(days=diff_day_of_week)
else:
d += relativedelta(days=diff_day_of_week, hour=0, minute=0, second=0)
return True, d
return False, d
def proc_hour(d):
if expanded[1][0] != '*':
diff_hour = nearest_diff_method(d.hour, expanded[1], 24)
if diff_hour != None and diff_hour != 0:
if is_prev:
d += relativedelta(hours = diff_hour)
else:
d += relativedelta(hours = diff_hour, minute=0, second=0)
return True, d
return False, d
def proc_minute(d):
if expanded[0][0] != '*':
diff_min = nearest_diff_method(d.minute, expanded[0], 60)
if diff_min != None and diff_min != 0:
if is_prev:
d += relativedelta(minutes = diff_min)
else:
d += relativedelta(minutes = diff_min, second=0)
return True, d
return False, d
def proc_second(d):
if len(expanded) == 6:
if expanded[5][0] != '*':
diff_sec = nearest_diff_method(d.second, expanded[5], 60)
if diff_sec != None and diff_sec != 0:
dst += relativedelta(seconds = diff_sec)
return True, d
else:
d += relativedelta(second = 0)
return False, d
if is_prev:
procs = [proc_second,
proc_minute,
proc_hour,
proc_day_of_week,
proc_day_of_month,
proc_month]
else:
procs = [proc_month,
proc_day_of_month,
proc_day_of_week,
proc_hour,
proc_minute,
proc_second]
while abs(year - current_year) <= 1:
next = False
for proc in procs:
(changed, dst) = proc(dst)
if changed:
next = True
break
if next:
continue
return mktime(dst.timetuple())
raise "failed to find prev date"
def _get_next_nearest(self, x, to_check):
small = [item for item in to_check if item < x]
large = [item for item in to_check if item >= x]
large.extend(small)
return large[0]
def _get_prev_nearest(self, x, to_check):
small = [item for item in to_check if item <= x]
large = [item for item in to_check if item > x]
small.reverse()
large.reverse()
small.extend(large)
return small[0]
def _get_next_nearest_diff(self, x, to_check, range_val):
for i, d in enumerate(to_check):
if d >= x:
return d - x
return to_check[0] - x + range_val
def _get_prev_nearest_diff(self, x, to_check, range_val):
candidates = to_check[:]
candidates.reverse()
for d in candidates:
if d <= x:
return d - x
return (candidates[0]) - x - range_val
def is_leap(self, year):
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
return True
else:
return False

View File

@@ -0,0 +1,31 @@
from . import utils as utils
class ZipExtractor:
def extract(self, zipFile, outLoc, progressBar):
utils.log("extracting zip archive")
result = True # result is true unless we fail
# update the progress bar
progressBar.updateProgress(0, utils.getString(30100))
# list the files
fileCount = float(len(zipFile.listFiles()))
currentFile = 0
try:
for aFile in zipFile.listFiles():
# update the progress bar
currentFile += 1
progressBar.updateProgress(int((currentFile / fileCount) * 100), utils.getString(30100))
# extract the file
zipFile.extract(aFile, outLoc)
except Exception:
utils.log("Error extracting file")
result = False
return result

View File

@@ -0,0 +1,55 @@
import json
import xbmc
from . import utils as utils
class GuiSettingsManager:
filename = 'kodi_settings.json'
systemSettings = None
def __init__(self):
# get all of the current Kodi settings
json_response = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "id":1, "method":"Settings.GetSettings","params":{"level":"expert"}}'))
self.systemSettings = json_response['result']['settings']
def list_addons(self):
# list all currently installed addons
addons = json.loads(xbmc.executeJSONRPC('{"jsonrpc":"2.0", "method":"Addons.GetAddons", "params":{"properties":["version","author"]}, "id":2}'))
return addons['result']['addons']
def backup(self):
utils.log('Backing up Kodi settings')
# return all current settings
return self.systemSettings
def restore(self, restoreSettings):
utils.log('Restoring Kodi settings')
updateJson = {"jsonrpc": "2.0", "id": 1, "method": "Settings.SetSettingValue", "params": {"setting": "", "value": ""}}
# create a setting=value dict of the current settings
settingsDict = {}
for aSetting in self.systemSettings:
# ignore action types, no value
if(aSetting['type'] != 'action'):
settingsDict[aSetting['id']] = aSetting['value']
restoreCount = 0
for aSetting in restoreSettings:
# Ensure key exists before referencing
if(aSetting['id'] in settingsDict.keys()):
# only update a setting if its different than the current (action types have no value)
if(aSetting['type'] != 'action' and settingsDict[aSetting['id']] != aSetting['value']):
if(utils.getSettingBool('verbose_logging')):
utils.log('%s different than current: %s' % (aSetting['id'], str(aSetting['value'])))
updateJson['params']['setting'] = aSetting['id']
updateJson['params']['value'] = aSetting['value']
xbmc.executeJSONRPC(json.dumps(updateJson))
restoreCount = restoreCount + 1
utils.log('Update %d settings' % restoreCount)

View File

@@ -0,0 +1,54 @@
import xbmcgui
from . import utils as utils
class BackupProgressBar:
NONE = 2
DIALOG = 0
BACKGROUND = 1
mode = 2
progressBar = None
override = False
def __init__(self, progressOverride):
self.override = progressOverride
# check if we should use the progress bar
if(utils.getSettingInt('progress_mode') != 2):
# check if background or normal
if(utils.getSettingInt('progress_mode') == 0 and not self.override):
self.mode = self.DIALOG
self.progressBar = xbmcgui.DialogProgress()
else:
self.mode = self.BACKGROUND
self.progressBar = xbmcgui.DialogProgressBG()
def create(self, heading, message):
if(self.mode != self.NONE):
self.progressBar.create(heading, message)
def updateProgress(self, percent, message=None):
# update the progress bar
if(self.mode != self.NONE):
if(message is not None):
# need different calls for dialog and background bars
if(self.mode == self.DIALOG):
self.progressBar.update(percent, message)
else:
self.progressBar.update(percent, message=message)
else:
self.progressBar.update(percent)
def checkCancel(self):
result = False
if(self.mode == self.DIALOG):
result = self.progressBar.iscanceled()
return result
def close(self):
if(self.mode != self.NONE):
self.progressBar.close()

View File

@@ -0,0 +1,194 @@
import time
from datetime import datetime
import xbmc
import xbmcvfs
import xbmcgui
from . import utils as utils
from resources.lib.croniter import croniter
from resources.lib.backup import XbmcBackup
UPGRADE_INT = 2 # to keep track of any upgrade notifications
class BackupScheduler:
monitor = None
enabled = False
next_run = 0
next_run_path = None
restore_point = None
def __init__(self):
self.monitor = UpdateMonitor(update_method=self.settingsChanged)
self.enabled = utils.getSettingBool("enable_scheduler")
self.next_run_path = xbmcvfs.translatePath(utils.data_dir()) + 'next_run.txt'
# display upgrade messages if they exist
if(utils.getSettingInt('upgrade_notes') < UPGRADE_INT):
xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30132))
utils.setSetting('upgrade_notes', str(UPGRADE_INT))
# check if a backup should be resumed
resumeRestore = self._resumeCheck()
if(resumeRestore):
restore = XbmcBackup()
restore.selectRestore(self.restore_point)
# skip the advanced settings check
restore.skipAdvanced()
restore.restore()
if(self.enabled):
# sleep for 2 minutes so Kodi can start and time can update correctly
xbmc.Monitor().waitForAbort(120)
nr = 0
if(xbmcvfs.exists(self.next_run_path)):
with xbmcvfs.File(self.next_run_path) as fh:
try:
# check if we saved a run time from the last run
nr = float(fh.read())
except ValueError:
nr = 0
# if we missed and the user wants to play catch-up
if(0 < nr <= time.time() and utils.getSettingBool('schedule_miss')):
utils.log("scheduled backup was missed, doing it now...")
progress_mode = utils.getSettingInt('progress_mode')
if(progress_mode == 0):
progress_mode = 1 # Kodi just started, don't block it with a foreground progress bar
self.doScheduledBackup(progress_mode)
self.setup()
def setup(self):
# scheduler was turned on, find next run time
utils.log("scheduler enabled, finding next run time")
self.findNextRun(time.time())
def start(self):
while(not self.monitor.abortRequested()):
if(self.enabled):
# scheduler is still on
now = time.time()
if(self.next_run <= now):
progress_mode = utils.getSettingInt('progress_mode')
self.doScheduledBackup(progress_mode)
# check if we should shut the computer down
if(utils.getSettingBool("cron_shutdown")):
# wait 10 seconds to make sure all backup processes and files are completed
time.sleep(10)
xbmc.executebuiltin('ShutDown()')
else:
# find the next run time like normal
self.findNextRun(now)
xbmc.sleep(500)
# delete monitor to free up memory
del self.monitor
def doScheduledBackup(self, progress_mode):
if(progress_mode != 2):
utils.showNotification(utils.getString(30053))
backup = XbmcBackup()
if(backup.remoteConfigured()):
if(utils.getSettingInt('progress_mode') in [0, 1]):
backup.backup(True)
else:
backup.backup(False)
# check if this is a "one-off"
if(utils.getSettingInt("schedule_interval") == 0):
# disable the scheduler after this run
self.enabled = False
utils.setSetting('enable_scheduler', 'false')
else:
utils.showNotification(utils.getString(30045))
def findNextRun(self, now):
progress_mode = utils.getSettingInt('progress_mode')
# find the cron expression and get the next run time
cron_exp = self.parseSchedule()
cron_ob = croniter(cron_exp, datetime.fromtimestamp(now))
new_run_time = cron_ob.get_next(float)
if(new_run_time != self.next_run):
self.next_run = new_run_time
utils.log("scheduler will run again on " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run), ['dateshort', 'time']))
# write the next time to a file
with xbmcvfs.File(self.next_run_path, 'w') as fh:
fh.write(str(self.next_run))
# only show when not in silent mode
if(progress_mode != 2):
utils.showNotification(utils.getString(30081) + " " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run), ['dateshort', 'time']))
def settingsChanged(self):
current_enabled = utils.getSettingBool("enable_scheduler")
if(current_enabled and not self.enabled):
# scheduler was just turned on
self.enabled = current_enabled
self.setup()
elif (not current_enabled and self.enabled):
# schedule was turn off
self.enabled = current_enabled
if(self.enabled):
# always recheck the next run time after an update
self.findNextRun(time.time())
def parseSchedule(self):
schedule_type = utils.getSettingInt("schedule_interval")
cron_exp = utils.getSetting("cron_schedule")
hour_of_day = utils.getSetting("schedule_time")
hour_of_day = int(hour_of_day[0:2])
if(schedule_type == 0 or schedule_type == 1):
# every day
cron_exp = "0 " + str(hour_of_day) + " * * *"
elif(schedule_type == 2):
# once a week
day_of_week = utils.getSetting("day_of_week")
cron_exp = "0 " + str(hour_of_day) + " * * " + day_of_week
elif(schedule_type == 3):
# first day of month
cron_exp = "0 " + str(hour_of_day) + " 1 * *"
return cron_exp
def _resumeCheck(self):
shouldContinue = False
if(xbmcvfs.exists(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"))):
rFile = xbmcvfs.File(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"), 'r')
self.restore_point = rFile.read()
rFile.close()
xbmcvfs.delete(xbmcvfs.translatePath(utils.data_dir() + "resume.txt"))
shouldContinue = xbmcgui.Dialog().yesno(utils.getString(30042), "%s\n%s" % (utils.getString(30043), utils.getString(30044)))
return shouldContinue
class UpdateMonitor(xbmc.Monitor):
update_method = None
def __init__(self, *args, **kwargs):
xbmc.Monitor.__init__(self)
self.update_method = kwargs['update_method']
def onSettingsChanged(self):
self.update_method()

View File

@@ -0,0 +1,12 @@
# this is duplicated in snipppets of code from all over the web, credit to no one
# in particular - to all those that have gone before me!
from future.moves.urllib.request import urlopen
def shorten(aUrl):
tinyurl = 'http://tinyurl.com/api-create.php?url='
req = urlopen(tinyurl + aUrl)
data = req.read()
# should be a tiny url
return data

View File

@@ -0,0 +1,71 @@
import xbmc
import xbmcgui
import xbmcaddon
import xbmcvfs
__addon_id__ = 'script.xbmcbackup'
__Addon = xbmcaddon.Addon(__addon_id__)
def data_dir():
return __Addon.getAddonInfo('profile')
def addon_dir():
return __Addon.getAddonInfo('path')
def openSettings():
__Addon.openSettings()
def log(message, loglevel=xbmc.LOGDEBUG):
xbmc.log(__addon_id__ + "-" + __Addon.getAddonInfo('version') + ": " + message, level=loglevel)
def showNotification(message):
xbmcgui.Dialog().notification(getString(30010), message, time=4000, icon=xbmcvfs.translatePath(__Addon.getAddonInfo('path') + "/resources/images/icon.png"))
def getSetting(name):
return __Addon.getSetting(name)
def getSettingStringStripped(name):
return __Addon.getSettingString(name).strip()
def getSettingBool(name):
return bool(__Addon.getSettingBool(name))
def getSettingInt(name):
return __Addon.getSettingInt(name)
def setSetting(name, value):
__Addon.setSetting(name, value)
def getString(string_id):
return __Addon.getLocalizedString(string_id)
def getRegionalTimestamp(date_time, dateformat=['dateshort']):
result = ''
for aFormat in dateformat:
result = result + ("%s " % date_time.strftime(xbmc.getRegion(aFormat)))
return result.strip()
def diskString(fSize):
# convert a size in kilobytes to the best possible match and return as a string
fSize = float(fSize)
i = 0
sizeNames = ['KB', 'MB', 'GB', 'TB']
while(fSize > 1024):
fSize = fSize / 1024
i = i + 1
return "%0.2f%s" % (fSize, sizeNames[i])

View File

@@ -0,0 +1,289 @@
from __future__ import unicode_literals
import zipfile
import os.path
import sys
import xbmcvfs
import xbmcgui
from dropbox import dropbox
from . import utils as utils
from dropbox.files import WriteMode, CommitInfo, UploadSessionCursor
from . authorizers import DropboxAuthorizer
class Vfs:
root_path = None
def __init__(self, rootString):
self.set_root(rootString)
def clean_path(self, path):
# fix slashes
path = path.replace("\\", "/")
# check if trailing slash is included
if(path[-1:] != '/'):
path = path + '/'
return path
def set_root(self, rootString):
old_root = self.root_path
self.root_path = self.clean_path(rootString)
# return the old root
return old_root
def listdir(self, directory):
return {}
def mkdir(self, directory):
return True
def put(self, source, dest):
return True
def rmdir(self, directory):
return True
def rmfile(self, aFile):
return True
def exists(self, aFile):
return True
def rename(self, aFile, newName):
return True
def cleanup(self):
return True
def fileSize(self, filename):
return 0 # result should be in KB
class XBMCFileSystem(Vfs):
def listdir(self, directory):
return xbmcvfs.listdir(directory)
def mkdir(self, directory):
return xbmcvfs.mkdir(xbmcvfs.translatePath(directory))
def put(self, source, dest):
return xbmcvfs.copy(xbmcvfs.translatePath(source), xbmcvfs.translatePath(dest))
def rmdir(self, directory):
return xbmcvfs.rmdir(directory, force=True) # use force=True to make sure it works recursively
def rmfile(self, aFile):
return xbmcvfs.delete(aFile)
def rename(self, aFile, newName):
return xbmcvfs.rename(aFile, newName)
def exists(self, aFile):
return xbmcvfs.exists(aFile)
def fileSize(self, filename):
with xbmcvfs.File(filename) as f:
result = f.size() / 1024 # bytes to kilobytes
return result
class ZipFileSystem(Vfs):
zip = None
def __init__(self, rootString, mode):
self.root_path = ""
self.zip = zipfile.ZipFile(rootString, mode=mode, compression=zipfile.ZIP_DEFLATED, allowZip64=True)
def listdir(self, directory):
return [[], []]
def mkdir(self, directory):
# self.zip.write(directory[len(self.root_path):])
return False
def put(self, source, dest):
aFile = xbmcvfs.File(xbmcvfs.translatePath(source), 'r')
self.zip.writestr(dest, aFile.readBytes())
return True
def rmdir(self, directory):
return False
def exists(self, aFile):
return False
def cleanup(self):
self.zip.close()
def extract(self, aFile, path):
# extract zip file to path
self.zip.extract(aFile, path)
def listFiles(self):
return self.zip.infolist()
class DropboxFileSystem(Vfs):
MAX_CHUNK = 50 * 1000 * 1000 # dropbox uses 150, reduced to 50 for small mem systems
client = None
APP_KEY = ''
APP_SECRET = ''
def __init__(self, rootString):
self.set_root(rootString)
authorizer = DropboxAuthorizer()
if(authorizer.isAuthorized()):
self.client = authorizer.getClient()
else:
# tell the user to go back and run the authorizer
xbmcgui.Dialog().ok(utils.getString(30010), utils.getString(30105))
sys.exit()
def listdir(self, directory):
directory = self._fix_slashes(directory)
if(self.client is not None and self.exists(directory)):
files = []
dirs = []
metadata = self.client.files_list_folder(directory)
for aFile in metadata.entries:
if(isinstance(aFile, dropbox.files.FolderMetadata)):
dirs.append(aFile.name)
else:
files.append(aFile.name)
return [dirs, files]
else:
return [[], []]
def mkdir(self, directory):
directory = self._fix_slashes(directory)
if(self.client is not None):
# sort of odd but always return true, folder create is implicit with file upload
return True
else:
return False
def rmdir(self, directory):
directory = self._fix_slashes(directory)
if(self.client is not None and self.exists(directory)):
# dropbox is stupid and will refuse to do this sometimes, need to delete recursively
dirs, files = self.listdir(directory)
for aDir in dirs:
self.rmdir(aDir)
# finally remove the root directory
self.client.files_delete(directory)
return True
else:
return False
def rmfile(self, aFile):
aFile = self._fix_slashes(aFile)
if(self.client is not None and self.exists(aFile)):
self.client.files_delete(aFile)
return True
else:
return False
def exists(self, aFile):
aFile = self._fix_slashes(aFile)
if(self.client is not None):
# can't list root metadata
if(aFile == ''):
return True
try:
self.client.files_get_metadata(aFile)
# if we make it here the file does exist
return True
except:
return False
else:
return False
def put(self, source, dest, retry=True):
dest = self._fix_slashes(dest)
if(self.client is not None):
# open the file and get its size
f = open(source, 'rb')
f_size = os.path.getsize(source)
try:
if(f_size < self.MAX_CHUNK):
# use the regular upload
self.client.files_upload(f.read(), dest, mode=WriteMode('overwrite'))
else:
# start the upload session
upload_session = self.client.files_upload_session_start(f.read(self.MAX_CHUNK))
upload_cursor = UploadSessionCursor(upload_session.session_id, f.tell())
while(f.tell() < f_size):
# check if we should finish the upload
if((f_size - f.tell()) <= self.MAX_CHUNK):
# upload and close
self.client.files_upload_session_finish(f.read(self.MAX_CHUNK), upload_cursor, CommitInfo(dest, mode=WriteMode('overwrite')))
else:
# upload a part and store the offset
self.client.files_upload_session_append_v2(f.read(self.MAX_CHUNK), upload_cursor)
upload_cursor.offset = f.tell()
# if no errors we're good!
return True
except Exception as anError:
utils.log(str(anError))
# if we have an exception retry
if(retry):
return self.put(source, dest, False)
else:
# tried once already, just quit
return False
else:
return False
def fileSize(self, filename):
result = 0
aFile = self._fix_slashes(filename)
if(self.client is not None):
metadata = self.client.files_get_metadata(aFile)
result = metadata.size / 1024 # bytes to KB
return result
def get_file(self, source, dest):
if(self.client is not None):
# write the file locally
self.client.files_download_to_file(dest, source)
return True
else:
return False
def _fix_slashes(self, filename):
result = filename.replace('\\', '/')
# root needs to be a blank string
if(result == '/'):
result = ""
# if dir ends in slash, remove it
if(result[-1:] == "/"):
result = result[:-1]
return result