fix various errors
[sfa.git] / sfa / clientbin / sfaadmin.py
index 276943b..0b22998 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+import os
 import sys
 import copy
 from pprint import pformat 
@@ -126,7 +127,7 @@ class RegistryCommands(Commands):
                     os.unlink(path)
         
     
-class CerficiateCommands(Commands):
+class CertCommands(Commands):
     
     def import_gid(self, xrn):
         pass
@@ -135,6 +136,9 @@ class CerficiateCommands(Commands):
     @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
     @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='output file', default=None)
     def export(self, xrn, type=None, outfile=None):
+        from sfa.storage.alchemy import dbsession
+        from sfa.storage.model import RegRecord
+        hrn = Xrn(xrn).get_hrn()
         request=dbsession.query(RegRecord).filter_by(hrn=hrn)
         if type: request = request.filter_by(type=type)
         record=request.first()
@@ -238,24 +242,36 @@ class SliceManagerCommands(AggregateCommands):
         self.api= Generic.the_flavour().make_api(interface='slicemgr')
 
 
-CATEGORIES = {'registry': RegistryCommands,
+CATEGORIES = {'cert': CertCommands,
+              'registry': RegistryCommands,
               'aggregate': AggregateCommands,
               'slicemgr': SliceManagerCommands}
 
+def category_usage():
+    print "Available categories:"
+    for k in CATEGORIES:
+        print "\t%s" % k
+
 def main():
     argv = copy.deepcopy(sys.argv)
     script_name = argv.pop(0)
+    # ensure category is specified    
     if len(argv) < 1:
         print script_name + " category action [<args>]"
-        print "Available categories:"
-        for k in CATEGORIES:
-            print "\t%s" % k
+        category_usage()
         sys.exit(2)
 
+    # ensure category is valid
     category = argv.pop(0)
     usage = "%%prog %s action <args> [options]" % (category)
     parser = OptionParser(usage=usage)
-    command_class =  CATEGORIES[category]
+    command_class =  CATEGORIES.get(category, None)
+    if not command_class:
+        print "no such category %s " % category
+        category_usage()
+        sys.exit(2)  
+    
+    # ensure command is valid      
     command_instance = command_class()
     actions = command_instance._get_commands()
     if len(argv) < 1:
@@ -272,6 +288,7 @@ def main():
         action = argv.pop(0)
         command = getattr(command_instance, action)
 
+    # ensure options are valid
     options = getattr(command, 'options', [])
     usage = "%%prog %s %s <args> [options]" % (category, action)
     parser = OptionParser(usage=usage)
@@ -285,6 +302,7 @@ def main():
         if v is None:
             del cmd_kwds[k]
 
+    # execute commadn
     try:
         command(*cmd_args, **cmd_kwds)
         sys.exit(0)