backup_env.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import json
  15. import os
  16. g_backup_envs = None
  17. def getenv_or_backup(name, default=None):
  18. global g_backup_envs
  19. if g_backup_envs is None:
  20. backup_path = os.getenv('PADDLE_BACKUP_ENV_PATH')
  21. if backup_path is None:
  22. g_backup_envs = {}
  23. else:
  24. with open(backup_path, 'r') as f:
  25. g_backup_envs = json.load(f)
  26. value = os.getenv(name)
  27. if value is not None:
  28. return value
  29. else:
  30. return g_backup_envs.get(name, default)