From f767cd9ff2f31a82be288cfa521fbdf876610aaf Mon Sep 17 00:00:00 2001 From: Faiyaz Ahmed Date: Mon, 15 Jun 2009 19:01:18 +0000 Subject: [PATCH] initial commit. --- cmdline/configSfi.sh | 6 +-- cmdline/setRecord.py | 108 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100755 cmdline/setRecord.py diff --git a/cmdline/configSfi.sh b/cmdline/configSfi.sh index 4765ddd3..07ec014d 100644 --- a/cmdline/configSfi.sh +++ b/cmdline/configSfi.sh @@ -6,8 +6,8 @@ export SFI_AUTH=plc.princeton export SFI_USER=$SFI_AUTH.faiyaza -#export SFI_REGISTRY=http://128.112.139.120:12345/ +export SFI_REGISTRY=http://128.112.139.120:12345/ #export SFI_SM=http://128.112.139.120:12347/ -export SFI_REGISTRY=http://www.planet-lab.org:12345/ -export SFI_SM=http://www.vini-veritas.org:12347/ +#export SFI_REGISTRY=http://www.planet-lab.org:12345/ +export SFI_SM=http://www.planet-lab.org:12347/ diff --git a/cmdline/setRecord.py b/cmdline/setRecord.py new file mode 100755 index 00000000..c710be15 --- /dev/null +++ b/cmdline/setRecord.py @@ -0,0 +1,108 @@ +#!/usr/bin/python + +""" +Updates record objects + + +faiyaza at cs dot princeton dot edu +Copyright (c) 2009 Board of Trustees, Princeton University + +$Id$ +$HeadURL$ +""" + +import sys +import os +from optparse import OptionParser +from pprint import pprint + +from geni.util.rspec import RecordSpec + + +def create_parser(): + command = sys.argv[0] + argv = sys.argv[1:] + usage = "%(command)s [options]" % locals() + description = """getRecord will open a record file and print all key/values, or filter results based on a given key or set of keys.""" + parser = OptionParser(usage=usage,description=description) + parser.add_option("-i", "--infile", dest="infile", metavar="FILE", + default=None, help = "record file path") + parser.add_option("-d", "--debug", dest="DEBUG", action="store_true", + default=False, help = "record file path") + + return parser + +def printRecord(r, depth = 0): + line = "" + # Set the dept + for tab in range(0,depth): line += " " + # check if it's nested + if type(r) == dict: + for i in r.keys(): + print line + "%s:" % i + printRecord(r[i], depth + 1) + elif type(r) in (tuple, list): + for j in r: printRecord(j, depth + 1) + # not nested so just print. + else: + print line + "%s" % r + +def editDict(replacewith, recordDict, options): + # first we find the part of the tree we want to replace + # and check it exists. + for (key, val) in replacewith.items(): + if not recordDict.has_key(key): + print "Cannot find key %s in record %s. Adding new key."\ + % (key, options.infile) + else: + print "Replacing %s = %s with %s\n" % (key, recordDict[key], val) + recordDict[key] = val + + +def patchDict(args): + """ + Takes the arg list, seperates into tag/value, creates a dict. + """ + patch = {} + for vect in args: + if vect.count("="): + patch[vect.split("=")[0]] = vect.split("=")[1] + else: + raise TypeError, "Argument error: Records are updated with key=val\n" \ + "%s Unknown key/val" % vect + return patch + + +def main(): + parser = create_parser(); + (options, args) = parser.parse_args() + + # Check the the file was specified + if not options.infile: + print "You must specify a record file" + return -1 + try: + print "Openning %s.\n" % options.infile + f = open(options.infile) + record = RecordSpec(xml = f) + f.close() + except: raise + + + if args: + patch = patchDict(args) + if options.DEBUG: print "Replace w/ %s" % patch + editDict(patch, record.dict["record"], options) + if options.DEBUG: + print "New Record:\n%s" % record.dict + + printRecord(record.dict) + record.rootNode = record.dict2dom(record.dict) + s = record.toxml() + f = open(options.infile,"w") + f.write(s) + +if __name__ == '__main__': + try: main() + except Exception, e: + print e -- 2.43.0