RSpecVersion.todict() needs to turn values() into a list()
[sfa.git] / sfa / client / sfaadmin.py
index 169bfed..49757b3 100755 (executable)
@@ -1,4 +1,6 @@
 #!/usr/bin/python
+from __future__ import print_function
+
 import os
 import sys
 import copy
@@ -8,14 +10,14 @@ from optparse import OptionParser
 from sfa.generic import Generic
 from sfa.util.xrn import Xrn
 from sfa.storage.record import Record 
-from sfa.client.sfi import save_records_to_file
+
 from sfa.trust.hierarchy import Hierarchy
 from sfa.trust.gid import GID
 from sfa.trust.certificate import convert_public_key
 
-from sfa.client.candidates import Candidates
-
 from sfa.client.common import optparse_listvalue_callback, optparse_dictvalue_callback, terminal_render, filter_records
+from sfa.client.candidates import Candidates
+from sfa.client.sfi import save_records_to_file
 
 pprinter = PrettyPrinter(indent=4)
 
@@ -24,9 +26,9 @@ try:
 except:
     help_basedir='*unable to locate Hierarchy().basedir'
 
-def args(*args, **kwargs):
+def add_options(*args, **kwargs):
     def _decorator(func):
-        func.__dict__.setdefault('options', []).insert(0, (args, kwargs))
+        func.__dict__.setdefault('add_options', []).insert(0, (args, kwargs))
         return func
     return _decorator
 
@@ -48,11 +50,11 @@ class RegistryCommands(Commands):
         version = self.api.manager.GetVersion(self.api, {})
         pprinter.pprint(version)
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='authority to list (hrn/urn - mandatory)') 
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type', default='all') 
-    @args('-r', '--recursive', dest='recursive', metavar='<recursive>', help='list all child records', 
-          action='store_true', default=False)
-    @args('-v', '--verbose', dest='verbose', action='store_true', default=False)
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='authority to list (hrn/urn - mandatory)') 
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type', default='all') 
+    @add_options('-r', '--recursive', dest='recursive', metavar='<recursive>', help='list all child records', 
+                 action='store_true', default=False)
+    @add_options('-v', '--verbose', dest='verbose', action='store_true', default=False)
     def list(self, xrn, type=None, recursive=False, verbose=False):
         """List names registered at a given authority - possibly filtered by type"""
         xrn = Xrn(xrn, type) 
@@ -66,11 +68,11 @@ class RegistryCommands(Commands):
         terminal_render (list, options)
 
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
-    @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file') 
-    @args('-f', '--format', dest='format', metavar='<display>', type='choice', 
-          choices=('text', 'xml', 'simple'), help='display record in different formats') 
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
+    @add_options('-o', '--outfile', dest='outfile', metavar='<outfile>', help='save record to file') 
+    @add_options('-f', '--format', dest='format', metavar='<display>', type='choice', 
+                 choices=('text', 'xml', 'simple'), help='display record in different formats') 
     def show(self, xrn, type=None, format=None, outfile=None):
         """Display details for a registered object"""
         records = self.api.manager.Resolve(self.api, xrn, type, details=True)
@@ -81,10 +83,9 @@ class RegistryCommands(Commands):
             save_records_to_file(outfile, records)  
 
 
-    def _record_dict(self, xrn=None, type=None, 
-                     url=None, description=None, email='', 
-                     key=None, 
-                     slices=[], researchers=[], pis=[], extras={}):
+    def _record_dict(self, xrn, type, email, key, 
+                     slices, researchers, pis, 
+                     url, description, extras):
         record_dict = {}
         if xrn:
             if type:
@@ -103,44 +104,43 @@ class RegistryCommands(Commands):
                 pubkey = open(key, 'r').read()
             except IOError:
                 pubkey = key
-            record_dict['keys'] = [pubkey]
+            record_dict['reg-keys'] = [pubkey]
         if slices:
             record_dict['slices'] = slices
         if researchers:
-            record_dict['researcher'] = researchers
+            record_dict['reg-researchers'] = researchers
         if email:
             record_dict['email'] = email
         if pis:
-            record_dict['pi'] = pis
+            record_dict['reg-pis'] = pis
         if extras:
             record_dict.update(extras)
         return record_dict
 
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type (mandatory)',)
-    @args('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False, help='check all users GID')
-    @args('-v', '--verbose', dest='verbose', metavar='<verbose>', action='store_true', default=False, help='verbose mode: display user\'s hrn ')
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn', default=None)
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type (mandatory)',)
+    @add_options('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False, help='check all users GID')
+    @add_options('-v', '--verbose', dest='verbose', metavar='<verbose>', action='store_true', default=False, help='verbose mode: display user\'s hrn ')
     def check_gid(self, xrn=None, type=None, all=None, verbose=None):
         """Check the correspondance between the GID and the PubKey"""
 
         # db records
-        from sfa.storage.alchemy import dbsession
         from sfa.storage.model import RegRecord
-        db_query = dbsession.query(RegRecord).filter_by(type=type)
+        db_query = self.api.dbsession().query(RegRecord).filter_by(type=type)
         if xrn and not all:
             hrn = Xrn(xrn).get_hrn()
             db_query = db_query.filter_by(hrn=hrn)
         elif all and xrn:
-            print "Use either -a or -x <xrn>, not both !!!"
+            print("Use either -a or -x <xrn>, not both !!!")
             sys.exit(1)
         elif not all and not xrn:
-            print "Use either -a or -x <xrn>, one of them is mandatory !!!"
+            print("Use either -a or -x <xrn>, one of them is mandatory !!!")
             sys.exit(1)
 
         records = db_query.all()
         if not records:
-            print "No Record found"
+            print("No Record found")
             sys.exit(1)
 
         OK = []
@@ -173,81 +173,88 @@ class RegistryCommands(Commands):
                  NOK.append(record.hrn)
 
         if not verbose:
-            print "Users NOT having a PubKey: %s\n\
+            print("Users NOT having a PubKey: %s\n\
 Users having a non RSA PubKey: %s\n\
 Users having a GID/PubKey correpondence OK: %s\n\
-Users having a GID/PubKey correpondence Not OK: %s\n"%(len(NOKEY), len(ERROR), len(OK), len(NOK))
+Users having a GID/PubKey correpondence Not OK: %s\n"%(len(NOKEY), len(ERROR), len(OK), len(NOK)))
         else:
-            print "Users NOT having a PubKey: %s and are: \n%s\n\n\
+            print("Users NOT having a PubKey: %s and are: \n%s\n\n\
 Users having a non RSA PubKey: %s and are: \n%s\n\n\
 Users having a GID/PubKey correpondence OK: %s and are: \n%s\n\n\
-Users having a GID/PubKey correpondence NOT OK: %s and are: \n%s\n\n"%(len(NOKEY),NOKEY, len(ERROR), ERROR, len(OK), OK, len(NOK), NOK)
-
-
-
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
-    @args('-e', '--email', dest='email', default="",
-          help="email (mandatory for users)")
-    @args('-u', '--url', dest='url', metavar='<url>', default=None,
-          help="URL, useful for slices")
-    @args('-d', '--description', dest='description', metavar='<description>', 
-          help='Description, useful for slices', default=None)
-    @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file', 
-          default=None)
-    @args('-s', '--slices', dest='slices', metavar='<slices>', help='Set/replace slice xrns', 
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
-    @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='Set/replace slice researchers', 
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
-    @args('-p', '--pis', dest='pis', metavar='<PIs>', 
-          help='Set/replace Principal Investigators/Project Managers', 
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
-    @args('-X','--extra',dest='extras',default={},type='str',metavar="<EXTRA_ASSIGNS>", action="callback", callback=optparse_dictvalue_callback, nargs=1, help="set extra/testbed-dependent flags, e.g. --extra enabled=true")
-    def register(self, xrn, type=None, url=None, description=None, key=None, slices='', 
-                 pis='', researchers='',email='', extras={}):
+Users having a GID/PubKey correpondence NOT OK: %s and are: \n%s\n\n"%(len(NOKEY),NOKEY, len(ERROR), ERROR, len(OK), OK, len(NOK), NOK))
+
+
+
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
+    @add_options('-e', '--email', dest='email', default="",
+                 help="email (mandatory for users)")
+    @add_options('-u', '--url', dest='url', metavar='<url>', default=None,
+                 help="URL, useful for slices")
+    @add_options('-d', '--description', dest='description', metavar='<description>', 
+                 help='Description, useful for slices', default=None)
+    @add_options('-k', '--key', dest='key', metavar='<key>', help='public key string or file', 
+                 default=None)
+    @add_options('-s', '--slices', dest='slices', metavar='<slices>', help='Set/replace slice xrns', 
+                 default='', type="str", action='callback', callback=optparse_listvalue_callback)
+    @add_options('-r', '--researchers', dest='researchers', metavar='<researchers>', help='Set/replace slice researchers', 
+                 default='', type="str", action='callback', callback=optparse_listvalue_callback)
+    @add_options('-p', '--pis', dest='pis', metavar='<PIs>', 
+                 help='Set/replace Principal Investigators/Project Managers', 
+                 default='', type="str", action='callback', callback=optparse_listvalue_callback)
+    @add_options('-X','--extra',dest='extras',default={},type='str',metavar="<EXTRA_ASSIGNS>", 
+                 action="callback", callback=optparse_dictvalue_callback, nargs=1, 
+                 help="set extra/testbed-dependent flags, e.g. --extra enabled=true")
+    def register(self, xrn, type=None, email='', key=None, 
+                 slices='', pis='', researchers='',
+                 url=None, description=None, extras={}):
         """Create a new Registry record"""
-        record_dict = self._record_dict(xrn=xrn, type=type, url=url, key=key, 
-                                        slices=slices, researchers=researchers, email=email, pis=pis, extras=extras)
+        record_dict = self._record_dict(xrn=xrn, type=type, email=email, key=key, 
+                                        slices=slices, researchers=researchers, pis=pis,
+                                        url=url, description=description, extras=extras)
         self.api.manager.Register(self.api, record_dict)         
 
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
-    @args('-u', '--url', dest='url', metavar='<url>', help='URL', default=None)
-    @args('-d', '--description', dest='description', metavar='<description>',
-          help='Description', default=None)
-    @args('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
-          default=None)
-    @args('-s', '--slices', dest='slices', metavar='<slices>', help='Set/replace slice xrns',
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
-    @args('-r', '--researchers', dest='researchers', metavar='<researchers>', help='Set/replace slice researchers',
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
-    @args('-p', '--pis', dest='pis', metavar='<PIs>',
-          help='Set/replace Principal Investigators/Project Managers',
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
-    @args('-X','--extra',dest='extras',default={},type='str',metavar="<EXTRA_ASSIGNS>", action="callback", callback=optparse_dictvalue_callback, nargs=1, help="set extra/testbed-dependent flags, e.g. --extra enabled=true")
-    def update(self, xrn, type=None, url=None, description=None, key=None, slices='', 
-               pis='', researchers='', extras={}):
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
+    @add_options('-u', '--url', dest='url', metavar='<url>', help='URL', default=None)
+    @add_options('-d', '--description', dest='description', metavar='<description>',
+                 help='Description', default=None)
+    @add_options('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
+                 default=None)
+    @add_options('-s', '--slices', dest='slices', metavar='<slices>', help='Set/replace slice xrns',
+                 default='', type="str", action='callback', callback=optparse_listvalue_callback)
+    @add_options('-r', '--researchers', dest='researchers', metavar='<researchers>', help='Set/replace slice researchers',
+                 default='', type="str", action='callback', callback=optparse_listvalue_callback)
+    @add_options('-p', '--pis', dest='pis', metavar='<PIs>',
+                 help='Set/replace Principal Investigators/Project Managers',
+                 default='', type="str", action='callback', callback=optparse_listvalue_callback)
+    @add_options('-X','--extra',dest='extras',default={},type='str',metavar="<EXTRA_ASSIGNS>", 
+                 action="callback", callback=optparse_dictvalue_callback, nargs=1, 
+                 help="set extra/testbed-dependent flags, e.g. --extra enabled=true")
+    def update(self, xrn, type=None, email='', key=None, 
+               slices='', pis='', researchers='',
+               url=None, description=None, extras={}):
         """Update an existing Registry record""" 
-        print 'incoming PIS',pis
-        record_dict = self._record_dict(xrn=xrn, type=type, url=url, description=description, 
-                                        key=key, slices=slices, researchers=researchers, pis=pis, extras=extras)
+        record_dict = self._record_dict(xrn=xrn, type=type, email=email, key=key, 
+                                        slices=slices, researchers=researchers, pis=pis,
+                                        url=url, description=description, extras=extras)
         self.api.manager.Update(self.api, record_dict)
         
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
     def remove(self, xrn, type=None):
         """Remove given object from the registry"""
         xrn = Xrn(xrn, type)
         self.api.manager.Remove(self.api, xrn)            
 
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type', default=None) 
     def credential(self, xrn, type=None):
         """Invoke GetCredential"""
         cred = self.api.manager.GetCredential(self.api, xrn, type, self.api.hrn)
-        print cred
+        print(cred)
    
 
     def import_registry(self):
@@ -260,14 +267,14 @@ Users having a GID/PubKey correpondence NOT OK: %s and are: \n%s\n\n"%(len(NOKEY
         """Initialize or upgrade the db"""
         from sfa.storage.dbschema import DBSchema
         dbschema=DBSchema()
-        dbschema.init_or_upgrade
+        dbschema.init_or_upgrade()
     
-    @args('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False,
-          help='Remove all registry records and all files in %s area' % help_basedir)
-    @args('-c', '--certs', dest='certs', metavar='<certs>', action='store_true', default=False,
-          help='Remove all cached certs/gids found in %s' % help_basedir )
-    @args('-0', '--no-reinit', dest='reinit', metavar='<reinit>', action='store_false', default=True,
-          help='Prevents new DB schema from being installed after cleanup')
+    @add_options('-a', '--all', dest='all', metavar='<all>', action='store_true', default=False,
+                 help='Remove all registry records and all files in %s area' % help_basedir)
+    @add_options('-c', '--certs', dest='certs', metavar='<certs>', action='store_true', default=False,
+                 help='Remove all cached certs/gids found in %s' % help_basedir )
+    @add_options('-0', '--no-reinit', dest='reinit', metavar='<reinit>', action='store_false', default=True,
+                 help='Prevents new DB schema from being installed after cleanup')
     def nuke(self, all=False, certs=False, reinit=True):
         """Cleanup local registry DB, plus various additional filesystem cleanups optionally"""
         from sfa.storage.dbschema import DBSchema
@@ -306,19 +313,21 @@ Users having a GID/PubKey correpondence NOT OK: %s and are: \n%s\n\n"%(len(NOKEY
         
     
 class CertCommands(Commands):
+
+    def __init__(self, *args, **kwds):
+        self.api= Generic.the_flavour().make_api(interface='registry')
     
     def import_gid(self, xrn):
         pass
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
-    @args('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
-    @args('-o', '--outfile', dest='outfile', metavar='<outfile>', help='output file', default=None)
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)')
+    @add_options('-t', '--type', dest='type', metavar='<type>', help='object type', default=None)
+    @add_options('-o', '--outfile', dest='outfile', metavar='<outfile>', help='output file', default=None)
     def export(self, xrn, type=None, outfile=None):
         """Fetch an object's GID from the Registry"""  
-        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)
+        request=self.api.dbsession().query(RegRecord).filter_by(hrn=hrn)
         if type: request = request.filter_by(type=type)
         record=request.first()
         if record:
@@ -330,19 +339,19 @@ class CertCommands(Commands):
                 auth_info = hierarchy.get_auth_info(hrn)
                 gid = auth_info.gid_object
             except:
-                print "Record: %s not found" % hrn
+                print("Record: %s not found" % hrn)
                 sys.exit(1)
         # save to file
         if not outfile:
             outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
         gid.save_to_file(outfile, save_parents=True)
         
-    @args('-g', '--gidfile', dest='gid', metavar='<gid>', help='path of gid file to display (mandatory)') 
+    @add_options('-g', '--gidfile', dest='gid', metavar='<gid>', help='path of gid file to display (mandatory)') 
     def display(self, gidfile):
         """Print contents of a GID file"""
         gid_path = os.path.abspath(gidfile)
         if not gid_path or not os.path.isfile(gid_path):
-            print "No such gid file: %s" % gidfile
+            print("No such gid file: %s" % gidfile)
             sys.exit(1)
         gid = GID(filename=gid_path)
         gid.dump(dump_parents=True)
@@ -358,25 +367,25 @@ class AggregateCommands(Commands):
         version = self.api.manager.GetVersion(self.api, {})
         pprinter.pprint(version)
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='object hrn/urn (mandatory)') 
     def status(self, xrn):
         """Retrieve the status of the slivers belonging to the named slice (Status)"""
         urns = [Xrn(xrn, 'slice').get_urn()]
         status = self.api.manager.Status(self.api, urns, [], {})
         pprinter.pprint(status)
  
-    @args('-r', '--rspec-version', dest='rspec_version', metavar='<rspec_version>', 
-          default='GENI', help='version/format of the resulting rspec response')  
+    @add_options('-r', '--rspec-version', dest='rspec_version', metavar='<rspec_version>', 
+                 default='GENI', help='version/format of the resulting rspec response')  
     def resources(self, rspec_version='GENI'):
         """Display the available resources at an aggregate"""  
         options = {'geni_rspec_version': rspec_version}
-        print options
+        print(options)
         resources = self.api.manager.ListResources(self.api, [], options)
-        print resources
+        print(resources)
         
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
-    @args('-r', '--rspec', dest='rspec', metavar='<rspec>', help='rspec file (mandatory)')
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
+    @add_options('-r', '--rspec', dest='rspec', metavar='<rspec>', help='rspec file (mandatory)')
     def allocate(self, xrn, rspec):
         """Allocate slivers"""
         xrn = Xrn(xrn, 'slice')
@@ -384,21 +393,21 @@ class AggregateCommands(Commands):
         rspec_string = open(rspec).read()
         options={}
         manifest = self.api.manager.Allocate(self.api, slice_urn, [], rspec_string, options)
-        print manifest
+        print(manifest)
 
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
     def provision(self, xrn):
         """Provision slivers"""
         xrn = Xrn(xrn, 'slice')
         slice_urn=xrn.get_urn()
         options={}
         manifest = self.api.manager.provision(self.api, [slice_urn], [], options)
-        print manifest
+        print(manifest)
 
 
 
-    @args('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
+    @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
     def delete(self, xrn):
         """Delete slivers""" 
         self.api.manager.Delete(self.api, [xrn], [], {})
@@ -426,14 +435,14 @@ class SfaAdmin:
         return (full_name,SfaAdmin.CATEGORIES[full_name])
 
     def summary_usage (self, category=None):
-        print "Usage:", self.script_name + " category command [<options>]"
+        print("Usage:", self.script_name + " category command [<options>]")
         if category and category in SfaAdmin.CATEGORIES: 
             categories=[category]
         else:
             categories=SfaAdmin.CATEGORIES
         for c in categories:
             cls=SfaAdmin.CATEGORIES[c]
-            print "==================== category=%s"%c
+            print("==================== category=%s"%c)
             names=cls.__dict__.keys()
             names.sort()
             for name in names:
@@ -441,15 +450,15 @@ class SfaAdmin:
                 if name.startswith('_'): continue
                 margin=15
                 format="%%-%ds"%margin
-                print "%-15s"%name,
+                print("%-15s"%name, end=' ')
                 doc=getattr(method,'__doc__',None)
                 if not doc: 
-                    print "<missing __doc__>"
+                    print("<missing __doc__>")
                     continue
                 lines=[line.strip() for line in doc.split("\n")]
                 line1=lines.pop(0)
-                print line1
-                for extra_line in lines: print margin*" ",extra_line
+                print(line1)
+                for extra_line in lines: print(margin*" ",extra_line)
         sys.exit(2)
 
     def main(self):
@@ -484,11 +493,10 @@ class SfaAdmin:
             self.summary_usage(category_name)
 
         # ensure options are valid
-        options = getattr(command, 'options', [])
         usage = "%%prog %s %s [options]" % (category_name, command_name)
         parser = OptionParser(usage=usage)
-        for arg, kwd in options:
-            parser.add_option(*arg, **kwd)
+        for args, kwdargs in getattr(command, 'add_options', []):
+            parser.add_option(*args, **kwdargs)
         (opts, cmd_args) = parser.parse_args(argv)
         cmd_kwds = vars(opts)
 
@@ -503,13 +511,14 @@ class SfaAdmin:
             command(*cmd_args, **cmd_kwds)
             sys.exit(0)
         except TypeError:
-            print "Possible wrong number of arguments supplied"
+            print("Possible wrong number of arguments supplied")
             #import traceback
             #traceback.print_exc()
-            print command.__doc__
+            print(command.__doc__)
             parser.print_help()
+            sys.exit(1)
             #raise
         except Exception:
-            print "Command failed, please check log for more info"
+            print("Command failed, please check log for more info")
             raise
-
+            sys.exit(1)