Пример #1
0
    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)
Пример #2
0
    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")
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)
Пример #6
0
    def __init__(self, snapfile, snapdirectory, encryption_password=None):
        '''initialize the snapfile

        @param snapfile - the path to the snapfile to create / read
        @param snapdirectory - the path to the directory to compress/extract
        @param encryption_password - the password to use in the encryption/decryption operations, if set to None encryption will be disabled
        @raises - MissingDirError - if the snapdirectory is invalid
        '''
        if not os.path.isdir(snapdirectory):
            raise MissingDirError(snapdirectory + " is an invalid snap working directory ")
        self.snapfile = snapfile
        self.snapdirectory = snapdirectory
        self.encryption_password = encryption_password

        if  not snap.osregistry.OS.is_windows() and self.encryption_password != None:
            self.encryption_key = Crypto.generate_key(self.encryption_password)
        else:
            self.encryption_key = None
Пример #7
0
    def __init__(self, snapfile, snapdirectory, encryption_password=None):
        '''initialize the snapfile

        @param snapfile - the path to the snapfile to create / read
        @param snapdirectory - the path to the directory to compress/extract
        @param encryption_password - the password to use in the encryption/decryption operations, if set to None encryption will be disabled
        @raises - MissingDirError - if the snapdirectory is invalid
        '''
        if not os.path.isdir(snapdirectory):
            raise MissingDirError(snapdirectory +
                                  " is an invalid snap working directory ")
        self.snapfile = snapfile
        self.snapdirectory = snapdirectory
        self.encryption_password = encryption_password

        if not snap.osregistry.OS.is_windows(
        ) and self.encryption_password != None:
            self.encryption_key = Crypto.generate_key(self.encryption_password)
        else:
            self.encryption_key = None