package.py revision f912c44b8056cb4fdfc93f47c56c8daabf40d10f
1""" 2Functions to handle software packages. The functions covered here aim to be 3generic, with implementations that deal with different package managers, such 4as dpkg and rpm. 5""" 6 7import os, re 8from error import * 9from autotest_utils import * 10 11 12def package_installed(package, package_type): 13 # Verify if a package file from a known package manager is intstalled. 14 if package_type == 'dpkg': 15 status = system_output('dpkg -s %s | grep Status' % package) 16 result = re.search('not-installed', status, re.IGNORECASE) 17 if result: 18 return False 19 else: 20 return True 21 elif package_type == 'rpm': 22 try: 23 system('rpm -q ' + package) 24 except: 25 return False 26 return True 27 else: 28 raise 'PackageError', 'Package method not implemented' 29 30 31def install_package(package): 32 if not os.path.isfile(package): 33 raise ValueError, 'invalid file %s to install' % package 34 # Use file and libmagic to determine the actual package file type. 35 package_type = system_output('file ' + package) 36 known_package = False 37 known_package_managers = ['rpm', 'dpkg'] 38 for package_manager in known_package_managers: 39 if package_manager == 'rpm': 40 package_version = system_output('rpm -qp ' + package) 41 package_pattern = 'RPM' 42 install_command = 'rpm -Uvh ' + package 43 elif package_manager == 'dpkg': 44 package_version = system_output('dpkg -f %s Package' % package) 45 package_pattern = 'Debian' 46 install_command = 'dpkg -i ' + package 47 if package_type.__contains__(package_pattern): 48 known_package = True 49 if not package_installed(package_version, package_manager): 50 return system(install_command) 51 if not known_package: 52 raise 'PackageError', 'Package method not implemented' 53