def dpkg_query(info, regex): """ Get the given information concerning all package matching the given regex. """ return system.get_output_cmd(["/usr/bin/dpkg-query", "--show", "--showformat", info, regex])
def get_distrib(): """ Find out wich distribution is currently running this script. """ operating_system = system.get_output_cmd(["/bin/uname", "--operating-system"]) if re.search("GNU/Linux", operating_system): if os.path.exists("/etc/debian_version"): return "debian" elif os.path.exists("/etc/redhat-release"): return "redhat" else: raise ValueError("cannot find the current distribution") else: raise ValueError("cannot find the current operating system")
def query_version(package): """ Get the version of the given package """ distribution = get_distrib() # Choose the appropriate package query tool if distribution == "debian": cmd = ["/usr/bin/apt-cache", "show", package] elif distribution == "redhat": cmd = ["/usr/bin/yum", "--quiet", "info", package] else: raise ValueError("cannot find the current distribution") output = system.get_output_cmd(cmd) # Retrieve package informations pattern = re.compile("Version[ \t]*:[ \t]+(?P<version>[0-9.]+)") m = pattern.search(output) # Extract package version if m: return m.group("version") else: raise ValueError("cannot find OpenNMS version") return None
def is_installed(package): """ Get the status of the given package """ #return dpkg_query("${Status}", package) distribution = get_distrib() if distribution == "debian": cmd = ["/usr/bin/dpkg-query", "--show", "--showformat", "${Status}", package] elif distribution == "redhat": cmd = ["/bin/rpm", "--query", package] else: raise ValueError("cannot find the current distribution") output = system.get_output_cmd(cmd) if distribution == "debian": return output == "install ok installed" elif distribution == "redhat": if re.match("package \w+ is not installed", package): return False else: return True