3
# This file is part of Checkbox.
5
# Copyright 2009 Canonical Ltd.
7
# Checkbox is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
12
# Checkbox is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
23
from io import StringIO
24
from optparse import OptionParser
25
from subprocess import Popen, PIPE
28
# Command to retrieve packages.
29
COMMAND = "COLUMNS=200 dpkg -l"
32
def get_packages(file):
51
"r": "Reinst-required",
54
columns = ["desired", "status", "error", "name", "version", "description"]
55
mandatory_columns = columns[:]
56
mandatory_columns.remove("error") # this is optional
58
"linux-image-" + os.uname()[2]: "linux"}
62
line = file.readline()
63
if line.startswith("+++"):
66
# Get length from separator
68
lengths.extend([len(i) + 1 for i in line.split("-")])
69
for i in range(4, len(lengths)):
70
lengths[i] += lengths[i - 1]
73
for line in file.readlines():
75
for i, column in enumerate(columns):
76
value = line[lengths[i]:lengths[i + 1]].strip()
79
if column == "desired":
80
value = desired_to_value.get(value)
81
elif column == "status":
82
value = status_to_value.get(value)
83
elif column == "error":
84
value = error_to_value.get(value)
88
package[column] = value
90
#Skip records not containing all mandatory columns
91
if not set(mandatory_columns).issubset(list(package.keys())):
94
name = package["name"]
97
package["name"] = aliases[name]
99
if name not in aliases.values():
104
usage = "Usage: %prog [FILE...]"
105
parser = OptionParser(usage=usage)
106
parser.add_option("-i", "--input",
108
help="Read packages from stdin")
109
(options, args) = parser.parse_args(args)
114
output = Popen(COMMAND, stdout=PIPE, shell=True).communicate()[0]
115
file = StringIO(output.decode("utf-8"))
117
packages = get_packages(file)
118
for package in packages:
119
for key, value in package.items():
120
print("%s: %s" % (key, value))
128
if __name__ == "__main__":
129
sys.exit(main(sys.argv[1:]))