Merge branch 'upstreammaster'
[sfa.git] / sfa / client / sfaadmin.py
index ca5edf0..7640e93 100755 (executable)
@@ -12,6 +12,8 @@ from sfa.client.sfi import save_records_to_file
 from sfa.trust.hierarchy import Hierarchy
 from sfa.trust.gid import GID
 
+from sfa.client.candidates import Candidates
+
 pprinter = PrettyPrinter(indent=4)
 
 try:
@@ -28,21 +30,6 @@ def args(*args, **kwargs):
         return func
     return _decorator
 
-### utility to match command-line args to names
-class Candidates:
-    def __init__ (self, names):
-        self.names=names
-    # is an input string acceptable for one of the known names?
-    @staticmethod
-    def fits (input, name):
-        return name.find(input)==0
-    # returns one of the names if the input name has a unique match
-    # or None otherwise
-    def only_match (self, input):
-        matches=[ name for name in self.names if Candidates.fits(input,name) ]
-        if len(matches)==1: return matches[0]
-        else: return None
-
 class Commands(object):
     def _get_commands(self):
         command_names = []
@@ -190,6 +177,12 @@ class RegistryCommands(Commands):
         from sfa.importer import Importer
         importer = Importer()
         importer.run()
+
+    def sync_db(self):
+        """Initiailize or upgrade the db"""
+        from sfa.storage.dbschema import DBSchema
+        dbschema=DBSchema()
+        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)
@@ -375,7 +368,7 @@ class SfaAdmin:
         return (full_name,SfaAdmin.CATEGORIES[full_name])
 
     def summary_usage (self, category=None):
-        print "Usage:", self.script_name + " category action [<options>]"
+        print "Usage:", self.script_name + " category command [<options>]"
         if category and category in SfaAdmin.CATEGORIES: 
             categories=[category]
         else:
@@ -414,7 +407,7 @@ class SfaAdmin:
         if not category_name or not category_class:
             self.summary_usage(category_name)
 
-        usage = "%%prog %s action [options]" % (category_name)
+        usage = "%%prog %s command [options]" % (category_name)
         parser = OptionParser(usage=usage)
     
         # ensure command is valid      
@@ -427,7 +420,7 @@ class SfaAdmin:
             command_input = argv.pop(0)
             command_name = Candidates (commands).only_match (command_input)
     
-        if hasattr(category_instance, command_name):
+        if command_name and hasattr(category_instance, command_name):
             command = getattr(category_instance, command_name)
         else:
             self.summary_usage(category_name)
@@ -462,30 +455,3 @@ class SfaAdmin:
             print "Command failed, please check log for more info"
             raise
 
-candidates_specs=[
-('create delete reset resources slices start status stop version', 
-  [ ('ver','version'),
-    ('r',None),
-    ('re',None),
-    ('res',None),
-    ('rese','reset'),
-    ('reset','reset'),
-    ('reso','resources'),
-    ('sli','slices'),
-    ('st',None),
-    ('sta',None),
-    ('stop','stop'),
-    ('a',None),
-])
-]
-
-def test_candidates ():
-    for (names, tuples) in candidates_specs:
-        names=names.split()
-        for (input,expected) in tuples:
-            got=Candidates(names).only_match(input)
-            if got==expected: print '.',
-            else: print 'X FAIL','names[',names,'] input',input,'expected',expected,'got',got
-
-if __name__ == '__main__':
-    test_candidates()