From 56fb24c24a69a4140b18f5e2a607b4e30531d02a Mon Sep 17 00:00:00 2001 From: Nikhil Mohite Date: Thu, 29 Sep 2022 12:29:32 +0530 Subject: [PATCH] Fixed the .psqlrc issue for PSQL Tool. #5167 --- docs/en_US/release_notes_6_15.rst | 1 + web/pgadmin/tools/psql/__init__.py | 42 +++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/docs/en_US/release_notes_6_15.rst b/docs/en_US/release_notes_6_15.rst index 1d3332f00..9784b5e20 100644 --- a/docs/en_US/release_notes_6_15.rst +++ b/docs/en_US/release_notes_6_15.rst @@ -27,6 +27,7 @@ Bug fixes ********* | `Issue #5145 `_ - Fixed intermittent error shown while OAuth2 login. + | `Issue #5167 `_ - Fixed the .psqlrc issue for PSQL Tool. | `Issue #5188 `_ - Ensure that the continue/start button should be disabled if the user stops the Debugger for the procedures. | `Issue #5210 `_ - Ensure that the query tool creates a new tab with the appropriate user when pressing Alt+Shift+Q. | `Issue #5212 `_ - Added the close button for all the notifications of the notistack. diff --git a/web/pgadmin/tools/psql/__init__.py b/web/pgadmin/tools/psql/__init__.py index e8cd399b6..6ece64c99 100644 --- a/web/pgadmin/tools/psql/__init__.py +++ b/web/pgadmin/tools/psql/__init__.py @@ -6,7 +6,7 @@ # This software is released under the PostgreSQL Licence # ########################################################################## - +import json import os import select import struct @@ -98,18 +98,10 @@ def panel(trans_id): app.config['sid_soid_mapping'] = dict() if request.args: params.update({k: v for k, v in request.args.items()}) - # Set TERM env for xterm. - os.environ['TERM'] = 'xterm' - - # If psql is enabled in server mode, set psqlrc and hist paths - # to individual user storage. - if config.ENABLE_PSQL and config.SERVER_MODE: - os.environ['PSQLRC'] = get_complete_file_path('.psqlrc', False) - os.environ['PSQL_HISTORY'] = \ - get_complete_file_path('.psql_history', False) o_db_name = _get_database(params['sid'], params['did']) + set_env_variables(is_win=_platform == 'win32') return render_template('editor_template.html', sid=params['sid'], db=underscore_unescape( @@ -125,6 +117,21 @@ def panel(trans_id): ) +def set_env_variables(is_win=False): + # Set TERM env for xterm. + os.environ['TERM'] = 'xterm' + if is_win: + os.environ['PYWINPTY_BACKEND'] = '1' + # If psql is enabled in server mode, set psqlrc and hist paths + # to individual user storage. + if config.ENABLE_PSQL and config.SERVER_MODE: + psql_data = { + 'PSQLRC': get_complete_file_path('.psqlrc', False), + 'PSQL_HISTORY': get_complete_file_path('.psql_history', False) + } + os.environ[current_user.username] = json.dumps(psql_data) + + def set_term_size(fd, row, col, xpix=0, ypix=0): """ Set the terminal size as per UI xterm size. @@ -156,6 +163,15 @@ def connect(): to=request.sid) +def get_user_env(): + env = os.environ + if config.ENABLE_PSQL and config.SERVER_MODE: + user_env = json.loads(os.environ[current_user.username]) + env['PSQLRC'] = user_env['PSQLRC'] + env['PSQL_HISTORY'] = user_env['PSQL_HISTORY'] + return env + + def create_pty_terminal(connection_data): # Create the pty terminal process, parent and fd are file descriptors # for parent and child. @@ -168,7 +184,8 @@ def create_pty_terminal(connection_data): stdin=fd, stdout=fd, stderr=fd, - universal_newlines=True + universal_newlines=True, + env=get_user_env() ) app.config['sessions'][request.sid] = parent @@ -215,8 +232,7 @@ def read_stdout(process, sid, max_read_bytes, win_emit_output=True): def windows_platform(connection_data, sid, max_read_bytes): - os.environ['PYWINPTY_BACKEND'] = '1' - process = PtyProcess.spawn('cmd.exe') + process = PtyProcess.spawn('cmd.exe', env=get_user_env()) process.write(r'"{0}" "{1}" 2>>&1'.format(connection_data[0], connection_data[1]))