def New(self, request, context): result = fms.ReturnStatus() try: if request.type == PATH_FILE: new_file = FileIO(request.path, "w+") new_file.close() elif request.type == PATH_DIR: os.mkdir(request.path) elif request.type == PATH_SYMLINK: raise Exception("creation of symlinks not supported") elif request.type == PATH_PACKAGE: raise Exception("creation of packages not supported") result.code = OK 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 IOError as ioe: result.code = IO_ERROR if ioe.errno: result.error_code = ioe.errno result.error_msg = utf8(ioe.strerror) result.error_file = utf8(ioe.filename) except Exception as err: result.code = ERROR result.error_msg = utf8(err) result.error_file = utf8(request.path) return result
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 Rename(self, request, context): result = fms.ReturnStatus() try: os.rename(request.old, request.new) result.code = OK 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.old) except Exception as err: result.code = ERROR result.error_msg = utf8(err) result.error_file = utf8(request.old) return result
def Delete(self, request, context): result = fms.ReturnStatus() try: if self._is_in_ros_root(request.path): if os.path.isfile(request.path): os.remove(request.path) result.code = OK elif os.path.isdir(request.path): if not self._contains_packages(request.path): shutil.rmtree(request.path) result.code = OK else: result.code = ERROR result.error_msg = utf8("path contains packages") result.error_file = utf8(request.path) else: result.code = ERROR result.error_msg = utf8("path not in ROS_PACKAGE_PATH") 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 IOError as ioe: result.code = IO_ERROR if ioe.errno: result.error_code = ioe.errno result.error_msg = utf8(ioe.strerror) result.error_file = utf8(ioe.filename) except Exception as err: result.code = ERROR result.error_msg = utf8(err) result.error_file = utf8(request.path) return result