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