• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright 2017 - The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17
18
19def SaveAndClearEnvVars(var_name_list):
20    """Saves and clears the values of existing environment variables.
21
22    Args:
23        var_name_list: a list of strings where each string is the environment
24                       variable name.
25
26    Returns:
27        a dict where the key is an environment variable and the value is the
28        saved environment variable value.
29    """
30    env_vars = {}
31    for var_name in var_name_list:
32        if var_name in os.environ:
33            env_vars[var_name] = os.environ[var_name]
34            os.environ[var_name] = ""
35    return env_vars
36
37
38def RestoreEnvVars(env_vars):
39    """Restores the values of existing environment variables.
40
41    Args:
42        env_vars: a dict where the key is an environment variable and
43                  the value is the environment variable value to set.
44    """
45    for var_name in env_vars:
46        os.environ[var_name] = env_vars[var_name]
47