Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance dotenv run: Switch to execvpe for better resource management and signal handling #523

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)