Skip to content

Commit

Permalink
Enhance dotenv run: Switch to execvpe for better resource management …
Browse files Browse the repository at this point in the history
…and signal handling
  • Loading branch information
eekstunt committed Jul 8, 2024
1 parent 08937a1 commit a8e22b9
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions src/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import shlex
import sys
from contextlib import contextmanager
from subprocess import Popen
from typing import Any, Dict, IO, Iterator, List

try:
Expand Down Expand Up @@ -161,14 +160,13 @@ def run(ctx: click.Context, override: bool, commandline: List[str]) -> None:
if not commandline:
click.echo('No command given.')
exit(1)
ret = run_command(commandline, dotenv_as_dict)
exit(ret)
run_command(commandline, dotenv_as_dict)


def run_command(command: List[str], env: Dict[str, str]) -> int:
"""Run command in sub process.
def run_command(command: List[str], env: Dict[str, str]) -> None:
"""Replace the current process with the specified command.
Runs the command in a sub process with the variables from `env`
Replaces the current process with the specified command and the variables from `env`
added in the current environment variables.
Parameters
Expand All @@ -180,20 +178,13 @@ def run_command(command: List[str], env: Dict[str, str]) -> int:
Returns
-------
int
The return code of the command
None
This function does not return any value. It replaces the current process with the new one.
"""
# copy the current environment variables and add the vales from
# `env`
cmd_env = os.environ.copy()
cmd_env.update(env)

p = Popen(command,
universal_newlines=True,
bufsize=0,
shell=False,
env=cmd_env)
_, _ = p.communicate()

return p.returncode
os.execvpe(command[0], args=command, env=cmd_env)

0 comments on commit a8e22b9

Please sign in to comment.