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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#!/usr/bin/python3
#
# This file is part of Checkbox.
#
# Copyright 2009 Canonical Ltd.
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
#
import os
import sys
from io import StringIO
from optparse import OptionParser
from subprocess import Popen, PIPE
# Command to retrieve packages.
COMMAND = "COLUMNS=200 dpkg -l"
def get_packages(file):
desired_to_value = {
"u": "Unknown",
"i": "Install",
"r": "Remove",
"p": "Purge",
"h": "Hold"}
status_to_value = {
"n": "Not Installed",
"i": "Installed",
"c": "Cfg-files",
"u": "Unpacked",
"u": "Failed-cfg",
"h": "Half-inst"}
error_to_value = {
"": None,
"h": "Hold",
"r": "Reinst-required",
"x": "both-problems"}
columns = ["desired", "status", "error", "name", "version", "description"]
mandatory_columns = columns[:]
mandatory_columns.remove("error") # this is optional
aliases = {
"linux-image-" + os.uname()[2]: "linux"}
# Skip header lines
while True:
line = file.readline()
if line.startswith("+++"):
break
# Get length from separator
lengths = [0, 1, 2]
lengths.extend([len(i) + 1 for i in line.split("-")])
for i in range(4, len(lengths)):
lengths[i] += lengths[i - 1]
# Get remaining lines
for line in file.readlines():
package = {}
for i, column in enumerate(columns):
value = line[lengths[i]:lengths[i + 1]].strip()
# Convert value
if column == "desired":
value = desired_to_value.get(value)
elif column == "status":
value = status_to_value.get(value)
elif column == "error":
value = error_to_value.get(value)
# Set value
if value:
package[column] = value
#Skip records not containing all mandatory columns
if not set(mandatory_columns).issubset(list(package.keys())):
continue
name = package["name"]
if name in aliases:
yield dict(package)
package["name"] = aliases[name]
if name not in aliases.values():
yield package
def main(args):
usage = "Usage: %prog [FILE...]"
parser = OptionParser(usage=usage)
parser.add_option("-i", "--input",
action="store_true",
help="Read packages from stdin")
(options, args) = parser.parse_args(args)
if options.input:
file = sys.stdin
else:
output = Popen(COMMAND, stdout=PIPE, shell=True).communicate()[0]
file = StringIO(output.decode("utf-8"))
packages = get_packages(file)
for package in packages:
for key, value in package.items():
print("%s: %s" % (key, value))
# Empty line
print()
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
|