Add an initial implementation of a install_package() function, that
basically takes as an argument a package file, tries to figure out its
file type, and if it's one file that we know how to handle (only rpm and
dpkg files implemented so far), check if it's installed, if it's not,
try to install it.
I've improved the patch based on previous input. Basically I've created
the file package.py, and added the PackageError definition on error.py.
Not sure if it's worth to make package a class. Any thoughts on that?
About the rpm_installed() function that's used on autotest_utils, I
believe it's better to have a function a little more generic, like
package_installed(), that's also implemented on this patch. My patch
also removes the rpm_installed function.
Signed of by: Lucas Meneghel Rodrigues <lucasmr@br.ibm.com>
git-svn-id: http://test.kernel.org/svn/autotest/trunk@879 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/bin/autotest_utils.py b/client/bin/autotest_utils.py
index 45628a5..a1311bc 100755
--- a/client/bin/autotest_utils.py
+++ b/client/bin/autotest_utils.py
@@ -527,15 +527,6 @@
return version + '::' + timestamp
-def rpm_installed(package):
- # Test if a package is installed or not
- try:
- system('rpm -q ' + package)
- except:
- return False
- return True
-
-
def write_keyval(dirname, dictionary):
keyval = open(os.path.join(dirname, 'keyval'), 'w')
for key in dictionary.keys():
diff --git a/client/bin/error.py b/client/bin/error.py
index 8ea71c0..a40e44d 100755
--- a/client/bin/error.py
+++ b/client/bin/error.py
@@ -35,6 +35,9 @@
def __str__(self):
return "Command <" + self.args[0] + "> failed, rc=%d" % (self.args[1])
+class PackageError(TestError):
+ """Indicates an error trying to perform a package operation."""
+
class UnhandledError(TestError):
"""Indicates an unhandled exception in a test."""
def __init__(self, prefix):
diff --git a/client/bin/package.py b/client/bin/package.py
new file mode 100644
index 0000000..1dd7b5f
--- /dev/null
+++ b/client/bin/package.py
@@ -0,0 +1,52 @@
+"""
+Functions to handle software packages. The functions covered here aim to be
+generic, with implementations that deal with different package managers, such
+as dpkg and rpm.
+"""
+
+import os, re
+from error import *
+from autotest_utils import *
+
+
+def package_installed(package, package_type):
+ # Verify if a package file from a known package manager is intstalled.
+ if package_type == 'dpkg':
+ status = system_output('dpkg -s %s | grep Status' % package)
+ result = re.search('not-installed', status, re.IGNORECASE)
+ if result:
+ return False
+ else:
+ return True
+ elif package_type == 'rpm':
+ try:
+ system('rpm -q ' + package)
+ except:
+ return False
+ return True
+ else:
+ raise 'PackageError', 'Package method not implemented'
+
+
+def install_package(package):
+ if not os.path.isfile(package):
+ raise ValueError, 'invalid file %s to install' % package
+ # Use file and libmagic to determine the actual package file type.
+ package_type = system_output('file ' + package)
+ known_package = False
+ known_package_managers = ['rpm', 'dpkg']
+ for package_manager in known_package_managers:
+ if package_manager == 'rpm':
+ package_version = system_output('rpm -qp ' + package)
+ package_pattern = 'RPM'
+ install_command = 'rpm -Uvh ' + package
+ elif package_manager == 'dpkg':
+ package_version = system_output('dpkg -f %s Package' % package)
+ package_pattern = 'Debian'
+ install_command = 'dpkg -i ' + package
+ if package_type.__contains__(package_pattern):
+ known_package = True
+ if not package_installed(package_version, package_manager):
+ return system(install_command)
+ if not known_package:
+ raise 'PackageError', 'Package method not implemented'