blob: e2a87bfe4afeb8b6093e15ca750cd6219b7d13c6 [file] [log] [blame]
Guido van Rossum582646a1996-05-28 22:30:17 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum582646a1996-05-28 22:30:17 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum582646a1996-05-28 22:30:17 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum582646a1996-05-28 22:30:17 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum582646a1996-05-28 22:30:17 +000029
30******************************************************************/
31
32/* Return the initial module search path. */
33
Guido van Rossum667d7041995-08-04 04:20:48 +000034#include "Python.h"
35#include "osdefs.h"
36
Guido van Rossum305e5d01997-04-11 17:18:45 +000037#include <sys/types.h>
38#include <sys/stat.h>
39#include <strings.h>
Guido van Rossum667d7041995-08-04 04:20:48 +000040
Guido van Rossum305e5d01997-04-11 17:18:45 +000041#if HAVE_UNISTD_H
42#include <unistd.h>
43#endif /* HAVE_UNISTD_H */
44
45/* Search in some common locations for the associated Python libraries.
46 *
47 * Two directories must be found, the platform independent directory
Barry Warsaw90126031997-04-11 20:27:03 +000048 * (prefix), containing the common .py and .pyc files, and the platform
49 * dependent directory (exec_prefix), containing the shared library
50 * modules. Note that prefix and exec_prefix can be the same directory,
51 * but for some installations, they are different.
Guido van Rossum305e5d01997-04-11 17:18:45 +000052 *
Barry Warsaw90126031997-04-11 20:27:03 +000053 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
54 * Each search tries a number of different locations until a ``landmark''
55 * file or directory is found. If no prefix or exec_prefix is found, a
56 * warning message is issued and the preprocessor defined PREFIX and
57 * EXEC_PREFIX are used (even though they will not work); python carries on
58 * as best as is possible, but most imports will fail.
Guido van Rossum305e5d01997-04-11 17:18:45 +000059 *
60 * Before any searches are done, the location of the executable is
Barry Warsaw90126031997-04-11 20:27:03 +000061 * determined. If argv[0] has one or more slashs in it, it is used
62 * unchanged. Otherwise, it must have been invoked from the shell's path,
63 * so we search $PATH for the named executable and use that. If the
64 * executable was not found on $PATH (or there was no $PATH environment
65 * variable), the original argv[0] string is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000066 *
Barry Warsaw90126031997-04-11 20:27:03 +000067 * Next, the executable location is examined to see if it is a symbolic
68 * link. If so, the link is chased (correctly interpreting a relative
69 * pathname if one is found) and the directory of the link target is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000070 *
Barry Warsaw90126031997-04-11 20:27:03 +000071 * Finally, argv0_path is set to the directory containing the executable
72 * (i.e. the last component is stripped).
Guido van Rossum305e5d01997-04-11 17:18:45 +000073 *
Barry Warsaw90126031997-04-11 20:27:03 +000074 * With argv0_path in hand, we perform a number of steps. The same steps
75 * are performed for prefix and for exec_prefix, but with a different
76 * landmark.
Guido van Rossum305e5d01997-04-11 17:18:45 +000077 *
78 * Step 1. Are we running python out of the build directory? This is
79 * checked by looking for a different kind of landmark relative to
Barry Warsaw90126031997-04-11 20:27:03 +000080 * argv0_path. For prefix, the landmark's path is derived from the VPATH
81 * preprocessor variable (taking into account that its value is almost, but
82 * not quite, what we need). For exec_prefix, the landmark is
83 * Modules/Setup. If the landmark is found, we're done.
Guido van Rossum305e5d01997-04-11 17:18:45 +000084 *
85 * For the remaining steps, the prefix landmark will always be
86 * lib/python$VERSION/string.py and the exec_prefix will always be
Barry Warsaw90126031997-04-11 20:27:03 +000087 * lib/python$VERSION/sharedmodules, where $VERSION is Python's version
88 * number as supplied by the Makefile. Note that this means that no more
89 * build directory checking is performed; if the first step did not find
90 * the landmarks, the assumption is that python is running from an
91 * installed setup.
Guido van Rossum305e5d01997-04-11 17:18:45 +000092 *
93 * Step 2. See if the $PYTHONHOME environment variable points to the
Barry Warsaw90126031997-04-11 20:27:03 +000094 * installed location of the Python libraries. If $PYTHONHOME is set, then
95 * it points to prefix and exec_prefix. $PYTHONHOME can be a single
96 * directory, which is used for both, or the prefix and exec_prefix
97 * directories separated by a colon.
Guido van Rossum305e5d01997-04-11 17:18:45 +000098 *
99 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
Barry Warsaw90126031997-04-11 20:27:03 +0000100 * backtracking up the path until it is exhausted. This is the most common
101 * step to succeed. Note that if prefix and exec_prefix are different,
102 * exec_prefix is more likely to be found; however if exec_prefix is a
103 * subdirectory of prefix, both will be found.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000104 *
Barry Warsaw90126031997-04-11 20:27:03 +0000105 * Step 4. Search the directories pointed to by the preprocessor variables
106 * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be
107 * passed in as options to the configure script.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000108 *
Barry Warsaw90126031997-04-11 20:27:03 +0000109 * Step 5. Search some `standard' directories, namely: /usr/local, /usr,
110 * then finally /.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000111 *
Barry Warsaw90126031997-04-11 20:27:03 +0000112 * That's it!
113 *
114 * Well, almost. Once we have determined prefix and exec_prefix, the
115 * preprocesor variable PYTHONPATH is used to construct a path. Each
116 * relative path on PYTHONPATH is prefixed with prefix. Then the directory
117 * containing the shared library modules is appended. The environment
118 * variable $PYTHONPATH is inserted in front of it all. Finally, the
119 * prefix and exec_prefix globals are tweaked so they reflect the values
120 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
121 * off. If either points to the build directory, the globals are reset to
122 * the corresponding preprocessor variables (so sys.prefix will reflect the
123 * installation location, even though sys.path points into the build
124 * directory). This seems to make more sense given that currently the only
125 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
126 * process to find the installed Python tree.
127 */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000128
129#ifndef VERSION
130#define VERSION "1.5"
131#endif
132
133#ifndef VPATH
134#define VPATH "."
Guido van Rossum667d7041995-08-04 04:20:48 +0000135#endif
136
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000137#ifndef PREFIX
138#define PREFIX "/usr/local"
139#endif
140
141#ifndef EXEC_PREFIX
Guido van Rossum6e12d561996-07-30 20:36:12 +0000142#define EXEC_PREFIX PREFIX
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000143#endif
144
Guido van Rossum305e5d01997-04-11 17:18:45 +0000145#ifndef PYTHONPATH
146/* I know this isn't K&R C, but the Makefile specifies it anyway */
147#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
148 PREFIX "/lib/python" VERSION "/test" ":" \
149 EXEC_PREFIX "/lib/python" VERSION "/sharedmodules"
150#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000151
Guido van Rossum305e5d01997-04-11 17:18:45 +0000152#ifndef LANDMARK
153#define LANDMARK "string.py"
154#endif
155
156static char *std_dirs[] = {"/usr/local/", "/usr/", "/", NULL};
157
158static char prefix[MAXPATHLEN+1];
159static char exec_prefix[MAXPATHLEN+1];
160static char *module_search_path = NULL;
161static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
162
163static void
164reduce(dir)
165 char *dir;
166{
167 int i = strlen(dir);
168 while (i > 0 && dir[i] != SEP)
169 --i;
170 dir[i] = '\0';
171}
172
173
174static int
175exists(filename)
176 char *filename;
177{
178 struct stat buf;
179 return stat(filename, &buf) == 0;
180}
181
182
183static void
184join(buffer, stuff)
185 char *buffer;
186 char *stuff;
187{
188 int n, k;
189 if (stuff[0] == SEP)
190 n = 0;
191 else {
192 n = strlen(buffer);
193 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
194 buffer[n++] = SEP;
195 }
196 k = strlen(stuff);
197 if (n + k > MAXPATHLEN)
198 k = MAXPATHLEN - n;
199 strncpy(buffer+n, stuff, k);
200 buffer[n+k] = '\0';
201}
202
203
Guido van Rossum305e5d01997-04-11 17:18:45 +0000204static int
205search_for_prefix(argv0_path, home)
206 char *argv0_path;
207 char *home;
208{
209 int i, n;
210 char *vpath;
211
212 /* Check VPATH to see if argv0_path is in the build directory.
213 * Complication: the VPATH passed in is relative to the
214 * Modules build directory and points to the Modules source
215 * directory; we need it relative to the build tree and
216 * pointing to the source tree. Solution: chop off a leading
217 * ".." (but only if it's there -- it could be an absolute
218 * path) and chop off the final component (assuming it's
219 * "Modules").
220 */
221 vpath = VPATH;
222 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
223 vpath += 3;
224 strcpy(prefix, argv0_path);
225 join(prefix, vpath);
226 reduce(prefix);
227 join(prefix, "Lib");
228 join(prefix, LANDMARK);
229 if (exists(prefix))
230 return -1;
231
232 if (home) {
233 /* Check $PYTHONHOME */
234 char *delim;
235 strcpy(prefix, home);
236 delim = strchr(prefix, DELIM);
237 if (delim)
238 *delim = '\0';
239 join(prefix, lib_python);
240 join(prefix, LANDMARK);
241 if (exists(prefix))
242 return 1;
243 }
244
245 /* Search from argv0_path, until root is found */
246 strcpy(prefix, argv0_path);
247 do {
248 n = strlen(prefix);
249 join(prefix, lib_python);
250 join(prefix, LANDMARK);
251 if (exists(prefix))
252 return 1;
253 prefix[n] = '\0';
254 reduce(prefix);
255 } while (prefix[0]);
256
257 /* Look at configure's PREFIX */
258 strcpy(prefix, PREFIX);
259 join(prefix, lib_python);
260 join(prefix, LANDMARK);
261 if (exists(prefix))
262 return 1;
263
264 /* Look at `standard' directories */
265 for (i = 0; std_dirs[i]; i++) {
266 strcpy(prefix, std_dirs[i]);
267 join(prefix, lib_python);
268 join(prefix, LANDMARK);
269 if (exists(prefix))
270 return 1;
271 }
272 return 0;
273}
274
275
276static int
277search_for_exec_prefix(argv0_path, home)
278 char *argv0_path;
279 char *home;
280{
281 int i, n;
282
283 /* Check to see if argv[0] is in the build directory */
284 strcpy(exec_prefix, argv0_path);
285 join(exec_prefix, "Modules/Setup");
286 if (exists(exec_prefix)) {
287 reduce(exec_prefix);
288 return -1;
289 }
290
291 if (home) {
292 /* Check $PYTHONHOME */
293 char *delim;
294 delim = strchr(home, DELIM);
295 if (delim)
296 strcpy(exec_prefix, delim+1);
297 else
298 strcpy(exec_prefix, home);
299 join(exec_prefix, lib_python);
300 join(exec_prefix, "sharedmodules");
301 if (exists(exec_prefix))
302 return 1;
303 }
304
305 /* Search from argv0_path, until root is found */
306 strcpy(exec_prefix, argv0_path);
307 do {
308 n = strlen(exec_prefix);
309 join(exec_prefix, lib_python);
310 join(exec_prefix, "sharedmodules");
311 if (exists(exec_prefix))
312 return 1;
313 exec_prefix[n] = '\0';
314 reduce(exec_prefix);
315 } while (exec_prefix[0]);
316
317 /* Look at configure's EXEC_PREFIX */
318 strcpy(exec_prefix, EXEC_PREFIX);
319 join(exec_prefix, lib_python);
320 join(exec_prefix, "sharedmodules");
321 if (exists(exec_prefix))
322 return 1;
323
324 /* Look at `standard' directories */
325 for (i = 0; std_dirs[i]; i++) {
326 strcpy(exec_prefix, std_dirs[i]);
327 join(exec_prefix, lib_python);
328 join(exec_prefix, "sharedmodules");
329 if (exists(exec_prefix))
330 return 1;
331 }
332 return 0;
333}
334
335
336static void
337calculate_path()
338{
339 extern char *Py_GetProgramName();
340
341 char delimiter[2] = {DELIM, '\0'};
342 char separator[2] = {SEP, '\0'};
343 char *pythonpath = PYTHONPATH;
344 char *rtpypath = getenv("PYTHONPATH");
345 char *home = getenv("PYTHONHOME");
346 char *path = getenv("PATH");
347 char *prog = Py_GetProgramName();
348 char argv0_path[MAXPATHLEN+1];
349 char progpath[MAXPATHLEN+1];
350 int pfound, efound; /* 1 if found; -1 if found build directory */
351 char *buf;
352 int bufsz;
353 int prefixsz;
354 char *defpath = pythonpath;
355
356 /* Initialize this dynamically for K&R C */
357 sprintf(lib_python, "lib/python%s", VERSION);
358
359 /* If there is no slash in the argv0 path, then we have to
360 * assume python is on the user's $PATH, since there's no
361 * other way to find a directory to start the search from. If
362 * $PATH isn't exported, you lose.
363 */
364 if (strchr(prog, SEP))
365 strcpy(progpath, prog);
366 else if (path) {
367 while (1) {
368 char *delim = strchr(path, DELIM);
369
370 if (delim) {
371 int len = delim - path;
372 strncpy(progpath, path, len);
373 *(progpath + len) = '\0';
374 }
375 else
376 strcpy(progpath, path);
377
378 join(progpath, prog);
379 if (exists(progpath))
380 break;
381
382 if (!delim) {
383 progpath[0] = '\0';
384 break;
385 }
386 path = delim + 1;
387 }
388 }
389 else
390 progpath[0] = '\0';
391
392 strcpy(argv0_path, progpath);
393
394#if HAVE_READLINK
395 {
396 char tmpbuffer[MAXPATHLEN+1];
397 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
398 if (linklen != -1) {
399 /* It's not null terminated! */
400 tmpbuffer[linklen] = '\0';
401 if (tmpbuffer[0] == SEP)
402 strcpy(argv0_path, tmpbuffer);
403 else {
404 /* Interpret relative to progpath */
405 reduce(argv0_path);
406 join(argv0_path, tmpbuffer);
407 }
408 }
409 }
410#endif /* HAVE_READLINK */
411
412 reduce(argv0_path);
413
414 if (!(pfound = search_for_prefix(argv0_path, home))) {
415 fprintf(stderr,
416 "Could not find platform independent libraries <prefix>\n");
417 strcpy(prefix, PREFIX);
418 join(prefix, lib_python);
419 }
420 else
421 reduce(prefix);
422
423 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
424 fprintf(stderr,
425 "Could not find platform dependent libraries <exec_prefix>\n");
426 strcpy(exec_prefix, EXEC_PREFIX);
427 join(exec_prefix, "lib/sharedmodules");
428 }
429 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
430
431 if (!pfound || !efound)
432 fprintf(stderr,
433 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
434
435 /* Calculate size of return buffer.
436 */
437 bufsz = 0;
438
439 if (rtpypath)
440 bufsz += strlen(rtpypath) + 1;
441
442 prefixsz = strlen(prefix) + 1;
443
444 while (1) {
445 char *delim = strchr(defpath, DELIM);
446
447 if (defpath[0] != SEP)
448 /* Paths are relative to prefix */
449 bufsz += prefixsz;
450
451 if (delim)
452 bufsz += delim - defpath + 1;
453 else {
454 bufsz += strlen(defpath) + 1;
455 break;
456 }
457 defpath = delim + 1;
458 }
459
460 bufsz += strlen(exec_prefix) + 1;
461
462 /* This is the only malloc call in this file */
463 buf = malloc(bufsz);
464
465 if (buf == NULL) {
466 /* We can't exit, so print a warning and limp along */
467 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
468 fprintf(stderr, "Using default static PYTHONPATH.\n");
469 module_search_path = PYTHONPATH;
470 }
471 else {
472 /* Run-time value of $PYTHONPATH goes first */
473 if (rtpypath) {
474 strcpy(buf, rtpypath);
475 strcat(buf, delimiter);
476 }
477
478 /* Next goes merge of compile-time $PYTHONPATH with
479 * dynamically located prefix.
480 */
481 defpath = pythonpath;
482 while (1) {
483 char *delim = strchr(defpath, DELIM);
484
485 if (defpath[0] != SEP) {
486 strcat(buf, prefix);
487 strcat(buf, separator);
488 }
489
490 if (delim) {
491 int len = delim - defpath + 1;
492 int end = strlen(buf) + len;
493 strncat(buf, defpath, len);
494 *(buf + end) = '\0';
495 }
496 else {
497 strcat(buf, defpath);
498 break;
499 }
500 defpath = delim + 1;
501 }
502 strcat(buf, delimiter);
503
504 /* Finally, on goes the directory for dynamic-load modules */
505 strcat(buf, exec_prefix);
506
507 /* And publish the results */
508 module_search_path = buf;
509 }
510
511 /* Reduce prefix and exec_prefix to their essence,
512 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
513 * If we're loading relative to the build directory,
514 * return the compiled-in defaults instead.
515 */
516 if (pfound > 0) {
517 reduce(prefix);
518 reduce(prefix);
519 }
520 else
521 strcpy(prefix, PREFIX);
522
523 if (efound > 0) {
524 reduce(exec_prefix);
525 reduce(exec_prefix);
526 reduce(exec_prefix);
527 }
528 else
529 strcpy(exec_prefix, EXEC_PREFIX);
530}
531
532
533/* External interface */
Guido van Rossum667d7041995-08-04 04:20:48 +0000534
535char *
Guido van Rossum582646a1996-05-28 22:30:17 +0000536Py_GetPath()
Guido van Rossum667d7041995-08-04 04:20:48 +0000537{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000538 if (!module_search_path)
539 calculate_path();
540 return module_search_path;
Guido van Rossum667d7041995-08-04 04:20:48 +0000541}
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000542
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000543char *
544Py_GetPrefix()
545{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000546 if (!module_search_path)
547 calculate_path();
548 return prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000549}
550
551char *
552Py_GetExecPrefix()
553{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000554 if (!module_search_path)
555 calculate_path();
556 return exec_prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000557}