From 9044b59a77d87add619b8c00b9ce8477d081e68a Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 27 Jul 2016 17:05:04 +0100 Subject: [PATCH 1/6] Add a test script to ensure continual compliance of the codebase This script will run pep8 and flake8 to ensure the codebase meets the PEP8 requirements, and has no stylistic or code issues prior to deployment and running of python-swupd. Signed-off-by: Ikey Doherty --- test.sh | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100755 test.sh diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..2c363f4 --- /dev/null +++ b/test.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +fileset=`find swupd/ -name '*.py'` +pep8 $fileset || exit 1 +flake8 $fileset || exit 1 + From 4a8316bd99d2bac7d8de25e8ca8a0262ea22d8cd Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 27 Jul 2016 17:06:40 +0100 Subject: [PATCH 2/6] Ensure the codebase is now PEP8 compliant This change also fixed a broken shebang Signed-off-by: Ikey Doherty --- swupd/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swupd/__init__.py b/swupd/__init__.py index 5c75ce0..b14c083 100755 --- a/swupd/__init__.py +++ b/swupd/__init__.py @@ -1,4 +1,4 @@ -#/usr/bin/env python +#!/usr/bin/env python import subprocess import urllib2 import os @@ -48,7 +48,7 @@ def install_bundles(bundle_list, F=None, url=None): err = "" exit_status = 1 else: - cmd = ['sudo','swupd','bundle-add'] + cmd = ['sudo', 'swupd', 'bundle-add'] if bundle in bundle_universe: cmd += [bundle] if F is not None: From 202fa4a020a50e2228577e7f3b2fda669ade68b0 Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 27 Jul 2016 17:10:23 +0100 Subject: [PATCH 3/6] swupd: Add missing license header to the Pythonic code This will ensure that everyone is aware of the project licensing conditions immediately without having to dig around for the LICENSE file Signed-off-by: Ikey Doherty --- swupd/__init__.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/swupd/__init__.py b/swupd/__init__.py index b14c083..af0ce23 100755 --- a/swupd/__init__.py +++ b/swupd/__init__.py @@ -1,4 +1,22 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# This file is part of python-swupd + +# Copyright © 2016 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import subprocess import urllib2 import os From 62da42dac6223a6296adb23aa802d1b8f3e3fc96 Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 27 Jul 2016 17:17:32 +0100 Subject: [PATCH 4/6] 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() From d188416a2cfb877e3e5183eb00bbef8277ff3279 Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 27 Jul 2016 17:18:43 +0100 Subject: [PATCH 5/6] Add new .gitignore to ignore any compiled python assets Signed-off-by: Ikey Doherty --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc From 841c711ba68c04ac337abe5198b6db739408dbcb Mon Sep 17 00:00:00 2001 From: Ikey Doherty Date: Wed, 27 Jul 2016 17:19:10 +0100 Subject: [PATCH 6/6] gitignore: Also add assets emitted during setup.py build Signed-off-by: Ikey Doherty --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0d20b64..864fa4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +/build/