@@ -130,60 +130,70 @@ def print_zypper_version():
130
130
info .info ({"zypper_version" : result .split ()[1 ]})
131
131
132
132
133
- def __extract_lu_data (raw : str ):
133
+ def __extract_data (raw , fields ):
134
134
raw_lines = raw .splitlines ()[2 :]
135
135
extracted_data = []
136
136
137
137
for line in raw_lines :
138
138
parts = [part .strip () for part in line .split ('|' )]
139
- if len (parts ) >= 5 :
139
+ if len (parts ) >= max ( fields . values ()) + 1 :
140
140
extracted_data .append ({
141
- "Repository" : parts [1 ],
142
- "Name" : parts [2 ],
143
- "Current Version" : parts [3 ],
144
- "Available Version" : parts [4 ],
145
- "Arch" : parts [5 ]
141
+ field : parts [index ] for field , index in fields .items ()
146
142
})
147
143
148
144
return extracted_data
149
145
150
146
151
- def __extract_lp_data (raw : str ):
152
- raw_lines = raw .splitlines ()[2 :]
153
- extracted_data = []
147
+ def stdout_zypper_command (command ):
148
+ result = subprocess .run (
149
+ command ,
150
+ stdout = subprocess .PIPE ,
151
+ check = False
152
+ )
154
153
155
- for line in raw_lines :
156
- parts = [part .strip () for part in line .split ('|' )]
157
- if len (parts ) >= 5 :
158
- extracted_data .append ({
159
- "Repository" : parts [0 ],
160
- "Name" : parts [1 ],
161
- "Category" : parts [2 ],
162
- "Severity" : parts [3 ],
163
- "Interactive" : parts [4 ],
164
- "Status" : parts [5 ]
165
- })
154
+ if result .returncode != 0 :
155
+ raise RuntimeError (f"zypper returned exit code { result .returncode } " )
166
156
167
- return extracted_data
157
+ return result . stdout . decode ( 'utf-8' )
168
158
169
159
170
- def __extract_orphaned_data (raw : str ):
171
- raw_lines = raw .splitlines ()[2 :]
172
- extracted_data = []
160
+ def extract_lu_data (raw : str ):
161
+ fields = {
162
+ "Repository" : 1 ,
163
+ "Name" : 2 ,
164
+ "Current Version" : 3 ,
165
+ "Available Version" : 4 ,
166
+ "Arch" : 5
167
+ }
173
168
174
- for line in raw_lines :
175
- parts = [part .strip () for part in line .split ('|' )]
176
- if len (parts ) >= 5 :
177
- extracted_data .append ({
178
- "Name" : parts [3 ],
179
- "Version" : parts [4 ]
180
- })
169
+ return __extract_data (raw , fields )
181
170
182
- return extracted_data
171
+
172
+ def extract_lp_data (raw : str ):
173
+ fields = {
174
+ "Repository" : 0 ,
175
+ "Name" : 1 ,
176
+ "Category" : 2 ,
177
+ "Severity" : 3 ,
178
+ "Interactive" : 4 ,
179
+ "Status" : 5
180
+ }
181
+
182
+ return __extract_data (raw , fields )
183
+
184
+
185
+ def extract_orphaned_data (raw : str ):
186
+ fields = {
187
+ "Name" : 3 ,
188
+ "Version" : 4
189
+ }
190
+
191
+ return __extract_data (raw , fields )
183
192
184
193
185
194
def __parse_arguments (argv ):
186
195
parser = argparse .ArgumentParser ()
196
+
187
197
parser .add_mutually_exclusive_group (required = False )
188
198
parser .add_argument (
189
199
"-m" ,
@@ -200,74 +210,46 @@ def __parse_arguments(argv):
200
210
help = "Print less package infos" ,
201
211
)
202
212
parser .set_defaults (all_info = True )
213
+
203
214
return parser .parse_args (argv )
204
215
205
216
206
217
def main (argv : Sequence [str ] | None = None ) -> int :
207
218
args = __parse_arguments (argv )
208
-
209
- raw_zypper_lu = subprocess .run (
210
- ['/usr/bin/zypper' , '--quiet' , 'lu' ],
211
- stdout = subprocess .PIPE ,
212
- check = False
219
+ data_zypper_lu = extract_lu_data (
220
+ stdout_zypper_command (['/usr/bin/zypper' , '--quiet' , 'lu' ])
213
221
)
214
- data_zypper_lu = []
215
- if raw_zypper_lu .returncode == 0 :
216
- data_zypper_lu = __extract_lu_data (raw_zypper_lu .stdout .decode ('utf-8' ))
217
- else :
218
- raise RuntimeError ("zypper returned exit code %d" % raw_zypper_lu .returncode )
219
-
220
- raw_zypper_lp = subprocess .run (
221
- ['/usr/bin/zypper' , '--quiet' , 'lp' ],
222
- stdout = subprocess .PIPE ,
223
- check = False
222
+ data_zypper_lp = extract_lp_data (
223
+ stdout_zypper_command (['/usr/bin/zypper' , '--quiet' , 'lp' ])
224
224
)
225
- data_zypper_lp = []
226
- if raw_zypper_lp .returncode == 0 :
227
- data_zypper_lp = __extract_lp_data (raw_zypper_lp .stdout .decode ('utf-8' ))
228
- else :
229
- raise RuntimeError ("zypper returned exit code %d" % raw_zypper_lp .returncode )
230
-
231
- raw_zypper_orphaned = subprocess .run (
232
- ['/usr/bin/zypper' , '--quiet' , 'pa' , '--orphaned' ],
233
- stdout = subprocess .PIPE ,
234
- check = False
225
+ data_zypper_orphaned = extract_orphaned_data (
226
+ stdout_zypper_command (['/usr/bin/zypper' , '--quiet' , 'pa' , '--orphaned' ])
235
227
)
236
- data_zypper_orphaned = []
237
- if raw_zypper_orphaned .returncode == 0 :
238
- data_zypper_orphaned = __extract_orphaned_data (raw_zypper_orphaned .stdout .decode ('utf-8' ))
239
- else :
240
- raise RuntimeError ("zypper returned exit code %d" % raw_zypper_orphaned .returncode )
241
-
242
- print_pending_updates (data_zypper_lu , args .all_info )
243
-
244
- print_data_sum (data_zypper_lu , "zypper_updates_pending_total" , "zypper packages updates available in total" )
245
-
246
- print_pending_patches (data_zypper_lp , args .all_info )
247
228
229
+ print_pending_updates (data_zypper_lu ,
230
+ args .all_info )
231
+ print_data_sum (data_zypper_lu ,
232
+ "zypper_updates_pending_total" ,
233
+ "zypper packages updates available in total" )
234
+ print_pending_patches (data_zypper_lp ,
235
+ args .all_info )
248
236
print_data_sum (data_zypper_lp ,
249
237
"zypper_patches_pending_total" ,
250
238
"zypper patches available total" )
251
-
252
239
print_data_sum (data_zypper_lp ,
253
240
"zypper_patches_pending_security_total" ,
254
241
"zypper patches available with category security total" ,
255
242
filters = {'Category' : 'security' })
256
-
257
243
print_data_sum (data_zypper_lp ,
258
244
"zypper_patches_pending_security_important_total" ,
259
245
"zypper patches available with category security severity important total" ,
260
246
filters = {'Category' : 'security' , 'Severity' : 'important' })
261
-
262
247
print_data_sum (data_zypper_lp ,
263
248
"zypper_patches_pending_reboot_total" ,
264
249
"zypper patches available which require reboot total" ,
265
250
filters = {'Interactive' : 'reboot' })
266
-
267
251
print_reboot_required ()
268
-
269
252
print_zypper_version ()
270
-
271
253
print_orphaned_packages (data_zypper_orphaned )
272
254
273
255
return 0
0 commit comments