]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tools/exec_utils.py
Merge pull request #2146 from mtmail/two-typos
[nominatim.git] / nominatim / tools / exec_utils.py
1 """
2 Helper functions for executing external programs.
3 """
4 import logging
5 import subprocess
6 from urllib.parse import urlencode
7
8 def run_legacy_script(script, *args, nominatim_env=None, throw_on_fail=False):
9     """ Run a Nominatim PHP script with the given arguments.
10
11         Returns the exit code of the script. If `throw_on_fail` is True
12         then throw a `CalledProcessError` on a non-zero exit.
13     """
14     cmd = ['/usr/bin/env', 'php', '-Cq',
15            nominatim_env.phplib_dir / 'admin' / script]
16     cmd.extend([str(a) for a in args])
17
18     env = nominatim_env.config.get_os_env()
19     env['NOMINATIM_DATADIR'] = str(nominatim_env.data_dir)
20     env['NOMINATIM_BINDIR'] = str(nominatim_env.data_dir / 'utils')
21     if not env['NOMINATIM_DATABASE_MODULE_PATH']:
22         env['NOMINATIM_DATABASE_MODULE_PATH'] = nominatim_env.module_dir
23     if not env['NOMINATIM_OSM2PGSQL_BINARY']:
24         env['NOMINATIM_OSM2PGSQL_BINARY'] = nominatim_env.osm2pgsql_path
25
26     proc = subprocess.run(cmd, cwd=str(nominatim_env.project_dir), env=env,
27                           check=throw_on_fail)
28
29     return proc.returncode
30
31 def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None,
32                    params=None):
33     """ Execute a Nominiatim API function.
34
35         The function needs a project directory that contains the website
36         directory with the scripts to be executed. The scripts will be run
37         using php_cgi. Query parameters can be added as named arguments.
38
39         Returns the exit code of the script.
40     """
41     log = logging.getLogger()
42     webdir = str(project_dir / 'website')
43     query_string = urlencode(params or {})
44
45     env = dict(QUERY_STRING=query_string,
46                SCRIPT_NAME='/{}.php'.format(endpoint),
47                REQUEST_URI='/{}.php?{}'.format(endpoint, query_string),
48                CONTEXT_DOCUMENT_ROOT=webdir,
49                SCRIPT_FILENAME='{}/{}.php'.format(webdir, endpoint),
50                HTTP_HOST='localhost',
51                HTTP_USER_AGENT='nominatim-tool',
52                REMOTE_ADDR='0.0.0.0',
53                DOCUMENT_ROOT=webdir,
54                REQUEST_METHOD='GET',
55                SERVER_PROTOCOL='HTTP/1.1',
56                GATEWAY_INTERFACE='CGI/1.1',
57                REDIRECT_STATUS='CGI')
58
59     if extra_env:
60         env.update(extra_env)
61
62     if phpcgi_bin is None:
63         cmd = ['/usr/bin/env', 'php-cgi']
64     else:
65         cmd = [str(phpcgi_bin)]
66
67     proc = subprocess.run(cmd, cwd=str(project_dir), env=env, capture_output=True,
68                           check=False)
69
70     if proc.returncode != 0 or proc.stderr:
71         log.error(proc.stderr.decode('utf-8').replace('\\n', '\n'))
72         return proc.returncode or 1
73
74     result = proc.stdout.decode('utf-8')
75     content_start = result.find('\r\n\r\n')
76
77     print(result[content_start + 4:].replace('\\n', '\n'))
78
79     return 0