More cleanup in pg_config detection from Windows registry

This commit is contained in:
Daniele Varrazzo 2011-06-07 22:15:37 +01:00
parent 575afa2e0e
commit d0b97feab3

View File

@ -149,57 +149,56 @@ or with the pg_config option in 'setup.cfg'.
# Now, try looking in the Windows Registry to find a PostgreSQL # Now, try looking in the Windows Registry to find a PostgreSQL
# installation, and infer the path from that. # installation, and infer the path from that.
pg_config_exe = self._get_pg_config_from_registry()
if pg_config_exe:
return pg_config_exe
return None
def _get_pg_config_from_registry(self):
try: try:
import winreg import winreg
except ImportError: except ImportError:
import _winreg as winreg import _winreg as winreg
pg_inst_base_dir = None
pg_config_path = None
reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
try: try:
pg_inst_list_key = winreg.OpenKey(reg, pg_inst_list_key = winreg.OpenKey(reg,
'SOFTWARE\\PostgreSQL\\Installations' 'SOFTWARE\\PostgreSQL\\Installations')
)
except EnvironmentError: except EnvironmentError:
pg_inst_list_key = None
if not pg_inst_list_key:
# No PostgreSQL installation, as best as we can tell. # No PostgreSQL installation, as best as we can tell.
return None return None
try: try:
# Determine the name of the first subkey, if any: # Determine the name of the first subkey, if any:
try: try:
first_sub_key_name = winreg.EnumKey(pg_inst_list_key, 0) first_sub_key_name = winreg.EnumKey(pg_inst_list_key, 0)
except EnvironmentError: except EnvironmentError:
first_sub_key_name = None return None
pg_first_inst_key = winreg.OpenKey(reg,
'SOFTWARE\\PostgreSQL\\Installations\\'
+ first_sub_key_name)
try:
pg_inst_base_dir = winreg.QueryValueEx(
pg_first_inst_key, 'Base Directory')[0]
finally:
winreg.CloseKey(pg_first_inst_key)
if first_sub_key_name is not None:
pg_first_inst_key = winreg.OpenKey(reg,
'SOFTWARE\\PostgreSQL\\Installations\\'
+ first_sub_key_name
)
try:
pg_inst_base_dir = winreg.QueryValueEx(
pg_first_inst_key, 'Base Directory'
)[0]
finally:
winreg.CloseKey(pg_first_inst_key)
finally: finally:
winreg.CloseKey(pg_inst_list_key) winreg.CloseKey(pg_inst_list_key)
if pg_inst_base_dir and os.path.exists(pg_inst_base_dir): pg_config_path = os.path.join(
pg_config_path = os.path.join( pg_inst_base_dir, 'bin', 'pg_config.exe')
pg_inst_base_dir, 'bin', 'pg_config.exe') if not os.path.exists(pg_config_path):
# Support unicode paths, if this version of Python provides the return None
# necessary infrastructure:
if sys.version_info[0] < 3 \ # Support unicode paths, if this version of Python provides the
and hasattr(sys, 'getfilesystemencoding'): # necessary infrastructure:
pg_config_path = pg_config_path.encode( if sys.version_info[0] < 3 \
sys.getfilesystemencoding()) and hasattr(sys, 'getfilesystemencoding'):
pg_config_path = pg_config_path.encode(
sys.getfilesystemencoding())
return pg_config_path return pg_config_path