1) Binary path set to the default location in which we install the binaries.

2) Validate path not working if there is a space in the binary path.
3) Handle the case when the path contains '$DIR'. 

refs #5370
This commit is contained in:
Akshay Joshi 2021-06-10 22:49:05 +05:30
parent b2042f48cd
commit c86a2b643a
6 changed files with 144 additions and 29 deletions

View File

@ -8,16 +8,16 @@
##########################################################################
import os
import sys
import json
import config
import copy
from flask import render_template
from flask_babelex import gettext as _
from pgadmin.utils.preferences import Preferences
from werkzeug.exceptions import InternalServerError
from pgadmin.utils.constants import BINARY_PATHS
from pgadmin.utils import set_default_binary_path
from pgadmin.utils import set_binary_path, replace_binary_path
class ServerType(object):
@ -60,15 +60,19 @@ class ServerType(object):
@classmethod
def register_preferences(cls):
paths = Preferences('paths', _('Paths'))
bin_paths = BINARY_PATHS
bin_paths = copy.deepcopy(BINARY_PATHS)
for key in cls.registry:
st = cls.registry[key]
default_bin_path = config.DEFAULT_BINARY_PATHS.get(key, "")
if default_bin_path != "":
set_default_binary_path(default_bin_path, bin_paths, key)
if key not in ['pg', 'ppas']:
continue
if key == 'pg':
# Set the DEFAULT_BINARY_PATHS if any
ServerType.set_default_binary_path(
bin_paths['pg_bin_paths'], key)
st.utility_path = paths.register(
'bin_paths', 'pg_bin_dir',
_("PostgreSQL Binary Path"), 'selectFile',
@ -76,6 +80,10 @@ class ServerType(object):
category_label=_('Binary paths')
)
elif key == 'ppas':
# Set the DEFAULT_BINARY_PATHS if any
ServerType.set_default_binary_path(
bin_paths['as_bin_paths'], key)
st.utility_path = paths.register(
'bin_paths', 'ppas_bin_dir',
_("EDB Advanced Server Binary Path"), 'selectFile',
@ -83,6 +91,32 @@ class ServerType(object):
category_label=_('Binary paths')
)
def path_converter(old_path):
"""
This function is used to convert old path to the
new paths which are in JSON format.
"""
bin_paths_server_based = \
copy.deepcopy(BINARY_PATHS['pg_bin_paths'])
if key == 'ppas':
bin_paths_server_based = \
copy.deepcopy(BINARY_PATHS['as_bin_paths'])
if not ServerType.is_binary_path_of_type_json(old_path):
set_binary_path(old_path, bin_paths_server_based,
key, set_as_default=True)
else:
bin_paths_server_based = json.loads(old_path)
# Set the DEFAULT_BINARY_PATHS if any
ServerType.set_default_binary_path(bin_paths_server_based, key)
return json.dumps(bin_paths_server_based)
# Run the migrate user preferences.
paths.migrate_user_preferences(st.utility_path.pid,
path_converter)
@property
def priority(self):
return self.spriority
@ -138,17 +172,8 @@ class ServerType(object):
if bin_path is None:
return None
if "$DIR" in bin_path:
# When running as an WSGI application, we will not find the
# '__file__' attribute for the '__main__' module.
main_module_file = getattr(
sys.modules['__main__'], '__file__', None
)
if main_module_file is not None:
bin_path = bin_path.replace(
"$DIR", os.path.dirname(main_module_file)
)
# Check if "$DIR" present in binary path
bin_path = replace_binary_path(bin_path)
return os.path.abspath(os.path.join(
bin_path,
@ -175,6 +200,47 @@ class ServerType(object):
return default_path
@staticmethod
def is_default_binary_path_set(binary_paths):
"""
This function is used to iterate through the binary paths
and check whether isDefault is set to true.
"""
for path in binary_paths:
if path['isDefault']:
return True
return False
@staticmethod
def is_binary_path_of_type_json(binary_path):
"""
This function will check if the binary path is of type json or not.
"""
try:
json.loads(binary_path)
except ValueError:
return False
return True
@staticmethod
def set_default_binary_path(bin_paths, server_type):
"""
This function is used to check whether default binary path is set
or not and then iterate through config.DEFAULT_BINARY_PATHS and
set the path based on version number.
"""
is_default_path_set = ServerType.is_default_binary_path_set(bin_paths)
for path in config.DEFAULT_BINARY_PATHS:
path_value = config.DEFAULT_BINARY_PATHS[path]
if path_value is not None and path_value != "" and \
path.find(server_type) == 0 and len(path.split('-')) > 1:
set_binary_path(path_value, bin_paths, server_type,
path.split('-')[1])
elif path_value is not None and path_value != "" and \
path.find(server_type) == 0:
set_binary_path(path_value, bin_paths, server_type,
set_as_default=not is_default_path_set)
# Default Server Type
ServerType('pg', _("PostgreSQL"), -1)

View File

@ -12,7 +12,7 @@
import pgadmin.utils.driver as driver
from flask import url_for, render_template, Response, request
from flask_babelex import gettext
from pgadmin.utils import PgAdminModule
from pgadmin.utils import PgAdminModule, replace_binary_path
from pgadmin.utils.csrf import pgCSRFProtect
from pgadmin.utils.session import cleanup_session_files
from pgadmin.misc.themes import get_all_themes
@ -191,11 +191,18 @@ def validate_binary_path():
version_str = ''
if 'utility_path' in data and data['utility_path'] is not None:
# Check if "$DIR" present in binary path
binary_path = replace_binary_path(data['utility_path'])
for utility in UTILITIES_ARRAY:
full_path = os.path.abspath(
os.path.join(data['utility_path'],
os.path.join(binary_path,
(utility if os.name != 'nt' else
(utility + '.exe'))))
# Replace the spaces with '\'
full_path = full_path.replace(" ", "\\ ")
try:
# Get the output of the '--version' command
version_string = subprocess.getoutput(full_path + ' --version')

View File

@ -1161,7 +1161,7 @@ define([
cell: Backgrid.Extension.SelectFileCell,
dialog_type: 'select_folder',
dialog_title: gettext('Select Folder'),
placeholder: gettext('Select binary path ...'),
placeholder: pgAdmin.server_mode === 'False' ? gettext('Select binary path...') : pgAdmin.enable_binary_path_browsing ? gettext('Select binary path...') : gettext('Enter binary path...'),
browse_btn_label: gettext('Select path'),
check_btn_label: gettext('Validate utilities'),
browse_btn_visible: pgAdmin.server_mode === 'False' ? true : pgAdmin.enable_binary_path_browsing ? true : false

View File

@ -296,23 +296,31 @@ def get_server(sid):
return server
def set_default_binary_path(binary_path, bin_paths, server_type):
def set_binary_path(binary_path, bin_paths, server_type,
version_number=None, set_as_default=False):
"""
This function is used to iterate through the utilities and set the
default binary path.
"""
# Check if "$DIR" present in binary path
binary_path = replace_binary_path(binary_path)
for utility in UTILITIES_ARRAY:
full_path = os.path.abspath(
os.path.join(binary_path, (utility if os.name != 'nt' else
(utility + '.exe'))))
# Replace the spaces with '\'
full_path = full_path.replace(" ", "\\ ")
try:
# Get the output of the '--version' command
version_string = subprocess.getoutput(full_path + ' --version')
# if version_number is provided then no need to fetch it.
if version_number is None:
# Get the output of the '--version' command
version_string = subprocess.getoutput(full_path + ' --version')
# Get the version number by splitting the result string
version_number = \
version_string.split(") ", 1)[1].split('.', 1)[0]
# Get the version number by splitting the result string
version_number = \
version_string.split(") ", 1)[1].split('.', 1)[0]
# Get the paths array based on server type
if 'pg_bin_paths' in bin_paths or 'as_bin_paths' in bin_paths:
@ -326,13 +334,35 @@ def set_default_binary_path(binary_path, bin_paths, server_type):
if path['version'].find(version_number) == 0 and \
path['binaryPath'] is None:
path['binaryPath'] = binary_path
path['isDefault'] = True
if set_as_default:
path['isDefault'] = True
break
break
except Exception:
continue
def replace_binary_path(binary_path):
"""
This function is used to check if $DIR is present in
the binary path. If it is there then replace it with
module.
"""
if "$DIR" in binary_path:
# When running as an WSGI application, we will not find the
# '__file__' attribute for the '__main__' module.
main_module_file = getattr(
sys.modules['__main__'], '__file__', None
)
if main_module_file is not None:
binary_path = binary_path.replace(
"$DIR", os.path.dirname(main_module_file)
)
return binary_path
# Shortcut configuration for Accesskey
ACCESSKEY_FIELDS = [
{

View File

@ -635,3 +635,15 @@ class Preferences(object):
return False, str(e)
return True, None
def migrate_user_preferences(self, pid, converter_func):
"""
This function is used to migrate user preferences.
"""
user_prefs = UserPrefTable.query.filter_by(
pid=pid
)
for pref in user_prefs:
pref.value = converter_func(pref.value)
db.session.commit()

View File

@ -36,7 +36,7 @@ from regression import test_setup
from pgadmin.utils.preferences import Preferences
from pgadmin.utils.constants import BINARY_PATHS
from pgadmin.utils import set_default_binary_path
from pgadmin.utils import set_binary_path
from functools import wraps
@ -766,7 +766,7 @@ def configure_preferences(default_binary_path=None):
# set the default binary paths based on server version
if server in default_binary_path and \
default_binary_path[server] != "":
set_default_binary_path(
set_binary_path(
default_binary_path[server], bin_paths, server)
bin_paths_server_based = json.dumps(bin_paths['pg_bin_paths'])