2 Helper functions for executing external programs.
6 import urllib.request as urlrequest
7 from urllib.parse import urlencode
9 from nominatim.version import NOMINATIM_VERSION
10 from nominatim.db.connection import get_pg_env
12 LOG = logging.getLogger()
14 def run_legacy_script(script, *args, nominatim_env=None, throw_on_fail=False):
15 """ Run a Nominatim PHP script with the given arguments.
17 Returns the exit code of the script. If `throw_on_fail` is True
18 then throw a `CalledProcessError` on a non-zero exit.
20 cmd = ['/usr/bin/env', 'php', '-Cq',
21 str(nominatim_env.phplib_dir / 'admin' / script)]
22 cmd.extend([str(a) for a in args])
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'] = str(nominatim_env.module_dir)
29 if not env['NOMINATIM_OSM2PGSQL_BINARY']:
30 env['NOMINATIM_OSM2PGSQL_BINARY'] = str(nominatim_env.osm2pgsql_path)
32 proc = subprocess.run(cmd, cwd=str(nominatim_env.project_dir), env=env,
35 return proc.returncode
37 def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None,
39 """ Execute a Nominiatim API function.
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.
45 Returns the exit code of the script.
47 log = logging.getLogger()
48 webdir = str(project_dir / 'website')
49 query_string = urlencode(params or {})
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',
61 SERVER_PROTOCOL='HTTP/1.1',
62 GATEWAY_INTERFACE='CGI/1.1',
63 REDIRECT_STATUS='CGI')
68 if phpcgi_bin is None:
69 cmd = ['/usr/bin/env', 'php-cgi']
71 cmd = [str(phpcgi_bin)]
73 proc = subprocess.run(cmd, cwd=str(project_dir), env=env, capture_output=True,
76 if proc.returncode != 0 or proc.stderr:
78 log.error(proc.stderr.decode('utf-8').replace('\\n', '\n'))
80 log.error(proc.stdout.decode('utf-8').replace('\\n', '\n'))
81 return proc.returncode or 1
83 result = proc.stdout.decode('utf-8')
84 content_start = result.find('\r\n\r\n')
86 print(result[content_start + 4:].replace('\\n', '\n'))
91 def run_php_server(server_address, base_dir):
92 """ Run the built-in server from the given directory.
94 subprocess.run(['/usr/bin/env', 'php', '-S', server_address],
95 cwd=str(base_dir), check=True)
98 def run_osm2pgsql(options):
99 """ Run osm2pgsql with the given options.
101 env = get_pg_env(options['dsn'])
102 cmd = [str(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'])
111 if options['append']:
112 cmd.append('--append')
114 cmd.append('--create')
116 if options['flatnode_file']:
117 cmd.extend(('--flat-nodes', options['flatnode_file']))
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]))
126 if options.get('disable_jit', False):
127 env['PGOPTIONS'] = '-c jit=off -c max_parallel_workers_per_gather=0'
129 cmd.append(str(options['import_file']))
131 subprocess.run(cmd, cwd=options.get('cwd', '.'), env=env, check=True)
135 """ Get the contents from the given URL and return it as a UTF-8 string.
137 headers = {"User-Agent" : "Nominatim/{0[0]}.{0[1]}.{0[2]}-{0[3]}".format(NOMINATIM_VERSION)}
140 with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response:
141 return response.read().decode('utf-8')
143 LOG.fatal('Failed to load URL: %s', url)