]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/db/utils.py
port wikipedia importance functions to python
[nominatim.git] / nominatim / db / utils.py
1 """
2 Helper functions for handling DB accesses.
3 """
4 import subprocess
5 import logging
6 import gzip
7
8 from .connection import get_pg_env
9 from ..errors import UsageError
10
11 LOG = logging.getLogger()
12
13 def _pipe_to_proc(proc, fdesc):
14     chunk = fdesc.read(2048)
15     while chunk and proc.poll() is None:
16         try:
17             proc.stdin.write(chunk)
18         except BrokenPipeError as exc:
19             raise UsageError("Failed to execute SQL file.") from exc
20         chunk = fdesc.read(2048)
21
22     return len(chunk)
23
24 def execute_file(dsn, fname, ignore_errors=False, pre_code=None, post_code=None):
25     """ Read an SQL file and run its contents against the given database
26         using psql. Use `pre_code` and `post_code` to run extra commands
27         before or after executing the file. The commands are run within the
28         same session, so they may be used to wrap the file execution in a
29         transaction.
30     """
31     cmd = ['psql']
32     if not ignore_errors:
33         cmd.extend(('-v', 'ON_ERROR_STOP=1'))
34     proc = subprocess.Popen(cmd, env=get_pg_env(dsn), stdin=subprocess.PIPE)
35
36     if not LOG.isEnabledFor(logging.INFO):
37         proc.stdin.write('set client_min_messages to WARNING;'.encode('utf-8'))
38
39     if pre_code:
40         proc.stdin.write((pre_code + ';').encode('utf-8'))
41
42     if fname.suffix == '.gz':
43         with gzip.open(str(fname), 'rb') as fdesc:
44             remain = _pipe_to_proc(proc, fdesc)
45     else:
46         with fname.open('rb') as fdesc:
47             remain = _pipe_to_proc(proc, fdesc)
48
49     if remain == 0 and post_code:
50         proc.stdin.write((';' + post_code).encode('utf-8'))
51
52     proc.stdin.close()
53
54     ret = proc.wait()
55     if ret != 0 or remain > 0:
56         raise UsageError("Failed to execute SQL file.")