1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Helper functions for executing external programs.
10 from typing import Any, Mapping
16 from nominatim_core.typing import StrPath
17 from nominatim_core.db.connection import get_pg_env
19 LOG = logging.getLogger()
21 def run_php_server(server_address: str, base_dir: StrPath) -> None:
22 """ Run the built-in server from the given directory.
24 subprocess.run(['/usr/bin/env', 'php', '-S', server_address],
25 cwd=str(base_dir), check=True)
28 def run_osm2pgsql(options: Mapping[str, Any]) -> None:
29 """ Run osm2pgsql with the given options.
31 env = get_pg_env(options['dsn'])
33 osm2pgsql_cmd = options['osm2pgsql']
34 if osm2pgsql_cmd is None:
35 osm2pgsql_cmd = shutil.which('osm2pgsql')
36 if osm2pgsql_cmd is None:
37 raise RuntimeError('osm2pgsql executable not found. Please install osm2pgsql first.')
39 cmd = [str(osm2pgsql_cmd),
41 '--log-progress', 'true',
42 '--number-processes', '1' if options['append'] else str(options['threads']),
43 '--cache', str(options['osm2pgsql_cache']),
44 '--style', str(options['osm2pgsql_style'])
47 if str(options['osm2pgsql_style']).endswith('.lua'):
48 env['LUA_PATH'] = ';'.join((str(options['osm2pgsql_style_path'] / '?.lua'),
49 os.environ.get('LUAPATH', ';')))
50 cmd.extend(('--output', 'flex'))
52 cmd.extend(('--output', 'gazetteer', '--hstore', '--latlon'))
54 cmd.append('--append' if options['append'] else '--create')
56 if options['flatnode_file']:
57 cmd.extend(('--flat-nodes', options['flatnode_file']))
59 for key, param in (('slim_data', '--tablespace-slim-data'),
60 ('slim_index', '--tablespace-slim-index'),
61 ('main_data', '--tablespace-main-data'),
62 ('main_index', '--tablespace-main-index')):
63 if options['tablespaces'][key]:
64 cmd.extend((param, options['tablespaces'][key]))
66 if options['tablespaces']['main_data']:
67 env['NOMINATIM_TABLESPACE_PLACE_DATA'] = options['tablespaces']['main_data']
68 if options['tablespaces']['main_index']:
69 env['NOMINATIM_TABLESPACE_PLACE_INDEX'] = options['tablespaces']['main_index']
71 if options.get('disable_jit', False):
72 env['PGOPTIONS'] = '-c jit=off -c max_parallel_workers_per_gather=0'
74 if 'import_data' in options:
75 cmd.extend(('-r', 'xml', '-'))
76 elif isinstance(options['import_file'], list):
77 for fname in options['import_file']:
78 cmd.append(str(fname))
80 cmd.append(str(options['import_file']))
82 subprocess.run(cmd, cwd=options.get('cwd', '.'),
83 input=options.get('import_data'),