|
| 1 | +import uuid |
| 2 | +import os |
| 3 | +import platformdirs |
| 4 | +from pathlib import Path |
| 5 | +import pybamm |
| 6 | +import sys |
| 7 | +import threading |
| 8 | +import time |
| 9 | + |
| 10 | + |
| 11 | +def is_running_tests(): # pragma: no cover |
| 12 | + """ |
| 13 | + Detect if the code is being run as part of a test suite or building docs with Sphinx. |
| 14 | +
|
| 15 | + Returns: |
| 16 | + bool: True if running tests or building docs, False otherwise. |
| 17 | + """ |
| 18 | + # Check if pytest or unittest is running |
| 19 | + if any( |
| 20 | + test_module in sys.modules for test_module in ["pytest", "unittest", "nose"] |
| 21 | + ): |
| 22 | + return True |
| 23 | + |
| 24 | + # Check for GitHub Actions environment variable |
| 25 | + if "GITHUB_ACTIONS" in os.environ: |
| 26 | + return True |
| 27 | + |
| 28 | + # Check for other common CI environment variables |
| 29 | + ci_env_vars = ["CI", "TRAVIS", "CIRCLECI", "JENKINS_URL", "GITLAB_CI"] |
| 30 | + if any(var in os.environ for var in ci_env_vars): |
| 31 | + return True |
| 32 | + |
| 33 | + # Check for common test runner names in command-line arguments |
| 34 | + test_runners = ["pytest", "unittest", "nose", "trial", "nox", "tox"] |
| 35 | + if any(runner in arg.lower() for arg in sys.argv for runner in test_runners): |
| 36 | + return True |
| 37 | + |
| 38 | + # Check if building docs with Sphinx |
| 39 | + if any(mod == "sphinx" or mod.startswith("sphinx.") for mod in sys.modules): |
| 40 | + print( |
| 41 | + f"Found Sphinx module: {[mod for mod in sys.modules if mod.startswith('sphinx')]}" |
| 42 | + ) |
| 43 | + return True |
| 44 | + |
| 45 | + return False |
| 46 | + |
| 47 | + |
| 48 | +def ask_user_opt_in(timeout=10): |
| 49 | + """ |
| 50 | + Ask the user if they want to opt in to telemetry. |
| 51 | +
|
| 52 | + Parameters |
| 53 | + ---------- |
| 54 | + timeout : float, optional |
| 55 | + The timeout for the user to respond to the prompt. Default is 10 seconds. |
| 56 | +
|
| 57 | + Returns |
| 58 | + ------- |
| 59 | + bool |
| 60 | + True if the user opts in, False otherwise. |
| 61 | + """ |
| 62 | + print( |
| 63 | + "PyBaMM can collect usage data and send it to the PyBaMM team to " |
| 64 | + "help us improve the software.\n" |
| 65 | + "We do not collect any sensitive information such as models, parameters, " |
| 66 | + "or simulation results - only information on which parts of the code are " |
| 67 | + "being used and how frequently.\n" |
| 68 | + "This is entirely optional and does not impact the functionality of PyBaMM.\n" |
| 69 | + "For more information, see https://docs.pybamm.org/en/latest/source/user_guide/index.html#telemetry" |
| 70 | + ) |
| 71 | + |
| 72 | + def get_input(): # pragma: no cover |
| 73 | + try: |
| 74 | + user_input = ( |
| 75 | + input("Do you want to enable telemetry? (Y/n): ").strip().lower() |
| 76 | + ) |
| 77 | + answer.append(user_input) |
| 78 | + except Exception: |
| 79 | + # Handle any input errors |
| 80 | + pass |
| 81 | + |
| 82 | + time_start = time.time() |
| 83 | + |
| 84 | + while True: |
| 85 | + if time.time() - time_start > timeout: |
| 86 | + print("\nTimeout reached. Defaulting to not enabling telemetry.") |
| 87 | + return False |
| 88 | + |
| 89 | + answer = [] |
| 90 | + # Create and start input thread |
| 91 | + input_thread = threading.Thread(target=get_input) |
| 92 | + input_thread.daemon = True |
| 93 | + input_thread.start() |
| 94 | + |
| 95 | + # Wait for either timeout or input |
| 96 | + input_thread.join(timeout) |
| 97 | + |
| 98 | + if answer: |
| 99 | + if answer[0] in ["yes", "y", ""]: |
| 100 | + print("\nTelemetry enabled.\n") |
| 101 | + return True |
| 102 | + elif answer[0] in ["no", "n"]: |
| 103 | + print("\nTelemetry disabled.\n") |
| 104 | + return False |
| 105 | + else: |
| 106 | + print("\nInvalid input. Please enter 'yes/y' for yes or 'no/n' for no.") |
| 107 | + else: |
| 108 | + print("\nTimeout reached. Defaulting to not enabling telemetry.") |
| 109 | + return False |
| 110 | + |
| 111 | + |
| 112 | +def generate(): |
| 113 | + if is_running_tests(): |
| 114 | + return |
| 115 | + |
| 116 | + # Check if the config file already exists |
| 117 | + if read() is not None: |
| 118 | + return |
| 119 | + |
| 120 | + # Ask the user if they want to opt in to telemetry |
| 121 | + opt_in = ask_user_opt_in() |
| 122 | + config_file = Path(platformdirs.user_config_dir("pybamm")) / "config.yml" |
| 123 | + write_uuid_to_file(config_file, opt_in) |
| 124 | + |
| 125 | + if opt_in: |
| 126 | + pybamm.telemetry.capture("user-opted-in") |
| 127 | + |
| 128 | + |
| 129 | +def read(): |
| 130 | + config_file = Path(platformdirs.user_config_dir("pybamm")) / "config.yml" |
| 131 | + return read_uuid_from_file(config_file) |
| 132 | + |
| 133 | + |
| 134 | +def write_uuid_to_file(config_file, opt_in): |
| 135 | + # Create the directory if it doesn't exist |
| 136 | + config_file.parent.mkdir(parents=True, exist_ok=True) |
| 137 | + |
| 138 | + # Write the UUID to the config file in YAML format |
| 139 | + with open(config_file, "w") as f: |
| 140 | + f.write("pybamm:\n") |
| 141 | + f.write(f" enable_telemetry: {opt_in}\n") |
| 142 | + if opt_in: |
| 143 | + unique_id = uuid.uuid4() |
| 144 | + f.write(f" uuid: {unique_id}\n") |
| 145 | + |
| 146 | + |
| 147 | +def read_uuid_from_file(config_file): |
| 148 | + # Check if the config file exists |
| 149 | + if not config_file.exists(): |
| 150 | + return None |
| 151 | + |
| 152 | + # Read the UUID from the config file |
| 153 | + with open(config_file) as f: |
| 154 | + content = f.read().strip() |
| 155 | + |
| 156 | + # Extract the UUID using YAML parsing |
| 157 | + try: |
| 158 | + import yaml |
| 159 | + |
| 160 | + config = yaml.safe_load(content) |
| 161 | + return config["pybamm"] |
| 162 | + except (yaml.YAMLError, ValueError): |
| 163 | + return None |
0 commit comments