Repository navigation
Expand file tree
/
Copy pathvenvlauncher.c
More file actions
280 lines (246 loc) · 8.92 KB
/
Copy pathvenvlauncher.c
File metadata and controls
280 lines (246 loc) · 8.92 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2025, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <string.h>
#include <limits.h>
#include <stdarg.h>
#include <wordexp.h>
#include <mach-o/dyld.h>
#define GRAAL_PYTHON_EXE_ARG "--python.Executable="
#define GRAAL_PYTHON_BASE_EXE_ARG "--python.VenvlauncherCommand="
static bool debug_enabled = false;
void debug(const char *format, ...) {
if (!debug_enabled) return;
va_list va;
va_start(va, format);
vfprintf(stderr, format, va);
va_end(va);
fflush(stderr);
}
/**
* Reads the 'venvlauncher_command' from a pyvenv.cfg file.
* Returns a newly allocated string. Caller must free() it.
*/
char *get_pyenvcfg_command(const char *pyenv_cfg_path) {
FILE *fp = fopen(pyenv_cfg_path, "r");
if (fp == NULL) {
fprintf(stderr, "Failed to open pyenv.cfg file!\n");
exit(1);
}
char *current_line = NULL;
size_t current_line_size = 0;
const char key[] = "venvlauncher_command";
size_t key_len = sizeof(key) - 1;
while (getline(¤t_line, ¤t_line_size, fp) != -1) {
char *p = current_line;
while (isspace((unsigned char) *p)) p++;
if (strncmp(p, key, key_len) == 0) {
p += key_len;
// Skip spaces and '='
while (isspace((unsigned char) *p)) p++;
if (*p == '=') {
p++;
} else {
fprintf(stderr, "venv command is not in correct format. Expected '=' after the venvlauncher_command");
free(current_line);
fclose(fp);
exit(1);
}
while (isspace((unsigned char) *p)) p++;
char *end = p + strlen(p);
while (end > p && isspace((unsigned char) end[-1])) {
*--end = '\0';
}
if (*p == '\"') {
if (end <= p + 1 || end[-1] != '\"') {
fprintf(stderr, "venv command is not in correct format");
free(current_line);
fclose(fp);
exit(1);
}
p++;
end[-1] = '\0';
}
char *result = strdup(p);
free(current_line);
fclose(fp);
return result;
}
}
free(current_line);
if (ferror(fp)) {
perror("getline failed");
fclose(fp);
exit(1);
}
fclose(fp);
fprintf(stderr, "venvlauncher_command not found in pyenv.cfg file");
exit(1);
}
char **split_venv_command_into_args(const char *venv_command, int *argc_out) {
if (access(venv_command, X_OK) == 0) {
char **args = malloc(sizeof(char *));
if (!args) {
fprintf(stderr, "allocation failed\n");
exit(1);
}
args[0] = strdup(venv_command);
if (!args[0]) {
fprintf(stderr, "allocation failed\n");
free(args);
exit(1);
}
*argc_out = 1;
return args;
}
wordexp_t expanded;
int rc = wordexp(venv_command, &expanded, WRDE_NOCMD);
if (rc != 0 || expanded.we_wordc == 0) {
fprintf(stderr, "Failed to parse venvlauncher_command\n");
if (rc == 0) {
wordfree(&expanded);
}
exit(1);
}
const int capacity = (int) expanded.we_wordc;
char **args = malloc(capacity * sizeof(char *));
if (!args) {
fprintf(stderr, "allocation failed\n");
wordfree(&expanded);
exit(1);
}
int count = 0;
for (size_t i = 0; i < expanded.we_wordc; i++) {
args[count] = strdup(expanded.we_wordv[i]);
if (!args[count]) {
fprintf(stderr, "allocation failed\n");
for (int j = 0; j < count; j++) {
free(args[j]);
}
free(args);
wordfree(&expanded);
exit(1);
}
count++;
}
wordfree(&expanded);
assert(capacity == count);
*argc_out = count;
return args;
}
void find_pyvenv(char *pyvenv_cfg_path, size_t path_size) {
char executable_path[PATH_MAX];
uint32_t executable_path_size = sizeof(executable_path);
// Get current executable path (macOS)
if (_NSGetExecutablePath(executable_path, &executable_path_size) != 0) {
fprintf(stderr, "Failed to get executable path\n");
exit(1);
}
// First try search for the pyvenv on top level
char *dir_path = dirname(executable_path);
snprintf(pyvenv_cfg_path, path_size, "%s/pyvenv.cfg", dir_path);
debug("Searching for pyenv.cfg file in %s\n", pyvenv_cfg_path);
if (access(pyvenv_cfg_path, F_OK) != 0) {
// Try searching one level up
dir_path = dirname(dir_path);
snprintf(pyvenv_cfg_path, path_size, "%s/pyvenv.cfg", dir_path);
debug("Searching for pyenv.cfg file in %s\n", pyvenv_cfg_path);
if (access(pyvenv_cfg_path, F_OK) != 0) {
fprintf(stderr, "Error: pyvenv.cfg file not found at %s\n", pyvenv_cfg_path);
exit(1);
}
}
}
int main(int argc, char *argv[]) {
if (getenv("PYLAUNCHER_DEBUG") != NULL) {
debug_enabled = true;
}
debug("Original argv are:\n");
for (int i = 1; i < argc; i++) {
debug("argv[%d] = %s\n", i, argv[i]);
}
char pyvenv_cfg_path[PATH_MAX];
find_pyvenv(pyvenv_cfg_path, sizeof(pyvenv_cfg_path));
char *venv_command = get_pyenvcfg_command(pyvenv_cfg_path);
int venv_argc = 0;
char **venv_args = split_venv_command_into_args(venv_command, &venv_argc);
// Adds "--python.VenvlauncherCommand="
// + 2 for quotes
// + 1 for '\0'
size_t python_base_exec_size = strlen(venv_command) + strlen(GRAAL_PYTHON_BASE_EXE_ARG) + 2 + 1;
char python_base_exec_command[python_base_exec_size];
snprintf(python_base_exec_command, python_base_exec_size, "%s\"%s\"", GRAAL_PYTHON_BASE_EXE_ARG, venv_command);
// Adds "--python.Executable="
size_t python_exec_arg_size = strlen(GRAAL_PYTHON_EXE_ARG) + strlen(argv[0]) + 1;
char python_exec_command[python_exec_arg_size];
snprintf(python_exec_command,
python_exec_arg_size,
"%s%s",
GRAAL_PYTHON_EXE_ARG,
argv[0]);
// venv_args + "--python.VenvlauncherCommand=" + Adds "--python.Executable=" + rest of argc (-1 because we are not interested in argv[0]) + NULL
size_t args_size = venv_argc + 2 + (argc - 1) + 1;
char *args[args_size];
// copy venv_args
int k = 0;
for (k = 0; k < venv_argc; k++) {
args[k] = venv_args[k];
}
args[k++] = python_base_exec_command;
args[k++] = python_exec_command;
// copy rest of the args
for (int i = 1; i < argc; i++) {
args[k++] = argv[i];
}
args[k] = NULL;
debug("Final arguments to execv: \n");
for (int i = 0; i < k; i++) {
debug("arg[%d] = %s\n", i, args[i]);
}
execv(args[0], args);
perror("execv failed"); // only runs if execv fails
return 1;
}