platform.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Platform and system specific function."""
  18. from __future__ import annotations
  19. import getpass
  20. import logging
  21. import os
  22. import pkgutil
  23. import platform
  24. import sys
  25. from airflow.compat.functools import cache
  26. IS_WINDOWS = platform.system() == "Windows"
  27. log = logging.getLogger(__name__)
  28. def is_tty():
  29. """Check if stdout is connected (is associated with a terminal device) to a tty(-like) device."""
  30. if not hasattr(sys.stdout, "isatty"):
  31. return False
  32. return sys.stdout.isatty()
  33. def is_terminal_support_colors() -> bool:
  34. """Try to determine if the current terminal supports colors."""
  35. if sys.platform == "win32":
  36. return False
  37. if not is_tty():
  38. return False
  39. if "COLORTERM" in os.environ:
  40. return True
  41. term = os.environ.get("TERM", "dumb").lower()
  42. if term in ("xterm", "linux") or "color" in term:
  43. return True
  44. return False
  45. def get_airflow_git_version():
  46. """Return the git commit hash representing the current version of the application."""
  47. git_version = None
  48. try:
  49. git_version = str(pkgutil.get_data("airflow", "git_version"), encoding="UTF-8")
  50. except Exception as e:
  51. log.debug(e)
  52. return git_version
  53. @cache
  54. def getuser() -> str:
  55. """
  56. Get the username of the current user, or error with a nice error message if there's no current user.
  57. We don't want to fall back to os.getuid() because not having a username
  58. probably means the rest of the user environment is wrong (e.g. no $HOME).
  59. Explicit failure is better than silently trying to work badly.
  60. """
  61. try:
  62. return getpass.getuser()
  63. except KeyError:
  64. # Inner import to avoid circular import
  65. from airflow.exceptions import AirflowConfigException
  66. raise AirflowConfigException(
  67. "The user that Airflow is running as has no username; you must run"
  68. "Airflow as a full user, with a username and home directory, "
  69. "in order for it to function properly."
  70. )