From 12702e4b67ab9eac7637efed66f1d9bb9e68a105 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Sat, 4 Nov 2023 13:31:27 -0700 Subject: [PATCH] new: Support proto v0.22 release. (#5) --- CHANGELOG.md | 10 ++++ Cargo.lock | 34 +++++------ Cargo.toml | 6 +- src/proto.rs | 141 +++++++++++++++++++++++++++++--------------- tests/shims_test.rs | 2 +- 5 files changed, 125 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87dfa81..bea1024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ # Changelog +## 0.2.0 + +#### 🚀 Updates + +- Updated to support proto v0.22 release. + +#### ⚙️ Internal + +- Updated dependencies. + ## 0.1.2 #### ⚙️ Internal diff --git a/Cargo.lock b/Cargo.lock index 6e683f4..55a3493 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1579,9 +1579,9 @@ dependencies = [ [[package]] name = "json_comments" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41ee439ee368ba4a77ac70d04f14015415af8600d6c894dc1f11bd79758c57d5" +checksum = "9dbbfed4e59ba9750e15ba154fdfd9329cee16ff3df539c2666b70f58cc32105" [[package]] name = "lazy_static" @@ -2017,9 +2017,9 @@ dependencies = [ [[package]] name = "proto_core" -version = "0.21.2" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ea826b40e2682f5eb5ee90d1065e09a710ec74807f5c7db85aa3c5fb397623e" +checksum = "857645ab2fe78d570056938494f135a33edd8788a6ec898188325b385d29671e" dependencies = [ "cached", "extism", @@ -2048,9 +2048,9 @@ dependencies = [ [[package]] name = "proto_pdk" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce0b4fe64801579bbf1c341db7c6bf9b67cd2d58e3bf32d28b6fd69908cfe102" +checksum = "a67a488f4dfb26b6fc53378526de346d09ba2b511883204992fdf3acebb24d16" dependencies = [ "anyhow", "extism-pdk", @@ -2060,9 +2060,9 @@ dependencies = [ [[package]] name = "proto_pdk_api" -version = "0.9.0" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0d49bf81e0c2033a4660270a2c8ff6f331ba0cb61500c6d3dc41d8f18cd5ce3" +checksum = "2c4cafcba5b79560a3bfba87ac7fe6fca1e7970e7459a2b60989dc41e152a9ad" dependencies = [ "anyhow", "semver", @@ -2076,9 +2076,9 @@ dependencies = [ [[package]] name = "proto_pdk_test_utils" -version = "0.9.1" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb47a6bf9be024ae8690f62e6e9c3e0c2537e9071c1562a74564bf70378b8a80" +checksum = "2e6c0a69d1302bd65f95043434d90b6e5cb9ff0aeb7bef7cd91f7fc49b9973ad" dependencies = [ "extism", "proto_core", @@ -2108,7 +2108,7 @@ dependencies = [ [[package]] name = "python_plugin" -version = "0.1.2" +version = "0.2.0" dependencies = [ "extism-pdk", "proto_pdk", @@ -2538,9 +2538,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.107" +version = "1.0.108" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" +checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b" dependencies = [ "itoa", "ryu", @@ -2746,9 +2746,9 @@ dependencies = [ [[package]] name = "starbase_utils" -version = "0.3.6" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9faf731068b919569efae7f6c7c6c3d2e29411c1cba666ef0c6fe20b82f50a1" +checksum = "d948dcd63f1dd11f2b7a9ab15c53e7866faa11ccd31377dca1625c106d40fcee" dependencies = [ "dirs 5.0.1", "fs4", @@ -2844,9 +2844,9 @@ dependencies = [ [[package]] name = "system_env" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9a9eb72230d28d8116aab631232e98675a7fd3e69b81b046d54205bd21e16d5" +checksum = "c1545ee71941615742defd942059f99d35bfd6e1e079d0f6502e020c4b60a928" dependencies = [ "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index b9b612e..0e1f528 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "python_plugin" -version = "0.1.2" +version = "0.2.0" edition = "2021" license = "MIT" publish = false @@ -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"] } diff --git a/src/proto.rs b/src/proto.rs index 0230d11..e956961 100644 --- a/src/proto.rs +++ b/src/proto.rs @@ -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) -> FnResult> { Ok(Json(ToolMetadataOutput { @@ -24,6 +30,30 @@ pub fn register_tool(Json(_): Json) -> FnResult FnResult> { + Ok(Json(DetectVersionOutput { + files: vec![".python-version".into()], + })) +} + +#[plugin_fn] +pub fn load_versions(Json(_): Json) -> FnResult> { + let tags = load_git_tags("https://github.com/python/cpython")?; + let regex = Regex::new( + r"v?(?[0-9]+)\.(?[0-9]+)(?:\.(?[0-9]+))?(?:(?
a|b|c|rc)(?[0-9]+))?",
+    )
+    .unwrap();
+
+    let tags = tags
+        .into_iter()
+        .filter(|t| t != "legacy-trunk")
+        .filter_map(|t| from_python_version(t, ®ex))
+        .collect::>();
+
+    Ok(Json(LoadVersionsOutput::from(tags)?))
+}
+
 // #[plugin_fn]
 // pub fn native_install(
 //     Json(input): Json,
@@ -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,
+) -> FnResult> {
+    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,
+) -> FnResult> {
+    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,
+) -> FnResult> {
+    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) -> FnResult> {
     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
@@ -135,30 +224,6 @@ pub fn locate_bins(Json(input): Json) -> FnResult) -> FnResult> {
-    let tags = load_git_tags("https://github.com/python/cpython")?;
-    let regex = Regex::new(
-        r"v?(?[0-9]+)\.(?[0-9]+)(?:\.(?[0-9]+))?(?:(?
a|b|c|rc)(?[0-9]+))?",
-    )
-    .unwrap();
-
-    let tags = tags
-        .into_iter()
-        .filter(|t| t != "legacy-trunk")
-        .filter_map(|t| from_python_version(t, ®ex))
-        .collect::>();
-
-    Ok(Json(LoadVersionsOutput::from(tags)?))
-}
-
-#[plugin_fn]
-pub fn detect_version_files(_: ()) -> FnResult> {
-    Ok(Json(DetectVersionOutput {
-        files: vec![".python-version".into()],
-    }))
-}
-
 #[plugin_fn]
 pub fn create_shims(Json(_): Json) -> FnResult> {
     let mut global_shims = HashMap::new();
@@ -170,21 +235,3 @@ pub fn create_shims(Json(_): Json) -> FnResult,
-) -> FnResult> {
-    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,
-) -> FnResult> {
-    let result = exec_command!(inherit, "pip", ["uninstall", "--yes", &input.dependency]);
-
-    Ok(Json(UninstallGlobalOutput::from_exec_command(result)))
-}
diff --git a/tests/shims_test.rs b/tests/shims_test.rs
index 24580ef..a586251 100644
--- a/tests/shims_test.rs
+++ b/tests/shims_test.rs
@@ -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"]);