--- /dev/null
+
+from pcsbackupinfo import *
+import zipfile
+import os
+import xml.dom.minidom
+
+
+def copyOssoBackupConfigFiles(destination, mountPath):
+ """ Copy all osso-backup .conf files to the given path. The device must be
+ already mounted in the mountPath.
+
+ Attributes:
+ - String mountPath - Path of the folder where the device is mounted
+ - String destination - Destination folder path where config files should be
+ copied to.
+
+ """
+ os.system("cp %s/etc/osso-backup/applications/*.conf %s" %
+ (mountPath, destination))
+
+
+def mountDevice(user, ip, path):
+ # Mount device file system using sshfs in the given path
+ try:
+ if not os.path.exists(path):
+ createFolder(path)
+ os.system('sshfs %s@%s:/ %s' % (user, ip, path))
+ except:
+ raise Exception("Error while mounting device file system")
+
+
+def unmountDevice(path):
+ try:
+ os.system('fusermount -uz %s' % path)
+ except:
+ raise Exception("Error while unmounting device file system")
+
+
+def createFolder(complete_path):
+ if not os.path.exists(complete_path):
+ os.makedirs(complete_path)
+
+ # FIXME
+ return True
+
+
+def removePath(complete_path):
+ for entry in os.listdir(complete_path):
+ if os.path.isdir(entry):
+ removePath(os.path.join(complete_path, entry))
+ else:
+ os.remove(os.path.join(complete_path, entry))
+ os.rmdir(complete_path)
+
+
+def getDeviceBackupList(mountPoint):
+ """This function return a list of backupInfo objects for each backup found
+ in the mount point.
+
+ """
+ deviceBackups = []
+ mmc1 = '%s/media/mmc1/backups' % mountPoint
+ mmc2 = '%s/media/mmc2/backups' % mountPoint
+
+ if os.path.exists(mmc1):
+ deviceBackups += _getDeviceBackupsInfo(mmc1)
+ if os.path.exists(mmc2):
+ deviceBackups += _getDeviceBackupsInfo(mmc2)
+
+ return deviceBackups
+
+
+def copy(original, destination):
+ original = original.replace(" ", "\ ")
+ destination = destination.replace(" ", "\ ")
+ createFolder(destination)
+ os.system("cp %s %s" % (original, destination))
+
+
+def getSize(path):
+ if not os.path.exists(path):
+ return False
+ if os.path.isdir(path):
+ files_and_folders = os.listdir(path)
+ sum_size = 0
+ for entry in files_and_folders:
+ if os.path.isdir(os.path.join(path, entry)):
+ sum_size += getSize(os.path.join(path, entry))
+ else:
+ try:
+ sum_size += os.stat(os.path.join(path, entry)).st_size
+ except:
+ sum_size += 1
+ return sum_size
+ else:
+ return os.stat(path).st_size
+
+
+def getBackupFilesPath(backupPath):
+ dic = {}
+ for entry in os.listdir(backupPath):
+ if entry.endswith(".zip"):
+ zip = openZip(os.path.join(backupPath, entry))
+ dic[entry.replace(".zip", "")] = zip.namelist()
+ return dic
+
+
+def getBackupCategories(backupInfo):
+ backupPath = str(backupInfo.path)
+ if not backupInfo.fromDevice:
+ backupPath = os.path.join(backupPath, str(backupInfo._name))
+ categoriesList = []
+ for entry in os.listdir(backupPath):
+ if entry.endswith(".zip"):
+ categoriesList.append(entry.replace(".zip", ""))
+ return categoriesList
+
+
+def writeBackupFilesPath(paths_dictionary, file_path):
+ try:
+ file = open(file_path, "w")
+ except:
+ return False
+ for category in paths_dictionary.keys():
+ file.write("[" + category + "]\n")
+ for path in paths_dictionary[category]:
+ file.write(path + "\n")
+
+ file.close()
+
+def openZip(zipPath, mode="r"):
+ """ Open a .zip file using python ZipFile library.
+
+ Attributes:
+ String zipPath - The directory path to the file
+ String mode - "w" to open file for writting.
+ "a" to open file for appending.
+ "r" to open file for reading.
+
+ """
+ try:
+ zip = zipfile.ZipFile(zipPath, mode)
+ return zip
+ except zipfile.BadZipfile, msg:
+ raise IOError("Problem while opening %s: %s" % (zipPath, msg))
+ except:
+ raise
+
+def closeZip(zipfile):
+ zipfile.close()
+
+def zip(zipfile, path):
+ # Compress the file in the given path to the zipfile
+ try:
+ zipfile.write(path.encode('UTF'))
+ return True
+ except:
+ return False
+
+def rebootDevice(deviceIp):
+ return os.system("ssh root@%s reboot" % deviceIp) == 0
+
+
+def _parseMetadata(metadata_path):
+ document = xml.dom.minidom.parse(metadata_path)
+ node = document.getElementsByTagName("size")[0]
+ size = int(str(node.firstChild.nodeValue))
+ node = document.getElementsByTagName("timestamp")[0]
+ objDate = datetime.fromtimestamp(float(str(node.firstChild.nodeValue)))
+ return size, str(objDate)
+
+def _getDeviceBackupsInfo(memoryCardPath):
+ deviceBackups = []
+ for backup in os.listdir(memoryCardPath):
+ temporaryFolder = os.path.join(memoryCardPath, backup)
+ if os.path.isdir(temporaryFolder):
+ metadataPath = os.path.join(temporaryFolder,'backup.metadata')
+ if os.path.exists(metadataPath):
+ size, date = _parseMetadata(metadataPath)
+ backupInfo = PcsBackupInfo(backup, temporaryFolder, size)
+ backupInfo.setDate(date)
+ deviceBackups.append(backupInfo)
+ return deviceBackups
+