[server] Use global registry variable of integrity checker functions
authorDenis Laxalde <denis.laxalde@logilab.fr>
Mon, 06 Feb 2017 15:40:39 +0100
changeset 11952 9dbb0af82628
parent 11951 330f240435b0
child 11953 f24b115cca74
[server] Use global registry variable of integrity checker functions Instead of querying globals() in checkintegrity.check(), we maintain a _CHECKERS dict mapping checker name to function in checkintegrity module. This is later used to build the list of available checkers in 'db-check' command help.
cubicweb/server/checkintegrity.py
cubicweb/server/serverctl.py
--- a/cubicweb/server/checkintegrity.py	Tue Feb 07 18:18:22 2017 +0100
+++ b/cubicweb/server/checkintegrity.py	Mon Feb 06 15:40:39 2017 +0100
@@ -140,6 +140,19 @@
         pb.finish()
 
 
+_CHECKERS = {}
+
+
+def _checker(func):
+    """Decorator to register a function as a checker for check()."""
+    fname = func.__name__
+    prefix = 'check_'
+    assert fname.startswith(prefix), 'cannot register %s as a checker' % func
+    _CHECKERS[fname[len(prefix):]] = func
+    return func
+
+
+@_checker
 def check_schema(schema, cnx, eids, fix=1):
     """check serialized schema"""
     print('Checking serialized schema')
@@ -157,6 +170,7 @@
                 print('dunno how to fix, do it yourself')
 
 
+@_checker
 def check_text_index(schema, cnx, eids, fix=1):
     """check all entities registered in the text index"""
     print('Checking text index')
@@ -171,6 +185,7 @@
             notify_fixed(fix)
 
 
+@_checker
 def check_entities(schema, cnx, eids, fix=1):
     """check all entities registered in the repo system table"""
     print('Checking entities system table')
@@ -259,6 +274,7 @@
     notify_fixed(fix)
 
 
+@_checker
 def check_relations(schema, cnx, eids, fix=1):
     """check that eids referenced by relations are registered in the repo system
     table
@@ -308,6 +324,7 @@
                     cnx.system_sql(sql)
 
 
+@_checker
 def check_mandatory_relations(schema, cnx, eids, fix=1):
     """check entities missing some mandatory relation"""
     print('Checking mandatory relations')
@@ -335,6 +352,7 @@
                     notify_fixed(fix)
 
 
+@_checker
 def check_mandatory_attributes(schema, cnx, eids, fix=1):
     """check for entities stored in the system source missing some mandatory
     attribute
@@ -355,6 +373,7 @@
                     notify_fixed(fix)
 
 
+@_checker
 def check_metadata(schema, cnx, eids, fix=1):
     """check entities has required metadata
 
@@ -397,7 +416,7 @@
         eids_cache = {}
         with cnx.security_enabled(read=False, write=False): # ensure no read security
             for check in checks:
-                check_func = globals()['check_%s' % check]
+                check_func = _CHECKERS[check]
                 check_func(repo.schema, cnx, eids_cache, fix=fix)
         if fix:
             cnx.commit()
--- a/cubicweb/server/serverctl.py	Tue Feb 07 18:18:22 2017 +0100
+++ b/cubicweb/server/serverctl.py	Mon Feb 06 15:40:39 2017 +0100
@@ -37,6 +37,7 @@
 from cubicweb.toolsutils import Command, CommandHandler, underline_title
 from cubicweb.cwctl import CWCTL, check_options_consistency, ConfigureInstanceCommand
 from cubicweb.server import SOURCE_TYPES
+from cubicweb.server import checkintegrity
 from cubicweb.server.serverconfig import (
     USER_OPTIONS, ServerConfiguration, SourceConfiguration,
     ask_source_config, generate_source_config)
@@ -902,12 +903,8 @@
     options = (
         ('checks',
          {'short': 'c', 'type': 'csv', 'metavar': '<check list>',
-          'default': ('entities', 'relations',
-                      'mandatory_relations', 'mandatory_attributes',
-                      'metadata', 'schema', 'text_index'),
-          'help': 'Comma separated list of check to run. By default run all \
-checks, i.e. entities, relations, mandatory_relations, mandatory_attributes, \
-metadata, text_index and schema.'}
+          'default': sorted(checkintegrity._CHECKERS),
+          'help': 'Comma separated list of check to run. By default run all checks.'}
          ),
 
         ('autofix',
@@ -930,13 +927,12 @@
     )
 
     def run(self, args):
-        from cubicweb.server.checkintegrity import check
         appid = args[0]
         config = ServerConfiguration.config_for(appid)
         config.repairing = self.config.force
         repo, _cnx = repo_cnx(config)
         with repo.internal_cnx() as cnx:
-            check(repo, cnx,
+            checkintegrity.check(repo, cnx,
                   self.config.checks,
                   self.config.reindex,
                   self.config.autofix)
@@ -953,11 +949,10 @@
     min_args = 1
 
     def run(self, args):
-        from cubicweb.server.checkintegrity import check_indexes
         config = ServerConfiguration.config_for(args[0])
         repo, cnx = repo_cnx(config)
         with cnx:
-            status = check_indexes(cnx)
+            status = checkintegrity.check_indexes(cnx)
         sys.exit(status)