def list_path(self, path): ''' Request content of path from remote gRPC-server. :param str path: a directory :retrun: a list of directories and files represented by `FileItem`. :rtype: list(:class:`file_item.FileItem`) :raise OSError: :raise IOError: :raise Exception: ''' result = [] # response = self.fm_stub.ListPath(fmsg.ListPathRequest(path=path), timeout=settings.GRPC_TIMEOUT, metadata=[('authorization', 'user:robot')]) response = self.fm_stub.ListPath(fmsg.ListPathRequest(path=path)) if response.status.code == OK: for p in response.items: item = file_item.FileItem(p.path, p.type, p.size, p.mtime) result.append(item) elif response.status.code == OS_ERROR: raise OSError(response.status.error_code, response.status.error_msg, response.status.error_file) elif response.status.code in [IO_ERROR, CHANGED_FILE, REMOVED_FILE]: raise IOError(response.status.error_code, response.status.error_msg, response.status.error_file) elif response.status.code == ERROR: raise Exception( "%s %s" % (response.status.error_msg, response.status.error_file)) return result
def test_list_path(self): fs = FileServicer() root_paths = set(os.getenv('ROS_PACKAGE_PATH').split(':')) # launch_response = fs.ListPath(fmsg.ListPathRequest(path=''), DummyContext()) # self.assertEqual(len(root_paths), len(launch_response.items), 'ROS root paths are not equal, expected: %s, got: %s' % (root_paths, launch_response.items)) launch_response = fs.ListPath(fmsg.ListPathRequest(path=os.getcwd()), DummyContext()) self.assertEqual(os.getcwd(), launch_response.path, 'reported list for different path: %s, expected: %s' % (launch_response.path, os.getcwd())) self.assertEqual(len(os.listdir(os.getcwd())), len(launch_response.items), 'reported different count of items in working directory: %s' % os.getcwd()) # test cache launch_response = fs.ListPath(fmsg.ListPathRequest(path='%s/../..' % os.getcwd()), DummyContext()) count_dirs = len([d for d in launch_response.items if d.type in [1, 3]]) launch_response = fs.ListPath(fmsg.ListPathRequest(path='%s/../..' % os.getcwd()), DummyContext()) self.assertEqual(len([d for d in launch_response.items if d.type in [1, 3]]), count_dirs, 'count of directories from cache is different') # test invalid path launch_response = fs.ListPath(fmsg.ListPathRequest(path='/unknown'), DummyContext()) self.assertEqual(0, len(launch_response.items), 'list of invalid path returns more then 0 items') self.assertEqual(fmsg.ReturnStatus.StatusType.Value('OS_ERROR'), launch_response.status.code, 'wrong status code if path not exists')
def get_file_content(self, path): ''' Requests the content of the file. :path str path: the path to the file. :retrun: the size, last modification time and the content of the file. :rtype: tuple(int, float, str) :raise OSError: :raise IOError: :raise Exception: ''' response_stream = self.fm_stub.GetFileContent( fmsg.ListPathRequest(path=path)) file_size = None file_mtime = None file_content = "" for response in response_stream: if self._running: if response.status.code == OK: if response.file.offset == 0: file_size = response.file.size file_mtime = response.file.mtime file_content += utf8(response.file.data) elif response.status.code == OS_ERROR: raise OSError(response.status.error_code, response.status.error_msg, response.status.error_file) elif response.status.code in [ IO_ERROR, CHANGED_FILE, REMOVED_FILE ]: raise IOError(response.status.error_code, response.status.error_msg, response.status.error_file) elif response.status.code == ERROR: raise Exception("%s %s" % (response.status.error_msg, response.status.error_file)) else: raise Exception( "receiving for '%s' aborted! %d of %d transmitted." % (response.file.path, response.file.offset, response.file.size)) return (file_size, file_mtime, file_content)