blob: fccd79f3aefcc20cd34b0739c6dbbe83bb630ef9 [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 * That's it!
110 *
111 * Well, almost. Once we have determined prefix and exec_prefix, the
112 * preprocesor variable PYTHONPATH is used to construct a path. Each
113 * relative path on PYTHONPATH is prefixed with prefix. Then the directory
114 * containing the shared library modules is appended. The environment
115 * variable $PYTHONPATH is inserted in front of it all. Finally, the
116 * prefix and exec_prefix globals are tweaked so they reflect the values
117 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
118 * off. If either points to the build directory, the globals are reset to
119 * the corresponding preprocessor variables (so sys.prefix will reflect the
120 * installation location, even though sys.path points into the build
121 * directory). This seems to make more sense given that currently the only
122 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
123 * process to find the installed Python tree.
124 */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000125
126#ifndef VERSION
127#define VERSION "1.5"
128#endif
129
130#ifndef VPATH
131#define VPATH "."
Guido van Rossum667d7041995-08-04 04:20:48 +0000132#endif
133
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000134#ifndef PREFIX
135#define PREFIX "/usr/local"
136#endif
137
138#ifndef EXEC_PREFIX
Guido van Rossum6e12d561996-07-30 20:36:12 +0000139#define EXEC_PREFIX PREFIX
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000140#endif
141
Guido van Rossum305e5d01997-04-11 17:18:45 +0000142#ifndef PYTHONPATH
143/* I know this isn't K&R C, but the Makefile specifies it anyway */
144#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
145 PREFIX "/lib/python" VERSION "/test" ":" \
146 EXEC_PREFIX "/lib/python" VERSION "/sharedmodules"
147#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000148
Guido van Rossum305e5d01997-04-11 17:18:45 +0000149#ifndef LANDMARK
150#define LANDMARK "string.py"
151#endif
152
Guido van Rossum305e5d01997-04-11 17:18:45 +0000153static char prefix[MAXPATHLEN+1];
154static char exec_prefix[MAXPATHLEN+1];
155static char *module_search_path = NULL;
156static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
157
158static void
159reduce(dir)
160 char *dir;
161{
162 int i = strlen(dir);
163 while (i > 0 && dir[i] != SEP)
164 --i;
165 dir[i] = '\0';
166}
167
168
169static int
170exists(filename)
171 char *filename;
172{
173 struct stat buf;
174 return stat(filename, &buf) == 0;
175}
176
177
178static void
179join(buffer, stuff)
180 char *buffer;
181 char *stuff;
182{
183 int n, k;
184 if (stuff[0] == SEP)
185 n = 0;
186 else {
187 n = strlen(buffer);
188 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
189 buffer[n++] = SEP;
190 }
191 k = strlen(stuff);
192 if (n + k > MAXPATHLEN)
193 k = MAXPATHLEN - n;
194 strncpy(buffer+n, stuff, k);
195 buffer[n+k] = '\0';
196}
197
198
Guido van Rossum305e5d01997-04-11 17:18:45 +0000199static int
200search_for_prefix(argv0_path, home)
201 char *argv0_path;
202 char *home;
203{
204 int i, n;
205 char *vpath;
206
Guido van Rossum573a24a1997-05-12 20:49:39 +0000207 /* Check to see if argv[0] is in the build directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000208 strcpy(prefix, argv0_path);
Guido van Rossum573a24a1997-05-12 20:49:39 +0000209 join(prefix, "Modules/Setup");
210 if (exists(prefix)) {
211 /* Check VPATH to see if argv0_path is in the build directory.
212 * Complication: the VPATH passed in is relative to the
213 * Modules build directory and points to the Modules source
214 * directory; we need it relative to the build tree and
215 * pointing to the source tree. Solution: chop off a leading
216 * ".." (but only if it's there -- it could be an absolute
217 * path) and chop off the final component (assuming it's
218 * "Modules").
219 */
220 vpath = VPATH;
221 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
222 vpath += 3;
223 strcpy(prefix, argv0_path);
224 join(prefix, vpath);
225 reduce(prefix);
226 join(prefix, "Lib");
227 join(prefix, LANDMARK);
228 if (exists(prefix))
229 return -1;
230 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000231
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
Guido van Rossumd7763621997-05-12 20:53:23 +0000264 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000265 return 0;
266}
267
268
269static int
270search_for_exec_prefix(argv0_path, home)
271 char *argv0_path;
272 char *home;
273{
274 int i, n;
275
276 /* Check to see if argv[0] is in the build directory */
277 strcpy(exec_prefix, argv0_path);
278 join(exec_prefix, "Modules/Setup");
279 if (exists(exec_prefix)) {
280 reduce(exec_prefix);
281 return -1;
282 }
283
284 if (home) {
285 /* Check $PYTHONHOME */
286 char *delim;
287 delim = strchr(home, DELIM);
288 if (delim)
289 strcpy(exec_prefix, delim+1);
290 else
291 strcpy(exec_prefix, home);
292 join(exec_prefix, lib_python);
293 join(exec_prefix, "sharedmodules");
294 if (exists(exec_prefix))
295 return 1;
296 }
297
298 /* Search from argv0_path, until root is found */
299 strcpy(exec_prefix, argv0_path);
300 do {
301 n = strlen(exec_prefix);
302 join(exec_prefix, lib_python);
303 join(exec_prefix, "sharedmodules");
304 if (exists(exec_prefix))
305 return 1;
306 exec_prefix[n] = '\0';
307 reduce(exec_prefix);
308 } while (exec_prefix[0]);
309
310 /* Look at configure's EXEC_PREFIX */
311 strcpy(exec_prefix, EXEC_PREFIX);
312 join(exec_prefix, lib_python);
313 join(exec_prefix, "sharedmodules");
314 if (exists(exec_prefix))
315 return 1;
316
Guido van Rossumd7763621997-05-12 20:53:23 +0000317 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000318 return 0;
319}
320
321
322static void
323calculate_path()
324{
325 extern char *Py_GetProgramName();
326
327 char delimiter[2] = {DELIM, '\0'};
328 char separator[2] = {SEP, '\0'};
329 char *pythonpath = PYTHONPATH;
330 char *rtpypath = getenv("PYTHONPATH");
331 char *home = getenv("PYTHONHOME");
332 char *path = getenv("PATH");
333 char *prog = Py_GetProgramName();
334 char argv0_path[MAXPATHLEN+1];
335 char progpath[MAXPATHLEN+1];
336 int pfound, efound; /* 1 if found; -1 if found build directory */
337 char *buf;
338 int bufsz;
339 int prefixsz;
340 char *defpath = pythonpath;
341
342 /* Initialize this dynamically for K&R C */
343 sprintf(lib_python, "lib/python%s", VERSION);
344
345 /* If there is no slash in the argv0 path, then we have to
346 * assume python is on the user's $PATH, since there's no
347 * other way to find a directory to start the search from. If
348 * $PATH isn't exported, you lose.
349 */
350 if (strchr(prog, SEP))
351 strcpy(progpath, prog);
352 else if (path) {
353 while (1) {
354 char *delim = strchr(path, DELIM);
355
356 if (delim) {
357 int len = delim - path;
358 strncpy(progpath, path, len);
359 *(progpath + len) = '\0';
360 }
361 else
362 strcpy(progpath, path);
363
364 join(progpath, prog);
365 if (exists(progpath))
366 break;
367
368 if (!delim) {
369 progpath[0] = '\0';
370 break;
371 }
372 path = delim + 1;
373 }
374 }
375 else
376 progpath[0] = '\0';
377
378 strcpy(argv0_path, progpath);
379
380#if HAVE_READLINK
381 {
382 char tmpbuffer[MAXPATHLEN+1];
383 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
384 if (linklen != -1) {
385 /* It's not null terminated! */
386 tmpbuffer[linklen] = '\0';
387 if (tmpbuffer[0] == SEP)
388 strcpy(argv0_path, tmpbuffer);
389 else {
390 /* Interpret relative to progpath */
391 reduce(argv0_path);
392 join(argv0_path, tmpbuffer);
393 }
394 }
395 }
396#endif /* HAVE_READLINK */
397
398 reduce(argv0_path);
399
400 if (!(pfound = search_for_prefix(argv0_path, home))) {
401 fprintf(stderr,
402 "Could not find platform independent libraries <prefix>\n");
403 strcpy(prefix, PREFIX);
404 join(prefix, lib_python);
405 }
406 else
407 reduce(prefix);
408
409 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
410 fprintf(stderr,
411 "Could not find platform dependent libraries <exec_prefix>\n");
412 strcpy(exec_prefix, EXEC_PREFIX);
413 join(exec_prefix, "lib/sharedmodules");
414 }
415 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
416
417 if (!pfound || !efound)
418 fprintf(stderr,
419 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
420
421 /* Calculate size of return buffer.
422 */
423 bufsz = 0;
424
425 if (rtpypath)
426 bufsz += strlen(rtpypath) + 1;
427
428 prefixsz = strlen(prefix) + 1;
429
430 while (1) {
431 char *delim = strchr(defpath, DELIM);
432
433 if (defpath[0] != SEP)
434 /* Paths are relative to prefix */
435 bufsz += prefixsz;
436
437 if (delim)
438 bufsz += delim - defpath + 1;
439 else {
440 bufsz += strlen(defpath) + 1;
441 break;
442 }
443 defpath = delim + 1;
444 }
445
446 bufsz += strlen(exec_prefix) + 1;
447
448 /* This is the only malloc call in this file */
449 buf = malloc(bufsz);
450
451 if (buf == NULL) {
452 /* We can't exit, so print a warning and limp along */
453 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
454 fprintf(stderr, "Using default static PYTHONPATH.\n");
455 module_search_path = PYTHONPATH;
456 }
457 else {
458 /* Run-time value of $PYTHONPATH goes first */
459 if (rtpypath) {
460 strcpy(buf, rtpypath);
461 strcat(buf, delimiter);
462 }
Guido van Rossum573a24a1997-05-12 20:49:39 +0000463 else
464 buf[0] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000465
466 /* Next goes merge of compile-time $PYTHONPATH with
467 * dynamically located prefix.
468 */
469 defpath = pythonpath;
470 while (1) {
471 char *delim = strchr(defpath, DELIM);
472
473 if (defpath[0] != SEP) {
474 strcat(buf, prefix);
475 strcat(buf, separator);
476 }
477
478 if (delim) {
479 int len = delim - defpath + 1;
480 int end = strlen(buf) + len;
481 strncat(buf, defpath, len);
482 *(buf + end) = '\0';
483 }
484 else {
485 strcat(buf, defpath);
486 break;
487 }
488 defpath = delim + 1;
489 }
490 strcat(buf, delimiter);
491
492 /* Finally, on goes the directory for dynamic-load modules */
493 strcat(buf, exec_prefix);
494
495 /* And publish the results */
496 module_search_path = buf;
497 }
498
499 /* Reduce prefix and exec_prefix to their essence,
500 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
501 * If we're loading relative to the build directory,
502 * return the compiled-in defaults instead.
503 */
504 if (pfound > 0) {
505 reduce(prefix);
506 reduce(prefix);
507 }
508 else
509 strcpy(prefix, PREFIX);
510
511 if (efound > 0) {
512 reduce(exec_prefix);
513 reduce(exec_prefix);
514 reduce(exec_prefix);
515 }
516 else
517 strcpy(exec_prefix, EXEC_PREFIX);
518}
519
520
521/* External interface */
Guido van Rossum667d7041995-08-04 04:20:48 +0000522
523char *
Guido van Rossum582646a1996-05-28 22:30:17 +0000524Py_GetPath()
Guido van Rossum667d7041995-08-04 04:20:48 +0000525{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000526 if (!module_search_path)
527 calculate_path();
528 return module_search_path;
Guido van Rossum667d7041995-08-04 04:20:48 +0000529}
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000530
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000531char *
532Py_GetPrefix()
533{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000534 if (!module_search_path)
535 calculate_path();
536 return prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000537}
538
539char *
540Py_GetExecPrefix()
541{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000542 if (!module_search_path)
543 calculate_path();
544 return exec_prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000545}