~jocave/checkbox/hybrid-amd-gpu-mods

« back to all changes in this revision

Viewing changes to scripts/package_resource

  • Committer: Zygmunt Krynicki
  • Date: 2013-05-17 13:54:25 UTC
  • mto: This revision was merged to the branch mainline in revision 2130.
  • Revision ID: zygmunt.krynicki@canonical.com-20130517135425-cxcenxx5t0qrtbxd
checkbox-ng: add CheckBoxNG sub-project

CheckBoxNG (or lowercase as checkbox-ng, pypi:checkbox-ng) is a clean
implementation of CheckBox on top of PlainBox. It provides a new
executable, 'checkbox' that has some of the same commands that were
previously implemented in the plainbox package.

In particular CheckBoxNG comes with the 'checkbox sru' command
(the same one as in plainbox). Later on this sub-command will be removed
from plainbox.

CheckBoxNG depends on plainbox >= 0.3

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
#
 
3
# This file is part of Checkbox.
 
4
#
 
5
# Copyright 2009 Canonical Ltd.
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
#
 
20
import os
 
21
import sys
 
22
 
 
23
from io import StringIO
 
24
from optparse import OptionParser
 
25
from subprocess import Popen, PIPE
 
26
 
 
27
 
 
28
# Command to retrieve packages.
 
29
COMMAND = "COLUMNS=200 dpkg -l"
 
30
 
 
31
 
 
32
def get_packages(file):
 
33
    desired_to_value = {
 
34
        "u": "Unknown",
 
35
        "i": "Install",
 
36
        "r": "Remove",
 
37
        "p": "Purge",
 
38
        "h": "Hold"}
 
39
 
 
40
    status_to_value = {
 
41
        "n": "Not Installed",
 
42
        "i": "Installed",
 
43
        "c": "Cfg-files",
 
44
        "u": "Unpacked",
 
45
        "u": "Failed-cfg",
 
46
        "h": "Half-inst"}
 
47
 
 
48
    error_to_value = {
 
49
        "":  None,
 
50
        "h": "Hold",
 
51
        "r": "Reinst-required",
 
52
        "x": "both-problems"}
 
53
 
 
54
    columns = ["desired", "status", "error", "name", "version", "description"]
 
55
    mandatory_columns = columns[:]
 
56
    mandatory_columns.remove("error")  # this is optional
 
57
    aliases = {
 
58
        "linux-image-" + os.uname()[2]: "linux"}
 
59
 
 
60
    # Skip header lines
 
61
    while True:
 
62
        line = file.readline()
 
63
        if line.startswith("+++"):
 
64
            break
 
65
 
 
66
    # Get length from separator
 
67
    lengths = [0, 1, 2]
 
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]
 
71
 
 
72
    # Get remaining lines
 
73
    for line in file.readlines():
 
74
        package = {}
 
75
        for i, column in enumerate(columns):
 
76
            value = line[lengths[i]:lengths[i + 1]].strip()
 
77
 
 
78
            # Convert value
 
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)
 
85
 
 
86
            # Set value
 
87
            if value:
 
88
                package[column] = value
 
89
 
 
90
        #Skip records not containing all mandatory columns
 
91
        if not set(mandatory_columns).issubset(list(package.keys())):
 
92
            continue
 
93
 
 
94
        name = package["name"]
 
95
        if name in aliases:
 
96
            yield dict(package)
 
97
            package["name"] = aliases[name]
 
98
 
 
99
        if name not in aliases.values():
 
100
            yield package
 
101
 
 
102
 
 
103
def main(args):
 
104
    usage = "Usage: %prog [FILE...]"
 
105
    parser = OptionParser(usage=usage)
 
106
    parser.add_option("-i", "--input",
 
107
        action="store_true",
 
108
        help="Read packages from stdin")
 
109
    (options, args) = parser.parse_args(args)
 
110
 
 
111
    if options.input:
 
112
        file = sys.stdin
 
113
    else:
 
114
        output = Popen(COMMAND, stdout=PIPE, shell=True).communicate()[0]
 
115
        file = StringIO(output.decode("utf-8"))
 
116
 
 
117
    packages = get_packages(file)
 
118
    for package in packages:
 
119
        for key, value in package.items():
 
120
            print("%s: %s" % (key, value))
 
121
 
 
122
        # Empty line
 
123
        print()
 
124
 
 
125
    return 0
 
126
 
 
127
 
 
128
if __name__ == "__main__":
 
129
    sys.exit(main(sys.argv[1:]))