]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/exec_utils.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / nominatim / tools / exec_utils.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Helper functions for executing external programs.
9 """
10 import logging
11 import subprocess
12 import urllib.request as urlrequest
13 from urllib.parse import urlencode
14
15 from nominatim.version import NOMINATIM_VERSION
16 from nominatim.db.connection import get_pg_env
17
18 LOG = logging.getLogger()
19
20 def run_legacy_script(script, *args, nominatim_env=None, throw_on_fail=False):
21     """ Run a Nominatim PHP script with the given arguments.
22
23         Returns the exit code of the script. If `throw_on_fail` is True
24         then throw a `CalledProcessError` on a non-zero exit.
25     """
26     cmd = ['/usr/bin/env', 'php', '-Cq',
27            str(nominatim_env.phplib_dir / 'admin' / script)]
28     cmd.extend([str(a) for a in args])
29
30     env = nominatim_env.config.get_os_env()
31     env['NOMINATIM_DATADIR'] = str(nominatim_env.data_dir)
32     env['NOMINATIM_SQLDIR'] = str(nominatim_env.sqllib_dir)
33     env['NOMINATIM_CONFIGDIR'] = str(nominatim_env.config_dir)
34     env['NOMINATIM_DATABASE_MODULE_SRC_PATH'] = str(nominatim_env.module_dir)
35     if not env['NOMINATIM_OSM2PGSQL_BINARY']:
36         env['NOMINATIM_OSM2PGSQL_BINARY'] = str(nominatim_env.osm2pgsql_path)
37
38     proc = subprocess.run(cmd, cwd=str(nominatim_env.project_dir), env=env,
39                           check=throw_on_fail)
40
41     return proc.returncode
42
43 def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None,
44                    params=None):
45     """ Execute a Nominiatim API function.
46
47         The function needs a project directory that contains the website
48         directory with the scripts to be executed. The scripts will be run
49         using php_cgi. Query parameters can be added as named arguments.
50
51         Returns the exit code of the script.
52     """
53     log = logging.getLogger()
54     webdir = str(project_dir / 'website')
55     query_string = urlencode(params or {})
56
57     env = dict(QUERY_STRING=query_string,
58                SCRIPT_NAME='/{}.php'.format(endpoint),
59                REQUEST_URI='/{}.php?{}'.format(endpoint, query_string),
60                CONTEXT_DOCUMENT_ROOT=webdir,
61                SCRIPT_FILENAME='{}/{}.php'.format(webdir, endpoint),
62                HTTP_HOST='localhost',
63                HTTP_USER_AGENT='nominatim-tool',
64                REMOTE_ADDR='0.0.0.0',
65                DOCUMENT_ROOT=webdir,
66                REQUEST_METHOD='GET',
67                SERVER_PROTOCOL='HTTP/1.1',
68                GATEWAY_INTERFACE='CGI/1.1',
69                REDIRECT_STATUS='CGI')
70
71     if extra_env:
72         env.update(extra_env)
73
74     if phpcgi_bin is None:
75         cmd = ['/usr/bin/env', 'php-cgi']
76     else:
77         cmd = [str(phpcgi_bin)]
78
79     proc = subprocess.run(cmd, cwd=str(project_dir), env=env,
80                           stdout=subprocess.PIPE,
81                           stderr=subprocess.PIPE,
82                           check=False)
83
84     if proc.returncode != 0 or proc.stderr:
85         if proc.stderr:
86             log.error(proc.stderr.decode('utf-8').replace('\\n', '\n'))
87         else:
88             log.error(proc.stdout.decode('utf-8').replace('\\n', '\n'))
89         return proc.returncode or 1
90
91     result = proc.stdout.decode('utf-8')
92     content_start = result.find('\r\n\r\n')
93
94     print(result[content_start + 4:].replace('\\n', '\n'))
95
96     return 0
97
98
99 def run_php_server(server_address, base_dir):
100     """ Run the built-in server from the given directory.
101     """
102     subprocess.run(['/usr/bin/env', 'php', '-S', server_address],
103                    cwd=str(base_dir), check=True)
104
105
106 def run_osm2pgsql(options):
107     """ Run osm2pgsql with the given options.
108     """
109     env = get_pg_env(options['dsn'])
110     cmd = [str(options['osm2pgsql']),
111            '--hstore', '--latlon', '--slim',
112            '--with-forward-dependencies', 'false',
113            '--log-progress', 'true',
114            '--number-processes', str(options['threads']),
115            '--cache', str(options['osm2pgsql_cache']),
116            '--output', 'gazetteer',
117            '--style', str(options['osm2pgsql_style'])
118           ]
119     if options['append']:
120         cmd.append('--append')
121     else:
122         cmd.append('--create')
123
124     if options['flatnode_file']:
125         cmd.extend(('--flat-nodes', options['flatnode_file']))
126
127     for key, param in (('slim_data', '--tablespace-slim-data'),
128                        ('slim_index', '--tablespace-slim-index'),
129                        ('main_data', '--tablespace-main-data'),
130                        ('main_index', '--tablespace-main-index')):
131         if options['tablespaces'][key]:
132             cmd.extend((param, options['tablespaces'][key]))
133
134     if options.get('disable_jit', False):
135         env['PGOPTIONS'] = '-c jit=off -c max_parallel_workers_per_gather=0'
136
137     if 'import_data' in options:
138         cmd.extend(('-r', 'xml', '-'))
139     elif isinstance(options['import_file'], list):
140         for fname in options['import_file']:
141             cmd.append(str(fname))
142     else:
143         cmd.append(str(options['import_file']))
144
145     subprocess.run(cmd, cwd=options.get('cwd', '.'),
146                    input=options.get('import_data'),
147                    env=env, check=True)
148
149
150 def get_url(url):
151     """ Get the contents from the given URL and return it as a UTF-8 string.
152     """
153     headers = {"User-Agent": "Nominatim/{0[0]}.{0[1]}.{0[2]}-{0[3]}".format(NOMINATIM_VERSION)}
154
155     try:
156         with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response:
157             return response.read().decode('utf-8')
158     except Exception:
159         LOG.fatal('Failed to load URL: %s', url)
160         raise