Skip to content

Commit d7291e4

Browse files
lonviajoto
authored andcommitted
replication script: check for external libraries
Prints a helpful error message, when the required libraries psycopg2 or osmium are not installed.
1 parent ec56312 commit d7291e4

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

scripts/osm2pgsql-replication

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,23 @@ from textwrap import dedent
3030
from pathlib import Path
3131
import urllib.request as urlrequest
3232

33-
import psycopg2
33+
missing_modules = []
3434

35-
from osmium.replication.server import ReplicationServer
36-
from osmium.replication.utils import get_replication_header
37-
from osmium import WriteHandler
35+
try:
36+
import psycopg2
37+
except ImportError:
38+
missing_modules.append('psycopg2')
39+
40+
try:
41+
from osmium.replication.server import ReplicationServer
42+
from osmium.replication.utils import get_replication_header
43+
from osmium import WriteHandler
44+
except ImportError:
45+
missing_modules.append('osmium')
3846

3947
LOG = logging.getLogger()
4048

49+
4150
def pretty_format_timedelta(seconds):
4251
minutes = int(seconds/60)
4352
(hours, minutes) = divmod(minutes, 60)
@@ -484,9 +493,17 @@ def main():
484493
parser = get_parser()
485494
args = parser.parse_args()
486495

496+
if missing_modules:
497+
LOG.fatal("Missing required Python libraries %(mods)s.\n\n"
498+
"To install them via pip run: pip install %(mods)s\n\n"
499+
"ERROR: libraries could not be loaded.",
500+
dict(mods=" ".join(missing_modules)))
501+
return 1
502+
503+
487504
if args.subcommand is None:
488505
parser.print_help()
489-
sys.exit(1)
506+
return 1
490507

491508
logging.basicConfig(stream=sys.stderr,
492509
format='{asctime} [{levelname}]: {message}',

0 commit comments

Comments
 (0)