2 from pcsbackupinfo import *
8 def copyOssoBackupConfigFiles(destination, mountPath):
9 """ Copy all osso-backup .conf files to the given path. The device must be
10 already mounted in the mountPath.
13 - String mountPath - Path of the folder where the device is mounted
14 - String destination - Destination folder path where config files should be
18 os.system("cp %s/etc/osso-backup/applications/*.conf %s" %
19 (mountPath, destination))
22 def mountDevice(user, ip, path):
23 # Mount device file system using sshfs in the given path
25 if not os.path.exists(path):
27 os.system('sshfs %s@%s:/ %s' % (user, ip, path))
29 raise Exception("Error while mounting device file system")
32 def unmountDevice(path):
34 os.system('fusermount -uz %s' % path)
36 raise Exception("Error while unmounting device file system")
39 def createFolder(complete_path):
40 if not os.path.exists(complete_path):
41 os.makedirs(complete_path)
47 def removePath(complete_path):
48 for entry in os.listdir(complete_path):
49 if os.path.isdir(entry):
50 removePath(os.path.join(complete_path, entry))
52 os.remove(os.path.join(complete_path, entry))
53 os.rmdir(complete_path)
56 def getDeviceBackupList(mountPoint):
57 """This function return a list of backupInfo objects for each backup found
62 mmc1 = '%s/media/mmc1/backups' % mountPoint
63 mmc2 = '%s/media/mmc2/backups' % mountPoint
65 if os.path.exists(mmc1):
66 deviceBackups += _getDeviceBackupsInfo(mmc1)
67 if os.path.exists(mmc2):
68 deviceBackups += _getDeviceBackupsInfo(mmc2)
73 def copy(original, destination):
74 original = original.replace(" ", "\ ")
75 destination = destination.replace(" ", "\ ")
76 createFolder(destination)
77 os.system("cp %s %s" % (original, destination))
81 if not os.path.exists(path):
83 if os.path.isdir(path):
84 files_and_folders = os.listdir(path)
86 for entry in files_and_folders:
87 if os.path.isdir(os.path.join(path, entry)):
88 sum_size += getSize(os.path.join(path, entry))
91 sum_size += os.stat(os.path.join(path, entry)).st_size
96 return os.stat(path).st_size
99 def getBackupFilesPath(backupPath):
101 for entry in os.listdir(backupPath):
102 if entry.endswith(".zip"):
103 zip = openZip(os.path.join(backupPath, entry))
104 dic[entry.replace(".zip", "")] = zip.namelist()
108 def getBackupCategories(backupInfo):
109 backupPath = str(backupInfo.path)
110 if not backupInfo.fromDevice:
111 backupPath = os.path.join(backupPath, str(backupInfo._name))
113 for entry in os.listdir(backupPath):
114 if entry.endswith(".zip"):
115 categoriesList.append(entry.replace(".zip", ""))
116 return categoriesList
119 def writeBackupFilesPath(paths_dictionary, file_path):
121 file = open(file_path, "w")
124 for category in paths_dictionary.keys():
125 file.write("[" + category + "]\n")
126 for path in paths_dictionary[category]:
127 file.write(path + "\n")
131 def openZip(zipPath, mode="r"):
132 """ Open a .zip file using python ZipFile library.
135 String zipPath - The directory path to the file
136 String mode - "w" to open file for writting.
137 "a" to open file for appending.
138 "r" to open file for reading.
142 zip = zipfile.ZipFile(zipPath, mode)
144 except zipfile.BadZipfile, msg:
145 raise IOError("Problem while opening %s: %s" % (zipPath, msg))
149 def closeZip(zipfile):
152 def zip(zipfile, path):
153 # Compress the file in the given path to the zipfile
155 zipfile.write(path.encode('UTF'))
160 def rebootDevice(deviceIp):
161 return os.system("ssh root@%s reboot" % deviceIp) == 0
164 def _parseMetadata(metadata_path):
165 document = xml.dom.minidom.parse(metadata_path)
166 node = document.getElementsByTagName("size")[0]
167 size = int(str(node.firstChild.nodeValue))
168 node = document.getElementsByTagName("timestamp")[0]
169 objDate = datetime.fromtimestamp(float(str(node.firstChild.nodeValue)))
170 return size, str(objDate)
172 def _getDeviceBackupsInfo(memoryCardPath):
174 for backup in os.listdir(memoryCardPath):
175 temporaryFolder = os.path.join(memoryCardPath, backup)
176 if os.path.isdir(temporaryFolder):
177 metadataPath = os.path.join(temporaryFolder,'backup.metadata')
178 if os.path.exists(metadataPath):
179 size, date = _parseMetadata(metadataPath)
180 backupInfo = PcsBackupInfo(backup, temporaryFolder, size)
181 backupInfo.setDate(date)
182 deviceBackups.append(backupInfo)