From d51311b7f7caedec77c3474f3232358bd2aba0f5 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Thu, 19 Mar 2020 18:52:49 +0530 Subject: [PATCH] fix: better exception handling for checking sudoers set --- bench/patches/v5/fix_user_permissions.py | 14 +++++++++++++- bench/utils.py | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/bench/patches/v5/fix_user_permissions.py b/bench/patches/v5/fix_user_permissions.py index 346c78b7..c11a9480 100644 --- a/bench/patches/v5/fix_user_permissions.py +++ b/bench/patches/v5/fix_user_permissions.py @@ -12,7 +12,19 @@ from bench.utils import exec_cmd, get_bench_name, get_cmd_output def is_sudoers_set(): cmd = ["sudo", "-n", "bench"] - return (not subprocess.call(cmd)) or (change_uid_msg in get_cmd_output(cmd, _raise=False)) + + with open(os.devnull, "wb") as f: + return_code_check = not subprocess.call(cmd, stdout=f) + + if return_code_check: + try: + bench_warn = change_uid_msg in get_cmd_output(cmd, _raise=False) + except Exception: + bench_warn = False + finally: + return_code_check = return_code_check and bench_warn + + return return_code_check def is_production_set(bench_path): diff --git a/bench/utils.py b/bench/utils.py index 7b1a4742..19fe9036 100755 --- a/bench/utils.py +++ b/bench/utils.py @@ -511,6 +511,7 @@ def check_git_for_shallow_clone(): def get_cmd_output(cmd, cwd='.', _raise=True): + output = "" try: output = subprocess.check_output(cmd, cwd=cwd, shell=True, stderr=subprocess.PIPE).strip() except subprocess.CalledProcessError as e: