def testEncryptDecrypt(self): temp_file_path = os.path.join(os.path.dirname(__file__), "data/cfile") f = open(temp_file_path, 'w') f.write("foobar") f.close() key = Crypto.generate_key("secret_key") Crypto.encrypt_file(key, temp_file_path) self.assertTrue(os.path.exists(temp_file_path + ".enc")) contents = FileManager.read_file(temp_file_path + ".enc") # TODO should do a better verification that it's actually encrypted properly self.assertTrue(contents != "foobar") os.remove(temp_file_path) Crypto.decrypt_file(key, temp_file_path + ".enc") contents = FileManager.read_file(temp_file_path) self.assertTrue(contents == "foobar") os.remove(temp_file_path) os.remove(temp_file_path + ".enc")
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)