Even with the nested try-exception code, I'm OK with this patch :-D
--
Reviewed-by: Paulo Vital <pvital(a)linux.vnet.ibm.com>
On Tue, 2014-03-11 at 17:34 -0300, Aline Manera wrote:
From: Aline Manera <alinefm(a)br.ibm.com>
Instead of using platform.linux_distribution() and keep a list of possible
values to match, this patch verifies the tools on the system to identify
which update tool to use in Kimchi.
So try to import yum module, apt module or run the zypper command and in
last case raise an error.
Signed-off-by: Aline Manera <alinefm(a)br.ibm.com>
---
src/kimchi/swupdate.py | 33 ++++++++++++++++-----------------
1 file changed, 16 insertions(+), 17 deletions(-)
diff --git a/src/kimchi/swupdate.py b/src/kimchi/swupdate.py
index 2c2713a..ff5c9d1 100644
--- a/src/kimchi/swupdate.py
+++ b/src/kimchi/swupdate.py
@@ -17,7 +17,6 @@
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-import platform
import subprocess
import time
@@ -25,11 +24,6 @@ from kimchi.basemodel import Singleton
from kimchi.exception import NotFoundError, OperationFailed
from kimchi.utils import kimchi_log, run_command
-YUM_DISTROS = ['fedora', 'red hat enterprise linux',
- 'red hat enterprise linux server']
-APT_DISTROS = ['debian', 'ubuntu']
-ZYPPER_DISTROS = ['opensuse ', 'suse linux enterprise server ']
-
class SoftwareUpdate(object):
__metaclass__ = Singleton
@@ -51,19 +45,24 @@ class SoftwareUpdate(object):
# Get the distro of host machine and creates an object related to
# correct package management system
- self._distro = platform.linux_distribution()[0].lower()
- if (self._distro in YUM_DISTROS):
+ try:
+ __import__('yum')
kimchi_log.info("Loading YumUpdate features.")
self._pkg_mnger = YumUpdate()
- elif (self._distro in APT_DISTROS):
- kimchi_log.info("Loading AptUpdate features.")
- self._pkg_mnger = AptUpdate()
- elif (self._distro in ZYPPER_DISTROS):
- kimchi_log.info("Loading ZypperUpdate features.")
- self._pkg_mnger = ZypperUpdate()
- else:
- raise Exception("There is no compatible package manager for "
- "this system.")
+ except ImportError:
+ try:
+ __import__('apt')
+ kimchi_log.info("Loading AptUpdate features.")
+ self._pkg_mnger = AptUpdate()
+ except ImportError:
+ zypper_help = "zypper --help"
+ (stdout, stderr, returncode) = run_command(zypper_help)
+ if returncode == 0:
+ kimchi_log.info("Loading ZypperUpdate features.")
+ self._pkg_mnger = ZypperUpdate()
+ else:
+ raise Exception("There is no compatible package manager "
+ "for this system.")
def _scanUpdates(self):
"""