def do_stats_general(instance: EdenInstance, options: StatsGeneralOptions) -> None: out, basic, json = options with instance.get_thrift_client_legacy(timeout=20) as client: statsMask = STATS_MOUNTS_STATS | STATS_RSS_BYTES if basic else STATS_ALL stat_info = client.getStatInfo(GetStatInfoParams(statsMask=statsMask)) if json: json_factory = TSimpleJSONProtocolFactory() out.write( ThriftSerializer.serialize(json_factory, stat_info).decode("utf-8")) else: print_stats(stat_info, out)
def _serialize(self, obj): trans = TTransport.TMemoryBuffer() prot = TSimpleJSONProtocolFactory().getProtocol(trans) obj.write(prot) return trans.getvalue()
def run(self, args: argparse.Namespace) -> int: if args.list: self._list_functions() return 0 if not args.function_name: print(f"Error: no function name specified", file=sys.stderr) print( "Use the --list argument to see a list of available functions, or " "specify a function name", file=sys.stderr, ) return 1 # Look up the function information try: fn_info = thrift.util.inspect.get_function_info( EdenService, args.function_name) except thrift.util.inspect.NoSuchFunctionError: print(f"Error: unknown function {args.function_name!r}", file=sys.stderr) print( 'Run "eden debug thrift --list" to see a list of available functions', file=sys.stderr, ) return 1 if len(args.args) != len(fn_info.arg_specs): print( f"Error: {args.function_name} requires {len(fn_info.arg_specs)} " f"arguments, but {len(args.args)} were supplied>", file=sys.stderr, ) return 1 python_args = self._eval_args(args.args, fn_info, eval_strings=args.eval_all_args) def lookup_module_member(modules, name): for module in modules: try: return getattr(module, name) except AttributeError: continue raise AttributeError(f"Failed to find {name} in {modules}") instance = cmd_util.get_eden_instance(args) with instance.get_thrift_client() as client: fn = getattr(client, args.function_name) result = fn(**python_args) if args.json: # The following back-and-forth is required to reliably # convert a Python Thrift client result into its JSON # form. The Python Thrift client returns native Python # lists and dicts for lists and maps, but they cannot # be passed directly to TSimpleJSONProtocol. Instead, # map the result back into a Thrift message, and then # serialize that as JSON. Finally, strip the message # container. # # NOTE: Stripping the root object means the output may # not have a root dict or array, which is required by # most JSON specs. But Python's json module and jq are # both fine with this deviation. result_type = lookup_module_member( [EdenService, BaseService], args.function_name + "_result") json_data = Serializer.serialize(TSimpleJSONProtocolFactory(), result_type(result)) json.dump( # If the method returns void, json_data will not # have a "success" field. Print `null` in that # case. json.loads(json_data).get("success"), sys.stdout, sort_keys=True, indent=2, ) sys.stdout.write("\n") else: print(result) return 0
def fromJson(msg, spec): return Serializer.deserialize(TSimpleJSONProtocolFactory(), msg, spec)