Compress three near-to equal functions into one

This commit is contained in:
maride 2018-02-02 23:39:37 +01:00
parent 5a3b4914a0
commit 226ad45325

89
run.py
View File

@ -42,21 +42,30 @@ def blockdevice(termsize, settings):
settings["blockdevice"] = _ask("Install where?", os.listdir("/sys/block/"), enforce=False) settings["blockdevice"] = _ask("Install where?", os.listdir("/sys/block/"), enforce=False)
def checks(termsize, settings): def rundir(termsize, settings, directory, validation_suffix, subfile="", ask=False):
for check in sorted(os.listdir("checks/")): for filename in sorted(os.listdir(directory)):
if check[-6:] == ".check": if filename[-len(validation_suffix):] == validation_suffix:
# Run loop # Run loop
run = True run = True
while run: while run:
if ask and _ask("Do you want to run %s?" % (filename), [ "yes", "no" ]) == "no":
continue
_separator(termsize) _separator(termsize)
print("Running '%s'" % (check)) print("Running '%s'" % (filename))
with subprocess.Popen(["checks/%s" % check], env=settings) as process:
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 # Wait for process termination
process.wait() process.wait()
if process.returncode > 0: if process.returncode > 0:
# Woops, check failed somehow # Woops, check failed somehow
print("Check %s returned non-null error code %i." % (check, process.returncode)) print("Executable %s returned non-null error code %i." % (executable, process.returncode))
answer = _ask("Do you want to re-run the check, skip the check or abort the installation?", [ "run", "skip", "abort"]) answer = _ask("Do you want to re-run the executable, skip the check or abort the installation?", [ "run", "skip", "abort"])
if answer == "run": if answer == "run":
# doesn't matter, run is already 'True' # doesn't matter, run is already 'True'
continue continue
@ -67,65 +76,7 @@ def checks(termsize, settings):
else: else:
run = False run = False
else: else:
print("Uh. Found this non-check script in the 'checks' folder: '%s' (Not executing)" % (check)) print("Uh. Found this executable: '%s' (Not executing)" % (filename))
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))
def finish(termsize, settings): def finish(termsize, settings):
@ -157,9 +108,9 @@ def main():
motd(termsize) motd(termsize)
efibios(termsize, settings) efibios(termsize, settings)
blockdevice(termsize, settings) blockdevice(termsize, settings)
checks(termsize, settings) rundir(termsize, settings, "checks", ".check")
steps(termsize, settings) rundir(termsize, settings, "steps", ".step")
flavours(termsize, settings) rundir(termsize, settings, "flavours", ".flavour", "install.sh", ask=True)
finish(termsize, settings) finish(termsize, settings)