From 02654243aab838f8aad3ff034ecd04232ae9a070 Mon Sep 17 00:00:00 2001 From: Jarred Wilson Date: Fri, 18 Oct 2024 17:09:28 +0000 Subject: [PATCH] fix: logging output configuration to recipe - move log config to main init - log_file path to /tmp - remove logging config from conn_check - set logging level to debug - set log file permissions --- debian/vanilla-first-setup.install | 1 - first-setup.txt | 1 - recipe.json | 2 +- vanilla_first_setup/defaults/conn_check.py | 1 - vanilla_first_setup/main.py | 29 +++++++++++++++++++--- vanilla_first_setup/utils/builder.py | 15 ----------- vanilla_first_setup/utils/recipe.py | 14 ++++------- vanilla_first_setup/window.py | 2 +- 8 files changed, 33 insertions(+), 32 deletions(-) delete mode 100644 first-setup.txt diff --git a/debian/vanilla-first-setup.install b/debian/vanilla-first-setup.install index 057aa5ce..63b45f08 100644 --- a/debian/vanilla-first-setup.install +++ b/debian/vanilla-first-setup.install @@ -1,6 +1,5 @@ recipe.json usr/share/org.vanillaos.FirstSetup/ __first_setup_reset_session usr/bin/ __first_setup_cleanup usr/bin/ -first-setup.txt etc/vanilla/ data/org.vanillaos.FirstSetup.policy usr/share/polkit-1/actions/ data/org.vanillaos.FirstSetup.rules usr/share/polkit-1/rules.d/ diff --git a/first-setup.txt b/first-setup.txt deleted file mode 100644 index 2f028ccb..00000000 --- a/first-setup.txt +++ /dev/null @@ -1 +0,0 @@ -# Vanilla First Setup Log File diff --git a/recipe.json b/recipe.json index 77633635..fc9f207b 100644 --- a/recipe.json +++ b/recipe.json @@ -1,5 +1,5 @@ { - "log_file": "/etc/vanilla/first-setup.log", + "log_file": "/tmp/first-setup.log", "distro_name": "Vanilla OS", "distro_logo": "org.vanillaos.FirstSetup-flower", "pre_run": [ diff --git a/vanilla_first_setup/defaults/conn_check.py b/vanilla_first_setup/defaults/conn_check.py index 2070ec26..f9b0ef4d 100644 --- a/vanilla_first_setup/defaults/conn_check.py +++ b/vanilla_first_setup/defaults/conn_check.py @@ -25,7 +25,6 @@ from vanilla_first_setup.utils.run_async import RunAsync from vanilla_first_setup.utils.network import check_connection -logging.basicConfig(level=logging.INFO) logger = logging.getLogger("FirstSetup::Conn_Check") diff --git a/vanilla_first_setup/main.py b/vanilla_first_setup/main.py index ce848c44..bb6b6419 100644 --- a/vanilla_first_setup/main.py +++ b/vanilla_first_setup/main.py @@ -30,19 +30,42 @@ import os import sys import logging +import json from gettext import gettext as _ from vanilla_first_setup.window import VanillaWindow +from vanilla_first_setup.utils.recipe import recipe_path import subprocess - -logging.basicConfig(level=logging.INFO) logger = logging.getLogger("FirstSetup::Main") - class FirstSetupApplication(Adw.Application): """The main application singleton class.""" def __init__(self): + # here we create a temporary file to store the output of the commands + # the log path is defined in the recipe + with open(recipe_path, 'r') as file: + recipe = json.load(file) + + if "log_file" not in recipe: + logger.critical("Missing 'log_file' in the recipe.") + sys.exit(1) + + log_path = recipe["log_file"] + + if not os.path.exists(log_path): + try: + open(log_path, "a").close() + os.chmod(log_path, 0o666) + logging.basicConfig(level=logging.DEBUG, + filename=log_path, + filemode='a', + ) + except OSError as e: + logger.warning(f"failed to create log file: {log_path}: {e}") + logging.warning("No log will be stored.") + + super().__init__( application_id="org.vanillaos.FirstSetup", flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, diff --git a/vanilla_first_setup/utils/builder.py b/vanilla_first_setup/utils/builder.py index 87c673d5..f9c8c3ed 100644 --- a/vanilla_first_setup/utils/builder.py +++ b/vanilla_first_setup/utils/builder.py @@ -65,21 +65,6 @@ def __init__(self, window, new_user: bool = False): self.__load() def __load(self): - # here we create a temporary file to store the output of the commands - # the log path is defined in the recipe - if "log_file" not in self.__recipe.raw: - logger.critical("Missing 'log_file' in the recipe.") - sys.exit(1) - - log_path = self.__recipe.raw["log_file"] - - if not os.path.exists(log_path): - try: - open(log_path, "a").close() - except OSError as e: - logger.warning(f"failed to create log file: {log_path}: {e}") - logging.warning("No log will be stored.") - for i, (key, step) in enumerate(self.__recipe.raw["steps"].items()): _status = True _protected = False diff --git a/vanilla_first_setup/utils/recipe.py b/vanilla_first_setup/utils/recipe.py index e4605c70..595a3845 100644 --- a/vanilla_first_setup/utils/recipe.py +++ b/vanilla_first_setup/utils/recipe.py @@ -22,22 +22,18 @@ logger = logging.getLogger("FirstSetup::RecipeLoader") +recipe_path = os.environ["VANILLA_CUSTOM_RECIPE"] if "VANILLA_CUSTOM_RECIPE" in os.environ else "/usr/share/org.vanillaos.FirstSetup/recipe.json" class RecipeLoader: - recipe_path = "/usr/share/org.vanillaos.FirstSetup/recipe.json" - def __init__(self): self.__recipe = {} self.__load() def __load(self): - if "VANILLA_CUSTOM_RECIPE" in os.environ: - self.recipe_path = os.environ["VANILLA_CUSTOM_RECIPE"] - - logger.info(f"Loading recipe from {self.recipe_path}") + logger.info(f"Loading recipe from {recipe_path}") - if os.path.exists(self.recipe_path): - with open(self.recipe_path, "r") as f: + if os.path.exists(recipe_path): + with open(recipe_path, "r") as f: self.__recipe = json.load(f) return @@ -45,7 +41,7 @@ def __load(self): logger.error("Invalid recipe file") sys.exit(1) - logger.error(f"Recipe not found at {self.recipe_path}") + logger.error(f"Recipe not found at {recipe_path}") sys.exit(1) def __validate(self): diff --git a/vanilla_first_setup/window.py b/vanilla_first_setup/window.py index 27ee60ed..6dd3f1f2 100644 --- a/vanilla_first_setup/window.py +++ b/vanilla_first_setup/window.py @@ -178,7 +178,7 @@ def __on_page_changed(self, *args): # process the commands res = Processor.get_setup_commands( - self.recipe.get("log_file", "/tmp/vanilla_first_setup.log"), + self.recipe.get("log_file"), self.recipe.get("pre_run", []), self.recipe.get("post_run"), commands,