From 53724675697ccdf34b8e6a5da21e9b4fc4d612c5 Mon Sep 17 00:00:00 2001 From: Akshay Joshi Date: Tue, 9 Feb 2021 15:27:12 +0530 Subject: [PATCH] =?UTF-8?q?Use=20'sys.executable'=20to=20get=20the=C2=A0ab?= =?UTF-8?q?solute=20path=20of=20the=20executable=20binary=20for=20the=20Py?= =?UTF-8?q?thon=20interpreter=20instead=20of=20existing=20complex=20logic.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/pgadmin/misc/bgprocess/processes.py | 89 +------------------------ 1 file changed, 1 insertion(+), 88 deletions(-) diff --git a/web/pgadmin/misc/bgprocess/processes.py b/web/pgadmin/misc/bgprocess/processes.py index 39852a33a..9bd8f1f9d 100644 --- a/web/pgadmin/misc/bgprocess/processes.py +++ b/web/pgadmin/misc/bgprocess/processes.py @@ -249,18 +249,9 @@ class BatchProcess(object): executor = file_quote(os.path.join( os.path.dirname(u_encode(__file__)), 'process_executor.py' )) - paths = os.environ['PATH'].split(os.pathsep) - current_app.logger.info( - "Process Executor: Operating System Path %s", - str(paths) - ) - - interpreter = self.get_interpreter(paths) - - p = None cmd = [ - interpreter if interpreter is not None else 'python', + sys.executable if sys.executable is not None else 'python', executor, self.cmd ] cmd.extend(self.args) @@ -370,84 +361,6 @@ class BatchProcess(object): # Explicitly ignoring signals in the child process signal.signal(signal.SIGINT, signal.SIG_IGN) - def get_interpreter(self, paths): - """ - Get interpreter. - :param paths: - :return: - """ - if os.name == 'nt': - paths.insert(0, os.path.join(u_encode(sys.prefix), 'Scripts')) - paths.insert(0, u_encode(sys.prefix)) - - interpreter = self.which('pythonw.exe', paths) - if interpreter is None: - interpreter = self.which('python.exe', paths) - - current_app.logger.info( - "Process Executor: Interpreter value in path: %s", - str(interpreter) - ) - if interpreter is None and current_app.PGADMIN_RUNTIME: - # We've faced an issue with Windows 2008 R2 (x86) regarding, - # not honouring the environment variables set under the Qt - # (e.g. runtime), and also setting PYTHONHOME same as - # sys.executable (i.e. pgAdmin4.exe). - # - # As we know, we're running it under the runtime, we can assume - # that 'venv' directory will be available outside of 'bin' - # directory. - # - # We would try out luck to find python executable based on that - # assumptions. - bin_path = os.path.dirname(sys.executable) - - venv = os.path.realpath( - os.path.join(bin_path, '..\\venv') - ) - - interpreter = self.which('pythonw.exe', [venv]) - if interpreter is None: - interpreter = self.which('python.exe', [venv]) - - current_app.logger.info( - "Process Executor: Interpreter value in virtual " - "environment: %s", str(interpreter) - ) - - if interpreter is not None: - # Our assumptions are proven right. - # Let's append the 'bin' directory to the PATH environment - # variable. And, also set PYTHONHOME environment variable - # to 'venv' directory. - os.environ['PATH'] = bin_path + ';' + os.environ['PATH'] - os.environ['PYTHONHOME'] = venv - else: - # Let's not use sys.prefix in runtime. - # 'sys.prefix' is not identified on *nix systems for some unknown - # reason, while running under the runtime. - # We're already adding '/pgAdmin 4/venv/bin' - # directory in the PATH environment variable. Hence - it will - # anyway be the redundant value in paths. - if not current_app.PGADMIN_RUNTIME: - paths.insert(0, os.path.join(u_encode(sys.prefix), 'bin')) - python_binary_name = 'python{0}'.format(sys.version_info[0]) - interpreter = self.which(u_encode(python_binary_name), paths) - - return interpreter - - def which(self, program, paths): - def is_exe(fpath): - return os.path.exists(fpath) and os.access(fpath, os.X_OK) - - for path in paths: - if not os.path.isdir(path): - continue - exe_file = os.path.join(u_encode(path, fs_encoding), program) - if is_exe(exe_file): - return file_quote(exe_file) - return None - def read_log(self, logfile, log, pos, ctime, ecode=None, enc='utf-8'): import re completed = True