/
dpkg_l.py
239 lines (196 loc) · 5.63 KB
/
dpkg_l.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
r"""jc - JSON Convert `dpkg -l` command output parser
Set the `COLUMNS` environment variable to a large value to avoid field
truncation. For example:
$ COLUMNS=500 dpkg -l | jc --dpkg-l
Usage (cli):
$ dpkg -l | jc --dpkg-l
or
$ jc dpkg -l
Usage (module):
import jc
result = jc.parse('dpkg_l', dpkg_command_output)
Schema:
[
{
"codes": string,
"name": string,
"version": string,
"architecture": string,
"description": string,
"desired": string,
"status": string,
"error": string
}
]
Examples:
$ dpkg -l | jc --dpkg-l -p
[
{
"codes": "ii",
"name": "accountsservice",
"version": "0.6.45-1ubuntu1.3",
"architecture": "amd64",
"description": "query and manipulate user account information",
"desired": "install",
"status": "installed"
},
{
"codes": "rc",
"name": "acl",
"version": "2.2.52-3build1",
"architecture": "amd64",
"description": "Access control list utilities",
"desired": "remove",
"status": "config-files"
},
{
"codes": "uWR",
"name": "acpi",
"version": "1.7-1.1",
"architecture": "amd64",
"description": "displays information on ACPI devices",
"desired": "unknown",
"status": "trigger await",
"error": "reinstall required"
},
{
"codes": "rh",
"name": "acpid",
"version": "1:2.0.28-1ubuntu1",
"architecture": "amd64",
"description": "Advanced Configuration and Power Interface...",
"desired": "remove",
"status": "half installed"
},
{
"codes": "pn",
"name": "adduser",
"version": "3.116ubuntu1",
"architecture": "all",
"description": "add and remove users and groups",
"desired": "purge",
"status": "not installed"
},
...
]
$ dpkg -l | jc --dpkg-l -p -r
[
{
"codes": "ii",
"name": "accountsservice",
"version": "0.6.45-1ubuntu1.3",
"architecture": "amd64",
"description": "query and manipulate user account information"
},
{
"codes": "rc",
"name": "acl",
"version": "2.2.52-3build1",
"architecture": "amd64",
"description": "Access control list utilities"
},
{
"codes": "uWR",
"name": "acpi",
"version": "1.7-1.1",
"architecture": "amd64",
"description": "displays information on ACPI devices"
},
{
"codes": "rh",
"name": "acpid",
"version": "1:2.0.28-1ubuntu1",
"architecture": "amd64",
"description": "Advanced Configuration and Power Interface..."
},
{
"codes": "pn",
"name": "adduser",
"version": "3.116ubuntu1",
"architecture": "all",
"description": "add and remove users and groups"
},
...
]
"""
import jc.utils
import jc.parsers.universal
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.3'
description = '`dpkg -l` command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
compatible = ['linux']
magic_commands = ['dpkg -l']
tags = ['command']
__version__ = info.version
def _process(proc_data):
"""
Final processing to conform to the schema.
Parameters:
proc_data: (List of Dictionaries) raw structured data to process
Returns:
List of Dictionaries. Structured data to conform to the schema:
"""
desired_map = {
'u': 'unknown',
'i': 'install',
'r': 'remove',
'p': 'purge',
'h': 'hold'
}
status_map = {
'n': 'not installed',
'i': 'installed',
'c': 'config-files',
'u': 'unpacked',
'f': 'failed config',
'h': 'half installed',
'w': 'trigger await',
't': 'trigger pending'
}
err_map = {
'r': 'reinstall required'
}
for entry in proc_data:
if 'codes' in entry:
desired, status, *err = list(entry['codes'].lower())
if desired in desired_map:
entry['desired'] = desired_map[desired]
if status in status_map:
entry['status'] = status_map[status]
if err and err[0] in err_map:
entry['error'] = err_map[err[0]]
return proc_data
def parse(data, raw=False, quiet=False):
"""
Main text parsing function
Parameters:
data: (string) text data to parse
raw: (boolean) unprocessed output if True
quiet: (boolean) suppress warning messages if True
Returns:
List of Dictionaries. Raw or processed structured data.
"""
jc.utils.compatibility(__name__, info.compatible, quiet)
jc.utils.input_type_check(data)
working_list = []
raw_output = []
header_found = False
if jc.utils.has_data(data):
# clean up headers
for line in filter(None, data.splitlines()):
if 'Architecture' in line:
header_found = True
working_list.append(line.lower().replace('||/', 'codes'))
continue
if '=========' in line:
continue
if header_found:
working_list.append(line)
raw_output = jc.parsers.universal.simple_table_parse(working_list)
if raw:
return raw_output
else:
return _process(raw_output)