for-all-repos: Add option to keep current env

Extend support for preserving the current environment to the
for-all-repos plugin with the --preserve-env flag.
This eases the usage of dynamic configuration done via environment
variables within the for-all-repos plugin, e.g. when calling a script.

Signed-off-by: Jasper Orschulko <jasper@fancydomain.eu>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Jasper Orschulko
2022-07-19 22:47:34 +02:00
committed by Jan Kiszka
parent 682db50916
commit a9cc7d06b3
3 changed files with 38 additions and 22 deletions

View File

@@ -391,3 +391,30 @@ def setup_parser_common_args(parser):
parser.add_argument('--update', action='store_true',
help='Pull new upstream changes to the desired '
'refspec even if it is already checked out locally')
def setup_parser_preserve_env_arg(parser):
parser.add_argument('-E', '--preserve-env',
help='Keep current user enviornment block',
action='store_true')
def run_handle_preserve_env_arg(ctx, os, args, SetupHome):
if args.preserve_env:
# Warn if there's any settings that setup_home would apply
# but are now ignored
for var in SetupHome.ENV_VARS:
if var in os.environ:
logging.warning('Environment variable "%s" ignored '
'because user environment is being used',
var)
if not os.isatty(sys.stdout.fileno()):
logging.error("Error: --preserve-env can only be "
"run from a tty")
sys.exit(1)
ctx.environ = os.environ.copy()
logging.warning("Preserving the current environment block may "
"have unintended side effects on the build.")