From e098f7f43a7e8e4e85149bc5d24aa854a98cec66 Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 18 Oct 2024 20:19:10 +0800 Subject: fix: js/py dotenv unable to parse quoted value or duplicated (#114) --- scripts/run-tool.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'scripts/run-tool.py') diff --git a/scripts/run-tool.py b/scripts/run-tool.py index 99af580..6ea3361 100755 --- a/scripts/run-tool.py +++ b/scripts/run-tool.py @@ -56,17 +56,29 @@ def setup_env(root_dir, tool_name): def load_env(file_path): try: with open(file_path, "r") as f: - for line in f: - line = line.strip() - if line.startswith("#") or line == "": - continue - - key, *value = line.split("=") - env_name = key.strip() - if env_name not in os.environ: - os.environ[env_name] = "=".join(value).strip() - except FileNotFoundError: - pass + lines = f.readlines() + except: + return + + env_vars = {} + + for line in lines: + line = line.strip() + if line.startswith("#") or not line: + continue + + key, *value_parts = line.split("=") + env_name = key.strip() + + if env_name not in os.environ: + env_value = "=".join(value_parts).strip() + if env_value.startswith('"') and env_value.endswith('"'): + env_value = env_value[1:-1] + elif env_value.startswith("'") and env_value.endswith("'"): + env_value = env_value[1:-1] + env_vars[env_name] = env_value + + os.environ.update(env_vars) def run(tool_path, tool_func, tool_data): -- cgit v1.2.3