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

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 3, 2023
1 parent 0d40ac0 commit fb143bd
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 71 deletions.
28 changes: 6 additions & 22 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.9.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.9.1", path = "../../proto/crates/pdk-test-utils" }
starbase_sandbox = "0.1.12"
tokio = { version = "1.33.0", features = ["full"] }

Expand Down
139 changes: 93 additions & 46 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,12 +125,71 @@ 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 = format_bin_name("install/bin/python3", env.os);
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()?;
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 fb143bd

Please sign in to comment.