def setUp(self): rootfs = TempFS() backup = TempFS(temp_dir=rootfs.getsyspath('/')) self.fs = VersioningFS(rootfs, backup=backup, tmp=TempFS(), testing={'time': 1})
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 download_data_networks(): selection = parse_qs(decode(request.query_string)) print('Received this selection to download: {}'.format(selection)) if not is_valid(selection): return 'Nothing to download!' wikis = extract_wikis_from_selection_dict(selection) network_code = selection['network'][0] lower_bound = '' upper_bound = '' if 'lower_bound' and 'upper_bound' in selection.keys(): lower_bound = int(selection['lower_bound'][0]) upper_bound = int(selection['upper_bound'][0]) upper_bound = datetime.fromtimestamp(upper_bound).strftime( "%Y-%m-%d %H:%M:%S") lower_bound = datetime.fromtimestamp(lower_bound).strftime( "%Y-%m-%d %H:%M:%S") network = data_controller.get_network(wikis[0], network_code, lower_bound, upper_bound) tmp = TempFS() tmp.create('network.gml') path = tmp.getsyspath('/network.gml') network.write_gml(file=path) return send_file(filename_or_fp=path, as_attachment=True, attachment_filename='network.gml')
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 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)
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)
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()
def download_data_server(): selection = parse_qs(decode(request.query_string)) print ('Received this selection to download: {}'.format(selection)) if not is_valid(selection): return 'Nothing to download!' wikis = extract_wikis_from_selection_dict(selection) network_code = selection['network'][0] network = data_controller.get_network(wiki = wikis[0], network_code = network_code) tmp = TempFS() tmp.create('network.gml') path = tmp.getsyspath('/network.gml') network.write_gml(file = path) return flask.send_file(filename_or_fp = path, as_attachment=True, attachment_filename='network.gml')
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()
def setUp(self): rootfs = TempFS() backup = TempFS(temp_dir=rootfs.getsyspath('/')) self.fs = VersioningFS(rootfs, backup=backup, tmp=TempFS())
try: import git except ImportError: SHA = "" else: repo = git.Repo(os.path.dirname(os.path.realpath(__file__)), search_parent_directories=True) SHA = repo.head.object.hexsha[0:10] CACHED_CONFIG_FILE = None SERVER_CONFIG_FILE = "mswms_settings.py" MSCOLAB_CONFIG_FILE = "mscolab_settings.py" ROOT_FS = TempFS(identifier=f"msui{SHA}") OSFS_URL = ROOT_FS.geturl("", purpose="fs") ROOT_DIR = ROOT_FS.getsyspath("") if not ROOT_FS.exists("msui/testdata"): ROOT_FS.makedirs("msui/testdata") SERVER_CONFIG_FS = fs.open_fs(fs.path.join(ROOT_DIR, "msui")) DATA_FS = fs.open_fs(fs.path.join(ROOT_DIR, "msui/testdata")) MSUI_CONFIG_PATH = OSFS_URL # MSUI_CONFIG_PATH = SERVER_CONFIG_FS.getsyspath("") would use a none osfs path os.environ["MSUI_CONFIG_PATH"] = MSUI_CONFIG_PATH SERVER_CONFIG_FILE_PATH = fs.path.join(SERVER_CONFIG_FS.getsyspath(""), SERVER_CONFIG_FILE) # we keep DATA_DIR until we move netCDF4 files to pyfilesystem2 DATA_DIR = DATA_FS.getsyspath("")
def test_os_fs_create_dir(self): _fs = TempFS(identifier="mss") _dir = _fs.getsyspath("") os_fs_create_dir(_dir) assert os.path.exists(_dir)
class COWFS(FS): def __init__( self, base_fs: FS, additions_fs: Optional[FS] = None, deletions_fs: Optional[FS] = None, ) -> None: FS.__init__(self) if additions_fs: self.additions_fs = additions_fs else: self.additions_fs = TempFS() if deletions_fs: _deletions_invariant(deletions_fs) self.deletions_fs = deletions_fs else: self.deletions_fs = TempFS() self.original_base_fs = base_fs self.base_fs = fs.wrap.read_only(base_fs) self.invariant() @staticmethod def create_cowfs(base_fs: FS, read_write_layer: FS, recreate: bool = False) -> "COWFS": additions_fs = read_write_layer.makedir("/additions", recreate=recreate) deletions_fs = read_write_layer.makedir("/deletions", recreate=recreate) return COWFS(base_fs, additions_fs, deletions_fs) def __str__(self) -> str: return (f"COWFS({self.original_base_fs}, " f"{self.additions_fs}, " f"{self.deletions_fs})") def __repr__(self) -> str: return (f"COWFS({self.original_base_fs!r}, " f"{self.additions_fs!r}, " f"{self.deletions_fs!r})") ############################################################ def invariant(self) -> bool: if not self.additions_fs: raise ValueError(f"Invalid additions_fs: {self.additions_fs}.") if not self.deletions_fs: raise ValueError(f"Invalid deletions_fs: {self.additions_fs}.") if not self.base_fs: raise ValueError(f"Invalid base_fs: {self.base_fs}.") _deletions_invariant(self.deletions_fs) additions_paths = set(paths(self.additions_fs)) deletions_paths = { fs.path.dirname(file) for file in self.deletions_fs.walk.files() } if additions_paths > deletions_paths: raise ValueError(f"Additions_paths {additions_paths} " + "is not a subset of deletions_path " + f"{deletions_paths}. Extras are " + f"{additions_paths - deletions_paths}.") return True def is_deletion(self, path: str) -> bool: """ Is the path marked in the deletions_fs" """ return self.deletions_fs.exists(del_path(path)) def mark_deletion(self, path: str) -> None: """ Mark the path in the deletions_fs. """ self.deletions_fs.makedirs(path, None, True) self.deletions_fs.touch(del_path(path)) def makedirs_mark_deletion( self, path: str, permissions: Optional[Permissions] = None, recreate: bool = False, ) -> None: for p in fs.path.recursepath(path)[:-1]: self.additions_fs.makedirs(p, permissions=permissions, recreate=True) self.mark_deletion(p) self.additions_fs.makedir(path, permissions=permissions, recreate=recreate) self.mark_deletion(path) def layer(self, path: str) -> int: """ Get the layer on which the file lives, or ROOT_LAYER if it's the root path. """ if path == "/": return ROOT_LAYER if self.additions_fs.exists(path): return ADD_LAYER elif self.is_deletion(path): return NO_LAYER elif self.base_fs.exists(path): return BASE_LAYER else: return NO_LAYER def copy_up(self, path: str) -> None: """ Copy the file from the base_fs to additions_fs. """ self.makedirs_mark_deletion(fs.path.dirname(path)) self.mark_deletion(path) fs.copy.copy_file(self.base_fs, path, self.additions_fs, path) def triple_tree(self) -> None: print("base_fs ------------------------------") self.base_fs.tree() print("additions_fs ------------------------------") self.additions_fs.tree() print("deletions_fs ------------------------------") self.deletions_fs.tree() ############################################################ def getmeta(self, namespace: str = "standard") -> Mapping[str, object]: return self.base_fs.getmeta(namespace) def getinfo(self, path: str, namespaces: Optional[Collection[str]] = None) -> Info: self.check() self.validatepath(path) layer = self.layer(path) if layer == NO_LAYER: raise fs.errors.ResourceNotFound(path) elif layer == BASE_LAYER: return self.base_fs.getinfo(path, namespaces) elif layer == ADD_LAYER: return self.additions_fs.getinfo(path, namespaces) elif layer == ROOT_LAYER: # TODO implement this raw_info = {} if namespaces is None or "basic" in namespaces: raw_info["basic"] = {"name": "", "is_dir": True} return Info(raw_info) else: raise RuntimeError(f"Unknown layer {layer}.") def getsyspath(self, path: str) -> str: self.check() # self.validatepath(path) layer = self.layer(path) if layer == NO_LAYER: raise fs.errors.NoSysPath(path=path) elif layer == BASE_LAYER: return self.base_fs.getsyspath(path) elif layer == ADD_LAYER: return self.additions_fs.getsyspath(path) elif layer == ROOT_LAYER: raise fs.errors.NoSysPath(path=path) else: raise RuntimeError(f"Unknown layer {layer}.") def listdir(self, path: str) -> List[str]: self.check() self.validatepath(path) layer = self.layer(path) if layer == NO_LAYER: raise fs.errors.ResourceNotFound(path) elif layer == BASE_LAYER: return [ name for name in self.base_fs.listdir(path) if self.layer(fs.path.join(path, name)) != NO_LAYER ] elif layer == ADD_LAYER: # Get the listing on the additions layer names = set(self.additions_fs.listdir(path)) # Add in the listing on the base layer (if it exists) if self.base_fs.isdir(path): names |= set(self.base_fs.listdir(path)) # Return the entries that actually exist return [ name for name in list(names) if self.layer(fs.path.join(path, name)) != NO_LAYER ] elif layer == ROOT_LAYER: # Get the listing of the root on the additions layer and # the base layer. names = set(self.additions_fs.listdir("/")) names |= set(self.base_fs.listdir("/")) # Return the entries that actually exist. return [ name for name in list(names) if self.layer(name) != NO_LAYER ] else: raise RuntimeError(f"Unknown layer {layer}.") def makedir( self, path: str, permissions: Optional[Permissions] = None, recreate: bool = False, ) -> SubFS["COWFS"]: self.check() self.validatepath(path) # Check if it *can* be created. # get a normalized parent_dir path. parent_dir = fs.path.dirname(fs.path.forcedir(path)[:-1]) if not parent_dir: parent_dir = "/" if not self.isdir(parent_dir): raise fs.errors.ResourceNotFound(path) layer = self.layer(path) if layer == NO_LAYER: self.makedirs_mark_deletion(path, permissions=permissions, recreate=recreate) return SubFS(self, path) elif layer in [BASE_LAYER, ADD_LAYER, ROOT_LAYER]: if recreate: return SubFS(self, path) else: # I think this is wrong. What if it's a file? raise fs.errors.DirectoryExists(path) else: raise RuntimeError(f"Unknown layer {layer}.") def openbin(self, path: str, mode: str = "r", buffering: int = -1, **options: Any) -> BinaryIO: self.check() self.validatepath(path) parent_dir = fs.path.dirname(fs.path.forcedir(path)[:-1]) if not parent_dir: parent_dir = "/" if not self.isdir(parent_dir): raise fs.errors.ResourceNotFound(path) mode_obj = Mode(mode) layer = self.layer(path) if layer == NO_LAYER: if mode_obj.create: for p in fs.path.recursepath(path)[:-1]: self.additions_fs.makedirs(p, recreate=True) self.mark_deletion(p) self.mark_deletion(path) return self.additions_fs.openbin(path, mode, buffering, **options) else: raise fs.errors.ResourceNotFound(path) elif layer == ADD_LAYER: self.mark_deletion(path) return self.additions_fs.openbin(path, mode, buffering, **options) elif layer == BASE_LAYER: if mode_obj.writing: self.copy_up(path) return self.additions_fs.openbin(path, mode, buffering, **options) else: return self.base_fs.openbin(path, mode, buffering, **options) elif layer == ROOT_LAYER: raise fs.errors.FileExpected(path) else: raise RuntimeError(f"Unknown layer {layer}.") def remove(self, path: str) -> None: self.check() self.validatepath(path) layer = self.layer(path) if layer == NO_LAYER: raise fs.errors.ResourceNotFound(path) elif layer == BASE_LAYER: if self.base_fs.isfile(path): self.mark_deletion(path) else: raise fs.errors.FileExpected(path) elif layer == ADD_LAYER: self.additions_fs.remove(path) self.mark_deletion(path) elif layer == ROOT_LAYER: raise fs.errors.FileExpected(path) else: raise RuntimeError(f"Unknown layer {layer}.") def removedir(self, path: str) -> None: self.check() layer = self.layer(path) if layer == NO_LAYER: raise fs.errors.ResourceNotFound(path) elif layer == BASE_LAYER: if self.base_fs.isdir(path): self.mark_deletion(path) else: raise fs.errors.FileExpected(path) elif layer == ADD_LAYER: if self.additions_fs.isdir(path): self.additions_fs.removedir(path) self.mark_deletion(path) else: raise fs.errors.DirectoryExpected(path) elif layer == ROOT_LAYER: raise fs.errors.RemoveRootError(path) else: raise RuntimeError(f"Unknown layer {layer}.") def setinfo(self, path: str, info: _INFO_DICT) -> None: self.check() self.validatepath(path) layer = self.layer(path) if layer == NO_LAYER: raise fs.errors.ResourceNotFound(path) elif layer == BASE_LAYER: self.copy_up(path) self.additions_fs.setinfo(path, info) elif layer == ADD_LAYER: self.additions_fs.setinfo(path, info) elif layer == ROOT_LAYER: pass else: raise RuntimeError(f"Unknown layer {layer}.") ############################################################ def makedirs( self, path: str, permissions: Optional[Permissions] = None, recreate: bool = False, ) -> SubFS[FS]: return FS.makedirs(self, path, permissions=permissions, recreate=recreate)
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 TestVersionView(unittest.TestCase): def setUp(self) -> None: # Build a working bundle containing a collection; both include # a set of files, not all on the same level. b_lidvid = LIDVID("urn:nasa:pds:b::1.2") b_files = { "foo.txt": "Hello, world!", "counter.txt": "12345", "subdir": { "subdir.txt": "xxx" }, } c_lidvid = LIDVID("urn:nasa:pds:b:c::1.1") c_files = { "undersea.txt": "I'm under c!", "deeper": { "leagues40k.txt": "Captain Nemo" }, } self.tempfs = TempFS() mv = Multiversioned(self.tempfs) mv[b_lidvid] = dictionary_to_contents({c_lidvid}, b_files) mv[c_lidvid] = dictionary_to_contents(set(), c_files) # Add a second version of the bundle containing nothing, just # to check that they stay independent. b2_lidvid = LIDVID("urn:nasa:pds:b::2.0") b2_files: Dict[Any, Any] = dict() mv[b2_lidvid] = dictionary_to_contents(set(), b2_files) self.vv = VersionView(mv, b_lidvid) self.vv2 = VersionView(mv, b2_lidvid) self.mv = mv def test_transform_vv_path(self) -> None: self.assertEqual("/", self.vv.transform_vv_path("/")) with self.assertRaises(ResourceNotFound): self.vv.transform_vv_path("/foo.txt") self.assertEqual("/b/v$1.2", self.vv.transform_vv_path("/b$")) self.assertEqual("/b/c/v$1.1", self.vv.transform_vv_path("/b$/c$")) def test_getinfo(self) -> None: triples = [ ("", True, "/"), ("b$", True, "/b$"), ("counter.txt", False, "/b$/counter.txt"), ("foo.txt", False, "/b$/foo.txt"), ("subdir", True, "/b$/subdir"), ("subdir.txt", False, "/b$/subdir/subdir.txt"), ("c$", True, "/b$/c$"), ("undersea.txt", False, "/b$/c$/undersea.txt"), ("deeper", True, "/b$/c$/deeper"), ("leagues40k.txt", False, "/b$/c$/deeper/leagues40k.txt"), ] for triple in triples: vv_name, is_dir, vv_path = triple info = self.vv.getinfo(vv_path) self.assertTrue(is_dir == info.is_dir, str(triple)) self.assertEqual(vv_name, info.name) def test_listdir(self) -> None: pairs = [ ("/", {"b$"}), ("/b$", {"counter.txt", "foo.txt", "subdir", "c$"}), ("/b$/subdir", {"subdir.txt"}), ("/b$/c$", {"undersea.txt", "deeper"}), ("/b$/c$/deeper", {"leagues40k.txt"}), ] for pair in pairs: vv_path, vv_dir_contents = pair self.assertEqual(vv_dir_contents, set(self.vv.listdir(vv_path))) def test_openbin(self) -> None: # We'll test through readtext() for part of this instead. # Check that the files contain the right values. self.assertEqual("12345", self.vv.readtext("/b$/counter.txt")) self.assertEqual("Hello, world!", self.vv.readtext("/b$/foo.txt")) self.assertEqual("xxx", self.vv.readtext("/b$/subdir/subdir.txt")) self.assertEqual("I'm under c!", self.vv.readtext("/b$/c$/undersea.txt")) self.assertEqual("Captain Nemo", self.vv.readtext("/b$/c$/deeper/leagues40k.txt")) # Try to read and write where files aren't allowed. with self.assertRaises(ResourceNotFound): self.vv.openbin("/foo.txt", "r") with self.assertRaises(ResourceReadOnly): self.vv.openbin("/foo.txt", "w") # Try to read a directory. with self.assertRaises(FileExpected): self.vv.openbin("/b$", "r") # Try to write or append to the read-only filesystem. with self.assertRaises(ResourceReadOnly): self.vv.openbin("/b$/counter.txt", "w") with self.assertRaises(ResourceReadOnly): self.vv.openbin("/b$/counter.txt", "a") def test_getitem(self) -> None: # A list of VersionViews and LIDVIDs that live inside them. vvs_lidvids = [ (self.vv, LIDVID("urn:nasa:pds:b::1.2")), (self.vv, LIDVID("urn:nasa:pds:b:c::1.1")), (self.vv2, LIDVID("urn:nasa:pds:b::2.0")), ] for vv, lidvid in vvs_lidvids: # Contents of a LIDVID can be found two different ways. # First, they can come directly from the Multiversioned. # Second, you can build a VersionView from that LIDVID or # from a parent of it, then get the contents from its LID. # Check that the two are equal, after reducing LIDVIDs to # LIDs. lid = lidvid.lid() vv_contents = vv[lid] mv_contents = self.mv[lidvid].to_lid_version_contents() self.assertEqual(mv_contents, vv_contents) def test_lid_to_lidvid(self) -> None: self.assertEqual(LIDVID("urn:nasa:pds:b::1.2"), self.vv.lid_to_lidvid(LID("urn:nasa:pds:b"))) self.assertEqual( LIDVID("urn:nasa:pds:b:c::1.1"), self.vv.lid_to_lidvid(LID("urn:nasa:pds:b:c")), ) self.assertEqual(LIDVID("urn:nasa:pds:b::2.0"), self.vv2.lid_to_lidvid(LID("urn:nasa:pds:b"))) def test_getsyspath(self) -> None: self.assertEqual( self.tempfs.getsyspath("/b/v$1.2/foo.txt"), self.vv.getsyspath("b$/foo.txt"), ) self.assertEqual( self.tempfs.getsyspath("/b/c/v$1.1/deeper/leagues40k.txt"), self.vv.getsyspath("b$/c$/deeper/leagues40k.txt"), )
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()