-
Notifications
You must be signed in to change notification settings - Fork 8
fix: find all installed python versions #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
not-matthias
wants to merge
11
commits into
main
from
cod-1315-statically-linked-libpythonso-adds-extra-frames-to-valgrind
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
df2a918
fix: find all installed python versions
not-matthias 5686ef8
fixup: remove unneeded python ignore check
not-matthias 6b4da5f
chore: also ignore python (which could be statically linked)
not-matthias b4577ba
fixup: minor changes
not-matthias 0a086f7
chore: do not normalize python paths
not-matthias 9d77cd4
fix: use cur dir
not-matthias 14e8ae7
chore: detect venv python
not-matthias 2bcf7bf
fixup: normalize all paths
not-matthias 79d3a33
fix: venv check
not-matthias 7d102ed
fix: venv compat script
not-matthias 0e1f651
fixup: dedup normalized objects
not-matthias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,28 +1,131 @@ | ||
| use crate::prelude::*; | ||
| use std::{path::PathBuf, process::Command}; | ||
|
|
||
| fn get_python_objects() -> Vec<String> { | ||
| let output = Command::new("python") | ||
| .arg("-c") | ||
| .arg("import sysconfig; print('/'.join(sysconfig.get_config_vars('LIBDIR', 'INSTSONAME')))") | ||
| .output(); | ||
| fn find_venv_python_paths() -> anyhow::Result<Vec<String>> { | ||
| let output = Command::new("uv") | ||
| .args(["python", "find"]) | ||
| .current_dir(std::env::current_dir()?) | ||
| .output()?; | ||
| if !output.status.success() { | ||
| bail!( | ||
| "Failed to get venv python path: {}", | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
| } | ||
| let python_path = PathBuf::from(String::from_utf8_lossy(&output.stdout).trim()); | ||
| if !python_path.exists() { | ||
| return Ok(vec![]); | ||
| } | ||
| debug!( | ||
| "Resolved venv python path: {}", | ||
| python_path.to_string_lossy() | ||
| ); | ||
|
|
||
| if output.is_err() { | ||
| let err = output.err().unwrap().to_string(); | ||
| debug!("Failed to get python shared objects: {err}"); | ||
| return vec![]; | ||
| Ok(vec![python_path.to_string_lossy().to_string()]) | ||
| } | ||
|
|
||
| fn find_uv_python_paths() -> anyhow::Result<Vec<String>> { | ||
| let output = Command::new("uv") | ||
| .args([ | ||
| "python", | ||
| "list", | ||
| "--only-installed", | ||
| "--output-format", | ||
| "json", | ||
| ]) | ||
| // IMPORTANT: Set to the cwd, so that we also find python | ||
| // installations in virtual environments. | ||
| .current_dir(std::env::current_dir()?) | ||
| .output()?; | ||
| if !output.status.success() { | ||
| bail!( | ||
| "Failed to get uv python paths: {}", | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
| } | ||
| let output = output.unwrap(); | ||
|
|
||
| let json_output = String::from_utf8_lossy(&output.stdout); | ||
| let json: serde_json::Value = serde_json::from_str(&json_output).unwrap_or_default(); | ||
| let arr = json | ||
| .as_array() | ||
| .context("Failed to parse uv python paths: not an array")?; | ||
| let paths: Vec<String> = arr | ||
| .iter() | ||
| .filter_map(|obj| obj.get("path")) | ||
| .filter_map(|p| p.as_str()) | ||
| .map(|s| s.to_string()) | ||
| .collect(); | ||
| Ok(paths) | ||
| } | ||
|
|
||
| fn find_system_python_paths() -> anyhow::Result<Vec<String>> { | ||
| let output = Command::new("which").args(["-a", "python"]).output()?; | ||
|
||
| if !output.status.success() { | ||
| debug!( | ||
| "Failed to get python shared objects: {}", | ||
| bail!( | ||
| "Failed to get system python path: {}", | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
| return vec![]; | ||
| } | ||
|
|
||
| let so_output = String::from_utf8_lossy(&output.stdout).trim().to_string(); | ||
| vec![so_output] | ||
| let paths = String::from_utf8_lossy(&output.stdout) | ||
| .lines() | ||
| .map(|line| line.trim().to_string()) | ||
| .collect(); | ||
| Ok(paths) | ||
| } | ||
|
|
||
| fn find_python_paths() -> anyhow::Result<Vec<String>> { | ||
| let uv_paths = find_uv_python_paths().unwrap_or_default(); | ||
| debug!("uv python paths: {uv_paths:?}"); | ||
| let system_paths = find_system_python_paths().unwrap_or_default(); | ||
| debug!("system python paths: {system_paths:?}"); | ||
| let venv_paths = find_venv_python_paths().unwrap_or_default(); | ||
| debug!("venv python paths: {venv_paths:?}"); | ||
|
|
||
| // For each python path, look at the folder to possibly identify more python versions | ||
|
|
||
| let mut paths = uv_paths; | ||
| paths.extend(system_paths); | ||
| paths.extend(venv_paths); | ||
| paths.sort(); | ||
| paths.dedup(); | ||
| Ok(paths) | ||
| } | ||
|
|
||
| fn get_python_objects() -> Vec<String> { | ||
| let mut python_objects = Vec::new(); | ||
| for path in find_python_paths().unwrap_or_default() { | ||
| // Get the parent directory of the python binary, then join with lib | ||
| let python_path = PathBuf::from(&path); | ||
| let Some(parent_dir) = python_path.parent() else { | ||
| continue; | ||
| }; | ||
| let Some(install_dir) = parent_dir.parent() else { | ||
| continue; | ||
| }; | ||
|
|
||
| let lib_dir = install_dir.join("lib"); | ||
| let Ok(entries) = std::fs::read_dir(&lib_dir) else { | ||
| continue; | ||
| }; | ||
|
|
||
| for entry in entries.flatten() { | ||
| let file_name = entry.file_name(); | ||
| let file_name_str = file_name.to_string_lossy(); | ||
|
|
||
| if !file_name_str.starts_with("libpython") { | ||
| continue; | ||
| } | ||
|
|
||
| let entry_path = entry.path(); | ||
| let Some(full_path) = entry_path.to_str() else { | ||
| continue; | ||
| }; | ||
| python_objects.push(full_path.to_string()); | ||
| } | ||
| } | ||
|
|
||
| python_objects | ||
| } | ||
|
|
||
| fn get_node_objects() -> Vec<String> { | ||
|
|
@@ -60,10 +163,15 @@ fn normalize_object_paths(objects_path_to_ignore: &mut [String]) { | |
|
|
||
| pub fn get_objects_path_to_ignore() -> Vec<String> { | ||
| let mut objects_path_to_ignore = vec![]; | ||
| objects_path_to_ignore.extend(get_python_objects()); | ||
| objects_path_to_ignore.extend(get_node_objects()); | ||
| debug!("objects_path_to_ignore before normalization: {objects_path_to_ignore:?}"); | ||
| objects_path_to_ignore.extend(get_python_objects()); | ||
| objects_path_to_ignore.extend(find_python_paths().unwrap_or_default()); | ||
|
|
||
| debug!("objects_path_to_ignore before normalization: {objects_path_to_ignore:#?}"); | ||
| normalize_object_paths(&mut objects_path_to_ignore); | ||
| debug!("objects_path_to_ignore after normalization: {objects_path_to_ignore:?}"); | ||
| objects_path_to_ignore.sort(); | ||
| objects_path_to_ignore.dedup(); | ||
| debug!("objects_path_to_ignore after normalization: {objects_path_to_ignore:#?}"); | ||
|
|
||
| objects_path_to_ignore | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
unwrap_or_default()on JSON parsing will silently return an empty JSON value for invalid JSON, which could mask parsing errors. Consider using proper error handling withcontext()to provide meaningful error messages when JSON parsing fails.