Setup python related env variables.
(environ_cp)
| 194 | |
| 195 | |
| 196 | def setup_python(environ_cp): |
| 197 | """Setup python related env variables.""" |
| 198 | # Get PYTHON_BIN_PATH, default is the current running python. |
| 199 | default_python_bin_path = sys.executable |
| 200 | ask_python_bin_path = ('Please specify the location of python. [Default is ' |
| 201 | '%s]: ') % default_python_bin_path |
| 202 | while True: |
| 203 | python_bin_path = get_from_env_or_user_or_default(environ_cp, |
| 204 | 'PYTHON_BIN_PATH', |
| 205 | ask_python_bin_path, |
| 206 | default_python_bin_path) |
| 207 | # Check if the path is valid |
| 208 | if os.path.isfile(python_bin_path) and os.access(python_bin_path, os.X_OK): |
| 209 | break |
| 210 | elif not os.path.exists(python_bin_path): |
| 211 | print('Invalid python path: %s cannot be found.' % python_bin_path) |
| 212 | else: |
| 213 | print('%s is not executable. Is it the python binary?' % python_bin_path) |
| 214 | environ_cp['PYTHON_BIN_PATH'] = '' |
| 215 | |
| 216 | # Convert python path to Windows style before checking lib and version |
| 217 | if is_windows() or is_cygwin(): |
| 218 | python_bin_path = cygpath(python_bin_path) |
| 219 | |
| 220 | # Get PYTHON_LIB_PATH |
| 221 | python_lib_path = environ_cp.get('PYTHON_LIB_PATH') |
| 222 | if not python_lib_path: |
| 223 | python_lib_paths = get_python_path(environ_cp, python_bin_path) |
| 224 | if environ_cp.get('USE_DEFAULT_PYTHON_LIB_PATH') == '1': |
| 225 | python_lib_path = python_lib_paths[0] |
| 226 | else: |
| 227 | print('Found possible Python library paths:\n %s' % |
| 228 | '\n '.join(python_lib_paths)) |
| 229 | default_python_lib_path = python_lib_paths[0] |
| 230 | python_lib_path = get_input( |
| 231 | 'Please input the desired Python library path to use. ' |
| 232 | 'Default is [%s]\n' % python_lib_paths[0]) |
| 233 | if not python_lib_path: |
| 234 | python_lib_path = default_python_lib_path |
| 235 | environ_cp['PYTHON_LIB_PATH'] = python_lib_path |
| 236 | |
| 237 | python_major_version = get_python_major_version(python_bin_path) |
| 238 | if python_major_version == '2': |
| 239 | write_to_bazelrc('build --host_force_python=PY2') |
| 240 | |
| 241 | # Convert python path to Windows style before writing into bazel.rc |
| 242 | if is_windows() or is_cygwin(): |
| 243 | python_lib_path = cygpath(python_lib_path) |
| 244 | |
| 245 | # Set-up env variables used by python_configure.bzl |
| 246 | write_action_env_to_bazelrc('PYTHON_BIN_PATH', python_bin_path) |
| 247 | write_action_env_to_bazelrc('PYTHON_LIB_PATH', python_lib_path) |
| 248 | write_to_bazelrc('build --python_path=\"%s"' % python_bin_path) |
| 249 | environ_cp['PYTHON_BIN_PATH'] = python_bin_path |
| 250 | |
| 251 | # If choosen python_lib_path is from a path specified in the PYTHONPATH |
| 252 | # variable, need to tell bazel to include PYTHONPATH |
| 253 | if environ_cp.get('PYTHONPATH'): |
no test coverage detected