+
+def get_pkg_check_cmd(dist_name):
+ '''Build the command to use for checking if a linux package is installed or not.'''
+
+ if dist_name in ["CO","FD","MG","MD","CO","OS"]: # linux using rpm
+ linux="RH"
+ manager_msg_err="Error : command failed because sat was not able to find apt command"
+ else:
+ linux="DB"
+ manager_msg_err="Error : command failed because sat was not able to find rpm command"
+
+ # 1- search for an installed package manager (rpm on rh, apt on db)
+ cmd_which_rpm=["which", "rpm"]
+ cmd_which_apt=["which", "apt"]
+ with open(os.devnull, 'w') as devnull:
+ # 1) we search for apt (debian based systems)
+ completed=subprocess.call(cmd_which_apt,stdout=devnull, stderr=subprocess.STDOUT)
+ if completed==0 and linux=="DB":
+ cmd_is_package_installed=["apt", "list", "--installed"]
+ else:
+ # 2) if apt not found search for rpm (redhat)
+ completed=subprocess.call(cmd_which_rpm,stdout=devnull, stderr=subprocess.STDOUT) # only 3.8! ,capture_output=True)
+ if completed==0 and linux=="RH":
+ cmd_is_package_installed=["rpm", "-q"]
+ else:
+ # no package manager was found corresponding to dist_name
+ raise src.SatException(manager_msg_err)
+ return cmd_is_package_installed
+
+def check_system_pkg(check_cmd,pkg):
+ '''Check if a package is installed
+ :param check_cmd list: the list of command to use system package manager
+ :param user str: the pkg name to check
+ :rtype: str
+ :return: a string with package name with status un message
+ '''
+ # build command
+ FNULL = open(os.devnull, 'w')
+ cmd_is_package_installed=[]
+ for cmd in check_cmd:
+ cmd_is_package_installed.append(cmd)
+ cmd_is_package_installed.append(pkg)
+
+
+ if check_cmd[0]=="apt":
+ # special treatment for apt
+ # apt output is too messy for being used
+ # some debian packages have version numbers in their name, we need to add a *
+ # also apt do not return status, we need to use grep
+ # and apt output is too messy for being used
+ cmd_is_package_installed[-1]+="*" # we don't specify in pyconf the exact name because of version numbers
+ p=subprocess.Popen(cmd_is_package_installed,
+ stdout=subprocess.PIPE,
+ stderr=FNULL)
+ try:
+ output = subprocess.check_output(['grep', pkg], stdin=p.stdout)
+ msg_status=src.printcolors.printcSuccess("OK")
+ except:
+ msg_status=src.printcolors.printcError("KO")
+ msg_status+=" (package is not installed!)\n"
+ else:
+ p=subprocess.Popen(cmd_is_package_installed,
+ stdout=subprocess.PIPE,
+ stderr=FNULL)
+ output, err = p.communicate()
+ rc = p.returncode
+ if rc==0:
+ msg_status=src.printcolors.printcSuccess("OK")
+ # in python3 output is a byte and should be decoded
+ if isinstance(output, bytes):
+ output = output.decode("utf-8", "ignore")
+ msg_status+=" (" + output.replace('\n',' ') + ")\n" # remove output trailing \n
+ else:
+ msg_status=src.printcolors.printcError("KO")
+ msg_status+=" (package is not installed!)\n"
+
+ return msg_status