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

Commit 12702e4

Browse files
authored
new: Support proto v0.22 release. (#5)
1 parent 0d40ac0 commit 12702e4

File tree

5 files changed

+125
-68
lines changed

5 files changed

+125
-68
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 0.2.0
4+
5+
#### 🚀 Updates
6+
7+
- Updated to support proto v0.22 release.
8+
9+
#### ⚙️ Internal
10+
11+
- Updated dependencies.
12+
313
## 0.1.2
414

515
#### ⚙️ Internal

Cargo.lock

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "python_plugin"
3-
version = "0.1.2"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "MIT"
66
publish = false
@@ -10,12 +10,12 @@ crate-type = ['cdylib']
1010

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

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

src/proto.rs

Lines changed: 94 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ extern "ExtismHost" {
1414

1515
static NAME: &str = "Python";
1616

17+
#[derive(Deserialize)]
18+
struct PythonManifest {
19+
python_exe: String,
20+
python_major_minor_version: String,
21+
}
22+
1723
#[plugin_fn]
1824
pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMetadataOutput>> {
1925
Ok(Json(ToolMetadataOutput {
@@ -24,6 +30,30 @@ pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMeta
2430
}))
2531
}
2632

33+
#[plugin_fn]
34+
pub fn detect_version_files(_: ()) -> FnResult<Json<DetectVersionOutput>> {
35+
Ok(Json(DetectVersionOutput {
36+
files: vec![".python-version".into()],
37+
}))
38+
}
39+
40+
#[plugin_fn]
41+
pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVersionsOutput>> {
42+
let tags = load_git_tags("https://github.com/python/cpython")?;
43+
let regex = Regex::new(
44+
r"v?(?<major>[0-9]+)\.(?<minor>[0-9]+)(?:\.(?<patch>[0-9]+))?(?:(?<pre>a|b|c|rc)(?<preid>[0-9]+))?",
45+
)
46+
.unwrap();
47+
48+
let tags = tags
49+
.into_iter()
50+
.filter(|t| t != "legacy-trunk")
51+
.filter_map(|t| from_python_version(t, &regex))
52+
.collect::<Vec<_>>();
53+
54+
Ok(Json(LoadVersionsOutput::from(tags)?))
55+
}
56+
2757
// #[plugin_fn]
2858
// pub fn native_install(
2959
// Json(input): Json<NativeInstallInput>,
@@ -95,16 +125,75 @@ pub fn download_prebuilt(
95125
}))
96126
}
97127

98-
#[derive(Deserialize)]
99-
struct PythonManifest {
100-
python_exe: String,
101-
python_major_minor_version: String,
128+
#[plugin_fn]
129+
pub fn locate_executables(
130+
Json(input): Json<LocateExecutablesInput>,
131+
) -> FnResult<Json<LocateExecutablesOutput>> {
132+
let env = get_proto_environment()?;
133+
let mut exe_path = env.os.get_exe_name("install/bin/python3");
134+
let mut globals_lookup_dirs = vec!["$HOME/.local/bin".to_owned()];
135+
136+
// Manifest is only available for pre-builts
137+
let manifest_path = input.context.tool_dir.join("PYTHON.json");
138+
139+
if manifest_path.exists() {
140+
let manifest: PythonManifest = json::from_slice(&fs::read(manifest_path)?)?;
141+
exe_path = manifest.python_exe;
142+
143+
if env.os == HostOS::Windows {
144+
let formatted_version = manifest.python_major_minor_version.replace('.', "");
145+
146+
globals_lookup_dirs.push(format!(
147+
"$APPDATA/Roaming/Python{}/Scripts",
148+
formatted_version
149+
));
150+
globals_lookup_dirs.push(format!("$APPDATA/Python{}/Scripts", formatted_version));
151+
}
152+
}
153+
154+
Ok(Json(LocateExecutablesOutput {
155+
globals_lookup_dirs,
156+
primary: Some(ExecutableConfig::new(exe_path)),
157+
secondary: HashMap::from_iter([
158+
// pip
159+
(
160+
"pip".into(),
161+
ExecutableConfig {
162+
no_bin: true,
163+
shim_before_args: Some("-m pip".into()),
164+
..ExecutableConfig::default()
165+
},
166+
),
167+
]),
168+
..LocateExecutablesOutput::default()
169+
}))
170+
}
171+
172+
#[plugin_fn]
173+
pub fn install_global(
174+
Json(input): Json<InstallGlobalInput>,
175+
) -> FnResult<Json<InstallGlobalOutput>> {
176+
let result = exec_command!(inherit, "pip", ["install", "--user", &input.dependency]);
177+
178+
Ok(Json(InstallGlobalOutput::from_exec_command(result)))
179+
}
180+
181+
#[plugin_fn]
182+
pub fn uninstall_global(
183+
Json(input): Json<UninstallGlobalInput>,
184+
) -> FnResult<Json<UninstallGlobalOutput>> {
185+
let result = exec_command!(inherit, "pip", ["uninstall", "--yes", &input.dependency]);
186+
187+
Ok(Json(UninstallGlobalOutput::from_exec_command(result)))
102188
}
103189

190+
// DEPRECATED
191+
// Removed in v0.23!
192+
104193
#[plugin_fn]
105194
pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBinsOutput>> {
106195
let env = get_proto_environment()?;
107-
let mut bin_path = format_bin_name("install/bin/python3", env.os);
196+
let mut bin_path = env.os.get_exe_name("install/bin/python3");
108197
let mut globals_lookup_dirs = vec!["$HOME/.local/bin".to_owned()];
109198

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

138-
#[plugin_fn]
139-
pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVersionsOutput>> {
140-
let tags = load_git_tags("https://github.com/python/cpython")?;
141-
let regex = Regex::new(
142-
r"v?(?<major>[0-9]+)\.(?<minor>[0-9]+)(?:\.(?<patch>[0-9]+))?(?:(?<pre>a|b|c|rc)(?<preid>[0-9]+))?",
143-
)
144-
.unwrap();
145-
146-
let tags = tags
147-
.into_iter()
148-
.filter(|t| t != "legacy-trunk")
149-
.filter_map(|t| from_python_version(t, &regex))
150-
.collect::<Vec<_>>();
151-
152-
Ok(Json(LoadVersionsOutput::from(tags)?))
153-
}
154-
155-
#[plugin_fn]
156-
pub fn detect_version_files(_: ()) -> FnResult<Json<DetectVersionOutput>> {
157-
Ok(Json(DetectVersionOutput {
158-
files: vec![".python-version".into()],
159-
}))
160-
}
161-
162227
#[plugin_fn]
163228
pub fn create_shims(Json(_): Json<CreateShimsInput>) -> FnResult<Json<CreateShimsOutput>> {
164229
let mut global_shims = HashMap::new();
@@ -170,21 +235,3 @@ pub fn create_shims(Json(_): Json<CreateShimsInput>) -> FnResult<Json<CreateShim
170235
..CreateShimsOutput::default()
171236
}))
172237
}
173-
174-
#[plugin_fn]
175-
pub fn install_global(
176-
Json(input): Json<InstallGlobalInput>,
177-
) -> FnResult<Json<InstallGlobalOutput>> {
178-
let result = exec_command!(inherit, "pip", ["install", "--user", &input.dependency]);
179-
180-
Ok(Json(InstallGlobalOutput::from_exec_command(result)))
181-
}
182-
183-
#[plugin_fn]
184-
pub fn uninstall_global(
185-
Json(input): Json<UninstallGlobalInput>,
186-
) -> FnResult<Json<UninstallGlobalOutput>> {
187-
let result = exec_command!(inherit, "pip", ["uninstall", "--yes", &input.dependency]);
188-
189-
Ok(Json(UninstallGlobalOutput::from_exec_command(result)))
190-
}

tests/shims_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
use proto_pdk_test_utils::*;
22

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

0 commit comments

Comments
 (0)