From 679aa8f6e615ab75a0b07aeeb923eae263711969 Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Mon, 25 Nov 2024 12:01:17 +0100 Subject: [PATCH 1/2] printEnvVariablesToFile.py overwrites itself if invoked without arguments I was the command in my terminal when launching the editor, was curious to see what it did, so I ran it again without arguments, expecting to get a `--help`-like message, instead it ended up overwriting itself. This change changes the script's default behavior (default being the invocation without arguments) from overwriting itself to throwing an exception about a missing output file argument. --- python_files/printEnvVariablesToFile.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/python_files/printEnvVariablesToFile.py b/python_files/printEnvVariablesToFile.py index c7ec70dd9684..2818d4cae042 100644 --- a/python_files/printEnvVariablesToFile.py +++ b/python_files/printEnvVariablesToFile.py @@ -4,8 +4,13 @@ import os import sys -# Last argument is the target file into which we'll write the env variables line by line. -output_file = sys.argv[-1] + +# Prevent overwriting itself, since sys.argv[0] is the path to this file +if len(sys.argv) > 1: + # Last argument is the target file into which we'll write the env variables line by line. + output_file = sys.argv[-1] +else: + raise ValueError("Missing output file argument") with open(output_file, "w") as outfile: # noqa: PTH123 for key, val in os.environ.items(): From d582451d4f6b915c001bb2a45a928d50d872fdfa Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Mon, 25 Nov 2024 15:43:09 +0100 Subject: [PATCH 2/2] Remove excessive newline --- python_files/printEnvVariablesToFile.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python_files/printEnvVariablesToFile.py b/python_files/printEnvVariablesToFile.py index 2818d4cae042..eae01b3d073c 100644 --- a/python_files/printEnvVariablesToFile.py +++ b/python_files/printEnvVariablesToFile.py @@ -4,7 +4,6 @@ import os import sys - # Prevent overwriting itself, since sys.argv[0] is the path to this file if len(sys.argv) > 1: # Last argument is the target file into which we'll write the env variables line by line.