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

1404.1.1 by Marc Tardif
Ran 2to3 on: backend, run, test, install/config, bin/, checkbox/, checkbox_*/, plugins/ and scripts/*_resource.
1
#!/usr/bin/python3
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
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
1404.1.5 by Marc Tardif
Decoded the output of Popen in *_resource scripts to use the appropriate encoding.
23
from io import StringIO
775 by Marc Tardif
Improved parsing of package resource which fixes bug #539691.
24
from optparse import OptionParser
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
25
from subprocess import Popen, PIPE
26
27
28
# Command to retrieve packages.
29
COMMAND = "COLUMNS=200 dpkg -l"
30
31
775 by Marc Tardif
Improved parsing of package resource which fixes bug #539691.
32
def get_packages(file):
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
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"]
1088.1.1 by Daniel Manrique
Ignore malformed dpkg entries in package_resource (LP: #794747)
55
    mandatory_columns = columns[:]
1394.1.1 by Javier Collado
Fixed python scripts are now pep8 compliant
56
    mandatory_columns.remove("error")  # this is optional
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
57
    aliases = {
58
        "linux-image-" + os.uname()[2]: "linux"}
59
60
    # Skip header lines
775 by Marc Tardif
Improved parsing of package resource which fixes bug #539691.
61
    while True:
62
        line = file.readline()
63
        if line.startswith("+++"):
64
            break
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
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):
1394.1.1 by Javier Collado
Fixed python scripts are now pep8 compliant
76
            value = line[lengths[i]:lengths[i + 1]].strip()
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
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
1088.1.1 by Daniel Manrique
Ignore malformed dpkg entries in package_resource (LP: #794747)
90
        #Skip records not containing all mandatory columns
1404.1.1 by Marc Tardif
Ran 2to3 on: backend, run, test, install/config, bin/, checkbox/, checkbox_*/, plugins/ and scripts/*_resource.
91
        if not set(mandatory_columns).issubset(list(package.keys())):
1088.1.1 by Daniel Manrique
Ignore malformed dpkg entries in package_resource (LP: #794747)
92
            continue
93
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
94
        name = package["name"]
95
        if name in aliases:
96
            yield dict(package)
97
            package["name"] = aliases[name]
98
1538.2.1 by Marc Tardif
Fixed package_resource script to prevent duplicate package names.
99
        if name not in aliases.values():
100
            yield package
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
101
1394.1.1 by Javier Collado
Fixed python scripts are now pep8 compliant
102
775 by Marc Tardif
Improved parsing of package resource which fixes bug #539691.
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:
1404.1.5 by Marc Tardif
Decoded the output of Popen in *_resource scripts to use the appropriate encoding.
114
        output = Popen(COMMAND, stdout=PIPE, shell=True).communicate()[0]
115
        file = StringIO(output.decode("utf-8"))
775 by Marc Tardif
Improved parsing of package resource which fixes bug #539691.
116
117
    packages = get_packages(file)
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
118
    for package in packages:
1404.1.1 by Marc Tardif
Ran 2to3 on: backend, run, test, install/config, bin/, checkbox/, checkbox_*/, plugins/ and scripts/*_resource.
119
        for key, value in package.items():
120
            print("%s: %s" % (key, value))
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
121
122
        # Empty line
1404.1.1 by Marc Tardif
Ran 2to3 on: backend, run, test, install/config, bin/, checkbox/, checkbox_*/, plugins/ and scripts/*_resource.
123
        print()
760.2.7 by Marc Tardif
Replaced the registry and resource scripts and centralized job iteration.
124
125
    return 0
126
127
128
if __name__ == "__main__":
775 by Marc Tardif
Improved parsing of package resource which fixes bug #539691.
129
    sys.exit(main(sys.argv[1:]))