/
rpm_qi.py
311 lines (263 loc) · 9.89 KB
/
rpm_qi.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
r"""jc - JSON Convert `rpm -qi` command output parser
Works with `rpm -qi [package]` or `rpm -qia`.
The `..._epoch` calculated timestamp fields are naive. (i.e. based on the
local time of the system the parser is run on)
The `..._epoch_utc` calculated timestamp fields are timezone-aware and are
only available if the timezone field is UTC.
Usage (cli):
$ rpm -qia | jc --rpm-qi
or
$ jc rpm -qia
Usage (module):
import jc
result = jc.parse('rpm_qi', rpm_qi_command_output)
Schema:
[
{
"name": string,
"epoch": integer,
"version": string,
"release": string,
"architecture": string,
"install_date": string,
"install_date_epoch": integer, # [0]
"install_date_epoch_utc": integer, # [1]
"group": string,
"size": integer,
"license": string,
"signature": string,
"source_rpm": string,
"build_date": string,
"build_epoch": integer, # [0]
"build_epoch_utc": integer, # [1]
"build_host": string,
"relocations": string,
"depends": [
string
],
"pre_depends": [
string
],
"recommends": [
string
],
"suggests": [
string
],
"conflicts": [
string
],
"breaks": [
string
],
"tag": [
string
],
"replaces": [
string
],
"packager": string,
"vendor": string,
"url": string,
"summary": string,
"description": string
}
]
[0] naive timestamp
[1] Aware timestamp if timezone is UTC
Examples:
$ rpm -qia | jc --rpm-qi -p
[
{
"name": "make",
"epoch": 1,
"version": "3.82",
"release": "24.el7",
"architecture": "x86_64",
"install_date": "Wed 16 Oct 2019 09:21:42 AM PDT",
"group": "Development/Tools",
"size": 1160660,
"license": "GPLv2+",
"signature": "RSA/SHA256, Thu 22 Aug 2019 02:34:59 PM PDT, Key ...",
"source_rpm": "make-3.82-24.el7.src.rpm",
"build_date": "Thu 08 Aug 2019 05:47:25 PM PDT",
"build_host": "x86-01.bsys.centos.org",
"relocations": "(not relocatable)",
"packager": "CentOS BuildSystem <http://bugs.centos.org>",
"vendor": "CentOS",
"url": "http://www.gnu.org/software/make/",
"summary": "A GNU tool which simplifies the build process for ...",
"description": "A GNU tool for controlling the generation of ex...",
"build_epoch": 1565311645,
"build_epoch_utc": null,
"install_date_epoch": 1571242902,
"install_date_epoch_utc": null
},
{
"name": "kbd-legacy",
"version": "1.15.5",
"release": "15.el7",
"architecture": "noarch",
"install_date": "Thu 15 Aug 2019 10:53:08 AM PDT",
"group": "System Environment/Base",
"size": 503608,
"license": "GPLv2+",
"signature": "RSA/SHA256, Mon 12 Nov 2018 07:17:49 AM PST, Key ...",
"source_rpm": "kbd-1.15.5-15.el7.src.rpm",
"build_date": "Tue 30 Oct 2018 03:40:00 PM PDT",
"build_host": "x86-01.bsys.centos.org",
"relocations": "(not relocatable)",
"packager": "CentOS BuildSystem <http://bugs.centos.org>",
"vendor": "CentOS",
"url": "http://ftp.altlinux.org/pub/people/legion/kbd",
"summary": "Legacy data for kbd package",
"description": "The kbd-legacy package contains original keymap...",
"build_epoch": 1540939200,
"build_epoch_utc": null,
"install_date_epoch": 1565891588,
"install_date_epoch_utc": null
},
...
]
$ rpm -qia | jc --rpm-qi -p -r
[
{
"name": "make",
"epoch": "1",
"version": "3.82",
"release": "24.el7",
"architecture": "x86_64",
"install_date": "Wed 16 Oct 2019 09:21:42 AM PDT",
"group": "Development/Tools",
"size": "1160660",
"license": "GPLv2+",
"signature": "RSA/SHA256, Thu 22 Aug 2019 02:34:59 PM PDT, Key ...",
"source_rpm": "make-3.82-24.el7.src.rpm",
"build_date": "Thu 08 Aug 2019 05:47:25 PM PDT",
"build_host": "x86-01.bsys.centos.org",
"relocations": "(not relocatable)",
"packager": "CentOS BuildSystem <http://bugs.centos.org>",
"vendor": "CentOS",
"url": "http://www.gnu.org/software/make/",
"summary": "A GNU tool which simplifies the build process for...",
"description": "A GNU tool for controlling the generation of exe..."
},
{
"name": "kbd-legacy",
"version": "1.15.5",
"release": "15.el7",
"architecture": "noarch",
"install_date": "Thu 15 Aug 2019 10:53:08 AM PDT",
"group": "System Environment/Base",
"size": "503608",
"license": "GPLv2+",
"signature": "RSA/SHA256, Mon 12 Nov 2018 07:17:49 AM PST, Key ...",
"source_rpm": "kbd-1.15.5-15.el7.src.rpm",
"build_date": "Tue 30 Oct 2018 03:40:00 PM PDT",
"build_host": "x86-01.bsys.centos.org",
"relocations": "(not relocatable)",
"packager": "CentOS BuildSystem <http://bugs.centos.org>",
"vendor": "CentOS",
"url": "http://ftp.altlinux.org/pub/people/legion/kbd",
"summary": "Legacy data for kbd package",
"description": "The kbd-legacy package contains original keymaps..."
},
...
]
"""
import jc.utils
class info():
"""Provides parser metadata (version, author, etc.)"""
version = '1.8'
description = '`rpm -qi` command parser'
author = 'Kelly Brazil'
author_email = 'kellyjonbrazil@gmail.com'
compatible = ['linux']
magic_commands = ['rpm -qi', 'rpm -qia', 'rpm -qai']
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.
"""
int_list = {'epoch', 'size', 'installed_size'}
split_list = {'depends', 'pre_depends', 'recommends', 'suggests', 'conflicts',
'breaks', 'tag', 'replaces'}
for entry in proc_data:
for key in entry:
if key in int_list:
entry[key] = jc.utils.convert_to_int(entry[key])
# for apt-cache show output
if key in split_list:
val_list = entry[key].split(',')
val_list = [x.strip() for x in val_list if x]
entry[key] = val_list
if 'build_date' in entry:
timestamp = jc.utils.timestamp(entry['build_date'], format_hint=(3000,))
entry['build_epoch'] = timestamp.naive
entry['build_epoch_utc'] = timestamp.utc
if 'install_date' in entry:
timestamp = jc.utils.timestamp(entry['install_date'], format_hint=(3000,))
entry['install_date_epoch'] = timestamp.naive
entry['install_date_epoch_utc'] = timestamp.utc
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)
raw_output = []
entry_obj = {}
last_entry = None
this_entry = None
desc_entry = False
desc_en_entry = False
description = []
if jc.utils.has_data(data):
for line in filter(None, data.splitlines()):
split_line = line.split(': ', maxsplit=1)
if (split_line[0].startswith('Name') or split_line[0] == 'Package') and len(split_line) == 2:
this_entry = split_line[1].strip()
if this_entry != last_entry:
if entry_obj:
if description:
entry_obj['description'] = ' '.join(description)
raw_output.append(entry_obj)
entry_obj = {}
last_entry = this_entry
desc_entry = False
desc_en_entry = False
if line.startswith('Description :'):
desc_entry = True
description = []
continue
# for apt-cache show output
if line.startswith('Description-en:'):
desc_en_entry = True
description = [split_line[1].strip()]
continue
if desc_entry:
description.append(line)
continue
if desc_en_entry and line.startswith(' '):
description.append(line)
continue
if len(split_line) == 2:
keyname = jc.utils.normalize_key(split_line[0])
entry_obj[keyname] = split_line[1].strip()
if entry_obj:
if description:
entry_obj['description'] = ' '.join(description)
raw_output.append(entry_obj)
return raw_output if raw else _process(raw_output)