refactor: simplify .env file loading logic and enhance API key handling in subprocess calls

This commit is contained in:
Timothy Kassis committed 2026-04-11 06:03:23 -07:00
1 parent 2550d1a89a
commit f42f4b59e3
9 files changed
+38 -110

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "scientific-agent-skills"
version = "2.34.3"
version = "2.35.0"
description = "A set of ready to use Agent Skills for research, science, engineering, analysis, finance and writing."
readme = "README.md"
requires-python = ">=3.13"
@@ -212,18 +212,18 @@ Environment Variables:
if args.iterations != 3:
cmd.extend(["--iterations", str(args.iterations)])
if api_key:
cmd.extend(["--api-key", api_key])
if args.verbose:
cmd.append("-v")
if args.research:
cmd.append("--research")
# Execute
# Execute — pass API key via environment to avoid exposure in process listings
try:
result = subprocess.run(cmd, check=False)
env = os.environ.copy()
if api_key:
env["OPENROUTER_API_KEY"] = api_key
result = subprocess.run(cmd, check=False, env=env)
sys.exit(result.returncode)
except Exception as e:
print(f"Error executing AI generation: {e}")
@@ -37,40 +37,17 @@ except ImportError:
def _load_env_file():
"""Load .env file from current directory, parent directories, or package directory."""
"""Load .env file from current directory or script directory only."""
try:
from dotenv import load_dotenv
except ImportError:
return False
# Try current working directory first
env_path = Path.cwd() / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
return True
# Try parent directories (up to 5 levels)
cwd = Path.cwd()
for _ in range(5):
env_path = cwd / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
for candidate in [Path.cwd() / ".env", Path(__file__).resolve().parent / ".env"]:
if candidate.exists():
load_dotenv(dotenv_path=candidate, override=False)
return True
cwd = cwd.parent
if cwd == cwd.parent:
break
# Try the package's parent directory
script_dir = Path(__file__).resolve().parent
for _ in range(5):
env_path = script_dir / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
return True
script_dir = script_dir.parent
if script_dir == script_dir.parent:
break
return False
@@ -119,15 +119,15 @@ Environment Variables:
if iterations != 2:
cmd.extend(["--iterations", str(iterations)])
if api_key:
cmd.extend(["--api-key", api_key])
if args.verbose:
cmd.append("-v")
# Execute
# Execute — pass API key via environment to avoid exposure in process listings
try:
result = subprocess.run(cmd, check=False)
env = os.environ.copy()
if api_key:
env["OPENROUTER_API_KEY"] = api_key
result = subprocess.run(cmd, check=False, env=env)
sys.exit(result.returncode)
except Exception as e:
print(f"Error executing AI generation: {e}")
@@ -35,44 +35,17 @@ except ImportError:
# Try to load .env file from multiple potential locations
def _load_env_file():
"""Load .env file from current directory, parent directories, or package directory.
Returns True if a .env file was found and loaded, False otherwise.
Note: This does NOT override existing environment variables.
"""
"""Load .env file from current directory or script directory only."""
try:
from dotenv import load_dotenv
except ImportError:
return False # python-dotenv not installed
# Try current working directory first
env_path = Path.cwd() / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
return True
# Try parent directories (up to 5 levels)
cwd = Path.cwd()
for _ in range(5):
env_path = cwd / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
return False
for candidate in [Path.cwd() / ".env", Path(__file__).resolve().parent / ".env"]:
if candidate.exists():
load_dotenv(dotenv_path=candidate, override=False)
return True
cwd = cwd.parent
if cwd == cwd.parent: # Reached root
break
# Try the package's parent directory (scientific-writer project root)
script_dir = Path(__file__).resolve().parent
for _ in range(5):
env_path = script_dir / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
return True
script_dir = script_dir.parent
if script_dir == script_dir.parent:
break
return False
@@ -121,15 +121,15 @@ Environment Variables:
if iterations != 2:
cmd.extend(["--iterations", str(iterations)])
if api_key:
cmd.extend(["--api-key", api_key])
if args.verbose:
cmd.append("-v")
# Execute
# Execute — pass API key via environment to avoid exposure in process listings
try:
result = subprocess.run(cmd, check=False)
env = os.environ.copy()
if api_key:
env["OPENROUTER_API_KEY"] = api_key
result = subprocess.run(cmd, check=False, env=env)
sys.exit(result.returncode)
except Exception as e:
print(f"Error executing AI generation: {e}")
@@ -47,40 +47,17 @@ except ImportError:
def _load_env_file():
"""Load .env file from current directory, parent directories, or package directory."""
"""Load .env file from current directory or script directory only."""
try:
from dotenv import load_dotenv
except ImportError:
return False
# Try current working directory first
env_path = Path.cwd() / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
return True
# Try parent directories (up to 5 levels)
cwd = Path.cwd()
for _ in range(5):
env_path = cwd / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
for candidate in [Path.cwd() / ".env", Path(__file__).resolve().parent / ".env"]:
if candidate.exists():
load_dotenv(dotenv_path=candidate, override=False)
return True
cwd = cwd.parent
if cwd == cwd.parent:
break
# Try the package's parent directory
script_dir = Path(__file__).resolve().parent
for _ in range(5):
env_path = script_dir / ".env"
if env_path.exists():
load_dotenv(dotenv_path=env_path, override=False)
return True
script_dir = script_dir.parent
if script_dir == script_dir.parent:
break
return False
@@ -245,7 +245,7 @@ class PresentationValidator:
try:
# Try pdflatex
result = subprocess.run(
['pdflatex', '-interaction=nonstopmode', self.filepath.name],
['pdflatex', '-no-shell-escape', '-interaction=nonstopmode', self.filepath.name],
cwd=self.filepath.parent,
capture_output=True,
timeout=60
@@ -16,6 +16,7 @@ Usage:
from __future__ import annotations
import argparse
import importlib
import json
import os
import platform
@@ -358,7 +359,7 @@ def check_package(pkg_name: str, import_name: str | None = None) -> CheckResult:
"""Check if a Python package is installed."""
import_name = import_name or pkg_name
try:
mod = __import__(import_name)
mod = importlib.import_module(import_name)
version = getattr(mod, "__version__", "unknown")
return CheckResult(
name=pkg_name,