From 8dc285b85fb95acbcaf5beaec9c23fb5db9e25ef Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Fri, 26 Oct 2007 12:43:00 -0400 Subject: This adds alternative output formats for func-inventory, so that other programs could more easily read the tree. The default is still pretty print as that makes the git diffs nicest. --- docs/func-inventory.pod | 7 +++++++ func/overlord/inventory.py | 31 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/docs/func-inventory.pod b/docs/func-inventory.pod index ce79691..cfe362d 100644 --- a/docs/func-inventory.pod +++ b/docs/func-inventory.pod @@ -49,6 +49,13 @@ be tracked using standard git-tools such as "git log" and "gitk", when run on th Additional built in hooks to notify changes can be written using git's own trigger mechanism, though something more specific to func will likely be developed in the future -- also eliminating the need to grok git internals. +=head1 ALTERNATIVE OUTPUT FORMATS + +func-inventory can be passed a --json or --xmlrpc parameter to override the default output format. These +output formats are much less readable in the git-produced diffs, but are more easily loaded by other programs +that may want to "mine" the output of a func-inventory tree. Using --json requires that the python-simplejson +RPM be installed. + =head1 ADDITONAL RESOURCES See https://hosted.fedoraproject.org/projects/func/ for more information. diff --git a/func/overlord/inventory.py b/func/overlord/inventory.py index e75aedb..737050c 100755 --- a/func/overlord/inventory.py +++ b/func/overlord/inventory.py @@ -22,6 +22,8 @@ import optparse import sys import glob import pprint +import exceptions +import xmlrpclib from func.minion import sub_process import func.overlord.client as func_client @@ -60,7 +62,16 @@ class FuncInventory(object): dest="nogit", action="store_true", help="disable useful change tracking features") + p.add_option("-x", "--xmlrpc", dest="xmlrpc", + help="output data using XMLRPC format", + action="store_true") + p.add_option("-j", "--json", dest="json", + help="output data using JSON", + action="store_true") + + (options, args) = p.parse_args(args) + self.options = options filtered_module_list = options.modules.split(",") filtered_function_list = options.methods.split(",") @@ -97,6 +108,24 @@ class FuncInventory(object): self.git_update(options) return 1 + def format_return(self, data): + """ + The call module supports multiple output return types, the default is pprint. + """ + + if self.options.xmlrpc: + return xmlrpclib.dumps((data,"")) + + if self.options.json: + try: + import simplejson + return simplejson.dumps(data) + except ImportError: + print "ERROR: json support not found, install python-simplejson" + sys.exit(1) + + return pprint.pformat(data) + # FUTURE: skvidal points out that guest symlinking would be an interesting feature def save_results(self, options, host_name, module_name, method_name, results): @@ -105,7 +134,7 @@ class FuncInventory(object): os.makedirs(dirname) filename = os.path.join(dirname, method_name) results_file = open(filename,"w+") - data = pprint.pformat(results) + data = self.format_return(results) results_file.write(data) results_file.close() -- cgit