]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/tools/exec_utils.py
port check-for-update function to python
[nominatim.git] / nominatim / tools / exec_utils.py
index ca30b2f74b7b83fdec79959a7d2eed936d3e3dc4..0d3db20474ae79c67f74f3338dee026eb3ca4ed7 100644 (file)
@@ -3,8 +3,13 @@ Helper functions for executing external programs.
 """
 import logging
 import subprocess
+import urllib.request as urlrequest
 from urllib.parse import urlencode
 
+from ..version import NOMINATIM_VERSION
+
+LOG = logging.getLogger()
+
 def run_legacy_script(script, *args, nominatim_env=None, throw_on_fail=False):
     """ Run a Nominatim PHP script with the given arguments.
 
@@ -34,7 +39,7 @@ def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None,
 
         The function needs a project directory that contains the website
         directory with the scripts to be executed. The scripts will be run
-        using php_cgi. Query parameters can be addd as named arguments.
+        using php_cgi. Query parameters can be added as named arguments.
 
         Returns the exit code of the script.
     """
@@ -68,7 +73,10 @@ def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None,
                           check=False)
 
     if proc.returncode != 0 or proc.stderr:
-        log.error(proc.stderr.decode('utf-8').replace('\\n', '\n'))
+        if proc.stderr:
+            log.error(proc.stderr.decode('utf-8').replace('\\n', '\n'))
+        else:
+            log.error(proc.stdout.decode('utf-8').replace('\\n', '\n'))
         return proc.returncode or 1
 
     result = proc.stdout.decode('utf-8')
@@ -77,3 +85,16 @@ def run_api_script(endpoint, project_dir, extra_env=None, phpcgi_bin=None,
     print(result[content_start + 4:].replace('\\n', '\n'))
 
     return 0
+
+
+def get_url(url):
+    """ Get the contents from the given URL and return it as a UTF-8 string.
+    """
+    headers = {"User-Agent" : "Nominatim/" + NOMINATIM_VERSION}
+
+    try:
+        with urlrequest.urlopen(urlrequest.Request(url, headers=headers)) as response:
+            return response.read().decode('utf-8')
+    except:
+        LOG.fatal('Failed to load URL: %s', url)
+        raise