X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fclient%2Fsfi.py;h=72a6f86eabd17a5668eab2019bf1a9af3f306e04;hb=d2433498da80a29da51d36d6b06f1084fd7a009a;hp=726138f84ceb10bfe6c513d08443e4aff75f042a;hpb=d3bd7ebad0c47a1bb04e83770ac0350a54f70c1f;p=sfa.git diff --git a/sfa/client/sfi.py b/sfa/client/sfi.py old mode 100644 new mode 100755 index 726138f8..72a6f86e --- a/sfa/client/sfi.py +++ b/sfa/client/sfi.py @@ -12,6 +12,7 @@ import random import datetime import zlib import codecs +import pickle from lxml import etree from StringIO import StringIO from types import StringTypes, ListType @@ -80,6 +81,17 @@ def filter_records(type, records): # save methods +def save_variable_to_file(var, filename, format="text"): + f = open(filename, "w") + if format == "text": + f.write(str(var)) + elif format == "pickled": + f.write(pickle.dumps(var)) + else: + # this should never happen + print "unknown output format", format + + def save_rspec_to_file(rspec, filename): if not filename.endswith(".rspec"): filename = filename + ".rspec" @@ -88,14 +100,32 @@ def save_rspec_to_file(rspec, filename): f.close() return -def save_records_to_file(filename, recordList): - index = 0 - for record in recordList: - if index > 0: - save_record_to_file(filename + "." + str(index), record) - else: - save_record_to_file(filename, record) - index = index + 1 +def save_records_to_file(filename, recordList, format="xml"): + if format == "xml": + index = 0 + for record in recordList: + if index > 0: + save_record_to_file(filename + "." + str(index), record) + else: + save_record_to_file(filename, record) + index = index + 1 + elif format == "xmllist": + f = open(filename, "w") + f.write("\n") + for record in recordList: + record = SfaRecord(dict=record) + f.write('\n') + f.write("\n"); + f.close() + elif format == "hrnlist": + f = open(filename, "w") + for record in recordList: + record = SfaRecord(dict=record) + f.write(record.get_name() + "\n") + f.close() + else: + # this should never happen + print "unknown output format", format def save_record_to_file(filename, record): if record['type'] in ['user']: @@ -229,12 +259,23 @@ class Sfi: if command in ("resources", "show", "list", "create_gid", 'create'): parser.add_option("-o", "--output", dest="file", help="output XML to file", metavar="FILE", default=None) - + if command in ("show", "list"): parser.add_option("-f", "--format", dest="format", type="choice", help="display format ([text]|xml)", default="text", choices=("text", "xml")) + parser.add_option("-F", "--fileformat", dest="fileformat", type="choice", + help="output file format ([xml]|xmllist|hrnlist)", default="xml", + choices=("xml", "xmllist", "hrnlist")) + + if command in ("status"): + parser.add_option("-o", "--output", dest="file", + help="output dictionary to file", metavar="FILE", default=None) + parser.add_option("-F", "--fileformat", dest="fileformat", type="choice", + help="output file format ([text]|pickled)", default="text", + choices=("text","pickled")) + if command in ("delegate"): parser.add_option("-u", "--user", action="store_true", dest="delegate_user", default=False, @@ -681,14 +722,14 @@ class Sfi: list = self.registry.List(hrn, user_cred) except IndexError: raise Exception, "Not enough parameters for the 'list' command" - - # filter on person, slice, site, node, etc. + + # filter on person, slice, site, node, etc. # THis really should be in the self.filter_records funct def comment... list = filter_records(opts.type, list) for record in list: - print "%s (%s)" % (record['hrn'], record['type']) + print "%s (%s)" % (record['hrn'], record['type']) if opts.file: - save_records_to_file(opts.file, list) + save_records_to_file(opts.file, list, opts.fileformat) return # show named registry record @@ -699,7 +740,6 @@ class Sfi: hrn = args[0] user_cred = self.get_user_cred().save_to_string(save_parents=True) records = self.registry.Resolve(hrn, user_cred) - print records records = filter_records(opts.type, records) if not records: print "No record of type", opts.type @@ -719,7 +759,7 @@ class Sfi: else: print record.save_to_string() if opts.file: - save_records_to_file(opts.file, records) + save_records_to_file(opts.file, records, opts.fileformat) return def delegate(self, opts, args): @@ -1135,7 +1175,10 @@ class Sfi: call_args = [slice_urn, creds] if self.server_supports_call_id_arg(server): call_args.append(unique_call_id()) - print server.SliverStatus(*call_args) + result = server.SliverStatus(*call_args) + print result + if opts.file: + save_variable_to_file(result, opts.file, opts.fileformat) def shutdown(self, opts, args):