Skip to content

Commit f866e93

Browse files
committed
Refactor async funcs
1 parent 2337d2f commit f866e93

File tree

1 file changed

+31
-51
lines changed

1 file changed

+31
-51
lines changed

zypperoni

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ from shlex import quote
2929
import xml.etree.ElementTree as ET
3030

3131
# Constants
32-
ZYPPERONI_VERSION = "1.0.6"
32+
ZYPPERONI_VERSION = "1.0.7"
3333
ZYPPER_PID_FILE = "/run/zypp.pid"
3434
ZYPPER_ENV = "ZYPP_CURL2=1 ZYPP_PCK_PRELOAD=1 ZYPP_SINGLE_RPMTRANS=1"
3535

@@ -142,50 +142,32 @@ def shell_exec(command):
142142
return output.strip(), res.returncode
143143

144144
# Async function to perform zypper shell commands
145-
async def zypper_task(lock, UUID, task_type, task_item, total_items, item_counter):
145+
async def zypper_task(lock, UUID, args, task_item, total_items, item_counter):
146146
try:
147147
async with lock:
148148
uuid = UUID.pop()
149149
log_messages = {}
150150
commands = ""
151151
temp_dir = f"{ZYPPERONI_TMP_DIR}/{uuid}/rootfs"
152-
if task_type == "ref":
153-
log_messages.update({"start": f"Refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
154-
log_messages.update({"success": f"Successfully refreshed repo {task_item!r}"})
155-
log_messages.update({"error": f"Error refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
156-
log_messages.update({"exception": f"Received SIGINT while refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
152+
if args.command_name in ["refresh", "ref"]:
153+
log_messages.update({"start": f"{'Force ' if args.force else ''}Refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
154+
log_messages.update({"success": f"Successfully {'force ' if args.force else ''}refreshed repo {task_item!r}"})
155+
log_messages.update({"error": f"Error {'force ' if args.force else ''}refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
156+
log_messages.update({"exception": f"Received SIGINT while {'force ' if args.force else ''}refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
157157
if not os.path.isdir(temp_dir):
158158
commands = refresh_mount_commands + refresh_shell_commands
159159
commands = commands.format(
160160
uuid=uuid,
161-
refresh_type="refresh",
161+
refresh_type="refresh --force" if args.force else "refresh",
162162
repo_alias=task_item,
163163
)
164164
else:
165165
commands = refresh_shell_commands.format(
166166
uuid=uuid,
167-
refresh_type="refresh",
167+
refresh_type="refresh --force" if args.force else "refresh",
168168
repo_alias=task_item,
169169
)
170-
elif task_type == "force-ref":
171-
log_messages.update({"start": f"Force refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
172-
log_messages.update({"success": f"Successfully force refreshed repo {task_item!r}"})
173-
log_messages.update({"error": f"Error force refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
174-
log_messages.update({"exception": f"Received SIGINT while force refreshing repo [{item_counter}/{total_items}] {task_item!r}"})
175-
if not os.path.isdir(temp_dir):
176-
commands = refresh_mount_commands + refresh_shell_commands
177-
commands = commands.format(
178-
uuid=uuid,
179-
refresh_type="refresh --force",
180-
repo_alias=task_item,
181-
)
182-
else:
183-
commands = refresh_shell_commands.format(
184-
uuid=uuid,
185-
refresh_type="refresh --force",
186-
repo_alias=task_item,
187-
)
188-
elif task_type in ["dup", "dup-download", "in", "in-download", "inr", "inr-download"]:
170+
elif args.command_name in ["dist-upgrade", "dup", "install", "in", "install-new-recommends", "inr"]:
189171
log_messages.update({"start": f"Downloading package [{item_counter}/{total_items}] {task_item!r}"})
190172
log_messages.update({"success": f"Successfully downloaded package {task_item!r}"})
191173
log_messages.update({"error": f"Error downloading package [{item_counter}/{total_items}] {task_item!r}"})
@@ -222,14 +204,14 @@ async def zypper_task(lock, UUID, task_type, task_item, total_items, item_counte
222204
logging.debug(log_messages.get("exception"))
223205

224206
# Async function to perform multiple tasks concurrently
225-
async def main_task(num_jobs, task_type, task_items, no_confirm=None):
207+
async def main_task(task_items, args):
226208
EXCEPTION_OCCUR = False
227209
# init array of temp dir UUIDs corresponding to max num of jobs
228-
UUID = [f"{uuid4()!s}" for _ in range(num_jobs)]
210+
UUID = [f"{uuid4()!s}" for _ in range(args.jobs)]
229211
UUID_UNCHANGED = UUID.copy()
230212
total_items = len(task_items)
231213
item_counter = 0
232-
if task_type == "in":
214+
if args.command_name in ["install", "in"]:
233215
install_pkgs = task_items.copy()
234216
try:
235217
# start processing tasks
@@ -241,13 +223,11 @@ async def main_task(num_jobs, task_type, task_items, no_confirm=None):
241223
log_messages = {}
242224
task_item = task_items.pop(0)
243225
item_counter += 1
244-
if task_type == "ref":
245-
log_messages.update({"exception": "Received SIGINT while processing tasks to refresh repo"})
246-
elif task_type == "force-ref":
247-
log_messages.update({"exception": "Received SIGINT while processing tasks to force refresh repo"})
248-
elif task_type in ["dup", "dup-download", "in", "in-download", "inr", "inr-download"]:
226+
if args.command_name in ["refresh", "ref"]:
227+
log_messages.update({"exception": f"Received SIGINT while processing tasks to {'force ' if args.force else ''}refresh repo"})
228+
elif args.command_name in ["dist-upgrade", "dup", "install", "in", "install-new-recommends", "inr"]:
249229
log_messages.update({"exception": "Received SIGINT while processing tasks to download packages"})
250-
asyncio.create_task(zypper_task(lock, UUID, task_type, task_item, total_items, item_counter))
230+
asyncio.create_task(zypper_task(lock, UUID, args, task_item, total_items, item_counter))
251231
await asyncio.sleep(0.1)
252232
# finished processing all tasks
253233
tasks = asyncio.all_tasks()
@@ -272,19 +252,19 @@ async def main_task(num_jobs, task_type, task_items, no_confirm=None):
272252
# release zypper exclusive lock
273253
release_zypp_lock()
274254
# perform additional zypper commands (if any) on no exception
275-
if not EXCEPTION_OCCUR:
255+
if not EXCEPTION_OCCUR and \
256+
args.command_name in ["dist-upgrade", "dup", "install", "in", "install-new-recommends", "inr"] and \
257+
not args.download_only:
276258
msg = "Zypperoni has finished its tasks. Handing you over to zypper..."
277-
if task_type == "dup":
278-
logging.info(color("info", msg))
279-
command = f"env {ZYPPER_ENV} zypper {'--non-interactive' if no_confirm else ''} --no-cd dist-upgrade"
259+
logging.info(color("info", msg))
260+
if args.command_name in ["dist-upgrade", "dup"]:
261+
command = f"env {ZYPPER_ENV} zypper {'--non-interactive' if args.no_confirm else ''} --no-cd dist-upgrade"
280262
os.system(command)
281-
elif task_type == "in":
282-
logging.info(color("info", msg))
283-
command = f"env {ZYPPER_ENV} zypper {'--non-interactive' if no_confirm else ''} --no-cd install {' '.join(install_pkgs)}"
263+
elif args.command_name in ["install", "in"]:
264+
command = f"env {ZYPPER_ENV} zypper {'--non-interactive' if args.no_confirm else ''} --no-cd install {' '.join(install_pkgs)}"
284265
os.system(command)
285-
elif task_type == "inr":
286-
logging.info(color("info", msg))
287-
command = f"env {ZYPPER_ENV} zypper {'--non-interactive' if no_confirm else ''} --no-cd install-new-recommends"
266+
elif args.command_name in ["install-new-recommends", "inr"]:
267+
command = f"env {ZYPPER_ENV} zypper {'--non-interactive' if args.no_confirm else ''} --no-cd install-new-recommends"
288268
os.system(command)
289269

290270
################################
@@ -466,7 +446,7 @@ if args.command_name in ["refresh", "ref"]:
466446
zypperoni_cleanup()
467447
sys.exit()
468448
try:
469-
asyncio.run(main_task(args.jobs, "force-ref" if args.force else "ref", REPO_ALIAS))
449+
asyncio.run(main_task(REPO_ALIAS, args))
470450
except asyncio.exceptions.CancelledError:
471451
logging.debug("Received SIGINT for asyncio runner")
472452
except:
@@ -542,7 +522,7 @@ elif args.command_name in ["dist-upgrade", "dup"]:
542522
zypperoni_cleanup()
543523
sys.exit()
544524
try:
545-
asyncio.run(main_task(args.jobs, "dup-download" if args.download_only else "dup", DUP_PKG, args.no_confirm))
525+
asyncio.run(main_task(DUP_PKG, args))
546526
except asyncio.exceptions.CancelledError:
547527
logging.debug("Received SIGINT for asyncio runner")
548528
except:
@@ -618,7 +598,7 @@ elif args.command_name in ["install", "in"]:
618598
zypperoni_cleanup()
619599
sys.exit()
620600
try:
621-
asyncio.run(main_task(args.jobs, "in-download" if args.download_only else "in", IN_PKG, args.no_confirm))
601+
asyncio.run(main_task(IN_PKG, args))
622602
except asyncio.exceptions.CancelledError:
623603
logging.debug("Received SIGINT for asyncio runner")
624604
except:
@@ -685,7 +665,7 @@ elif args.command_name in ["install-new-recommends", "inr"]:
685665
zypperoni_cleanup()
686666
sys.exit()
687667
try:
688-
asyncio.run(main_task(args.jobs, "inr-download" if args.download_only else "inr", INR_PKG, args.no_confirm))
668+
asyncio.run(main_task(INR_PKG, args))
689669
except asyncio.exceptions.CancelledError:
690670
logging.debug("Received SIGINT for asyncio runner")
691671
except:

0 commit comments

Comments
 (0)