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