2 from __future__ import with_statement
7 from types import StringTypes, ListType
8 from optparse import OptionParser
10 from sfa.util.sfalogging import logger
12 from sfa.trust.certificate import Certificate
13 from sfa.trust.credential import Credential
14 from sfa.trust.gid import GID
16 from sfa.storage.record import Record
18 def determine_sfa_filekind(fn):
20 if fn.endswith('.gid'): return 'gid'
21 elif fn.endswith('.cert'): return 'certificate'
22 elif fn.endswith('cred'): return 'credential'
25 cred=Credential(filename=fn)
31 if gid.uuid: return 'gid'
35 cert = Certificate(filename = fn)
40 # if "gidCaller" in dict:
50 lastpart = hrn.split(".")[-1]
51 filename = lastpart + ".gid"
53 if os.path.exists(filename):
54 print filename, ": already exists... skipping"
57 print filename, ": extracting gid of", hrn
59 gid.save_to_file(filename, save_parents = True)
61 def extract_gids(cred, extract_parents):
62 gidCaller = cred.get_gid_caller()
66 gidObject = cred.get_gid_object()
67 if gidObject and ((gidCaller == None) or (gidCaller.get_hrn() != gidObject.get_hrn())):
70 # no such method Credential.get_parent
72 # parent = cred.get_parent()
74 # extract_gids(parent, extract_parents)
76 def handle_input (filename, options):
77 kind = determine_sfa_filekind(filename)
78 handle_input_kind (filename,options,kind)
80 def handle_input_kind (filename, options, kind):
83 # dump methods current do 'print' so let's go this road for now
84 if kind=="certificate":
85 cert=Certificate (filename=filename)
86 print '--------------------',filename,'IS A',kind
87 cert.dump(show_extensions=options.show_extensions)
88 elif kind=="credential":
89 cred = Credential(filename = filename)
90 print '--------------------',filename,'IS A',kind
91 cred.dump(dump_parents = options.dump_parents, show_xml=options.show_xml)
92 if options.extract_gids:
93 print '--------------------',filename,'embedded GIDS'
94 extract_gids(cred, extract_parents = options.dump_parents)
96 gid = GID(filename = filename)
97 print '--------------------',filename,'IS A',kind
98 gid.dump(dump_parents = options.dump_parents)
100 print "%s: unknown filekind '%s'"% (filename,kind)
103 usage = """%prog file1 [ .. filen]
104 display info on input files"""
105 parser = OptionParser(usage=usage)
107 parser.add_option("-g", "--extract-gids", action="store_true", dest="extract_gids", default=False, help="Extract GIDs from credentials")
108 parser.add_option("-p", "--dump-parents", action="store_true", dest="dump_parents", default=False, help="Show parents")
109 parser.add_option("-e", "--extensions", action="store_true", dest="show_extensions", default="False", help="Show certificate extensions")
110 parser.add_option("-v", "--verbose", action='count', dest='verbose', default=0, help="More and more verbose")
111 parser.add_option("-x", "--xml", action='store_true', dest='show_xml', default=False, help="dumps xml tree (cred. only)")
112 (options, args) = parser.parse_args()
114 logger.setLevelFromOptVerbose(options.verbose)
119 handle_input(f,options)
121 if __name__=="__main__":