Пример #1
0
    def testGetAllFiles(self):
        data_path = os.path.join(os.path.dirname(__file__), "data", "tmp")
        files = FileManager.get_all_files(include=[data_path])
        self.assertIn(os.path.join(data_path, "file1"), files)
        self.assertIn(os.path.join(data_path, "subdir", "file2"), files)

        files = FileManager.get_all_files(include=[data_path],
                                          exclude=[os.path.join(data_path, 'subdir')])
        self.assertIn(os.path.join(data_path, "file1"), files)
        self.assertNotIn(os.path.join(data_path, "subdir", "file2"), files)

        files = FileManager.get_all_files(include=[data_path], recursive=False)
        self.assertIn(os.path.join(data_path, "file1"), files)
        self.assertNotIn(os.path.join(data_path, "subdir", "file2"), files)
Пример #2
0
    def testGetAllFiles(self):
        data_path = os.path.join(os.path.dirname(__file__), "data", "tmp")
        files = FileManager.get_all_files(include=[data_path])
        self.assertIn(os.path.join(data_path, "file1"), files)
        self.assertIn(os.path.join(data_path, "subdir", "file2"), files)

        files = FileManager.get_all_files(
            include=[data_path], exclude=[os.path.join(data_path, 'subdir')])
        self.assertIn(os.path.join(data_path, "file1"), files)
        self.assertNotIn(os.path.join(data_path, "subdir", "file2"), files)

        files = FileManager.get_all_files(include=[data_path], recursive=False)
        self.assertIn(os.path.join(data_path, "file1"), files)
        self.assertNotIn(os.path.join(data_path, "subdir", "file2"), files)
Пример #3
0
    def backup(self, basedir, include=[], exclude=[]):
        """backup the files modified outside the apt package system"""

        if snap.config.options.log_level_at_least('verbose'):
            snap.callback.snapcallback.message("Backing up files using apt backend");

        if len(include) == 0:
            include = ['/']

        for additional_exclude in ['/proc', '/sys', '/selinux']:
            if not additional_exclude in exclude:
                exclude.append(additional_exclude)

        # remove duplicates
        include = list(set(include))
        exclude = list(set(exclude))

        # determine which files have been modified since installation
        #   and copy those to basedir
        sfiles = []
        files = FileManager.get_all_files(include, exclude)
        for tfile in files:
            if self.__file_modified(tfile):
                if snap.config.options.log_level_at_least('verbose'):
                    snap.callback.snapcallback.message("Backing up file " + tfile);
                sfile = SFile(tfile)
                sfile.copy_to(basedir)
                sfiles.append(sfile)

        # write record file to basedir
        record = FilesRecordFile(basedir + "/files.xml")
        record.write(sfiles)
Пример #4
0
    def restore(self, basedir):
        '''restore the packages from the snapfile'''
        # if package record file isn't found, simply return
        if not os.path.isfile(os.path.join(basedir, "packages.xml")):
            return

        if snap.config.options.log_level_at_least('verbose'):
            snap.callback.snapcallback.message("Restoring software on windows")

        # read files from the record file
        record = PackagesRecordFile(os.path.join(basedir, "packages.xml"))
        packages = record.read()

        # TODO restore registry?

        # restore program files
        for pkg_file in FileManager.get_all_files(
                include=[os.path.join(basedir, "windows-packages")]):
            partial_path = pkg_file.replace(
                os.path.join(basedir, "windows-packages") + "\\", "")
            try:
                SFile(partial_path).copy_to(
                    path_prefix=os.path.join(basedir, "windows-packages"))
            except:
                if snap.config.options.log_level_at_least('normal'):
                    snap.callback.snapcallback.message(
                        "Failed to restore windows package file " +
                        partial_path)
Пример #5
0
    def testBackupRepos(self):
        snapshot_target = snap.backends.repos.syum.Syum()
        snapshot_target.backup(self.fs_root)

        self.assertTrue(os.path.exists(self.fs_root + "/etc/yum.conf"))
        for repo in os.listdir("/etc/yum.repos.d"):
            self.assertTrue(os.path.exists(self.fs_root + "/etc/yum.repos.d/" + repo))

        repos = []
        self.assertTrue(os.path.exists(self.fs_root + "/repos.xml"))
        record = ReposRecordFile(self.fs_root + "/repos.xml")
        record_repos = record.read()
        for repo in record_repos:
            repos.append(repo.url)

        # verify repos contents
        urls = []
        for yum_repo in FileManager.get_all_files(include=['/etc/yum.repos.d']):
            baseurl = re.compile('baseurl=(.*)\n')
            mirrorlist = re.compile('mirrorlist=(.*)\n')
            contents = FileManager.read_file(yum_repo)
            for match in baseurl.findall(contents):
                urls.append(match)
            for match in mirrorlist.findall(contents):
                urls.append(match)

        for url in urls:
            self.assertIn(url, repos)
Пример #6
0
    def backup(self, basedir, include=[], exclude=[]):
        '''backup yum configuration and repositories'''
        # first backup the yum config
        SFile("/etc/yum.conf").copy_to(basedir)

        # then backup the individual repo files
        for yum_repo in FileManager.get_all_files(include=['/etc/yum.repos.d']):
            SFile(yum_repo).copy_to(basedir)
Пример #7
0
    def restore(self, basedir):
        '''restore yum configuration and repositories'''
        # return if we cannot find require files
        if not os.path.isdir(basedir + "/etc/apt"):
            return

        # restore the apt config to /etc/apt
        for apt_conf in FileManager.get_all_files(include=[basedir + "/etc/apt"]):
            partial_path = apt_conf.replace(basedir + "/", "")
            SFile(partial_path).copy_to(self.fs_root, path_prefix=basedir)
Пример #8
0
    def backup(self, basedir, include=[], exclude=[]):
        '''backup apt configuration and repositories'''
        # backup the apt config in /etc/apt
        repos = []
        for apt_conf in FileManager.get_all_files(include=['/etc/apt']):
            SFile(apt_conf).copy_to(basedir)
            repos.append(Repo()) # FIXME parse repo info

        # write record file to basedir
        record = ReposRecordFile(basedir + "/repos.xml")
        record.write(repos)
Пример #9
0
    def restore(self, basedir):
        '''restore yum configuration and repositories'''
        # return if we cannot find require files
        if not os.path.isdir(basedir + "/etc/apt"):
            return

        # restore the apt config to /etc/apt
        for apt_conf in FileManager.get_all_files(
                include=[basedir + "/etc/apt"]):
            partial_path = apt_conf.replace(basedir + "/", "")
            SFile(partial_path).copy_to(self.fs_root, path_prefix=basedir)
Пример #10
0
    def restore(self, basedir):
        '''restore yum configuration and repositories'''
        # return if we cannot find require files
        if not os.path.isdir(basedir + "/etc/yum.repos.d"):
            return

        # first restore yum configuration
        SFile("etc/yum.conf").copy_to(self.fs_root, path_prefix=basedir)

        # then restore individual repos
        for yum_repo in FileManager.get_all_files(include=[basedir + "/etc/yum.repos.d"]):
            partial_path = yum_repo.replace(basedir + "/" , "")
            SFile(partial_path).copy_to(self.fs_root, path_prefix=basedir)
Пример #11
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)
Пример #12
0
    def restore(self, basedir):
        '''restore yum configuration and repositories'''
        # return if we cannot find require files
        if not os.path.isdir(basedir + "/etc/apt"):
            return

        # read files from the record file
        # tho we don't do anything with this info here
        record = ReposRecordFile(basedir + "/repos.xml")
        repos = record.read()

        # restore the apt config to /etc/apt
        for apt_conf in FileManager.get_all_files(include=[basedir + "/etc/apt"]):
            partial_path = apt_conf.replace(basedir + "/", "")
            SFile(partial_path).copy_to(self.fs_root, path_prefix=basedir)
Пример #13
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)
Пример #14
0
    def backup(self, basedir, include=[], exclude=[]):
        '''backup the packages installed locally'''
        if snap.config.options.log_level_at_least('verbose'):
            snap.callback.snapcallback.message("Backing up software on windows");

        packages = Win.get_packages()

        # backup program files
        for pkg in packages:
            for pkg_file in FileManager.get_all_files(include=[pkg.name]):
                SFile(pkg_file).copy_to(os.path.join(basedir, "windows-packages"))

        # TODO Backup registry?

        # write record file to basedir
        record = PackagesRecordFile(os.path.join(basedir, "packages.xml"))
        record.write(packages)
Пример #15
0
    def restore(self, basedir):
        '''restore yum configuration and repositories'''
        # return if we cannot find require files
        if not os.path.isdir(basedir + "/etc/yum.repos.d"):
            return

        # read files from the record file
        # tho we don't do anything with this info here
        record = ReposRecordFile(basedir + "/repos.xml")
        repos = record.read()

        # first restore yum configuration
        SFile("etc/yum.conf").copy_to(self.fs_root, path_prefix=basedir)

        # then restore individual repos
        for yum_repo in FileManager.get_all_files(include=[basedir + "/etc/yum.repos.d"]):
            partial_path = yum_repo.replace(basedir + "/" , "")
            SFile(partial_path).copy_to(self.fs_root, path_prefix=basedir)
Пример #16
0
    def backup(self, basedir, include=[], exclude=[]):
        '''backup the packages installed locally'''
        if snap.config.options.log_level_at_least('verbose'):
            snap.callback.snapcallback.message(
                "Backing up software on windows")

        packages = Win.get_packages()

        # backup program files
        for pkg in packages:
            for pkg_file in FileManager.get_all_files(include=[pkg.name]):
                SFile(pkg_file).copy_to(
                    os.path.join(basedir, "windows-packages"))

        # TODO Backup registry?

        # write record file to basedir
        record = PackagesRecordFile(os.path.join(basedir, "packages.xml"))
        record.write(packages)
Пример #17
0
    def backup(self, basedir, include=[], exclude=[]):
        '''backup yum configuration and repositories'''
        # first backup the yum config
        SFile("/etc/yum.conf").copy_to(basedir)

        # then backup the individual repo files
        repos = []
        for yum_repo in FileManager.get_all_files(include=['/etc/yum.repos.d']):
            SFile(yum_repo).copy_to(basedir)

            # parse/extract repo info
            baseurl = re.compile('baseurl=(.*)\n')
            mirrorlist = re.compile('mirrorlist=(.*)\n')
            contents = FileManager.read_file(yum_repo)
            for match in baseurl.findall(contents):
                repos.append(Repo(url=match))
            for match in mirrorlist.findall(contents):
                repos.append(Repo(url=match))

        # write record file to basedir
        record = ReposRecordFile(basedir + "/repos.xml")
        record.write(repos)
Пример #18
0
    def restore(self, basedir):
        '''restore the packages from the snapfile'''
        # if package record file isn't found, simply return
        if not os.path.isfile(os.path.join(basedir, "packages.xml")):
            return

        if snap.config.options.log_level_at_least('verbose'):
            snap.callback.snapcallback.message("Restoring software on windows");

        # read files from the record file
        record = PackagesRecordFile(os.path.join(basedir, "packages.xml"))
        packages = record.read()

        # TODO restore registry?

        # restore program files
        for pkg_file in FileManager.get_all_files(include=[os.path.join(basedir, "windows-packages")]):
            partial_path = pkg_file.replace(os.path.join(basedir, "windows-packages") + "\\", "")
            try:
                SFile(partial_path).copy_to(path_prefix=os.path.join(basedir, "windows-packages"))
            except:
                if snap.config.options.log_level_at_least('normal'):
                    snap.callback.snapcallback.message("Failed to restore windows package file " + partial_path);
Пример #19
0
        else:
            for i in range(len(include)):
                include[i] = SFile.windows_path_escape(include[i])

        for additional_exclude in Win.EXCLUDE_DIRS:
            if not additional_exclude in exclude:
                exclude.append(additional_exclude)

        # remove duplicates
        include = list(set(include))
        exclude = list(set(exclude))

        # determine which files have been modified since installation
        #   and copy those to basedir
        sfiles = []
        files = FileManager.get_all_files(include, exclude)
        for tfile in files:
            if snap.config.options.log_level_at_least('verbose'):
                snap.callback.snapcallback.message("Backing up file " + tfile)
            try:
                sfile = SFile(tfile)
                sfile.copy_to(basedir)
                sfiles.append(sfile)
            except:
                pass

        # write record file to basedir
        record = FilesRecordFile(os.path.join(basedir, "files.xml"))
        record.write(sfiles)

    def restore(self, basedir):
Пример #20
0
 def backup(self, basedir, include=[], exclude=[]):
     '''backup apt configuration and repositories'''
     # backup the apt config in /etc/apt
     for apt_conf in FileManager.get_all_files(include=['/etc/apt']):
         SFile(apt_conf).copy_to(basedir)
Пример #21
0
 def backup(self, basedir, include=[], exclude=[]):
     '''backup apt configuration and repositories'''
     # backup the apt config in /etc/apt
     for apt_conf in FileManager.get_all_files(include=['/etc/apt']):
         SFile(apt_conf).copy_to(basedir)
Пример #22
0
        else:
            for i in range(len(include)):
                include[i] = SFile.windows_path_escape(include[i])

        for additional_exclude in Win.EXCLUDE_DIRS:
            if not additional_exclude in exclude:
                exclude.append(additional_exclude)

        # remove duplicates
        include = list(set(include))
        exclude = list(set(exclude))

        # determine which files have been modified since installation
        #   and copy those to basedir
        sfiles = []
        files = FileManager.get_all_files(include, exclude)
        for tfile in files:
            if snap.config.options.log_level_at_least('verbose'):
                snap.callback.snapcallback.message("Backing up file " + tfile);
            try:
                sfile = SFile(tfile)
                sfile.copy_to(basedir)
                sfiles.append(sfile)
            except:
                pass

        # write record file to basedir
        record = FilesRecordFile(os.path.join(basedir, "files.xml"))
        record.write(sfiles)