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

Commit

Permalink
new: Support proto v0.22 release. (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Nov 4, 2023
1 parent 0d40ac0 commit 12702e4
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 68 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.2.0

#### 🚀 Updates

- Updated to support proto v0.22 release.

#### ⚙️ Internal

- Updated dependencies.

## 0.1.2

#### ⚙️ Internal
Expand Down
34 changes: 17 additions & 17 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "python_plugin"
version = "0.1.2"
version = "0.2.0"
edition = "2021"
license = "MIT"
publish = false
Expand All @@ -10,12 +10,12 @@ crate-type = ['cdylib']

[dependencies]
extism-pdk = "0.3.4"
proto_pdk = { version = "0.9.0" } # , path = "../../proto/crates/pdk" }
proto_pdk = { version = "0.10.0" } # , path = "../../proto/crates/pdk" }
regex = { version = "1.10.2", default-features = false, features = ["std"] }
serde = "1.0.190"

[dev-dependencies]
proto_pdk_test_utils = { version = "0.9.1" } # , path = "../../proto/crates/pdk-test-utils" }
proto_pdk_test_utils = { version = "0.10.0" } # , path = "../../proto/crates/pdk-test-utils" }
starbase_sandbox = "0.1.12"
tokio = { version = "1.33.0", features = ["full"] }

Expand Down
141 changes: 94 additions & 47 deletions src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ extern "ExtismHost" {

static NAME: &str = "Python";

#[derive(Deserialize)]
struct PythonManifest {
python_exe: String,
python_major_minor_version: String,
}

#[plugin_fn]
pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMetadataOutput>> {
Ok(Json(ToolMetadataOutput {
Expand All @@ -24,6 +30,30 @@ pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMeta
}))
}

#[plugin_fn]
pub fn detect_version_files(_: ()) -> FnResult<Json<DetectVersionOutput>> {
Ok(Json(DetectVersionOutput {
files: vec![".python-version".into()],
}))
}

#[plugin_fn]
pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVersionsOutput>> {
let tags = load_git_tags("https://github.com/python/cpython")?;
let regex = Regex::new(
r"v?(?<major>[0-9]+)\.(?<minor>[0-9]+)(?:\.(?<patch>[0-9]+))?(?:(?<pre>a|b|c|rc)(?<preid>[0-9]+))?",
)
.unwrap();

let tags = tags
.into_iter()
.filter(|t| t != "legacy-trunk")
.filter_map(|t| from_python_version(t, &regex))
.collect::<Vec<_>>();

Ok(Json(LoadVersionsOutput::from(tags)?))
}

// #[plugin_fn]
// pub fn native_install(
// Json(input): Json<NativeInstallInput>,
Expand Down Expand Up @@ -95,16 +125,75 @@ pub fn download_prebuilt(
}))
}

#[derive(Deserialize)]
struct PythonManifest {
python_exe: String,
python_major_minor_version: String,
#[plugin_fn]
pub fn locate_executables(
Json(input): Json<LocateExecutablesInput>,
) -> FnResult<Json<LocateExecutablesOutput>> {
let env = get_proto_environment()?;
let mut exe_path = env.os.get_exe_name("install/bin/python3");
let mut globals_lookup_dirs = vec!["$HOME/.local/bin".to_owned()];

// Manifest is only available for pre-builts
let manifest_path = input.context.tool_dir.join("PYTHON.json");

if manifest_path.exists() {
let manifest: PythonManifest = json::from_slice(&fs::read(manifest_path)?)?;
exe_path = manifest.python_exe;

if env.os == HostOS::Windows {
let formatted_version = manifest.python_major_minor_version.replace('.', "");

globals_lookup_dirs.push(format!(
"$APPDATA/Roaming/Python{}/Scripts",
formatted_version
));
globals_lookup_dirs.push(format!("$APPDATA/Python{}/Scripts", formatted_version));
}
}

Ok(Json(LocateExecutablesOutput {
globals_lookup_dirs,
primary: Some(ExecutableConfig::new(exe_path)),
secondary: HashMap::from_iter([
// pip
(
"pip".into(),
ExecutableConfig {
no_bin: true,
shim_before_args: Some("-m pip".into()),
..ExecutableConfig::default()
},
),
]),
..LocateExecutablesOutput::default()
}))
}

#[plugin_fn]
pub fn install_global(
Json(input): Json<InstallGlobalInput>,
) -> FnResult<Json<InstallGlobalOutput>> {
let result = exec_command!(inherit, "pip", ["install", "--user", &input.dependency]);

Ok(Json(InstallGlobalOutput::from_exec_command(result)))
}

#[plugin_fn]
pub fn uninstall_global(
Json(input): Json<UninstallGlobalInput>,
) -> FnResult<Json<UninstallGlobalOutput>> {
let result = exec_command!(inherit, "pip", ["uninstall", "--yes", &input.dependency]);

Ok(Json(UninstallGlobalOutput::from_exec_command(result)))
}

// DEPRECATED
// Removed in v0.23!

#[plugin_fn]
pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBinsOutput>> {
let env = get_proto_environment()?;
let mut bin_path = format_bin_name("install/bin/python3", env.os);
let mut bin_path = env.os.get_exe_name("install/bin/python3");
let mut globals_lookup_dirs = vec!["$HOME/.local/bin".to_owned()];

// Manifest is only available for pre-builts
Expand Down Expand Up @@ -135,30 +224,6 @@ pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBi
}))
}

#[plugin_fn]
pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVersionsOutput>> {
let tags = load_git_tags("https://github.com/python/cpython")?;
let regex = Regex::new(
r"v?(?<major>[0-9]+)\.(?<minor>[0-9]+)(?:\.(?<patch>[0-9]+))?(?:(?<pre>a|b|c|rc)(?<preid>[0-9]+))?",
)
.unwrap();

let tags = tags
.into_iter()
.filter(|t| t != "legacy-trunk")
.filter_map(|t| from_python_version(t, &regex))
.collect::<Vec<_>>();

Ok(Json(LoadVersionsOutput::from(tags)?))
}

#[plugin_fn]
pub fn detect_version_files(_: ()) -> FnResult<Json<DetectVersionOutput>> {
Ok(Json(DetectVersionOutput {
files: vec![".python-version".into()],
}))
}

#[plugin_fn]
pub fn create_shims(Json(_): Json<CreateShimsInput>) -> FnResult<Json<CreateShimsOutput>> {
let mut global_shims = HashMap::new();
Expand All @@ -170,21 +235,3 @@ pub fn create_shims(Json(_): Json<CreateShimsInput>) -> FnResult<Json<CreateShim
..CreateShimsOutput::default()
}))
}

#[plugin_fn]
pub fn install_global(
Json(input): Json<InstallGlobalInput>,
) -> FnResult<Json<InstallGlobalOutput>> {
let result = exec_command!(inherit, "pip", ["install", "--user", &input.dependency]);

Ok(Json(InstallGlobalOutput::from_exec_command(result)))
}

#[plugin_fn]
pub fn uninstall_global(
Json(input): Json<UninstallGlobalInput>,
) -> FnResult<Json<UninstallGlobalOutput>> {
let result = exec_command!(inherit, "pip", ["uninstall", "--yes", &input.dependency]);

Ok(Json(UninstallGlobalOutput::from_exec_command(result)))
}
2 changes: 1 addition & 1 deletion tests/shims_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proto_pdk_test_utils::*;

#[cfg(not(windows))]
generate_global_shims_test!("python-test", ["pip"]);
generate_shims_test!("python-test", ["pip"]);

0 comments on commit 12702e4

Please sign in to comment.