Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
new: Create a pip3 shim. (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Jun 16, 2024
1 parent e242297 commit 9eb0ebd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 33 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.10.2

#### 🚀 Updates

- Will now create a pip shim that includes the major version, for example, `pip3`.

## 0.10.1

#### 🚀 Updates
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "python_plugin"
version = "0.10.1"
version = "0.10.2"
edition = "2021"
license = "MIT"
publish = false
Expand Down
72 changes: 42 additions & 30 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static NAME: &str = "Python";
struct PythonManifest {
python_exe: String,
python_major_minor_version: String,
python_paths: HashMap<String, String>,
}

#[plugin_fn]
Expand Down Expand Up @@ -145,19 +146,15 @@ pub fn locate_executables(
) -> FnResult<Json<LocateExecutablesOutput>> {
let env = get_host_environment()?;
let id = get_plugin_id()?;
let exe_path;
let mut exe_path = env
.os
.for_native("install/bin/python", "install/python.exe")
.to_owned();
let mut scripts_dir = env
.os
.for_native("install/bin", "install/Scripts")
.to_owned();
let mut major_version = "3".to_owned();
let mut secondary = HashMap::from_iter([
// pip
(
"pip".into(),
ExecutableConfig {
no_bin: true,
shim_before_args: Some(StringOrVec::Vec(vec!["-m".into(), "pip".into()])),
..ExecutableConfig::default()
},
),
]);

// Manifest is only available for pre-builts
let manifest_path = input.context.tool_dir.join("PYTHON.json");
Expand All @@ -167,36 +164,51 @@ pub fn locate_executables(

exe_path = manifest.python_exe;

if let Some(i) = manifest.python_major_minor_version.find('.') {
major_version = manifest.python_major_minor_version[0..i].to_string();
if let Some(dir) = manifest.python_paths.get("scripts") {
scripts_dir = dir.to_owned();
}

if let Some(index) = manifest.python_major_minor_version.find('.') {
major_version = manifest.python_major_minor_version[0..index].to_string();
}
}
// Otherwise this was built from source
else {
if let VersionSpec::Version(version) = input.context.version {
major_version = version.major.to_string();
};

exe_path = env
.os
.for_native(
format!("install/bin/python{major_version}").as_str(),
"install/python.exe",
)
.to_owned();
}

// Create a secondary executable that includes the major version as a suffix
secondary.insert(
format!("{id}{major_version}"),
ExecutableConfig::new(&exe_path),
);
// https://gregoryszorc.com/docs/python-build-standalone/main/quirks.html#no-pip-exe-on-windows
let secondary = HashMap::from_iter([
// python3
(
format!("{id}{major_version}"),
ExecutableConfig::new(&exe_path),
),
// pip
(
format!("pip"),
ExecutableConfig {
no_bin: true,
shim_before_args: Some(StringOrVec::Vec(vec!["-m".into(), "pip".into()])),
..ExecutableConfig::default()
},
),
// pip3
(
format!("pip{major_version}"),
ExecutableConfig {
no_bin: true,
shim_before_args: Some(StringOrVec::Vec(vec!["-m".into(), "pip".into()])),
..ExecutableConfig::default()
},
),
]);

Ok(Json(LocateExecutablesOutput {
globals_lookup_dirs: vec![env
.os
.for_native("$TOOL_DIR/install/bin", "$TOOL_DIR/install/Scripts")
.into()],
globals_lookup_dirs: vec![format!("$TOOL_DIR/{scripts_dir}")],
primary: Some(ExecutableConfig::new(exe_path)),
secondary,
..LocateExecutablesOutput::default()
Expand Down
7 changes: 7 additions & 0 deletions tests/snapshots/shims_test__creates_shims.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ expression: "std::fs::read_to_string(sandbox.path().join(\".proto/shims/registry
"pip"
]
},
"pip3": {
"parent": "python-test",
"before_args": [
"-m",
"pip"
]
},
"python-test": {},
"python-test3": {
"alt_bin": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/versions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ generate_resolve_versions_tests!("python-test", {
"2.3" => "2.3.7",
"3.10.1" => "3.10.1",
"3.10" => "3.10.14",
"3" => "3.12.3",
"3" => "3.12.4",
});

#[test]
Expand Down

0 comments on commit 9eb0ebd

Please sign in to comment.