def testFilePath(self): """ Test that all files report a FUSE path, and that this results in the same file as the non-fuse path. """ files = list(File().find()) for file in files: adapter = File().getAssetstoreAdapter(file) filesystempath = adapter.fullPath(file) filepath = File().getLocalFilePath(file) fusepath = File().getGirderMountFilePath(file) self.assertTrue(os.path.exists(filesystempath)) self.assertTrue(os.path.exists(filepath)) self.assertTrue(os.path.exists(fusepath)) self.assertEqual(filesystempath, filepath) self.assertNotEqual(filesystempath, fusepath) self.assertEqual(fusepath[:len(self.mountPath)], self.mountPath) with open(filepath) as file1: with open(fusepath) as file2: self.assertEqual(file1.read(), file2.read()) subpath = fusepath[len(self.mountPath):].lstrip('/') if self.knownPaths.get(subpath): with open(fusepath) as file1: self.assertEqual(file1.read().strip(), self.knownPaths[subpath])
def testFilePath(self): """ Test that all files report a FUSE path, and that this results in the same file as the non-fuse path. """ from girder.plugins import fuse as girder_fuse files = list(File().find()) for file in files: adapter = File().getAssetstoreAdapter(file) filesystempath = adapter.fullPath(file) filepath = girder_fuse.getFilePath(file) fusepath = girder_fuse.getFuseFilePath(file) self.assertTrue(os.path.exists(filesystempath)) self.assertTrue(os.path.exists(filepath)) self.assertTrue(os.path.exists(fusepath)) self.assertEqual(filesystempath, filepath) self.assertNotEqual(filesystempath, fusepath) self.assertEqual(fusepath[:len(self.mainMountPath)], self.mainMountPath) with open(filepath) as file1: with open(fusepath) as file2: self.assertEqual(file1.read(), file2.read()) subpath = fusepath[len(self.mainMountPath):].lstrip('/') if self.knownPaths.get(subpath): with open(fusepath) as file1: self.assertEqual(file1.read().strip(), self.knownPaths[subpath])
def adjustDBUri(cls, uri, *args, **kwargs): """ Adjust a uri to match the form sqlite requires. This can convert a Girder resource path to an aprpopriate physical file reference. :param uri: the uri to adjust. :returns: the adjusted uri """ uri = super(SqliteSAConnector, cls).adjustDBUri(uri, *args, **kwargs) if '://' in uri: uri = uri.split('://', 1)[0] + ':////' + uri.split('://', 1)[1].lstrip('/') uri = super(SqliteSAConnector, cls).adjustDBUri(uri, *args, **kwargs) # If we have a Girder resource path, convert it. If this looks like a # file but doesn't exist, check if it is a resource path. If this is # not a resoruce path to a file that we can read directly, treat this # the same as a missing file. if (':///' in uri and not os.path.exists(uri.split(':///', 1)[1])): resourcepath = path_util.lookUpPath( uri.split(':///', 1)[1], test=True, filter=False, force=True) if resourcepath and resourcepath['model'] == 'file': file = resourcepath['document'] adapter = File().getAssetstoreAdapter(file) if hasattr(adapter, 'fullPath'): filepath = adapter.fullPath(file) if os.path.exists(filepath): uri = uri.split(':///', 1)[0] + ':///' + filepath log.debug('Using Girder file for SQLite database') return uri
def getFilePath(file): """ Given a file resource, return a path on the local file system. For assetstores that have a fullPath method, this returns the results of that call. Otherwise, it returns a path on the main mounted FUSE. :param file: file resource document. :returns: a path on the local file system. """ adapter = File().getAssetstoreAdapter(file) if callable(getattr(adapter, 'fullPath', None)): return adapter.fullPath(file) return server_fuse.getServerFusePath(MAIN_FUSE_KEY, 'file', file)
def girderInputSpec(resource, resourceType='file', name=None, token=None, dataType='string', dataFormat='text', fetchParent=False): """ Downstream plugins that are building Girder worker jobs that use Girder IO should use this to generate the input specs more easily. :param resource: The resource document to be downloaded at runtime. :type resource: dict :param resourceType: The resource type to download for the input. Should be "folder", "item", or "file". :type resourceType: str :param name: The name of the resource to download. If not passed, uses the "name" field of the resource document. :type name: str or None :param token: The Girder token document or raw token string to use to authenticate when downloading. Pass `None` for anonymous downloads. :type token: dict, str, or None :param dataType: The worker `type` field. :type dataType: str :param dataFormat: The worker `format` field. :type dataFormat: str :param fetchParent: Whether to fetch the whole parent resource of the specified resource as a side effect. :type fetchParent: bool """ if isinstance(token, dict): token = token['_id'] result = { 'mode': 'girder', 'api_url': getWorkerApiUrl(), 'token': token, 'id': str(resource['_id']), 'name': name or resource['name'], 'resource_type': resourceType, 'type': dataType, 'format': dataFormat, 'fetch_parent': fetchParent } if resourceType == 'file' and not fetchParent and Setting().get(PluginSettings.DIRECT_PATH): # If we are adding a file and it exists on the local filesystem include # that location. This can permit the user of the specification to # access the file directly instead of downloading the file. adapter = File().getAssetstoreAdapter(resource) if callable(getattr(adapter, 'fullPath', None)): result['direct_path'] = adapter.fullPath(resource) return result