(version: RequestedVersion, args: &[String])
| 284 | } |
| 285 | |
| 286 | fn find_executable(version: RequestedVersion, args: &[String]) -> crate::Result<PathBuf> { |
| 287 | let mut requested_version = version; |
| 288 | let mut chosen_path: Option<PathBuf> = None; |
| 289 | |
| 290 | if requested_version == RequestedVersion::Any { |
| 291 | if let Some(venv_path) = venv_executable() { |
| 292 | chosen_path = Some(venv_path); |
| 293 | } else if !args.is_empty() { |
| 294 | // Using the first argument because it's the simplest and sanest. |
| 295 | // We can't use the last argument because that could actually be an argument |
| 296 | // to the Python module being executed. This is the same reason we can't go |
| 297 | // searching for the first/last file path that we find. The only safe way to |
| 298 | // get the file path regardless of its position is to replicate Python's arg |
| 299 | // parsing and that's a **lot** of work for little gain. Hence we only care |
| 300 | // about the first argument. |
| 301 | let possible_file = &args[0]; |
| 302 | log::info!("Checking {possible_file:?} for a shebang"); |
| 303 | if let Ok(mut open_file) = File::open(possible_file) { |
| 304 | if let Some(shebang_version) = parse_python_shebang(&mut open_file) { |
| 305 | requested_version = shebang_version; |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if chosen_path.is_none() { |
| 312 | if let Some(env_var) = requested_version.env_var() { |
| 313 | log::info!("Checking the {env_var} environment variable"); |
| 314 | if let Ok(env_var_value) = env::var(&env_var) { |
| 315 | if !env_var_value.is_empty() { |
| 316 | log::debug!("{env_var} = '{env_var_value}'"); |
| 317 | let env_requested_version = RequestedVersion::from_str(&env_var_value)?; |
| 318 | requested_version = env_requested_version; |
| 319 | } |
| 320 | } else { |
| 321 | log::info!("{env_var} not set"); |
| 322 | }; |
| 323 | } |
| 324 | |
| 325 | if let Some(executable_path) = crate::find_executable(requested_version) { |
| 326 | chosen_path = Some(executable_path); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | chosen_path.ok_or(crate::Error::NoExecutableFound(requested_version)) |
| 331 | } |
| 332 | |
| 333 | #[cfg(test)] |
| 334 | mod tests { |
no test coverage detected