2121
2222# Standard Library
2323import asyncio
24+ import copy
2425import datetime
2526import errno
2627import logging
28+ import os
2729import typing
2830
2931# Exec-Helpers Implementation
@@ -189,6 +191,7 @@ async def _execute_async( # type: ignore # pylint: disable=arguments-differ
189191 chroot_path : typing .Optional [str ] = None ,
190192 cwd : typing .Optional [typing .Union [str , bytes ]] = None ,
191193 env : _EnvT = None ,
194+ env_patch : _EnvT = None ,
192195 ** kwargs : typing .Any ,
193196 ) -> SubprocessExecuteAsyncResult :
194197 """Execute command in async mode and return Popen with IO objects.
@@ -207,6 +210,8 @@ async def _execute_async( # type: ignore # pylint: disable=arguments-differ
207210 :type cwd: typing.Optional[typing.Union[str, bytes]]
208211 :param env: Defines the environment variables for the new process.
209212 :type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
213+ :param env_patch: Defines the environment variables to ADD for the new process.
214+ :type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
210215 :param kwargs: additional parameters for call.
211216 :type kwargs: typing.Any
212217 :return: Tuple with control interface and file-like objects for STDIN/STDERR/STDOUT
@@ -224,6 +229,11 @@ async def _execute_async( # type: ignore # pylint: disable=arguments-differ
224229 """
225230 started = datetime .datetime .utcnow ()
226231
232+ if env_patch is not None :
233+ # make mutable copy
234+ env = dict (copy .deepcopy (os .environ ) if env is None else copy .deepcopy (env )) # type: ignore
235+ env .update (env_patch ) # type: ignore
236+
227237 process : asyncio .subprocess .Process = await asyncio .create_subprocess_shell ( # pylint: disable=no-member
228238 cmd = self ._prepare_command (cmd = command , chroot_path = chroot_path ),
229239 stdout = asyncio .subprocess .PIPE if open_stdout else asyncio .subprocess .DEVNULL ,
@@ -282,6 +292,7 @@ async def execute( # type: ignore # pylint: disable=arguments-differ
282292 open_stderr : bool = True ,
283293 cwd : typing .Optional [typing .Union [str , bytes ]] = None ,
284294 env : _EnvT = None ,
295+ env_patch : _EnvT = None ,
285296 ** kwargs : typing .Any ,
286297 ) -> exec_result .ExecResult :
287298 """Execute command and wait for return code.
@@ -305,6 +316,8 @@ async def execute( # type: ignore # pylint: disable=arguments-differ
305316 :type cwd: typing.Optional[typing.Union[str, bytes]]
306317 :param env: Defines the environment variables for the new process.
307318 :type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
319+ :param env_patch: Defines the environment variables to ADD for the new process.
320+ :type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
308321 :param kwargs: additional parameters for call.
309322 :type kwargs: typing.Any
310323 :return: Execution result
@@ -324,6 +337,7 @@ async def execute( # type: ignore # pylint: disable=arguments-differ
324337 open_stderr = open_stderr ,
325338 cwd = cwd ,
326339 env = env ,
340+ env_patch = env_patch ,
327341 ** kwargs ,
328342 )
329343
@@ -339,6 +353,7 @@ async def __call__( # type: ignore
339353 open_stderr : bool = True ,
340354 cwd : typing .Optional [typing .Union [str , bytes ]] = None ,
341355 env : _EnvT = None ,
356+ env_patch : _EnvT = None ,
342357 ** kwargs : typing .Any ,
343358 ) -> exec_result .ExecResult :
344359 """Execute command and wait for return code.
@@ -362,6 +377,8 @@ async def __call__( # type: ignore
362377 :type cwd: typing.Optional[typing.Union[str, bytes]]
363378 :param env: Defines the environment variables for the new process.
364379 :type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
380+ :param env_patch: Defines the environment variables to ADD for the new process.
381+ :type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
365382 :param kwargs: additional parameters for call.
366383 :type kwargs: typing.Any
367384 :return: Execution result
@@ -381,6 +398,7 @@ async def __call__( # type: ignore
381398 open_stderr = open_stderr ,
382399 cwd = cwd ,
383400 env = env ,
401+ env_patch = env_patch ,
384402 ** kwargs ,
385403 )
386404
@@ -399,6 +417,7 @@ async def check_call( # type: ignore # pylint: disable=arguments-differ
399417 open_stderr : bool = True ,
400418 cwd : typing .Optional [typing .Union [str , bytes ]] = None ,
401419 env : _EnvT = None ,
420+ env_patch : _EnvT = None ,
402421 exception_class : "typing.Type[exceptions.CalledProcessError]" = exceptions .CalledProcessError ,
403422 ** kwargs : typing .Any ,
404423 ) -> exec_result .ExecResult :
@@ -429,6 +448,8 @@ async def check_call( # type: ignore # pylint: disable=arguments-differ
429448 :type cwd: typing.Optional[typing.Union[str, bytes]]
430449 :param env: Defines the environment variables for the new process.
431450 :type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
451+ :param env_patch: Defines the environment variables to ADD for the new process.
452+ :type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
432453 :param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
433454 :type exception_class: typing.Type[exceptions.CalledProcessError]
434455 :param kwargs: additional parameters for call.
@@ -455,6 +476,7 @@ async def check_call( # type: ignore # pylint: disable=arguments-differ
455476 open_stderr = open_stderr ,
456477 cwd = cwd ,
457478 env = env ,
479+ env_patch = env_patch ,
458480 exception_class = exception_class ,
459481 ** kwargs ,
460482 )
@@ -474,6 +496,7 @@ async def check_stderr( # type: ignore # pylint: disable=arguments-differ
474496 open_stderr : bool = True ,
475497 cwd : typing .Optional [typing .Union [str , bytes ]] = None ,
476498 env : _EnvT = None ,
499+ env_patch : _EnvT = None ,
477500 exception_class : "typing.Type[exceptions.CalledProcessError]" = exceptions .CalledProcessError ,
478501 ** kwargs : typing .Any ,
479502 ) -> exec_result .ExecResult :
@@ -504,6 +527,8 @@ async def check_stderr( # type: ignore # pylint: disable=arguments-differ
504527 :type cwd: typing.Optional[typing.Union[str, bytes]]
505528 :param env: Defines the environment variables for the new process.
506529 :type env: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
530+ :param env_patch: Defines the environment variables to ADD for the new process.
531+ :type env_patch: typing.Optional[typing.Mapping[typing.Union[str, bytes], typing.Union[str, bytes]]]
507532 :param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
508533 :type exception_class: typing.Type[exceptions.CalledProcessError]
509534 :param kwargs: additional parameters for call.
@@ -530,6 +555,7 @@ async def check_stderr( # type: ignore # pylint: disable=arguments-differ
530555 open_stderr = open_stderr ,
531556 cwd = cwd ,
532557 env = env ,
558+ env_patch = env_patch ,
533559 exception_class = exception_class ,
534560 ** kwargs ,
535561 )
0 commit comments