def test_loader_methods(self): t = TempFS() self._init_modules(t) ih = FSImportHook(t) sys.meta_path.append(ih) try: self.assertEquals(ih.find_module("fsih_hello"),ih) self.assertEquals(ih.find_module("fsih_helo"),None) self.assertEquals(ih.find_module("fsih_pkg"),ih) self.assertEquals(ih.find_module("fsih_pkg.sub1"),ih) self.assertEquals(ih.find_module("fsih_pkg.sub2"),ih) self.assertEquals(ih.find_module("fsih_pkg.sub3"),None) m = ih.load_module("fsih_hello") self.assertEquals(m.message,"hello world!") self.assertRaises(ImportError,ih.load_module,"fsih_helo") ih.load_module("fsih_pkg") m = ih.load_module("fsih_pkg.sub1") self.assertEquals(m.message,"hello world!") self.assertEquals(m.a,42) m = ih.load_module("fsih_pkg.sub2") self.assertEquals(m.message,"hello world!") self.assertEquals(m.a,42 * 2) self.assertRaises(ImportError,ih.load_module,"fsih_pkg.sub3") finally: sys.meta_path.remove(ih) t.close()
class TestDokan(unittest.TestCase,DokanTestCases,ThreadingTestCases): def setUp(self): self.temp_fs = TempFS() self.drive = "K" while os.path.exists(self.drive+":\\") and self.drive <= "Z": self.drive = chr(ord(self.drive) + 1) if self.drive > "Z": raise RuntimeError("no free drive letters") fs_to_mount = OSFS(self.temp_fs.getsyspath("/")) self.mount_proc = dokan.mount(fs_to_mount,self.drive)#,flags=dokan.DOKAN_OPTION_DEBUG|dokan.DOKAN_OPTION_STDERR,numthreads=1) self.fs = OSFS(self.mount_proc.path) def tearDown(self): self.mount_proc.unmount() for _ in xrange(10): try: if self.mount_proc.poll() is None: self.mount_proc.terminate() except EnvironmentError: time.sleep(0.1) else: break else: if self.mount_proc.poll() is None: self.mount_proc.terminate() self.temp_fs.close()
class TestDokan(unittest.TestCase, DokanTestCases, ThreadingTestCases): def setUp(self): self.temp_fs = TempFS() self.drive = "K" while os.path.exists(self.drive + ":\\") and self.drive <= "Z": self.drive = chr(ord(self.drive) + 1) if self.drive > "Z": raise RuntimeError("no free drive letters") fs_to_mount = OSFS(self.temp_fs.getsyspath("/")) self.mount_proc = dokan.mount( fs_to_mount, self.drive ) #,flags=dokan.DOKAN_OPTION_DEBUG|dokan.DOKAN_OPTION_STDERR,numthreads=1) self.fs = OSFS(self.mount_proc.path) def tearDown(self): self.mount_proc.unmount() for _ in xrange(10): try: if self.mount_proc.poll() is None: self.mount_proc.terminate() except EnvironmentError: time.sleep(0.1) else: break else: if self.mount_proc.poll() is None: self.mount_proc.terminate() self.temp_fs.close()
def test_loader_methods(self): t = TempFS() self._init_modules(t) ih = FSImportHook(t) sys.meta_path.append(ih) try: self.assertEqual(ih.find_module("fsih_hello"), ih) self.assertEqual(ih.find_module("fsih_helo"), None) self.assertEqual(ih.find_module("fsih_pkg"), ih) self.assertEqual(ih.find_module("fsih_pkg.sub1"), ih) self.assertEqual(ih.find_module("fsih_pkg.sub2"), ih) self.assertEqual(ih.find_module("fsih_pkg.sub3"), None) m = ih.load_module("fsih_hello") self.assertEqual(m.message, "hello world!") self.assertRaises(ImportError, ih.load_module, "fsih_helo") ih.load_module("fsih_pkg") m = ih.load_module("fsih_pkg.sub1") self.assertEqual(m.message, "hello world!") self.assertEqual(m.a, 42) m = ih.load_module("fsih_pkg.sub2") self.assertEqual(m.message, "hello world!") self.assertEqual(m.a, 42 * 2) self.assertRaises(ImportError, ih.load_module, "fsih_pkg.sub3") finally: sys.meta_path.remove(ih) t.close()
def test_importer_on_meta_path(self): t = TempFS() self._init_modules(t) ih = FSImportHook(t) sys.meta_path.append(ih) try: self._check_imports_are_working() finally: sys.meta_path.remove(ih) t.close()
def test_url_on_sys_path(self): t = TempFS() zpath = t.getsyspath("modules.zip") z = ZipFS(zpath, "w") self._init_modules(z) z.close() z = ZipFS(zpath, "r") assert z.isfile("fsih_hello.py") z.close() sys.path.append("zip://" + zpath) FSImportHook.install() try: self._check_imports_are_working() finally: sys.path_hooks.remove(FSImportHook) sys.path.pop() t.close()
def test_url_on_sys_path(self): t = TempFS() zpath = t.getsyspath("modules.zip") z = ZipFS(zpath,"w") self._init_modules(z) z.close() z = ZipFS(zpath,"r") assert z.isfile("fsih_hello.py") z.close() sys.path.append("zip://" + zpath) FSImportHook.install() try: self._check_imports_are_working() finally: sys.path_hooks.remove(FSImportHook) sys.path.pop() t.close()
class TestFUSE(unittest.TestCase,FSTestCases): def setUp(self): self.temp_fs = TempFS() self.temp_fs.makedir("root") self.temp_fs.makedir("mount") self.mounted_fs = self.temp_fs.opendir("root") self.mount_point = self.temp_fs.getsyspath("mount") self.fs = OSFS(self.temp_fs.getsyspath("mount")) self.mount_proc = fuse.mount(self.mounted_fs,self.mount_point) def tearDown(self): self.mount_proc.unmount() self.temp_fs.close() def check(self,p): return self.mounted_fs.exists(p)
def snapshot(self, path): """Takes a snapshot of an individual file.""" # try grabbing the temp filesystem system path temp_dir = None temp_dir = self.tmp.getsyspath('/') # Create a temp file system to be snapshotted temp_snapshot_fs = TempFS(temp_dir=temp_dir) src_path = temp_snapshot_fs.getsyspath('/') with self.fs.open(path, 'rb') as source_file: with temp_snapshot_fs.open('datafile', 'wb') as temp_file: shutil.copyfileobj(source_file, temp_file) # snapshot destination directory dest_dir = self.snapshot_snap_path(path) command = ['rdiff-backup', '--parsable-output', '--no-eas', '--no-file-statistics', '--no-acls', '--tempdir', self.tmp.getsyspath('/'), src_path, dest_dir] # speed up the tests if self.__testing: command.insert(5, '--current-time') command.insert(6, str(self.__testing['time'])) self.__testing['time'] += 1 process = Popen(command, stdout=PIPE, stderr=PIPE) stderr = process.communicate()[1] ignore = [lambda x: x.startswith("Warning: could not determine case")] if len(stderr) is not 0: for rule in ignore: if not rule(stderr): raise SnapshotError(stderr) # close the temp snapshot filesystem temp_snapshot_fs.close()
def snapshot(self, path): """Takes a snapshot of an individual file.""" # try grabbing the temp filesystem system path temp_dir = None temp_dir = self.tmp.getsyspath('/') # Create a temp file system to be snapshotted temp_snapshot_fs = TempFS(temp_dir=temp_dir) src_path = temp_snapshot_fs.getsyspath('/') with self.fs.open(path, 'rb') as source_file: with temp_snapshot_fs.open('datafile', 'wb') as temp_file: shutil.copyfileobj(source_file, temp_file) # snapshot destination directory dest_dir = self.snapshot_snap_path(path) command = [ 'rdiff-backup', '--parsable-output', '--no-eas', '--no-file-statistics', '--no-acls', '--tempdir', self.tmp.getsyspath('/'), src_path, dest_dir ] # speed up the tests if self.__testing: command.insert(5, '--current-time') command.insert(6, str(self.__testing['time'])) self.__testing['time'] += 1 process = Popen(command, stdout=PIPE, stderr=PIPE) stderr = process.communicate()[1] ignore = [lambda x: x.startswith("Warning: could not determine case")] if len(stderr) is not 0: for rule in ignore: if not rule(stderr): raise SnapshotError(stderr) # close the temp snapshot filesystem temp_snapshot_fs.close()
class TestFUSE(unittest.TestCase, FSTestCases, ThreadingTestCases): def setUp(self): self.temp_fs = TempFS() self.temp_fs.makedir("root") self.temp_fs.makedir("mount") self.mounted_fs = self.temp_fs.opendir("root") self.mount_point = self.temp_fs.getsyspath("mount") self.fs = OSFS(self.temp_fs.getsyspath("mount")) self.mount_proc = fuse.mount(self.mounted_fs, self.mount_point) def tearDown(self): self.mount_proc.unmount() try: self.temp_fs.close() except OSError: # Sometimes FUSE hangs onto the mountpoint if mount_proc is # forcibly killed. Shell out to fusermount to make sure. fuse.unmount(self.mount_point) self.temp_fs.close() def check(self, p): return self.mounted_fs.exists(p)
class TestFUSE(unittest.TestCase,FSTestCases,ThreadingTestCases): def setUp(self): self.temp_fs = TempFS() self.temp_fs.makedir("root") self.temp_fs.makedir("mount") self.mounted_fs = self.temp_fs.opendir("root") self.mount_point = self.temp_fs.getsyspath("mount") self.fs = OSFS(self.temp_fs.getsyspath("mount")) self.mount_proc = fuse.mount(self.mounted_fs,self.mount_point) def tearDown(self): self.mount_proc.unmount() try: self.temp_fs.close() except OSError: # Sometimes FUSE hangs onto the mountpoint if mount_proc is # forcibly killed. Shell out to fusermount to make sure. fuse.unmount(self.mount_point) self.temp_fs.close() def check(self,p): return self.mounted_fs.exists(p)
class TestRPCFS(unittest.TestCase, FSTestCases, ThreadingTestCases): def makeServer(self, fs, addr): return RPCFSServer(fs, addr, logRequests=False) def startServer(self): port = 3000 self.temp_fs = TempFS() self.server = None self.serve_more_requests = True self.server_thread = threading.Thread(target=self.runServer) self.server_thread.setDaemon(True) self.start_event = threading.Event() self.end_event = threading.Event() self.server_thread.start() self.start_event.wait() def runServer(self): """Run the server, swallowing shutdown-related execptions.""" port = 3000 while not self.server: try: self.server = self.makeServer(self.temp_fs, ("127.0.0.1", port)) except socket.error as e: if e.args[1] == "Address already in use": port += 1 else: raise self.server_addr = ("127.0.0.1", port) self.server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.start_event.set() try: #self.server.serve_forever() while self.serve_more_requests: self.server.handle_request() except Exception as e: pass self.end_event.set() def setUp(self): self.startServer() self.fs = rpcfs.RPCFS("http://%s:%d" % self.server_addr) def tearDown(self): self.serve_more_requests = False try: self.bump() self.server.server_close() except Exception: pass #self.server_thread.join() self.temp_fs.close() def bump(self): host, port = self.server_addr for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): af, socktype, proto, cn, sa = res sock = None try: sock = socket.socket(af, socktype, proto) sock.settimeout(.1) sock.connect(sa) sock.send(b("\n")) except socket.error as e: pass finally: if sock is not None: sock.close()
def test_clean_error(self, rmtree): rmtree.side_effect = Exception("boom") with self.assertRaises(errors.OperationFailed): t = TempFS(ignore_clean_errors=False) t.writebytes("foo", b"bar") t.close()
def test_clean(self): t = TempFS() _temp_dir = t.getsyspath("/") self.assertTrue(os.path.isdir(_temp_dir)) t.close() self.assertFalse(os.path.isdir(_temp_dir))
class RegistryFileOpener(WinRegistryFileReader): """ This is a callback class used by dfwinreg to open registry hive files. We are using dfvfs as the backend to open files within our image. To resolve system variables, we make use of the variable database of the WindowsSystem instance. """ # pylint: disable=too-few-public-methods def __init__(self, dfvfs, partition, windows_system): super(RegistryFileOpener, self).__init__() self.dfvfs = dfvfs self.partition = partition self.not_present = set() self.open_handles = [] self.tmpfs = TempFS() self.windows_system = windows_system # callbacks.register_on_job_end(self._cleanup_open_files) def _cleanup_open_files(self, __): for path, handle in self.open_handles: try: handle.close() self.tmpfs.remove(path) except (OSError, FSError) as err: LOGGER.warning("Error cleaning up %s: %s", path, err) self.tmpfs.close() def Open(self, path, ascii_codepage='cp1252'): LOGGER.info("open registry %s", path) """ Opens a path within the dfVFS volume """ realpath = path.replace('\\', '/') if path in self.not_present: return None # check for variables and if we know them realpath = path for match in re.finditer('%[a-zA-Z0-9_]+%', path): key = match.group(0) val = self.windows_system.get_var(key) if val: realpath = realpath.replace(key, val) else: LOGGER.warning("Could not resolve variable %s", key) return None realpath = realpath.replace('\\', '/') if realpath.lower().startswith('c:/'): # catch absolute paths realpath = '/' + realpath[3:] if not realpath[0] == '/': realpath = '/' + realpath if realpath in self.not_present: return None path_specs = list( self.dfvfs.find_paths([realpath], partitions=[self.partition])) if not path_specs: LOGGER.warning("Could not find requested registry hive %s [%s]", path, realpath) self.not_present.add(path) self.not_present.add(realpath) return None if len(path_specs) > 1: LOGGER.warning( "Found multiple registry hives for query %s, using %s", path, dfvfs_helper.reconstruct_full_path(path_specs[0])) # extract the file locally filename = realpath.replace('/', '_') dfvfs_helper.export_file(path_specs[0], self.tmpfs, filename) try: file_object = self.tmpfs.open(filename, 'rb') except ResourceNotFound: files = self.tmpfs.listdir("/") LOGGER.warning("Could not open registry hive %s [%s] (%s)", path, realpath, files) return None self.open_handles.append((filename, file_object)) reg_file = regfile_impl.REGFWinRegistryFile( ascii_codepage=ascii_codepage) reg_file.Open(file_object) return reg_file
class DocuService: def __init__(self, file_system, root_path='/'): "docstring" self.fs = file_system.opendir(root_path) self.temp_fs = TempFS() self.fillers = {} self._temp_files = {} def _load_filler(self, key): module = importlib.import_module(_FILLERS[key]) self.fillers[key] = module.Filler(self) def fill(self, data, template_name, docu_path): template_type = template_name.split('.')[-1] doc_type = docu_path.split('.')[-1] filler_key = '{}2{}'.format(template_type, doc_type) if filler_key not in self.fillers: # Lazzy loading self._load_filler(filler_key) filler = self.fillers[filler_key] filler.fill(data, template_name, docu_path) logger.info('filled document {}'.format(docu_path)) def getsyspath(self, path): if path in self._temp_files: return self._temp_files[path] if self.fs.hassyspath(path): return self.fs.getsyspath(path) else: dirname = fs.path.dirname(path) if not self.temp_fs.isdir(dirname): self.temp_fs.makedirs(dirname, recreate=True) fs.copy.copy_file(self.fs, path, self.temp_fs, path) logger.info('Copied {} file to temporary fs'.format(path)) self._temp_files[path] = self.temp_fs.getsyspath(path) return self._temp_files[path] def print_to_cups_printer(self, printer_name, file_path, media='A4', quality=5): """Print to cups printer using command line """ path = self.getsyspath(file_path) command = 'lp -d {} -o media={} -o print-quality={} {}'.format( printer_name, media, quality, path) subprocess.check_call(command, shell=True) def export(self, source_path, fs, destination_path=None): destination_path = destination_path if destination_path else source_path fs.copy.copy_file(self.fs, source_path, fs, destination_path) def __del__(self): self.temp_fs.close() self.fs.close()