@@ -14,6 +14,12 @@ extern "ExtismHost" {
14
14
15
15
static NAME : & str = "Python" ;
16
16
17
+ #[ derive( Deserialize ) ]
18
+ struct PythonManifest {
19
+ python_exe : String ,
20
+ python_major_minor_version : String ,
21
+ }
22
+
17
23
#[ plugin_fn]
18
24
pub fn register_tool ( Json ( _) : Json < ToolMetadataInput > ) -> FnResult < Json < ToolMetadataOutput > > {
19
25
Ok ( Json ( ToolMetadataOutput {
@@ -24,6 +30,30 @@ pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMeta
24
30
} ) )
25
31
}
26
32
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
+
27
57
// #[plugin_fn]
28
58
// pub fn native_install(
29
59
// Json(input): Json<NativeInstallInput>,
@@ -95,16 +125,75 @@ pub fn download_prebuilt(
95
125
} ) )
96
126
}
97
127
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) ) )
102
188
}
103
189
190
+ // DEPRECATED
191
+ // Removed in v0.23!
192
+
104
193
#[ plugin_fn]
105
194
pub fn locate_bins ( Json ( input) : Json < LocateBinsInput > ) -> FnResult < Json < LocateBinsOutput > > {
106
195
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" ) ;
108
197
let mut globals_lookup_dirs = vec ! [ "$HOME/.local/bin" . to_owned( ) ] ;
109
198
110
199
// Manifest is only available for pre-builts
@@ -135,30 +224,6 @@ pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBi
135
224
} ) )
136
225
}
137
226
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
-
162
227
#[ plugin_fn]
163
228
pub fn create_shims ( Json ( _) : Json < CreateShimsInput > ) -> FnResult < Json < CreateShimsOutput > > {
164
229
let mut global_shims = HashMap :: new ( ) ;
@@ -170,21 +235,3 @@ pub fn create_shims(Json(_): Json<CreateShimsInput>) -> FnResult<Json<CreateShim
170
235
..CreateShimsOutput :: default ( )
171
236
} ) )
172
237
}
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
- }
0 commit comments