Skip to content

Commit 6a59a7f

Browse files
committed
Refactor cleanup on exit
1 parent 89bb9c0 commit 6a59a7f

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

zypperoni

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ def recursive_delete(path):
184184
os.system(command)
185185
return True
186186

187+
# Function to cleanup on zypperoni exit
188+
def zypperoni_cleanup():
189+
release_zypp_lock()
190+
recursive_delete(ZYPPERONI_TMP_DIR)
191+
187192
# Function to get output of shell command
188193
def shell_exec(command):
189194
res = subprocess.run(command, shell=True, capture_output=True, encoding="utf8", errors="replace")
@@ -461,8 +466,7 @@ if COMMAND in ["ref", "force-ref"]:
461466
logging.debug(f"Enabled repos: {REPO_ALIAS}")
462467
if not REPO_ALIAS:
463468
logging.info("No repos found. Exiting...")
464-
release_zypp_lock()
465-
recursive_delete(ZYPPERONI_TMP_DIR)
469+
zypperoni_cleanup()
466470
sys.exit()
467471
try:
468472
asyncio.run(main_task(MAX_JOBS, COMMAND, REPO_ALIAS))
@@ -501,27 +505,23 @@ elif COMMAND in ["dup", "dup-download"]:
501505
DUP_PKG.sort()
502506
if not DUP_PKG:
503507
logging.info("Nothing to do. Exiting...")
504-
release_zypp_lock()
505-
recursive_delete(ZYPPERONI_TMP_DIR)
508+
zypperoni_cleanup()
506509
sys.exit()
507510
# do not download if all packages are already in cache
508511
if COMMAND == "dup-download" and download_size_bytes == 0:
509512
logging.info("Nothing to do. Exiting...")
510-
release_zypp_lock()
511-
recursive_delete(ZYPPERONI_TMP_DIR)
513+
zypperoni_cleanup()
512514
sys.exit()
513515
# proceed straight to dup if all packages are in cache
514516
if COMMAND == "dup" and download_size_bytes == 0:
515-
release_zypp_lock()
516-
recursive_delete(ZYPPERONI_TMP_DIR)
517+
zypperoni_cleanup()
517518
logging.info("Zypperoni has finished its tasks. Handing you over to zypper...")
518519
command = f"env ZYPP_SINGLE_RPMTRANS=1 zypper {'--non-interactive' if NO_CONFIRM else ''} dist-upgrade"
519520
os.system(command)
520521
sys.exit()
521522
logging.info(f"Packages to download: {' '.join(DUP_PKG)}")
522523
if not NO_CONFIRM and not query_yes_no("Would you like to continue?", default="yes"):
523-
release_zypp_lock()
524-
recursive_delete(ZYPPERONI_TMP_DIR)
524+
zypperoni_cleanup()
525525
sys.exit()
526526
try:
527527
asyncio.run(main_task(MAX_JOBS, COMMAND, DUP_PKG, NO_CONFIRM))
@@ -551,8 +551,7 @@ elif COMMAND in ["in", "in-download"]:
551551
for item in docroot.iter("message"):
552552
friendly_output += item.text + "\n"
553553
logging.error(f"There was an error processing your request.\n[zypper output]\n{friendly_output.strip()}")
554-
release_zypp_lock()
555-
recursive_delete(ZYPPERONI_TMP_DIR)
554+
zypperoni_cleanup()
556555
sys.exit(6)
557556
# parse all packages from xml output
558557
IN_PKG = []
@@ -569,27 +568,23 @@ elif COMMAND in ["in", "in-download"]:
569568
IN_PKG.sort()
570569
if not IN_PKG:
571570
logging.info("Nothing to do. Exiting...")
572-
release_zypp_lock()
573-
recursive_delete(ZYPPERONI_TMP_DIR)
571+
zypperoni_cleanup()
574572
sys.exit()
575573
# do not download if all packages are already in cache
576574
if COMMAND == "in-download" and download_size_bytes == 0:
577575
logging.info("Nothing to do. Exiting...")
578-
release_zypp_lock()
579-
recursive_delete(ZYPPERONI_TMP_DIR)
576+
zypperoni_cleanup()
580577
sys.exit()
581578
# proceed straight to install if all packages are in cache
582579
if COMMAND == "in" and download_size_bytes == 0:
583-
release_zypp_lock()
584-
recursive_delete(ZYPPERONI_TMP_DIR)
580+
zypperoni_cleanup()
585581
logging.info("Zypperoni has finished its tasks. Handing you over to zypper...")
586582
command = f"env ZYPP_SINGLE_RPMTRANS=1 zypper {'--non-interactive' if NO_CONFIRM else ''} install {' '.join(ARG)}"
587583
os.system(command)
588584
sys.exit()
589585
logging.info(f"Packages to download: {' '.join(IN_PKG)}")
590586
if not NO_CONFIRM and not query_yes_no("Would you like to continue?", default="yes"):
591-
release_zypp_lock()
592-
recursive_delete(ZYPPERONI_TMP_DIR)
587+
zypperoni_cleanup()
593588
sys.exit()
594589
try:
595590
asyncio.run(main_task(MAX_JOBS, COMMAND, IN_PKG, NO_CONFIRM))
@@ -627,27 +622,23 @@ elif COMMAND in ["inr", "inr-download"]:
627622
INR_PKG.sort()
628623
if not INR_PKG:
629624
logging.info("Nothing to do. Exiting...")
630-
release_zypp_lock()
631-
recursive_delete(ZYPPERONI_TMP_DIR)
625+
zypperoni_cleanup()
632626
sys.exit()
633627
# do not download if all packages are already in cache
634628
if COMMAND == "inr-download" and download_size_bytes == 0:
635629
logging.info("Nothing to do. Exiting...")
636-
release_zypp_lock()
637-
recursive_delete(ZYPPERONI_TMP_DIR)
630+
zypperoni_cleanup()
638631
sys.exit()
639632
# proceed straight to inr if all packages are in cache
640633
if COMMAND == "inr" and download_size_bytes == 0:
641-
release_zypp_lock()
642-
recursive_delete(ZYPPERONI_TMP_DIR)
634+
zypperoni_cleanup()
643635
logging.info("Zypperoni has finished its tasks. Handing you over to zypper...")
644636
command = f"env ZYPP_SINGLE_RPMTRANS=1 zypper {'--non-interactive' if NO_CONFIRM else ''} install-new-recommends"
645637
os.system(command)
646638
sys.exit()
647639
logging.info(f"Packages to download: {' '.join(INR_PKG)}")
648640
if not NO_CONFIRM and not query_yes_no("Would you like to continue?", default="yes"):
649-
release_zypp_lock()
650-
recursive_delete(ZYPPERONI_TMP_DIR)
641+
zypperoni_cleanup()
651642
sys.exit()
652643
try:
653644
asyncio.run(main_task(MAX_JOBS, COMMAND, INR_PKG, NO_CONFIRM))

0 commit comments

Comments
 (0)