-
Notifications
You must be signed in to change notification settings - Fork 3
/
centos.py
executable file
·58 lines (48 loc) · 1.38 KB
/
centos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/env python
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import sys
_stdout = sys.stdout
_stderr = sys.stderr
sys.stdout = StringIO()
sys.stderr = StringIO()
sys.path.insert(0, '/usr/share/yum-cli')
import yum
import cli
import json
# Get verions of all installed packages
base = yum.YumBase()
base.conf.cache = 1
versions = dict()
for pkg in base.rpmdb.returnPackages():
versions[pkg.name] = pkg.printVer()
# Get list of packages available for update
base_cli = cli.YumBaseCli()
try:
base_cli.getOptionsConfig(["list", "updates"])
base_cli.doLock()
base_cli.doCommands()
updates = base_cli.doPackageLists(pkgnarrow="updates").updates
finally:
base_cli.closeRpmDB()
base_cli.doUnlock()
# Build list of updated packages info
updates_info = []
for pkg in sorted(updates):
info = dict(
name=pkg.name,
candidate_version=pkg.printVer(),
current_version=versions[pkg.name],
priority="standard", # can be ignored
section=pkg.repo.id, # whichever repo is belongs to
security=False, # did not found any way to retreive the list of security updates.
# yum-plugin-security is not supported well in CentOS.
)
updates_info.append(info)
# Dump and print list of info
dump = json.dumps(updates_info)
sys.stdout = _stdout
sys.stderr = _stderr
print dump