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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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 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)
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)
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)
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)
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)
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)
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);
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):
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)
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)
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)