This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proto.rs
210 lines (181 loc) · 6.3 KB
/
proto.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::version::from_python_version;
use extism_pdk::*;
use proto_pdk::*;
use regex::Regex;
use serde::Deserialize;
use std::collections::{BTreeMap, HashMap};
use std::fs;
#[host_fn]
extern "ExtismHost" {
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
fn host_log(input: Json<HostLogInput>);
}
static NAME: &str = "Python";
#[derive(Deserialize)]
struct PythonManifest {
python_exe: String,
python_major_minor_version: String,
python_paths: HashMap<String, String>,
}
#[plugin_fn]
pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMetadataOutput>> {
Ok(Json(ToolMetadataOutput {
name: NAME.into(),
type_of: PluginType::Language,
plugin_version: Some(env!("CARGO_PKG_VERSION").into()),
..ToolMetadataOutput::default()
}))
}
#[plugin_fn]
pub fn detect_version_files(_: ()) -> FnResult<Json<DetectVersionOutput>> {
Ok(Json(DetectVersionOutput {
files: vec![".python-version".into()],
ignore: vec![],
}))
}
#[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_map(|tag| {
if tag == "legacy-trunk" {
None
} else {
from_python_version(tag, ®ex)
}
})
.collect::<Vec<_>>();
Ok(Json(LoadVersionsOutput::from(tags)?))
}
// #[plugin_fn]
// pub fn native_install(
// Json(input): Json<NativeInstallInput>,
// ) -> FnResult<Json<NativeInstallOutput>> {
// let mut output = NativeInstallOutput::default();
// let env = get_host_environment()?;
// // https://github.com/pyenv/pyenv/tree/master/plugins/python-build
// if command_exists(&env, "python-build") {
// host_log!("Building with `python-build` instead of downloading a pre-built");
// let result = exec_command!(
// inherit,
// "python-build",
// [
// input.context.version.as_str(),
// input.install_dir.real_path().to_str().unwrap(),
// ]
// );
// output.installed = result.exit_code == 0;
// } else {
// output.skip_install = true;
// }
// Ok(Json(output))
// }
#[derive(Deserialize)]
struct ReleaseEntry {
download: String,
checksum: Option<String>,
}
#[plugin_fn]
pub fn download_prebuilt(
Json(input): Json<DownloadPrebuiltInput>,
) -> FnResult<Json<DownloadPrebuiltOutput>> {
let env = get_host_environment()?;
let version = &input.context.version;
if version.is_canary() {
return Err(plugin_err!(PluginError::UnsupportedCanary {
tool: NAME.into()
}));
}
let releases: BTreeMap<Version, BTreeMap<String, ReleaseEntry>> =
fetch_url("https://raw.githubusercontent.com/moonrepo/python-plugin/master/releases.json")?;
let Some(release_triples) = version.as_version().and_then(|v| releases.get(v)) else {
return Err(plugin_err!(
"No pre-built available for version <hash>{}</hash> (via <url>https://github.com/indygreg/python-build-standalone</url>)! Try installing another version for the time being.",
version
));
};
let triple = get_target_triple(&env, NAME)?;
let Some(release) = release_triples.get(&triple) else {
return Err(plugin_err!(
"No pre-built available for architecture <id>{}</id>!",
triple
));
};
Ok(Json(DownloadPrebuiltOutput {
archive_prefix: Some("python".into()),
checksum_url: release.checksum.clone(),
download_url: release.download.clone(),
..DownloadPrebuiltOutput::default()
}))
}
#[plugin_fn]
pub fn locate_executables(
Json(input): Json<LocateExecutablesInput>,
) -> FnResult<Json<LocateExecutablesOutput>> {
let env = get_host_environment()?;
let id = get_plugin_id()?;
let mut exe_path = env
.os
.for_native("install/bin/python", "install/python.exe")
.to_owned();
let mut exes_dir = env
.os
.for_native("install/bin", "install/Scripts")
.to_owned();
let mut major_version = "3".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 let Some(dir) = manifest.python_paths.get("scripts") {
dir.clone_into(&mut exes_dir);
}
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 Some(version) = input.context.version.as_version() {
major_version = version.major.to_string();
}
// Create a secondary executable that includes the major version as a suffix
// 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
(
"pip".into(),
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![format!("$TOOL_DIR/{exes_dir}")],
exes_dir: Some(exes_dir.into()),
primary: Some(ExecutableConfig::new(exe_path)),
secondary,
..LocateExecutablesOutput::default()
}))
}