[Python 3 compability] Allow to run the auto-discovery implementation on

Python 3. Refs #1389
pull/3/head
Ashesh Vashi 2016-07-09 13:21:47 +05:30
parent 61698b7b4d
commit 889b69ac82
1 changed files with 43 additions and 17 deletions

View File

@ -17,7 +17,7 @@ from importlib import import_module
from flask import Flask, abort, request, current_app from flask import Flask, abort, request, current_app
from flask.ext.babel import Babel, gettext from flask.ext.babel import Babel, gettext
from flask.ext.login import user_logged_in from flask.ext.login import user_logged_in
from flask.ext.security import current_user, Security, SQLAlchemyUserDatastore from flask.ext.security import Security, SQLAlchemyUserDatastore
from flask_mail import Mail from flask_mail import Mail
from flask_security.utils import login_user from flask_security.utils import login_user
from htmlmin.minify import html_minify from htmlmin.minify import html_minify
@ -30,8 +30,15 @@ from pgadmin.model import db, Role, Server, ServerGroup, User, Version
# Configuration settings # Configuration settings
import config import config
if os.name == 'nt': # If script is running under python3, it will not have the xrange function
from _winreg import * # defined
winreg = None
if not hasattr(__builtins__, 'xrange'):
xrange = range
if os.name == 'nt':
import winreg
elif os.name == 'nt':
import _winreg as winreg
class PgAdmin(Flask): class PgAdmin(Flask):
def find_submodules(self, basemodule): def find_submodules(self, basemodule):
@ -262,7 +269,7 @@ def create_app(app_name=config.APP_NAME):
db.session.commit() db.session.commit()
# Figure out what servers are present # Figure out what servers are present
if os.name == 'nt': if winreg is not None:
proc_arch = os.environ['PROCESSOR_ARCHITECTURE'].lower() proc_arch = os.environ['PROCESSOR_ARCHITECTURE'].lower()
try: try:
@ -273,31 +280,50 @@ def create_app(app_name=config.APP_NAME):
if proc_arch == 'x86' and not proc_arch64: if proc_arch == 'x86' and not proc_arch64:
arch_keys = {0} arch_keys = {0}
elif proc_arch == 'x86' or proc_arch == 'amd64': elif proc_arch == 'x86' or proc_arch == 'amd64':
arch_keys = { KEY_WOW64_32KEY, KEY_WOW64_64KEY } arch_keys = {winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY}
for arch_key in arch_keys: for arch_key in arch_keys:
for server_type in { 'PostgreSQL', 'EnterpriseDB'}: for server_type in { 'PostgreSQL', 'EnterpriseDB'}:
try: try:
root_key = OpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\" + server_type + "\Services", 0, KEY_READ | arch_key) root_key = winreg.OpenKey(
for i in xrange(0, QueryInfoKey(root_key)[0]): winreg.HKEY_LOCAL_MACHINE,
inst_id = EnumKey(root_key, i) "SOFTWARE\\" + server_type + "\Services", 0,
inst_key = OpenKey(root_key, inst_id) winreg.KEY_READ | arch_key
)
for i in xrange(0, winreg.QueryInfoKey(root_key)[0]):
inst_id = winreg.EnumKey(root_key, i)
inst_key = winreg.OpenKey(root_key, inst_id)
svr_name = QueryValueEx(inst_key, 'Display Name')[0] svr_name = winreg.QueryValueEx(
svr_superuser = QueryValueEx(inst_key, 'Database Superuser')[0] inst_key, 'Display Name'
svr_port = QueryValueEx(inst_key, 'Port')[0] )[0]
svr_superuser = winreg.QueryValueEx(
inst_key, 'Database Superuser'
)[0]
svr_port = winreg.QueryValueEx(inst_key, 'Port')[0]
svr_discovery_id = inst_id svr_discovery_id = inst_id
svr_comment = gettext("Auto-detected %s installation with the data directory at %s" % ( svr_comment = gettext(
QueryValueEx(inst_key, 'Display Name')[0], "Auto-detected %s installation with the data directory at %s" % (
QueryValueEx(inst_key, 'Data Directory')[0])) winreg.QueryValueEx(
inst_key, 'Display Name'
)[0],
winreg.QueryValueEx(
inst_key, 'Data Directory'
)[0]
)
)
add_server(user_id, servergroup_id, svr_name, svr_superuser, svr_port, svr_discovery_id, svr_comment) add_server(
user_id, servergroup_id, svr_name,
svr_superuser, svr_port,
svr_discovery_id, svr_comment
)
inst_key.Close() inst_key.Close()
except: except:
pass pass
else: else:
# We use the postgres-reg.ini file on non-Windows # We use the postgres-winreg.ini file on non-Windows
try: try:
from configparser import ConfigParser from configparser import ConfigParser
except ImportError: except ImportError: