def CopyFileTo(self, request, context): result = fms.ReturnStatus() try: path = request.path dest_uri, _dest_path = nmdurl.split(request.uri) # get package from path dpath = path if os.path.basename(path) == os.path.basename(_dest_path): dpath = _dest_path pname, ppath = package_name(dpath) if pname is not None: prest = dpath.replace(ppath, '') with open(path, 'r') as outfile: mtime = 0.0 if request.overwrite else os.path.getmtime( path) content = outfile.read() # get channel to the remote grpc server # TODO: get secure channel, if available channel = remote.get_insecure_channel(dest_uri) if channel is not None: # save file on remote server fs = fms_grpc.FileServiceStub(channel) response_stream = fs.SaveFileContent( self._gen_save_content_list( prest, content, mtime, pname), timeout=settings.GRPC_TIMEOUT) for response in response_stream: if response.status.code == OK: result.code = OK else: result.code = response.status.code result.error_code = response.status.error_code result.error_msg = response.status.error_msg result.error_file = response.status.error_file return result else: result.code = ERROR result.error_msg = utf8( "can not establish insecure channel to '%s'" % dest_uri) result.error_file = utf8(request.path) else: result.code = ERROR result.error_msg = utf8( "no package found! Only launch files from packages can be copied!" ) result.error_file = utf8(request.path) except OSError as ose: result.code = OS_ERROR if ose.errno: result.error_code = ose.errno result.error_msg = utf8(ose.strerror) result.error_file = utf8(request.path) except Exception as err: result.code = ERROR result.error_msg = utf8(err) result.error_file = utf8(request.path) return result
def __init__(self, channel): self.fm_stub = fgrpc.FileServiceStub(channel) self._running = True