]> git.openstreetmap.org Git - nominatim.git/commitdiff
consolidate warm and db-check into single admin command
authorSarah Hoffmann <lonvia@denofr.de>
Mon, 8 Feb 2021 20:05:06 +0000 (21:05 +0100)
committerSarah Hoffmann <lonvia@denofr.de>
Mon, 8 Feb 2021 20:05:06 +0000 (21:05 +0100)
nominatim/cli.py
nominatim/clicmd/__init__.py
nominatim/clicmd/admin.py [new file with mode: 0644]
test/python/test_cli.py

index 0bca03a31959b566c3d6b08137eac3dcf252a92a..845b1451db6455d15b661637e6311d0e82b5fc16 100644 (file)
@@ -265,45 +265,6 @@ class UpdateAddData:
         return run_legacy_script(*params, nominatim_env=args)
 
 
         return run_legacy_script(*params, nominatim_env=args)
 
 
-class AdminCheckDatabase:
-    """\
-    Check that the database is complete and operational.
-    """
-
-    @staticmethod
-    def add_args(parser):
-        pass # No options
-
-    @staticmethod
-    def run(args):
-        return run_legacy_script('check_import_finished.php', nominatim_env=args)
-
-
-class AdminWarm:
-    """\
-    Warm database caches for search and reverse queries.
-    """
-
-    @staticmethod
-    def add_args(parser):
-        group = parser.add_argument_group('Target arguments')
-        group.add_argument('--search-only', action='store_const', dest='target',
-                           const='search',
-                           help="Only pre-warm tables for search queries")
-        group.add_argument('--reverse-only', action='store_const', dest='target',
-                           const='reverse',
-                           help="Only pre-warm tables for reverse queries")
-
-    @staticmethod
-    def run(args):
-        params = ['warm.php']
-        if args.target == 'reverse':
-            params.append('--reverse-only')
-        if args.target == 'search':
-            params.append('--search-only')
-        return run_legacy_script(*params, nominatim_env=args)
-
-
 class QueryExport:
     """\
     Export addresses as CSV file from the database.
 class QueryExport:
     """\
     Export addresses as CSV file from the database.
@@ -393,15 +354,14 @@ def nominatim(**kwargs):
     parser.add_subcommand('freeze', SetupFreeze)
     parser.add_subcommand('replication', clicmd.UpdateReplication)
 
     parser.add_subcommand('freeze', SetupFreeze)
     parser.add_subcommand('replication', clicmd.UpdateReplication)
 
-    parser.add_subcommand('check-database', AdminCheckDatabase)
-    parser.add_subcommand('warm', AdminWarm)
-
     parser.add_subcommand('special-phrases', SetupSpecialPhrases)
 
     parser.add_subcommand('add-data', UpdateAddData)
     parser.add_subcommand('index', clicmd.UpdateIndex)
     parser.add_subcommand('refresh', clicmd.UpdateRefresh)
 
     parser.add_subcommand('special-phrases', SetupSpecialPhrases)
 
     parser.add_subcommand('add-data', UpdateAddData)
     parser.add_subcommand('index', clicmd.UpdateIndex)
     parser.add_subcommand('refresh', clicmd.UpdateRefresh)
 
+    parser.add_subcommand('admin', clicmd.AdminFuncs)
+
     parser.add_subcommand('export', QueryExport)
     parser.add_subcommand('serve', AdminServe)
 
     parser.add_subcommand('export', QueryExport)
     parser.add_subcommand('serve', AdminServe)
 
index b7dfa47f348cc2a53bb1aade984d1945985cd5a1..9a686df256b48bd87635a880f241152845defbe3 100644 (file)
@@ -6,3 +6,4 @@ from .replication import UpdateReplication
 from .api import APISearch, APIReverse, APILookup, APIDetails, APIStatus
 from .index import UpdateIndex
 from .refresh import UpdateRefresh
 from .api import APISearch, APIReverse, APILookup, APIDetails, APIStatus
 from .index import UpdateIndex
 from .refresh import UpdateRefresh
+from .admin import AdminFuncs
diff --git a/nominatim/clicmd/admin.py b/nominatim/clicmd/admin.py
new file mode 100644 (file)
index 0000000..040b923
--- /dev/null
@@ -0,0 +1,49 @@
+"""
+Implementation of the 'admin' subcommand.
+"""
+from ..tools.exec_utils import run_legacy_script
+
+# Do not repeat documentation of subcommand classes.
+# pylint: disable=C0111
+# Using non-top-level imports to avoid eventually unused imports.
+# pylint: disable=E0012,C0415
+
+class AdminFuncs:
+    """\
+    Analyse and maintain the database.
+    """
+
+    @staticmethod
+    def add_args(parser):
+        group = parser.add_argument_group('Admin task arguments')
+        group.add_argument('--warm', action='store_true',
+                           help='Warm database caches for search and reverse queries.')
+        group.add_argument('--check-database', action='store_true',
+                           help='Check that the database is complete and operational.')
+        group = parser.add_argument_group('Arguments for cache warming')
+        group.add_argument('--search-only', action='store_const', dest='target',
+                           const='search',
+                           help="Only pre-warm tables for search queries")
+        group.add_argument('--reverse-only', action='store_const', dest='target',
+                           const='reverse',
+                           help="Only pre-warm tables for reverse queries")
+
+    @staticmethod
+    def run(args):
+        if args.warm:
+            AdminFuncs._warm(args)
+
+        if args.check_database:
+            run_legacy_script('check_import_finished.php', nominatim_env=args)
+
+        return 0
+
+
+    @staticmethod
+    def _warm(args):
+        params = ['warm.php']
+        if args.target == 'reverse':
+            params.append('--reverse-only')
+        if args.target == 'search':
+            params.append('--search-only')
+        return run_legacy_script(*params, nominatim_env=args)
index 983b792ba1375b5bd84fb5975499fa2dc4eb2c0d..9b39f580adb0919a982c416e2f29edd7f7b9fdaf 100644 (file)
@@ -13,6 +13,7 @@ import time
 import nominatim.cli
 import nominatim.clicmd.api
 import nominatim.clicmd.refresh
 import nominatim.cli
 import nominatim.clicmd.api
 import nominatim.clicmd.refresh
+import nominatim.clicmd.admin
 import nominatim.indexer.indexer
 import nominatim.tools.refresh
 import nominatim.tools.replication
 import nominatim.indexer.indexer
 import nominatim.tools.refresh
 import nominatim.tools.replication
@@ -63,8 +64,6 @@ def test_cli_help(capsys):
                          (('special-phrases',), 'specialphrases'),
                          (('add-data', '--tiger-data', 'tiger'), 'setup'),
                          (('add-data', '--file', 'foo.osm'), 'update'),
                          (('special-phrases',), 'specialphrases'),
                          (('add-data', '--tiger-data', 'tiger'), 'setup'),
                          (('add-data', '--file', 'foo.osm'), 'update'),
-                         (('check-database',), 'check_import_finished'),
-                         (('warm',), 'warm'),
                          (('export',), 'export')
                          ])
 def test_legacy_commands_simple(mock_run_legacy, command, script):
                          (('export',), 'export')
                          ])
 def test_legacy_commands_simple(mock_run_legacy, command, script):
@@ -74,6 +73,19 @@ def test_legacy_commands_simple(mock_run_legacy, command, script):
     assert mock_run_legacy.last_args[0] == script + '.php'
 
 
     assert mock_run_legacy.last_args[0] == script + '.php'
 
 
+@pytest.mark.parametrize("params", [('--warm', ),
+                                    ('--warm', '--reverse-only'),
+                                    ('--warm', '--search-only'),
+                                    ('--check-database', )])
+def test_admin_command_legacy(monkeypatch, params):
+    mock_run_legacy = MockParamCapture()
+    monkeypatch.setattr(nominatim.clicmd.admin, 'run_legacy_script', mock_run_legacy)
+
+    assert 0 == call_nominatim('admin', *params)
+
+    assert mock_run_legacy.called == 1
+
+
 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
                                       ('node', 12), ('way', 8), ('relation', 32)])
 def test_add_data_command(mock_run_legacy, name, oid):
 @pytest.mark.parametrize("name,oid", [('file', 'foo.osm'), ('diff', 'foo.osc'),
                                       ('node', 12), ('way', 8), ('relation', 32)])
 def test_add_data_command(mock_run_legacy, name, oid):