This repository was archived by the owner on May 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathenvironment.py
More file actions
101 lines (77 loc) · 3.07 KB
/
Copy pathenvironment.py
File metadata and controls
101 lines (77 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
import json
import os
import pathlib
Path = pathlib.Path
# The identifier for GCP VS Code extension
# https://cloud.google.com/code/docs/vscode/install
GOOGLE_CLOUD_CODE_EXTENSION_NAME = "googlecloudtools.cloudcode"
# The identifier for BigQuery Jupyter notebook plugin
# https://cloud.google.com/bigquery/docs/jupyterlab-plugin
BIGQUERY_JUPYTER_PLUGIN_NAME = "bigquery_jupyter_plugin"
def _is_vscode_extension_installed(extension_id: str) -> bool:
"""
Checks if a given Visual Studio Code extension is installed.
Args:
extension_id: The ID of the extension (e.g., "ms-python.python").
Returns:
True if the extension is installed, False otherwise.
"""
try:
# Determine the user's VS Code extensions directory.
user_home = Path.home()
vscode_extensions_dir = user_home / ".vscode" / "extensions"
# Check if the extensions directory exists.
if not vscode_extensions_dir.exists():
return False
# Iterate through the subdirectories in the extensions directory.
extension_dirs = filter(
lambda p: p.is_dir() and p.name.startswith(extension_id + "-"),
vscode_extensions_dir.iterdir(),
)
for extension_dir in extension_dirs:
# As a more robust check, the manifest file must exist.
manifest_path = extension_dir / "package.json"
if not manifest_path.exists() or not manifest_path.is_file():
continue
# Finally, the manifest file must be a valid json
with open(manifest_path, "r", encoding="utf-8") as f:
json.load(f)
return True
except Exception:
pass
return False
def _is_package_installed(package_name: str) -> bool:
"""
Checks if a Python package is installed.
Args:
package_name: The name of the package to check (e.g., "requests", "numpy").
Returns:
True if the package is installed, False otherwise.
"""
try:
importlib.import_module(package_name)
return True
except Exception:
return False
def is_vscode() -> bool:
return os.getenv("VSCODE_PID") is not None
def is_jupyter() -> bool:
return os.getenv("JPY_PARENT_PID") is not None
def is_vscode_google_cloud_code_extension_installed() -> bool:
return _is_vscode_extension_installed(GOOGLE_CLOUD_CODE_EXTENSION_NAME)
def is_jupyter_bigquery_plugin_installed() -> bool:
return _is_package_installed(BIGQUERY_JUPYTER_PLUGIN_NAME)