Skip to content

Commit 68a414f

Browse files
committed
refactor(zypper.py): better error handling and increase readability
Signed-off-by: Wabri <[email protected]>
1 parent aac8a84 commit 68a414f

File tree

1 file changed

+57
-75
lines changed

1 file changed

+57
-75
lines changed

Diff for: zypper.py

+57-75
Original file line numberDiff line numberDiff line change
@@ -130,60 +130,70 @@ def print_zypper_version():
130130
info.info({"zypper_version": result.split()[1]})
131131

132132

133-
def __extract_lu_data(raw: str):
133+
def __extract_data(raw, fields):
134134
raw_lines = raw.splitlines()[2:]
135135
extracted_data = []
136136

137137
for line in raw_lines:
138138
parts = [part.strip() for part in line.split('|')]
139-
if len(parts) >= 5:
139+
if len(parts) >= max(fields.values()) + 1:
140140
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()
146142
})
147143

148144
return extracted_data
149145

150146

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+
)
154153

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}")
166156

167-
return extracted_data
157+
return result.stdout.decode('utf-8')
168158

169159

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+
}
173168

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)
181170

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)
183192

184193

185194
def __parse_arguments(argv):
186195
parser = argparse.ArgumentParser()
196+
187197
parser.add_mutually_exclusive_group(required=False)
188198
parser.add_argument(
189199
"-m",
@@ -200,74 +210,46 @@ def __parse_arguments(argv):
200210
help="Print less package infos",
201211
)
202212
parser.set_defaults(all_info=True)
213+
203214
return parser.parse_args(argv)
204215

205216

206217
def main(argv: Sequence[str] | None = None) -> int:
207218
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'])
213221
)
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'])
224224
)
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'])
235227
)
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)
247228

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)
248236
print_data_sum(data_zypper_lp,
249237
"zypper_patches_pending_total",
250238
"zypper patches available total")
251-
252239
print_data_sum(data_zypper_lp,
253240
"zypper_patches_pending_security_total",
254241
"zypper patches available with category security total",
255242
filters={'Category': 'security'})
256-
257243
print_data_sum(data_zypper_lp,
258244
"zypper_patches_pending_security_important_total",
259245
"zypper patches available with category security severity important total",
260246
filters={'Category': 'security', 'Severity': 'important'})
261-
262247
print_data_sum(data_zypper_lp,
263248
"zypper_patches_pending_reboot_total",
264249
"zypper patches available which require reboot total",
265250
filters={'Interactive': 'reboot'})
266-
267251
print_reboot_required()
268-
269252
print_zypper_version()
270-
271253
print_orphaned_packages(data_zypper_orphaned)
272254

273255
return 0

0 commit comments

Comments
 (0)