From ec5d8e4dfb8cdff9033dcbb9db72159d7d6f3679 Mon Sep 17 00:00:00 2001 From: Romuald Brunet Date: Thu, 11 Jan 2018 18:13:17 +0100 Subject: [PATCH] Try to sort search paths by psql version Previously the search paths for the psql binaries was not sorted. So multiples postgresql versions on the same system may be used (usually the older one was choosen first) Try to return the paths sorted by postgresql version (most recent first) for each glob search path --- src/testing/postgresql.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/testing/postgresql.py b/src/testing/postgresql.py index 592f707..870d18c 100644 --- a/src/testing/postgresql.py +++ b/src/testing/postgresql.py @@ -19,18 +19,36 @@ import subprocess from glob import glob from contextlib import closing +from distutils.version import LooseVersion from testing.common.database import ( Database, DatabaseFactory, get_path_of, SkipIfNotInstalledDecorator ) - __all__ = ['Postgresql', 'skipIfNotFound'] + +def sorted_paths(glob_pattern): + """ + Try to return search paths sorted by version number + (latest version first) + + """ + values = glob(glob_pattern) + try: + idx = glob_pattern.index('*') + + # -> [('9.4', '/usr/lib/postgresql/9.4')] + matches = [(LooseVersion(match[idx:]), match) for match in values] + + return [path for _, path in sorted(matches, reverse=True)] + except Exception: + return values + SEARCH_PATHS = (['/usr/local/pgsql', '/usr/local'] + - glob('/usr/pgsql-*') + # for CentOS/RHEL - glob('/usr/lib/postgresql/*') + # for Debian/Ubuntu - glob('/opt/local/lib/postgresql*')) # for MacPorts + sorted_paths('/usr/pgsql-*') + # for CentOS/RHEL + sorted_paths('/usr/lib/postgresql/*') + # for Debian/Ubuntu + sorted_paths('/opt/local/lib/postgresql*')) # for MacPorts class Postgresql(Database):