From 226ad453250073047730d277969a3bf936c94d82 Mon Sep 17 00:00:00 2001 From: maride Date: Fri, 2 Feb 2018 23:39:37 +0100 Subject: [PATCH] Compress three near-to equal functions into one --- run.py | 89 +++++++++++++--------------------------------------------- 1 file changed, 20 insertions(+), 69 deletions(-) diff --git a/run.py b/run.py index 8cef4ad..2564b82 100755 --- a/run.py +++ b/run.py @@ -42,21 +42,30 @@ def blockdevice(termsize, settings): settings["blockdevice"] = _ask("Install where?", os.listdir("/sys/block/"), enforce=False) -def checks(termsize, settings): - for check in sorted(os.listdir("checks/")): - if check[-6:] == ".check": +def rundir(termsize, settings, directory, validation_suffix, subfile="", ask=False): + for filename in sorted(os.listdir(directory)): + if filename[-len(validation_suffix):] == validation_suffix: # Run loop run = True while run: + if ask and _ask("Do you want to run %s?" % (filename), [ "yes", "no" ]) == "no": + continue + _separator(termsize) - print("Running '%s'" % (check)) - with subprocess.Popen(["checks/%s" % check], env=settings) as process: + print("Running '%s'" % (filename)) + + if subfile: + executable = os.path.join(directory, filename, subfile) + else: + executable = os.path.join(directory, filename) + + with subprocess.Popen([executable], env=settings) as process: # Wait for process termination process.wait() if process.returncode > 0: # Woops, check failed somehow - print("Check %s returned non-null error code %i." % (check, process.returncode)) - answer = _ask("Do you want to re-run the check, skip the check or abort the installation?", [ "run", "skip", "abort"]) + print("Executable %s returned non-null error code %i." % (executable, process.returncode)) + answer = _ask("Do you want to re-run the executable, skip the check or abort the installation?", [ "run", "skip", "abort"]) if answer == "run": # doesn't matter, run is already 'True' continue @@ -67,65 +76,7 @@ def checks(termsize, settings): else: run = False else: - print("Uh. Found this non-check script in the 'checks' folder: '%s' (Not executing)" % (check)) - - -def steps(termsize, settings): - for step in sorted(os.listdir("steps/")): - if step[-5:] == ".step": - # Run loop - run = True - while run: - _separator(termsize) - print("Running '%s'" % (step)) - with subprocess.Popen(["steps/%s" % step], env=settings) as process: - # Wait for process termination - process.wait() - if process.returncode > 0: - # Woops, step failed somehow - print("step %s returned non-null error code %i." % (step, process.returncode)) - answer = _ask("Do you want to re-run the step, skip the step or abort the installation?", [ "run", "skip", "abort"]) - if answer == "run": - # doesn't matter, run is already 'True' - continue - elif answer == "skip": - run = False - elif answer == "abort": - quit() - else: - run = False - else: - print("Uh. Found this non-step script in the 'steps' folder: '%s' (Not executing)" % (step)) - - -def flavours(termsize, settings): - for flavour in sorted(os.listdir("flavours/")): - if flavour[-8:] == ".flavour": - # Run loop - if _ask("Do you want to run flavour %s?" % (flavour), [ "yes", "no" ]) == "no": - continue - - run = True - while run: - _separator(termsize) - with subprocess.Popen(["flavours/%s/install.sh" % flavour], env=settings) as process: - # Wait for process termination - process.wait() - if process.returncode > 0: - # Woops, flavour failed somehow - print("Flavour %s returned non-null error code %i." % (flavour, process.returncode)) - answer = _ask("Do you want to re-run the flavour, skip the flavour or abort the installation?", [ "run", "skip", "abort"]) - if answer == "run": - # doesn't matter, run is already 'True' - continue - elif answer == "skip": - run = False - elif answer == "abort": - quit() - else: - run = False - else: - print("Uh. Found this non-flavour script in the 'flavours' folder: '%s' (Not executing)" % (flavour)) + print("Uh. Found this executable: '%s' (Not executing)" % (filename)) def finish(termsize, settings): @@ -157,9 +108,9 @@ def main(): motd(termsize) efibios(termsize, settings) blockdevice(termsize, settings) - checks(termsize, settings) - steps(termsize, settings) - flavours(termsize, settings) + rundir(termsize, settings, "checks", ".check") + rundir(termsize, settings, "steps", ".step") + rundir(termsize, settings, "flavours", ".flavour", "install.sh", ask=True) finish(termsize, settings)