def testGetAllSubdirectories(self): data_path = os.path.join(os.path.dirname(__file__), "data") subdirs = FileManager.get_all_subdirectories(data_path, recursive=True) self.assertIn(os.path.join(data_path, "tmp"), subdirs) self.assertIn(os.path.join(data_path, "tmp", "subdir"), subdirs) subdirs = FileManager.get_all_subdirectories(data_path, recursive=False) self.assertIn(os.path.join(data_path, "tmp"), subdirs) self.assertNotIn(os.path.join(data_path, "tmp/subdir"), subdirs)
def testGetAllSubdirectories(self): data_path = os.path.join(os.path.dirname(__file__), "data") subdirs = FileManager.get_all_subdirectories(data_path, recursive=True) self.assertIn(os.path.join(data_path, "tmp"), subdirs) self.assertIn(os.path.join(data_path, "tmp", "subdir"), subdirs) subdirs = FileManager.get_all_subdirectories(data_path, recursive=False) self.assertIn(os.path.join(data_path, "tmp"), subdirs) self.assertNotIn(os.path.join(data_path, "tmp/subdir"), subdirs)
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 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)