configuration.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. import json
  20. import os
  21. from tempfile import mkstemp
  22. from airflow.configuration import conf
  23. from airflow.utils.platform import IS_WINDOWS
  24. def tmp_configuration_copy(chmod=0o600, include_env=True, include_cmds=True):
  25. """
  26. Return a path for a temporary file including a full copy of the configuration settings.
  27. :param include_env: Should the value of configuration from ``AIRFLOW__``
  28. environment variables be included or not
  29. :param include_cmds: Should the result of calling any *_cmd config be
  30. set (True, default), or should the _cmd options be left as the
  31. command to run (False)
  32. :return: a path to a temporary file
  33. """
  34. cfg_dict = conf.as_dict(
  35. display_sensitive=True, raw=True, include_cmds=include_cmds, include_env=include_env
  36. )
  37. temp_fd, cfg_path = mkstemp()
  38. with os.fdopen(temp_fd, "w") as temp_file:
  39. # Set the permissions before we write anything to it.
  40. if chmod is not None and not IS_WINDOWS:
  41. os.fchmod(temp_fd, chmod)
  42. json.dump(cfg_dict, temp_file)
  43. return cfg_path