blob: 8dcf14ef95414fbddd41613b0bfa19491c02c617 [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
48 * (prefix), containing the common .py and .pyc files, and the
49 * platform dependent directory (exec_prefix), containing the shared
50 * library modules. Note that prefix and exec_prefix can be the same
51 * directory, but for some installations, they are different.
52 *
53 * Py_GetPath() carries out separate searches for prefix and
54 * exec_prefix. Each search tries a number of different locations
55 * until a ``landmark'' file or directory is found. If no prefix or
56 * exec_prefix is found, a warning message is issued and the
57 * preprocessor defines PREFIX and EXEC_PREFIX are used (even though
58 * they will not work); python carries on as best as is possible, but
59 * most imports will fail.
60 *
61 * Before any searches are done, the location of the executable is
62 * determined. If argv[0] has a slash in it (or more), this is it;
63 * otherwise, it must have been invoked from the shell's path, so we
64 * search $PATH for the named executable and use that. If the
65 * executable was not found on $PATH (or there was no $PATH
66 * environment variable), the original argv[0] string is used.
67 *
68 * Next, either the executable location is examined to see if it is a
69 * symbolic link. If so, the link is chased (correctly interpreting a
70 * relative pathname if one is found) and the directory of the link
71 * target is used as instead.
72 *
73 * Finally, argv0_path is set to the directory containing the
74 * executable (i.e. the last component is stripped).
75 *
76 * With argv0_path in hand, we perform a number of steps. The same
77 * steps are performed for prefix and for exec_prefix, but with a
78 * different landmark.
79 *
80 * Step 1. Are we running python out of the build directory? This is
81 * checked by looking for a different kind of landmark relative to
82 * argv0_path. For prefix, the landmark's path is derived from the
83 * VPATH preprocessor variable (taking into account that its value is
84 * almost, but not quite, what we need). If the landmark is found,
85 * we're done.
86 *
87 * For the remaining steps, the prefix landmark will always be
88 * lib/python$VERSION/string.py and the exec_prefix will always be
89 * lib/python$VERSION/sharedmodules, where $VERSION is Python's
90 * version number as supplied by the Makefile. Note that this means
91 * that no more build directory checking is performed; if the first
92 * two steps did not find the landmarks, the assumption is that python
93 * is running from an installed setup.
94 *
95 * Step 2. See if the $PYTHONHOME environment variable points to the
96 * installed location of the Python libraries. If $PYTHONHOME is set,
97 * then it points to prefix and exec_prefix. $PYTHONHOME can be a
98 * single directory, which is used for both, or the prefix and
99 * exec_prefix directories separated by a colon.
100 *
101 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
102 * backtracking up the path until it is exhausted. This is the most
103 * common step to succeed. Note that if prefix and exec_prefix are
104 * different, exec_prefix is more likely to be found; however if
105 * exec_prefix is a subdirectory of prefix, both will be found.
106 *
107 * Step 4. Search the directories pointed to by the preprocessor
108 * variables PREFIX and EXEC_PREFIX. These are supplied by the
109 * Makefile but can be passed in as options to the configure script.
110 *
111 * Step 5. Search some `standard' directories, namely: /usr/local,
112 * /usr, then finally /.
113 *
114 * That's it! Well, almost. Once we have determined prefix and
115 * exec_prefix, the preprocesor variable PYTHONPATH is used to
116 * construct a path. Each relative path on PYTHONPATH is prefixed
117 * with prefix. Then the directory containing the shared library
118 * modules is appended. The environment variable $PYTHONPATH is
119 * inserted in front of it all. Finally, the prefix and exec_prefix
120 * globals are tweaked so the reflect the values expected by other
121 * code, by stripping the "lib/python$VERSION/..." stuff off. If
122 * either points to the build directory, the globals are reset to the
123 * corresponding preprocessor variables (so sys.prefix will reflect
124 * the installation location, even though sys.path points into the
125 * build directory). This seems to make more sense given that
126 * currently the only known use of sys.prefix and sys.exec_prefix is
127 * for the ILU installation process to find the installed Python tree. */
128
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
204/* Make sure path ends in a slash.
205 * Assume path is MAXPATHLEN+1 sized buffer.
206 */
207static void
208string_addsep(path)
209 char *path;
210{
211 int len = strlen(path);
212
213 if (path[len-1] != SEP) {
214 path[len] = SEP;
215 if (len < MAXPATHLEN)
216 len++;
217 path[len] = '\0';
218 }
219}
220
221
222/* Append a source string onto the destination string, watching for
223 * buffer overruns. Assumes dest is MAXPATHLEN+1 sized buffer.
224 */
225static void
226string_append(dest, source)
227 char *dest;
228 char *source;
229{
230 int dlen = strlen(dest);
231 int slen = strlen(source);
232 int len = dlen + slen;
233
234 strncat(dest, source, MAXPATHLEN - dlen);
235 if (len < MAXPATHLEN)
236 len++;
237 dest[len] = '\0';
238}
239
240
241static int
242search_for_prefix(argv0_path, home)
243 char *argv0_path;
244 char *home;
245{
246 int i, n;
247 char *vpath;
248
249 /* Check VPATH to see if argv0_path is in the build directory.
250 * Complication: the VPATH passed in is relative to the
251 * Modules build directory and points to the Modules source
252 * directory; we need it relative to the build tree and
253 * pointing to the source tree. Solution: chop off a leading
254 * ".." (but only if it's there -- it could be an absolute
255 * path) and chop off the final component (assuming it's
256 * "Modules").
257 */
258 vpath = VPATH;
259 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
260 vpath += 3;
261 strcpy(prefix, argv0_path);
262 join(prefix, vpath);
263 reduce(prefix);
264 join(prefix, "Lib");
265 join(prefix, LANDMARK);
266 if (exists(prefix))
267 return -1;
268
269 if (home) {
270 /* Check $PYTHONHOME */
271 char *delim;
272 strcpy(prefix, home);
273 delim = strchr(prefix, DELIM);
274 if (delim)
275 *delim = '\0';
276 join(prefix, lib_python);
277 join(prefix, LANDMARK);
278 if (exists(prefix))
279 return 1;
280 }
281
282 /* Search from argv0_path, until root is found */
283 strcpy(prefix, argv0_path);
284 do {
285 n = strlen(prefix);
286 join(prefix, lib_python);
287 join(prefix, LANDMARK);
288 if (exists(prefix))
289 return 1;
290 prefix[n] = '\0';
291 reduce(prefix);
292 } while (prefix[0]);
293
294 /* Look at configure's PREFIX */
295 strcpy(prefix, PREFIX);
296 join(prefix, lib_python);
297 join(prefix, LANDMARK);
298 if (exists(prefix))
299 return 1;
300
301 /* Look at `standard' directories */
302 for (i = 0; std_dirs[i]; i++) {
303 strcpy(prefix, std_dirs[i]);
304 join(prefix, lib_python);
305 join(prefix, LANDMARK);
306 if (exists(prefix))
307 return 1;
308 }
309 return 0;
310}
311
312
313static int
314search_for_exec_prefix(argv0_path, home)
315 char *argv0_path;
316 char *home;
317{
318 int i, n;
319
320 /* Check to see if argv[0] is in the build directory */
321 strcpy(exec_prefix, argv0_path);
322 join(exec_prefix, "Modules/Setup");
323 if (exists(exec_prefix)) {
324 reduce(exec_prefix);
325 return -1;
326 }
327
328 if (home) {
329 /* Check $PYTHONHOME */
330 char *delim;
331 delim = strchr(home, DELIM);
332 if (delim)
333 strcpy(exec_prefix, delim+1);
334 else
335 strcpy(exec_prefix, home);
336 join(exec_prefix, lib_python);
337 join(exec_prefix, "sharedmodules");
338 if (exists(exec_prefix))
339 return 1;
340 }
341
342 /* Search from argv0_path, until root is found */
343 strcpy(exec_prefix, argv0_path);
344 do {
345 n = strlen(exec_prefix);
346 join(exec_prefix, lib_python);
347 join(exec_prefix, "sharedmodules");
348 if (exists(exec_prefix))
349 return 1;
350 exec_prefix[n] = '\0';
351 reduce(exec_prefix);
352 } while (exec_prefix[0]);
353
354 /* Look at configure's EXEC_PREFIX */
355 strcpy(exec_prefix, EXEC_PREFIX);
356 join(exec_prefix, lib_python);
357 join(exec_prefix, "sharedmodules");
358 if (exists(exec_prefix))
359 return 1;
360
361 /* Look at `standard' directories */
362 for (i = 0; std_dirs[i]; i++) {
363 strcpy(exec_prefix, std_dirs[i]);
364 join(exec_prefix, lib_python);
365 join(exec_prefix, "sharedmodules");
366 if (exists(exec_prefix))
367 return 1;
368 }
369 return 0;
370}
371
372
373static void
374calculate_path()
375{
376 extern char *Py_GetProgramName();
377
378 char delimiter[2] = {DELIM, '\0'};
379 char separator[2] = {SEP, '\0'};
380 char *pythonpath = PYTHONPATH;
381 char *rtpypath = getenv("PYTHONPATH");
382 char *home = getenv("PYTHONHOME");
383 char *path = getenv("PATH");
384 char *prog = Py_GetProgramName();
385 char argv0_path[MAXPATHLEN+1];
386 char progpath[MAXPATHLEN+1];
387 int pfound, efound; /* 1 if found; -1 if found build directory */
388 char *buf;
389 int bufsz;
390 int prefixsz;
391 char *defpath = pythonpath;
392
393 /* Initialize this dynamically for K&R C */
394 sprintf(lib_python, "lib/python%s", VERSION);
395
396 /* If there is no slash in the argv0 path, then we have to
397 * assume python is on the user's $PATH, since there's no
398 * other way to find a directory to start the search from. If
399 * $PATH isn't exported, you lose.
400 */
401 if (strchr(prog, SEP))
402 strcpy(progpath, prog);
403 else if (path) {
404 while (1) {
405 char *delim = strchr(path, DELIM);
406
407 if (delim) {
408 int len = delim - path;
409 strncpy(progpath, path, len);
410 *(progpath + len) = '\0';
411 }
412 else
413 strcpy(progpath, path);
414
415 join(progpath, prog);
416 if (exists(progpath))
417 break;
418
419 if (!delim) {
420 progpath[0] = '\0';
421 break;
422 }
423 path = delim + 1;
424 }
425 }
426 else
427 progpath[0] = '\0';
428
429 strcpy(argv0_path, progpath);
430
431#if HAVE_READLINK
432 {
433 char tmpbuffer[MAXPATHLEN+1];
434 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
435 if (linklen != -1) {
436 /* It's not null terminated! */
437 tmpbuffer[linklen] = '\0';
438 if (tmpbuffer[0] == SEP)
439 strcpy(argv0_path, tmpbuffer);
440 else {
441 /* Interpret relative to progpath */
442 reduce(argv0_path);
443 join(argv0_path, tmpbuffer);
444 }
445 }
446 }
447#endif /* HAVE_READLINK */
448
449 reduce(argv0_path);
450
451 if (!(pfound = search_for_prefix(argv0_path, home))) {
452 fprintf(stderr,
453 "Could not find platform independent libraries <prefix>\n");
454 strcpy(prefix, PREFIX);
455 join(prefix, lib_python);
456 }
457 else
458 reduce(prefix);
459
460 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
461 fprintf(stderr,
462 "Could not find platform dependent libraries <exec_prefix>\n");
463 strcpy(exec_prefix, EXEC_PREFIX);
464 join(exec_prefix, "lib/sharedmodules");
465 }
466 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
467
468 if (!pfound || !efound)
469 fprintf(stderr,
470 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
471
472 /* Calculate size of return buffer.
473 */
474 bufsz = 0;
475
476 if (rtpypath)
477 bufsz += strlen(rtpypath) + 1;
478
479 prefixsz = strlen(prefix) + 1;
480
481 while (1) {
482 char *delim = strchr(defpath, DELIM);
483
484 if (defpath[0] != SEP)
485 /* Paths are relative to prefix */
486 bufsz += prefixsz;
487
488 if (delim)
489 bufsz += delim - defpath + 1;
490 else {
491 bufsz += strlen(defpath) + 1;
492 break;
493 }
494 defpath = delim + 1;
495 }
496
497 bufsz += strlen(exec_prefix) + 1;
498
499 /* This is the only malloc call in this file */
500 buf = malloc(bufsz);
501
502 if (buf == NULL) {
503 /* We can't exit, so print a warning and limp along */
504 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
505 fprintf(stderr, "Using default static PYTHONPATH.\n");
506 module_search_path = PYTHONPATH;
507 }
508 else {
509 /* Run-time value of $PYTHONPATH goes first */
510 if (rtpypath) {
511 strcpy(buf, rtpypath);
512 strcat(buf, delimiter);
513 }
514
515 /* Next goes merge of compile-time $PYTHONPATH with
516 * dynamically located prefix.
517 */
518 defpath = pythonpath;
519 while (1) {
520 char *delim = strchr(defpath, DELIM);
521
522 if (defpath[0] != SEP) {
523 strcat(buf, prefix);
524 strcat(buf, separator);
525 }
526
527 if (delim) {
528 int len = delim - defpath + 1;
529 int end = strlen(buf) + len;
530 strncat(buf, defpath, len);
531 *(buf + end) = '\0';
532 }
533 else {
534 strcat(buf, defpath);
535 break;
536 }
537 defpath = delim + 1;
538 }
539 strcat(buf, delimiter);
540
541 /* Finally, on goes the directory for dynamic-load modules */
542 strcat(buf, exec_prefix);
543
544 /* And publish the results */
545 module_search_path = buf;
546 }
547
548 /* Reduce prefix and exec_prefix to their essence,
549 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
550 * If we're loading relative to the build directory,
551 * return the compiled-in defaults instead.
552 */
553 if (pfound > 0) {
554 reduce(prefix);
555 reduce(prefix);
556 }
557 else
558 strcpy(prefix, PREFIX);
559
560 if (efound > 0) {
561 reduce(exec_prefix);
562 reduce(exec_prefix);
563 reduce(exec_prefix);
564 }
565 else
566 strcpy(exec_prefix, EXEC_PREFIX);
567}
568
569
570/* External interface */
Guido van Rossum667d7041995-08-04 04:20:48 +0000571
572char *
Guido van Rossum582646a1996-05-28 22:30:17 +0000573Py_GetPath()
Guido van Rossum667d7041995-08-04 04:20:48 +0000574{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000575 if (!module_search_path)
576 calculate_path();
577 return module_search_path;
Guido van Rossum667d7041995-08-04 04:20:48 +0000578}
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000579
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000580char *
581Py_GetPrefix()
582{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000583 if (!module_search_path)
584 calculate_path();
585 return prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000586}
587
588char *
589Py_GetExecPrefix()
590{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000591 if (!module_search_path)
592 calculate_path();
593 return exec_prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000594}