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 <michael.i.doherty@intel.com>
This commit is contained in:
Ikey Doherty
2016-07-27 17:17:32 +01:00
parent 202fa4a020
commit 62da42dac6
+13 -3
View File
@@ -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()