def extract(self): '''extract the snapfile into the snapdirectory @raises - MissingFileError if the snapfile does not exist ''' # decrypt the file if we've set a key if not snap.osregistry.OS.is_windows() and self.encryption_key != None: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Decyrpting snapfile") Crypto.decrypt_file(self.encryption_key, self.snapfile, self.snapfile + ".dec") FileManager.mv(self.snapfile + ".dec", self.snapfile) # open the tarball tarball = tarfile.open(self.snapfile) # temp store the working directory, before changing to the snapdirectory cwd = os.getcwd() os.chdir(self.snapdirectory) # extract files from it for tarinfo in tarball: tarball.extract(tarinfo) # close it out tarball.close() if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message("Snapfile " + self.snapfile + " opened") # restore the working directory os.chdir(cwd)
def extract(self): '''extract the snapfile into the snapdirectory @raises - MissingFileError if the snapfile does not exist ''' # decrypt the file if we've set a key if not snap.osregistry.OS.is_windows() and self.encryption_key != None: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Decyrpting snapfile") Crypto.decrypt_file(self.encryption_key, self.snapfile, self.snapfile + ".dec") FileManager.mv(self.snapfile + ".dec", self.snapfile) # open the tarball tarball = tarfile.open(self.snapfile) # temp store the working directory, before changing to the snapdirectory cwd = os.getcwd() os.chdir(self.snapdirectory) # extract files from it for tarinfo in tarball: tarball.extract(tarinfo) # close it out tarball.close() if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message("Snapfile " + self.snapfile + " opened") # restore the working directory os.chdir(cwd)
def compress(self): '''create a snapfile from the snapdirectory @raises - MissingFileError - if the snapfile cannot be created ''' # if snapfile == '-' write to stdout snapfileo = None if self.snapfile == '-': snapfileo = sys.stdout else: snapfileo = open(self.snapfile, 'w') # create the tarball tarball = tarfile.open(fileobj=snapfileo, mode="w:gz") # temp store the working directory, before changing to the snapdirectory cwd = os.getcwd() os.chdir(self.snapdirectory) seperator = snap.osregistry.OS.get_path_seperator() # copy directories into snapfile for sdir in FileManager.get_all_subdirectories(os.getcwd(), recursive=True): partialpath = sdir.replace(self.snapdirectory + seperator, "") tarball.addfile(self.__prepare_file_for_tarball(tarball, sdir, partialpath)) # copy files into snapfile for tfile in FileManager.get_all_files(include=[os.getcwd()]): partialpath = tfile.replace(self.snapdirectory + seperator, "") if os.path.exists(tfile): tarball.addfile(self.__prepare_file_for_tarball(tarball, tfile, partialpath), file(tfile, 'rb')) # finish up tarball creation tarball.close() if self.snapfile != '-': snapfileo.close() # encrypt the snapshot if we've set a key if not snap.osregistry.OS.is_windows() and self.encryption_key != None: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Encyrpting snapfile") Crypto.encrypt_file(self.encryption_key, self.snapfile, self.snapfile + ".enc") FileManager.mv(self.snapfile + ".enc", self.snapfile) if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message("Snapfile " + self.snapfile + " created") # restore the working directory os.chdir(cwd)
def testMv(self): temp_source_file_path = os.path.join(os.path.dirname(__file__), "data", "temp-source-file") temp_dest_file_path = os.path.join(os.path.dirname(__file__), "data", "temp-dest-file") f = open(temp_source_file_path, 'w') f.write("foo") f.close() self.assertTrue(os.path.isfile(temp_source_file_path)) FileManager.mv(temp_source_file_path, temp_dest_file_path) self.assertFalse(os.path.isfile(temp_source_file_path)) self.assertTrue(os.path.isfile(temp_dest_file_path)) os.remove(temp_dest_file_path)
def testMv(self): temp_source_file_path = os.path.join(os.path.dirname(__file__), "data", "temp-source-file") temp_dest_file_path = os.path.join(os.path.dirname(__file__), "data", "temp-dest-file") f = open(temp_source_file_path, 'w') f.write("foo") f.close() self.assertTrue(os.path.isfile(temp_source_file_path)) FileManager.mv(temp_source_file_path, temp_dest_file_path) self.assertFalse(os.path.isfile(temp_source_file_path)) self.assertTrue(os.path.isfile(temp_dest_file_path)) os.remove(temp_dest_file_path)
def compress(self): '''create a snapfile from the snapdirectory @raises - MissingFileError - if the snapfile cannot be created ''' # create the tarball tarball = tarfile.open(self.snapfile, "w:gz") # temp store the working directory, before changing to the snapdirectory cwd = os.getcwd() os.chdir(self.snapdirectory) seperator = snap.osregistry.OS.get_path_seperator() # copy directories into snapfile for sdir in FileManager.get_all_subdirectories(os.getcwd(), recursive=True): partialpath = sdir.replace(self.snapdirectory + seperator, "") tarball.addfile( self.__prepare_file_for_tarball(tarball, sdir, partialpath)) # copy files into snapfile for tfile in FileManager.get_all_files(include=[os.getcwd()]): partialpath = tfile.replace(self.snapdirectory + seperator, "") if os.path.exists(tfile): tarball.addfile( self.__prepare_file_for_tarball(tarball, tfile, partialpath), file(tfile, 'rb')) # finish up tarball creation tarball.close() # encrypt the snapshot if we've set a key if not snap.osregistry.OS.is_windows() and self.encryption_key != None: if snap.config.options.log_level_at_least('verbose'): snap.callback.snapcallback.message("Encyrpting snapfile") Crypto.encrypt_file(self.encryption_key, self.snapfile, self.snapfile + ".enc") FileManager.mv(self.snapfile + ".enc", self.snapfile) if snap.config.options.log_level_at_least('normal'): snap.callback.snapcallback.message("Snapfile " + self.snapfile + " created") # restore the working directory os.chdir(cwd)