From 877f8291b0f2fb86be82e01596656f0f4761d3e9 Mon Sep 17 00:00:00 2001 From: mirkobrombin Date: Wed, 21 Dec 2022 21:49:33 +0100 Subject: [PATCH] core: Add support for !nextBoot commands --- vanilla_first_setup/utils/processor.py | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/vanilla_first_setup/utils/processor.py b/vanilla_first_setup/utils/processor.py index a0956a3..33a089a 100644 --- a/vanilla_first_setup/utils/processor.py +++ b/vanilla_first_setup/utils/processor.py @@ -30,11 +30,48 @@ class Processor: def run(log_path, pre_run, post_run, commands): commands = pre_run + commands + post_run out_run = "" + next_boot = [] + next_boot_script_path = os.path.expanduser("~/.local/org.vanillaos.FirstSetup.nextBoot") + next_boot_autostart_path = os.path.expanduser("~/.config/autostart/org.vanillaos.FirstSetup.nextBoot.desktop") abroot_bin = shutil.which("abroot") logger.info("processing the following commands: \n%s" % '\n'.join(commands)) + # nextBoot commands are collected in ~/.local/org.vanillaos.FirstSetup.nextBoot + # and executed at the next boot by a desktop entry + for command in commands: + if command.startswith("!nextBoot"): + next_boot.append(command.replace("!nextBoot", "")) + continue + + if len(next_boot) > 0: + with open(next_boot_script_path, "w") as f: + f.write("#!/bin/sh\n") + f.write("# This file was created by FirstSetup\n") + f.write("# Do not edit this file manually\n\n") + + for command in next_boot: + f.write(f"{command}\n") + + f.flush() + f.close() + + # setting the file executable + os.chmod(next_boot_script_path, 0o755) + + # creating the desktop entry + with open(next_boot_autostart_path, "w") as f: + f.write("[Desktop Entry]\n") + f.write("Name=FirstSetup Next Boot\n") + f.write("Comment=Run FirstSetup commands at the next boot\n") + f.write("Exec=kgx -e bash -c 'sh %s'\n" % next_boot_script_path) + f.write("Terminal=false\n") + f.write("Type=Application\n") + f.write("X-GNOME-Autostart-enabled=true\n") + f.flush() + f.close() + # generating a temporary file to store all the commands so we can # run them all at once with tempfile.NamedTemporaryFile(mode='w', delete=False) as f: @@ -43,6 +80,9 @@ class Processor: f.write("# Do not edit this file manually\n\n") for command in commands: + if command.startswith("!nextBoot"): + continue + if command.startswith("!noSudo"): command = command.replace("!noSudo", "sudo -u $USER")