]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/clicmd/transition.py
Merge pull request #2186 from lonvia/port-import-to-python
[nominatim.git] / nominatim / clicmd / transition.py
1 """
2 Implementation of the 'transition' subcommand.
3
4 This subcommand provides standins for functions that were available
5 through the PHP scripts but are now no longer directly accessible.
6 This module will be removed as soon as the transition phase is over.
7 """
8 import logging
9 from pathlib import Path
10
11 from ..db.connection import connect
12 from ..db import status
13 from ..errors import UsageError
14
15 # Do not repeat documentation of subcommand classes.
16 # pylint: disable=C0111
17 # Using non-top-level imports to avoid eventually unused imports.
18 # pylint: disable=E0012,C0415
19
20 LOG = logging.getLogger()
21
22 class AdminTransition:
23     """\
24     Internal functions for code transition. Do not use.
25     """
26
27     @staticmethod
28     def add_args(parser):
29         group = parser.add_argument_group('Sub-functions')
30         group.add_argument('--create-db', action='store_true',
31                            help='Create nominatim db')
32         group.add_argument('--setup-db', action='store_true',
33                            help='Build a blank nominatim db')
34         group.add_argument('--import-data', action='store_true',
35                            help='Import a osm file')
36         group.add_argument('--load-data', action='store_true',
37                            help='Copy data to live tables from import table')
38         group.add_argument('--index', action='store_true',
39                            help='Index the data')
40         group = parser.add_argument_group('Options')
41         group.add_argument('--no-partitions', action='store_true',
42                            help='Do not partition search indices')
43         group.add_argument('--osm-file', metavar='FILE',
44                            help='File to import')
45         group.add_argument('--drop', action='store_true',
46                            help='Drop tables needed for updates, making the database readonly')
47         group.add_argument('--osm2pgsql-cache', metavar='SIZE', type=int,
48                            help='Size of cache to be used by osm2pgsql (in MB)')
49         group.add_argument('--no-analyse', action='store_true',
50                            help='Do not perform analyse operations during index')
51         group.add_argument('--ignore-errors', action='store_true',
52                            help="Ignore certain erros on import.")
53
54     @staticmethod
55     def run(args):
56         from ..tools import database_import
57
58         if args.create_db:
59             LOG.warning('Create DB')
60             database_import.create_db(args.config.get_libpq_dsn())
61
62         if args.setup_db:
63             LOG.warning('Setup DB')
64
65             with connect(args.config.get_libpq_dsn()) as conn:
66                 database_import.setup_extensions(conn)
67                 database_import.install_module(args.module_dir, args.project_dir,
68                                                args.config.DATABASE_MODULE_PATH,
69                                                conn=conn)
70
71             database_import.import_base_data(args.config.get_libpq_dsn(),
72                                              args.data_dir, args.no_partitions)
73
74         if args.import_data:
75             LOG.warning('Import data')
76             if not args.osm_file:
77                 raise UsageError('Missing required --osm-file argument')
78             database_import.import_osm_data(Path(args.osm_file),
79                                             args.osm2pgsql_options(0, 1),
80                                             drop=args.drop,
81                                             ignore_errors=args.ignore_errors)
82
83         if args.load_data:
84             LOG.warning('Load data')
85             with connect(args.config.get_libpq_dsn()) as conn:
86                 database_import.truncate_data_tables(conn, args.config.MAX_WORD_FREQUENCY)
87             database_import.load_data(args.config.get_libpq_dsn(),
88                                       args.data_dir,
89                                       args.threads or 1)
90
91             with connect(args.config.get_libpq_dsn()) as conn:
92                 try:
93                     status.set_status(conn, status.compute_database_date(conn))
94                 except Exception as exc: # pylint: disable=broad-except
95                     LOG.error('Cannot determine date of database: %s', exc)
96
97         if args.index:
98             LOG.warning('Indexing')
99             from ..indexer.indexer import Indexer
100             indexer = Indexer(args.config.get_libpq_dsn(), args.threads or 1)
101             indexer.index_full()