|
| 1 | +# Copyright (c) 2025 pandas-gbq Authors All rights reserved. |
| 2 | +# Use of this source code is governed by a BSD-style |
| 3 | +# license that can be found in the LICENSE file. |
| 4 | + |
| 5 | + |
| 6 | +import importlib |
| 7 | +import json |
| 8 | +import os |
| 9 | + |
| 10 | +# The identifier for GCP VS Code extension |
| 11 | +# https://cloud.google.com/code/docs/vscode/install |
| 12 | +GOOGLE_CLOUD_CODE_EXTENSION_NAME = "googlecloudtools.cloudcode" |
| 13 | + |
| 14 | + |
| 15 | +# The identifier for BigQuery Jupyter notebook plugin |
| 16 | +# https://cloud.google.com/bigquery/docs/jupyterlab-plugin |
| 17 | +BIGQUERY_JUPYTER_PLUGIN_NAME = "bigquery_jupyter_plugin" |
| 18 | + |
| 19 | + |
| 20 | +def _is_vscode_extension_installed(extension_id: str) -> bool: |
| 21 | + """ |
| 22 | + Checks if a given Visual Studio Code extension is installed. |
| 23 | +
|
| 24 | + Args: |
| 25 | + extension_id: The ID of the extension (e.g., "ms-python.python"). |
| 26 | +
|
| 27 | + Returns: |
| 28 | + True if the extension is installed, False otherwise. |
| 29 | + """ |
| 30 | + try: |
| 31 | + # Determine the user's VS Code extensions directory. |
| 32 | + user_home = os.path.expanduser("~") |
| 33 | + if os.name == "nt": # Windows |
| 34 | + vscode_extensions_dir = os.path.join(user_home, ".vscode", "extensions") |
| 35 | + elif os.name == "posix": # macOS and Linux |
| 36 | + vscode_extensions_dir = os.path.join(user_home, ".vscode", "extensions") |
| 37 | + else: |
| 38 | + raise OSError("Unsupported operating system.") |
| 39 | + |
| 40 | + # Check if the extensions directory exists. |
| 41 | + if os.path.exists(vscode_extensions_dir): |
| 42 | + # Iterate through the subdirectories in the extensions directory. |
| 43 | + for item in os.listdir(vscode_extensions_dir): |
| 44 | + item_path = os.path.join(vscode_extensions_dir, item) |
| 45 | + if os.path.isdir(item_path) and item.startswith(extension_id + "-"): |
| 46 | + # Check if the folder starts with the extension ID. |
| 47 | + # Further check for manifest file, as a more robust check. |
| 48 | + manifest_path = os.path.join(item_path, "package.json") |
| 49 | + if os.path.exists(manifest_path): |
| 50 | + try: |
| 51 | + with open(manifest_path, "r", encoding="utf-8") as f: |
| 52 | + json.load(f) |
| 53 | + return True |
| 54 | + except (FileNotFoundError, json.JSONDecodeError): |
| 55 | + # Corrupted or incomplete extension, or manifest missing. |
| 56 | + pass |
| 57 | + except Exception: |
| 58 | + pass |
| 59 | + |
| 60 | + return False |
| 61 | + |
| 62 | + |
| 63 | +def _is_package_installed(package_name: str) -> bool: |
| 64 | + """ |
| 65 | + Checks if a Python package is installed. |
| 66 | +
|
| 67 | + Args: |
| 68 | + package_name: The name of the package to check (e.g., "requests", "numpy"). |
| 69 | +
|
| 70 | + Returns: |
| 71 | + True if the package is installed, False otherwise. |
| 72 | + """ |
| 73 | + try: |
| 74 | + importlib.import_module(package_name) |
| 75 | + return True |
| 76 | + except Exception: |
| 77 | + return False |
| 78 | + |
| 79 | + |
| 80 | +def is_vscode() -> bool: |
| 81 | + return os.getenv("VSCODE_PID") is not None |
| 82 | + |
| 83 | + |
| 84 | +def is_jupyter() -> bool: |
| 85 | + return os.getenv("JPY_PARENT_PID") is not None |
| 86 | + |
| 87 | + |
| 88 | +def is_vscode_google_cloud_code_extension_installed() -> bool: |
| 89 | + return _is_vscode_extension_installed(GOOGLE_CLOUD_CODE_EXTENSION_NAME) |
| 90 | + |
| 91 | + |
| 92 | +def is_jupyter_bigquery_plugin_installed() -> bool: |
| 93 | + return _is_package_installed(BIGQUERY_JUPYTER_PLUGIN_NAME) |
0 commit comments