Skip to content

Commit deb72c7

Browse files
committed
Add run_with_env.py script to load environment variables and execute commands (e.g. VSCode tasks)
1 parent 60118aa commit deb72c7

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

.vscode/tasks.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"label": "🧽 Ruff Formatter",
88
"command": "${command:python.interpreterPath}",
99
"args": [
10+
"scripts/run_with_env.py",
11+
"${command:python.interpreterPath}",
1012
"-m",
1113
"ruff",
1214
"format"
@@ -32,6 +34,8 @@
3234
"label": "🔦 Ruff Linter",
3335
"command": "${command:python.interpreterPath}",
3436
"args": [
37+
"scripts/run_with_env.py",
38+
"${command:python.interpreterPath}",
3539
"-m",
3640
"ruff",
3741
"check",
@@ -58,6 +62,8 @@
5862
"label": "🔦 Pylint",
5963
"command": "${command:python.interpreterPath}",
6064
"args": [
65+
"scripts/run_with_env.py",
66+
"${command:python.interpreterPath}",
6167
"-m",
6268
"pylint",
6369
"plotpy",
@@ -92,6 +98,8 @@
9298
"label": "🔎 Scan translations",
9399
"command": "${command:python.interpreterPath}",
94100
"args": [
101+
"scripts/run_with_env.py",
102+
"${command:python.interpreterPath}",
95103
"-m",
96104
"guidata.utils.translations",
97105
"scan",
@@ -123,6 +131,8 @@
123131
"label": "📚 Compile translations",
124132
"command": "${command:python.interpreterPath}",
125133
"args": [
134+
"scripts/run_with_env.py",
135+
"${command:python.interpreterPath}",
126136
"-m",
127137
"guidata.utils.translations",
128138
"compile",
@@ -152,6 +162,8 @@
152162
"label": "🚀 Pytest",
153163
"command": "${command:python.interpreterPath}",
154164
"args": [
165+
"scripts/run_with_env.py",
166+
"${command:python.interpreterPath}",
155167
"-m",
156168
"pytest",
157169
"--ff",
@@ -180,6 +192,8 @@
180192
"label": "Generate requirements",
181193
"command": "${command:python.interpreterPath}",
182194
"args": [
195+
"scripts/run_with_env.py",
196+
"${command:python.interpreterPath}",
183197
"-m",
184198
"guidata.utils.genreqs",
185199
"all",
@@ -260,6 +274,8 @@
260274
"type": "shell",
261275
"command": "${command:python.interpreterPath}",
262276
"args": [
277+
"scripts/run_with_env.py",
278+
"${command:python.interpreterPath}",
263279
"-m",
264280
"pip",
265281
"install",
@@ -295,6 +311,8 @@
295311
"label": "📚 Build documentation",
296312
"command": "${command:python.interpreterPath}",
297313
"args": [
314+
"scripts/run_with_env.py",
315+
"${command:python.interpreterPath}",
298316
"-m",
299317
"sphinx",
300318
"build",
@@ -347,6 +365,8 @@
347365
"type": "shell",
348366
"command": "${command:python.interpreterPath}",
349367
"args": [
368+
"scripts/run_with_env.py",
369+
"${command:python.interpreterPath}",
350370
"-m",
351371
"guidata.utils.securebuild",
352372
],
@@ -441,6 +461,8 @@
441461
"type": "shell",
442462
"command": "${command:python.interpreterPath}",
443463
"args": [
464+
"scripts/run_with_env.py",
465+
"${command:python.interpreterPath}",
444466
"-m",
445467
"coverage",
446468
"run",

scripts/run_with_env.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
2+
3+
"""Run a command with environment variables loaded from a .env file."""
4+
5+
from __future__ import annotations
6+
7+
import os
8+
import subprocess
9+
import sys
10+
from pathlib import Path
11+
12+
13+
def load_env_file(env_path: str | None = None) -> None:
14+
"""Load environment variables from a .env file."""
15+
if env_path is None:
16+
# Get ".eenv" file from the current directory
17+
env_path = Path.cwd() / ".env"
18+
if not Path(env_path).is_file():
19+
raise FileNotFoundError(f"Environment file not found: {env_path}")
20+
print(f"Loading environment variables from: {env_path}")
21+
with open(env_path, encoding="utf-8") as f:
22+
for line in f:
23+
line = line.strip()
24+
if not line or line.startswith("#") or "=" not in line:
25+
continue
26+
key, value = line.split("=", 1)
27+
os.environ[key.strip()] = value.strip()
28+
print(f" Loaded variable: {key.strip()}={value.strip()}")
29+
30+
31+
def execute_command(command: list[str]) -> int:
32+
"""Execute a command with the loaded environment variables."""
33+
print("Executing command:")
34+
print(" ".join(command))
35+
print("")
36+
result = subprocess.call(command)
37+
print(f"Process exited with code {result}")
38+
return result
39+
40+
41+
def main() -> None:
42+
"""Main function to load environment variables and execute a command."""
43+
if len(sys.argv) < 2:
44+
print("Usage: python run_with_env.py <command> [args ...]")
45+
sys.exit(1)
46+
print("🏃 Running with environment variables")
47+
load_env_file()
48+
return execute_command(sys.argv[1:])
49+
50+
51+
if __name__ == "__main__":
52+
main()

0 commit comments

Comments
 (0)