From 62da42dac6223a6296adb23aa802d1b8f3e3fc96 Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 27 Jul 2016 17:17:32 +0100 Subject: [PATCH] swupd: Ensure we use sudo only when necessary It may happen that python-swupd is used in an EUID 0 root process, as such running sudo is not appropriate. With this change, we introduce a command builder that will insert sudo as appropriate, depending on the current EUID. Signed-off-by: Ikey Doherty --- swupd/__init__.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/swupd/__init__.py b/swupd/__init__.py index af0ce23..a91a91a 100755 --- a/swupd/__init__.py +++ b/swupd/__init__.py @@ -49,6 +49,13 @@ def get_installed_bundles(): return p +def _get_swupd_base_command(command): + """ Return the base part of the command based on the EUID """ + if os.geteuid() != 0: + return ['sudo', 'swupd', command] + return ['swupd', command] + + def install_bundles(bundle_list, F=None, url=None): latest_version = get_latest_version() installed_bundles = get_installed_bundles() @@ -66,7 +73,7 @@ def install_bundles(bundle_list, F=None, url=None): err = "" exit_status = 1 else: - cmd = ['sudo', 'swupd', 'bundle-add'] + cmd = _get_swupd_base_command('bundle-add') if bundle in bundle_universe: cmd += [bundle] if F is not None: @@ -98,7 +105,9 @@ def remove_bundles(bundle_list): err_ = "" for bundle in bundle_list: if(bundle in installed_bundles): - p = subprocess.Popen(['sudo', 'swupd', 'bundle-remove', bundle], + cmd = _get_swupd_base_command('bundle-remove') + cmd.append(bundle) + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) o, e = p.communicate() @@ -117,7 +126,8 @@ def update(): exit_status = 0 out = "" err = "" - p = subprocess.Popen(['sudo', 'swupd', 'update'], + cmd = _get_swupd_base_command('update') + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = p.communicate()