4
4
from pathlib import Path
5
5
from typing import Optional
6
6
7
+ sys .path .append ("bazel" )
8
+ from bazelisk import get_bazel_path , make_bazel_cmd
9
+
7
10
# WARNING: this file is imported from outside of any virtual env so the main import block MUST NOT
8
11
# import any third-party non-std libararies. Libraries needed when running as a script can be
9
12
# conditionally imported below.
13
16
sys .path .append (os .path .dirname (os .path .dirname (os .path .abspath (os .path .realpath (__file__ )))))
14
17
15
18
16
- SUPPORTED_VERSIONS = ( "v4" , " v5")
19
+ SUPPORTED_VERSIONS = " v5"
17
20
18
21
19
22
class MongoToolchainError (RuntimeError ):
@@ -76,13 +79,20 @@ def _get_lib_dir_path(self) -> Path:
76
79
return self ._root_dir / "lib"
77
80
78
81
79
- def _run_command (cmd : str ) -> str :
80
- return subprocess .check_output (cmd , shell = True , text = True ).strip ()
82
+ def _execute_bazel (argv ):
83
+ bazel_cmd = make_bazel_cmd (get_bazel_path (), argv )
84
+ cmd = f"{ bazel_cmd ['exec' ]} { ' ' .join (bazel_cmd ['args' ])} "
85
+ return subprocess .check_output (
86
+ cmd ,
87
+ env = bazel_cmd ["env" ],
88
+ shell = True ,
89
+ text = True ,
90
+ ).strip ()
81
91
82
92
83
93
def _fetch_bazel_toolchain (version : str ) -> None :
84
94
try :
85
- _run_command ( f"bazel build --config=local @mongo_toolchain_{ version } //:all" )
95
+ _execute_bazel ([ " build" , " --config=local" , f" @mongo_toolchain_{ version } //:all"] )
86
96
except subprocess .CalledProcessError as e :
87
97
raise MongoToolchainNotFoundError (
88
98
f"Failed to fetch bazel toolchain: `{ e .cmd } ` exited with code { e .returncode } "
@@ -91,13 +101,12 @@ def _fetch_bazel_toolchain(version: str) -> None:
91
101
92
102
def _get_bazel_execroot () -> Path :
93
103
try :
94
- execroot_str = _run_command ( "bazel info execution_root" )
104
+ execroot_str = _execute_bazel ([ " info" , " execution_root"] )
95
105
except subprocess .CalledProcessError as e :
96
106
raise MongoToolchainNotFoundError (
97
107
f"Couldn't find bazel execroot: `{ e .cmd } ` exited with code { e .returncode } "
98
108
)
99
- execroot = Path (execroot_str )
100
- return execroot
109
+ return Path (execroot_str )
101
110
102
111
103
112
def _get_bazel_toolchain_path (version : str ) -> Path :
@@ -139,15 +148,15 @@ def get_mongo_toolchain(
139
148
if toolchain_path is not None :
140
149
return _get_toolchain_from_path (toolchain_path )
141
150
142
- # If no version given, look in the environment or default to v4 .
151
+ # If no version given, look in the environment or default to v5 .
143
152
if version is None :
144
- version = os .environ .get ("MONGO_TOOLCHAIN_VERSION" , "v4 " )
153
+ version = os .environ .get ("MONGO_TOOLCHAIN_VERSION" , "v5 " )
145
154
assert version is not None
146
155
if version not in SUPPORTED_VERSIONS :
147
156
raise MongoToolchainNotFoundError (f"Unknown toolchain version { version } " )
148
157
149
- # If from_bazel is unspecified. Look in the environment or fall back to a default based on
150
- # the version (v4 -> not from bazel, v5 -> from bazel) .
158
+ # If from_bazel is unspecified, let's query from bazel where querying toolchain
159
+ # version is supported since v5 .
151
160
def _parse_from_bazel_envvar (value : str ) -> bool :
152
161
v = value .lower ()
153
162
if v in ("true" , "1" ):
@@ -158,8 +167,7 @@ def _parse_from_bazel_envvar(value: str) -> bool:
158
167
raise ValueError (f"Invalid value { value } for MONGO_TOOLCHAIN_FROM_BAZEL" )
159
168
160
169
if from_bazel is None :
161
- from_bazel_default = "false" if version == "v4" else "true"
162
- from_bazel_value = os .environ .get ("MONGO_TOOLCHAIN_FROM_BAZEL" , from_bazel_default )
170
+ from_bazel_value = os .environ .get ("MONGO_TOOLCHAIN_FROM_BAZEL" , "true" )
163
171
from_bazel = _parse_from_bazel_envvar (from_bazel_value )
164
172
165
173
if from_bazel :
0 commit comments