removed imports on sfa.util.rspec that is gone
[sfa.git] / sfa / client / sfadump.py
1 #! /usr/bin/env python
2 from __future__ import with_statement
3
4 import sys
5 import os, os.path
6 import tempfile
7 import xmlrpclib
8 from types import StringTypes, ListType
9 from optparse import OptionParser
10
11 from sfa.trust.certificate import Certificate
12 from sfa.trust.credential import Credential
13 from sfa.trust.gid import GID
14 from sfa.util.record import SfaRecord
15 from sfa.util.sfalogging import logger
16
17 def determine_sfa_filekind(fn):
18
19     if fn.endswith('.gid'): return 'gid'
20     elif fn.endswith('.cert'): return 'certificate'
21     elif fn.endswith('cred'): return 'credential'
22
23     try:
24         cred=Credential(filename=fn)
25         return 'credential'
26     except: pass
27
28     try: 
29         gid=GID(filename=fn)
30         if gid.uuid: return 'gid'
31     except: pass
32
33     try:
34         cert = Certificate(filename = fn)
35         return 'certificate'
36     except: pass
37
38     # to be completed
39 #    if "gidCaller" in dict:
40 #        return "credential"
41 #
42 #    if "uuid" in dict:
43 #        return "gid"
44
45     return "unknown"
46
47 def save_gid(gid):
48    hrn = gid.get_hrn()
49    lastpart = hrn.split(".")[-1]
50    filename = lastpart + ".gid"
51
52    if os.path.exists(filename):
53        print filename, ": already exists... skipping"
54        return
55
56    print filename, ": extracting gid of", hrn
57
58    gid.save_to_file(filename, save_parents = True)
59
60 def extract_gids(cred, extract_parents):
61    gidCaller = cred.get_gid_caller()
62    if gidCaller:
63        save_gid(gidCaller)
64
65    gidObject = cred.get_gid_object()
66    if gidObject and ((gidCaller == None) or (gidCaller.get_hrn() != gidObject.get_hrn())):
67        save_gid(gidObject)
68
69    # no such method Credential.get_parent
70 #   if extract_parents:
71 #       parent = cred.get_parent()
72 #       if parent:
73 #           extract_gids(parent, extract_parents)
74
75 def handle_input (filename, options):
76     kind = determine_sfa_filekind(filename)
77     handle_input_kind (filename,options,kind)
78
79 def handle_input_kind (filename, options, kind):
80     
81
82 # dump methods current do 'print' so let's go this road for now
83     if kind=="certificate":
84         cert=Certificate (filename=filename)
85         print '--------------------',filename,'IS A',kind
86         cert.dump(show_extensions=options.show_extensions)
87     elif kind=="credential":
88         cred = Credential(filename = filename)
89         print '--------------------',filename,'IS A',kind
90         cred.dump(dump_parents = options.dump_parents)
91         if options.extract_gids:
92             print '--------------------',filename,'embedded GIDS'
93             extract_gids(cred, extract_parents = options.dump_parents)
94     elif kind=="gid":
95         gid = GID(filename = filename)
96         print '--------------------',filename,'IS A',kind
97         gid.dump(dump_parents = options.dump_parents)
98     else:
99         print "%s: unknown filekind '%s'"% (filename,kind)
100
101 def main():
102     usage = """%prog file1 [ .. filen]
103 display info on input files"""
104     parser = OptionParser(usage=usage)
105
106     parser.add_option("-g", "--extract-gids", action="store_true", dest="extract_gids", default=False, help="Extract GIDs from credentials")
107     parser.add_option("-p", "--dump-parents", action="store_true", dest="dump_parents", default=False, help="Show parents")
108     parser.add_option("-e", "--extensions", action="store_true", dest="show_extensions", default="False", help="Show certificate extensions")
109     parser.add_option("-v", "--verbose", action='count', dest='verbose', default=0)
110     (options, args) = parser.parse_args()
111
112     logger.setLevelFromOptVerbose(options.verbose)
113     if len(args) <= 0:
114         parser.print_help()
115         sys.exit(1)
116     for f in args: 
117         handle_input(f,options)
118
119 if __name__=="__main__":
120    main()