2to3 -f print
[sfa.git] / sfa / client / sfaadmin.py
index 2ddf609..49757b3 100755 (executable)
@@ -1,4 +1,6 @@
 #!/usr/bin/python
+from __future__ import print_function
+
 import os
 import sys
 import copy
@@ -51,7 +53,7 @@ class RegistryCommands(Commands):
     @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)
+                 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"""
@@ -70,7 +72,7 @@ class RegistryCommands(Commands):
     @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') 
+                 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,9 +83,9 @@ class RegistryCommands(Commands):
             save_records_to_file(outfile, records)  
 
 
-    def _record_dict(self, xrn=None, type=None, email='', key=None
-                     slices=[], researchers=[], pis=[]
-                     url=None, description=None, extras={}):
+    def _record_dict(self, xrn, type, email, key
+                     slices, researchers, pis
+                     url, description, extras):
         record_dict = {}
         if xrn:
             if type:
@@ -102,7 +104,7 @@ 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:
@@ -110,7 +112,7 @@ class RegistryCommands(Commands):
         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
@@ -130,15 +132,15 @@ class RegistryCommands(Commands):
             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 = []
@@ -171,36 +173,38 @@ 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)
+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)")
+                 help="email (mandatory for users)")
     @add_options('-u', '--url', dest='url', metavar='<url>', default=None,
-          help="URL, useful for slices")
+                 help="URL, useful for slices")
     @add_options('-d', '--description', dest='description', metavar='<description>', 
-          help='Description, useful for slices', default=None)
+                 help='Description, useful for slices', default=None)
     @add_options('-k', '--key', dest='key', metavar='<key>', help='public key string or file', 
-          default=None)
+                 default=None)
     @add_options('-s', '--slices', dest='slices', metavar='<slices>', help='Set/replace slice xrns', 
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
+                 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)
+                 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")
+                 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={}):
@@ -215,17 +219,19 @@ Users having a GID/PubKey correpondence NOT OK: %s and are: \n%s\n\n"%(len(NOKEY
     @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)
+                 help='Description', default=None)
     @add_options('-k', '--key', dest='key', metavar='<key>', help='public key string or file',
-          default=None)
+                 default=None)
     @add_options('-s', '--slices', dest='slices', metavar='<slices>', help='Set/replace slice xrns',
-          default='', type="str", action='callback', callback=optparse_listvalue_callback)
+                 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)
+                 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")
+                 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={}):
@@ -248,7 +254,7 @@ Users having a GID/PubKey correpondence NOT OK: %s and are: \n%s\n\n"%(len(NOKEY
     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):
@@ -264,11 +270,11 @@ Users having a GID/PubKey correpondence NOT OK: %s and are: \n%s\n\n"%(len(NOKEY
         dbschema.init_or_upgrade()
     
     @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)
+                 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 )
+                 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')
+                 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
@@ -333,7 +339,7 @@ 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:
@@ -345,7 +351,7 @@ class CertCommands(Commands):
         """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)
@@ -369,13 +375,13 @@ class AggregateCommands(Commands):
         pprinter.pprint(status)
  
     @add_options('-r', '--rspec-version', dest='rspec_version', metavar='<rspec_version>', 
-          default='GENI', help='version/format of the resulting rspec response')  
+                 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)
         
 
     @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
@@ -387,7 +393,7 @@ 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)
 
 
     @add_options('-x', '--xrn', dest='xrn', metavar='<xrn>', help='slice hrn/urn (mandatory)')
@@ -397,7 +403,7 @@ class AggregateCommands(Commands):
         slice_urn=xrn.get_urn()
         options={}
         manifest = self.api.manager.provision(self.api, [slice_urn], [], options)
-        print manifest
+        print(manifest)
 
 
 
@@ -429,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:
@@ -444,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):
@@ -505,14 +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)