From 91e481d489d341ea01bdb8a30f31d276886373ec Mon Sep 17 00:00:00 2001 From: Lukas Waslowski Date: Thu, 18 Apr 2024 20:30:28 +0200 Subject: [PATCH] feat: Add the option to specify external script files in addition to inline scripts --- README.md | 8 ++++++++ src/ublue_update/update_inhibitors/custom.py | 21 ++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index eb3f0e5..13b6ee5 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,9 @@ Each entry in the `checks.scripts` array must specify the following settings: * `name`: an optional human-readable name for this check +The parameters `run` and `file` are mutually exclusive, but at least one must be specified. +The `shell` parameter is mandatory when using `run`. + The custom script should use its exit code to indicate whether the updater should proceed (`exit code = 0`) or whether updates should be inhibited right now (any non-0 exit code). If `message` is not specified but the script has written text to `stdout`, @@ -167,6 +170,11 @@ print("Python also works when installed") exit(1) """ + [[checks.scripts]] + name = "Example external script" + # shell = "bash" # specifying a shell is optional for external scripts/programs + file = "/bin/true" + [notify] dbus_notify = false # Do not show notifications ``` diff --git a/src/ublue_update/update_inhibitors/custom.py b/src/ublue_update/update_inhibitors/custom.py index 4f1280c..cb83398 100644 --- a/src/ublue_update/update_inhibitors/custom.py +++ b/src/ublue_update/update_inhibitors/custom.py @@ -9,17 +9,22 @@ def run_custom_check_script(script) -> dict: - if script.get('shell') != 'bash': - raise Exception('checks.scripts.*.shell must be set to \'bash\'') + if 'run' in script and 'shell' not in script: + raise Exception('checks.scripts.*: \'shell\' must be specified when \'run\' is used') + + if 'run' in script and 'file' in script: + raise Exception('checks.scripts.*: Only one of \'run\' and \'file\' must be set for a given script') log.debug(f"Running script {script}") - # Run the specified custom script via bash - script_result = subprocess.run( - ['bash', '-c', script['run']], - capture_output=True, - check=False, - ) + # Run the specified custom script + if 'run' in script: + run_args = [script['shell'], '-c', script['run']] + elif 'shell' in script: + run_args = [script['shell'], script['file']] + else: + run_args = [script['file']] + script_result = subprocess.run(run_args, capture_output=True, text=True, check=False) # An exit code of 0 means "OK", a non-zero exit code # means "Do not download or perform updates right now"