# Run in Docker
# docker run -v /home/martin/temp/:/var/scripts -i -t centos:latest python /var/scripts/packagecentos.py
import subprocess
import re
installed_packages_dict = {}
installed_packages = subprocess.Popen(["yum",'--color=never','list', 'installed'], stdout=subprocess.PIPE, close_fds=True,
).communicate()[0]
for index, line in enumerate(installed_packages.splitlines()):
regex = re.compile('^(.*?)\s+(.*?)\s+@(.*)$')
match = regex.match(line)
if match:
name, version, repo = match.groups()
installed_packages_dict[name] = version
packages_for_upgrade_list = []
packages_for_upgrade = subprocess.Popen(["yum",'check-update'], stdout=subprocess.PIPE, close_fds=True,
).communicate()[0]
for index, line in enumerate(packages_for_upgrade.splitlines()):
regex = re.compile('^(\S+\.\S+)\s+(\S+)\s+\S+$')
match = regex.match(line)
if match:
name, new_version = match.groups()
# Check if this version is new
currently_installed_version = installed_packages_dict.get(name)
if currently_installed_version != None:
if currently_installed_version != new_version:
package_dict = {'name': name,
'current_version': currently_installed_version,
'new_version': new_version
}
packages_for_upgrade_list.append(package_dict)
print packages_for_upgrade_list