2 from __future__ import with_statement
4 # sfi -- slice-based facility interface
9 from util.cert import Keypair, Certificate
10 from util.credential import Credential
11 from util.geniclient import GeniClient
12 from util.record import GeniRecord
13 from util.gid import GID
26 long_opts = ["infile=", "outfile=", "email=", "ip=", "dns=", "gidfile=", "hrn=", "type=", "addresearcher=", "delresearcher=", "dump"]
29 print "syntax: editRecord.py <options>"
30 print " --help ... show help"
31 print " --infile <name> ... read record from file"
32 print " --outfile <name> ... write record to file"
33 print " --dump ... dump record to stdout"
34 print " --gidfile <fn> ... load gid from file"
35 print " --hrn <name> ... set hrn"
36 print " --type <type> ... set type (user|slice|sa|ma|...)"
37 print " --email <addr> ... user: set email address"
38 print " --ip <addr> ... node: set ip address"
39 print " --dns <hostname> ... node: set hostname"
40 print " --addresearcher <hrn> ... slice: add researcher"
41 print " --delresearcher <hrn> ... slice: delete researcher"
43 def process_options():
44 global infile, outfile
45 global email, ip, dns, gidfile, hrn, type
49 (options, args) = getopt.getopt(sys.argv[1:], '', long_opts)
57 elif name == "--infile":
59 elif name == "--outfile":
61 elif name == "--email":
67 elif name == "--gidfile":
71 elif name == "--type":
73 elif name == "--dump":
75 elif name == "--addresearcher":
76 researcher.append(val)
77 elif name == "--delresearcher":
78 researcher.append("-" + val)
80 def errorcheck(record):
81 geni_info = record.get_geni_info()
84 print "Warning: no type specified"
85 if not record.type in ["user", "sa", "ma", "slice", "node"]:
86 print "Warning: unknown record type"
88 print "Warning: unknown record name"
90 print "Warning: unknown record gid"
92 if record.type == "user":
93 if not geni_info.get("email",None):
94 print "Warning: unknown email in user record"
96 if record.type == "node":
97 if not geni_info.get("ip",None):
98 print "Warning: unknown ip in node record"
99 if not geni_info.get("dns",None):
100 print "Warning: unknown dns in node record"
102 # updates is a list of items to add or remove. If an item starts with "-", then
103 # it will be removed. Otherwise it will be added
104 def update_list(dict, listname, updates):
105 list = dict.get(listname, [])
107 if hrn.startswith("-"):
110 list.delete(real_hrn)
115 dict[listname] = list
120 # if the user didn't tell us to do much of anything, then maybe he needs
122 if (not infile) and (not outfile) and (not dump):
127 str = file(infile, "r").read()
128 record = GeniRecord(string = str)
130 record = GeniRecord()
132 geni_info = record.get_geni_info()
133 geni_info_orig = geni_info.copy()
136 geni_info["email"] = email
142 geni_info["dns"] = dns
151 gid_str = file(gidfile, "r").read()
152 gid = GID(string=gid_str)
156 update_list(geni_info, "researcher", researcher)
158 if (geni_info != geni_info_orig):
159 record.set_geni_info(geni_info)
167 str = record.save_to_string()
168 file(outfile, "w").write(str)
169 print "wrote record to", outfile
171 if __name__=="__main__":