|
| 1 | +# Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 2 | +"""Environment meta utils and globals. |
| 3 | +
|
| 4 | +This module contains functions for detecting environment and runtime information. |
| 5 | +""" |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | +from contextlib import suppress |
| 11 | +from functools import lru_cache |
| 12 | +from pathlib import Path |
| 13 | +from platform import python_implementation, python_version, system |
| 14 | + |
| 15 | +import requests |
| 16 | + |
| 17 | + |
| 18 | +COLAB_SESSION_URL = "http://172.28.0.12:9000/api/sessions" |
| 19 | +"""URL to get the current Google Colab session information.""" |
| 20 | + |
| 21 | + |
| 22 | +def get_colab_release_version() -> str | None: |
| 23 | + if "COLAB_RELEASE_TAG" in os.environ: |
| 24 | + return os.environ["COLAB_RELEASE_TAG"] |
| 25 | + |
| 26 | + return None |
| 27 | + |
| 28 | + |
| 29 | +def is_ci() -> bool: |
| 30 | + return "CI" in os.environ |
| 31 | + |
| 32 | + |
| 33 | +@lru_cache |
| 34 | +def is_colab() -> bool: |
| 35 | + return bool(get_colab_release_version()) |
| 36 | + |
| 37 | + |
| 38 | +@lru_cache |
| 39 | +def is_jupyter() -> bool: |
| 40 | + """Return True if running in a Jupyter notebook or qtconsole. |
| 41 | +
|
| 42 | + Will return False in Colab (use is_colab() instead). |
| 43 | + """ |
| 44 | + try: |
| 45 | + shell = get_ipython().__class__.__name__ # type: ignore # noqa: PGH003 |
| 46 | + except NameError: |
| 47 | + return False # If 'get_ipython' undefined, we're probably in a standard Python interpreter. |
| 48 | + |
| 49 | + if shell == "ZMQInteractiveShell": |
| 50 | + return True # Jupyter notebook or qtconsole. |
| 51 | + |
| 52 | + if shell == "TerminalInteractiveShell": |
| 53 | + return False # Terminal running IPython |
| 54 | + |
| 55 | + return False # Other type (?) |
| 56 | + |
| 57 | + |
| 58 | +@lru_cache |
| 59 | +def get_notebook_name() -> str | None: |
| 60 | + if is_colab(): |
| 61 | + session_info = None |
| 62 | + response = None |
| 63 | + with suppress(Exception): |
| 64 | + response = requests.get(COLAB_SESSION_URL) |
| 65 | + if response.status_code == 200: # noqa: PLR2004 # Magic number |
| 66 | + session_info = response.json() |
| 67 | + |
| 68 | + if session_info and "name" in session_info: |
| 69 | + return session_info["name"] |
| 70 | + |
| 71 | + return None |
| 72 | + |
| 73 | + |
| 74 | +@lru_cache |
| 75 | +def get_vscode_notebook_name() -> str | None: |
| 76 | + with suppress(Exception): |
| 77 | + import IPython |
| 78 | + |
| 79 | + return Path( |
| 80 | + IPython.extract_module_locals()[1]["__vsc_ipynb_file__"], |
| 81 | + ).name |
| 82 | + |
| 83 | + return None |
| 84 | + |
| 85 | + |
| 86 | +def is_vscode_notebook() -> bool: |
| 87 | + return get_vscode_notebook_name() is not None |
| 88 | + |
| 89 | + |
| 90 | +@lru_cache |
| 91 | +def get_python_script_name() -> str | None: |
| 92 | + script_name = None |
| 93 | + with suppress(Exception): |
| 94 | + script_name = sys.argv[0] # When running a python script, this is the script name. |
| 95 | + |
| 96 | + if script_name: |
| 97 | + return Path(script_name).name |
| 98 | + |
| 99 | + return None |
| 100 | + |
| 101 | + |
| 102 | +@lru_cache |
| 103 | +def get_application_name() -> str | None: |
| 104 | + return get_notebook_name() or get_python_script_name() or get_vscode_notebook_name() or None |
| 105 | + |
| 106 | + |
| 107 | +def get_python_version() -> str: |
| 108 | + return f"{python_version()} ({python_implementation()})" |
| 109 | + |
| 110 | + |
| 111 | +def get_os() -> str: |
| 112 | + if is_colab(): |
| 113 | + return f"Google Colab ({get_colab_release_version()})" |
| 114 | + |
| 115 | + return f"{system()}" |
0 commit comments