forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Native locator (microsoft#23435)
- Loading branch information
Showing
106 changed files
with
4,137 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,3 +48,5 @@ dist/** | |
*.xlf | ||
package.nls.*.json | ||
l10n/ | ||
native_locator/target/** | ||
native_locator/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "python-finder" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
winreg = "0.52.0" | ||
|
||
[dependencies] | ||
serde = { version = "1.0.152", features = ["derive"] } | ||
serde_json = "1.0.93" | ||
serde_repr = "0.1.10" | ||
regex = "1.10.4" | ||
log = "0.4.21" | ||
env_logger = "0.10.2" | ||
|
||
|
||
[lib] | ||
doctest = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
use crate::known::Environment; | ||
use crate::locator::{Locator, LocatorResult}; | ||
use crate::messaging::PythonEnvironment; | ||
use crate::utils::{self, PythonEnv}; | ||
use std::env; | ||
use std::path::{Path, PathBuf}; | ||
|
||
fn get_env_path(python_executable_path: &PathBuf) -> Option<PathBuf> { | ||
let parent = python_executable_path.parent()?; | ||
if parent.file_name()? == "Scripts" { | ||
return Some(parent.parent()?.to_path_buf()); | ||
} else { | ||
return Some(parent.to_path_buf()); | ||
} | ||
} | ||
|
||
pub struct PythonOnPath<'a> { | ||
pub environment: &'a dyn Environment, | ||
} | ||
|
||
impl PythonOnPath<'_> { | ||
pub fn with<'a>(environment: &'a impl Environment) -> PythonOnPath { | ||
PythonOnPath { environment } | ||
} | ||
} | ||
|
||
impl Locator for PythonOnPath<'_> { | ||
fn resolve(&self, env: &PythonEnv) -> Option<PythonEnvironment> { | ||
let bin = if cfg!(windows) { | ||
"python.exe" | ||
} else { | ||
"python" | ||
}; | ||
if env.executable.file_name().unwrap().to_ascii_lowercase() != bin { | ||
return None; | ||
} | ||
Some(PythonEnvironment { | ||
display_name: None, | ||
name: None, | ||
python_executable_path: Some(env.executable.clone()), | ||
version: env.version.clone(), | ||
category: crate::messaging::PythonEnvironmentCategory::System, | ||
env_path: env.path.clone(), | ||
env_manager: None, | ||
project_path: None, | ||
python_run_command: Some(vec![env.executable.to_str().unwrap().to_string()]), | ||
}) | ||
} | ||
|
||
fn find(&mut self) -> Option<LocatorResult> { | ||
let paths = self.environment.get_env_var("PATH".to_string())?; | ||
let bin = if cfg!(windows) { | ||
"python.exe" | ||
} else { | ||
"python" | ||
}; | ||
|
||
// Exclude files from this folder, as they would have been discovered elsewhere (widows_store) | ||
// Also the exe is merely a pointer to another file. | ||
let home = self.environment.get_user_home()?; | ||
let apps_path = Path::new(&home) | ||
.join("AppData") | ||
.join("Local") | ||
.join("Microsoft") | ||
.join("WindowsApps"); | ||
let mut environments: Vec<PythonEnvironment> = vec![]; | ||
env::split_paths(&paths) | ||
.filter(|p| !p.starts_with(apps_path.clone())) | ||
.map(|p| p.join(bin)) | ||
.filter(|p| p.exists()) | ||
.for_each(|full_path| { | ||
let version = utils::get_version(&full_path); | ||
let env_path = get_env_path(&full_path); | ||
if let Some(env) = self.resolve(&PythonEnv::new(full_path, env_path, version)) { | ||
environments.push(env); | ||
} | ||
}); | ||
|
||
if environments.is_empty() { | ||
None | ||
} else { | ||
Some(LocatorResult { | ||
environments, | ||
managers: vec![], | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.