Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 4 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 5 | All rights reserved. |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame] | 7 | See the file "Misc/COPYRIGHT" for information on usage and |
| 8 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 9 | ******************************************************************/ |
| 10 | |
| 11 | /* Return the initial module search path. */ |
| 12 | |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 13 | #include "Python.h" |
| 14 | #include "osdefs.h" |
| 15 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
Guido van Rossum | 21f8497 | 1997-06-02 22:18:31 +0000 | [diff] [blame] | 18 | #include <string.h> |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 19 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 20 | #if HAVE_UNISTD_H |
| 21 | #include <unistd.h> |
| 22 | #endif /* HAVE_UNISTD_H */ |
| 23 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 24 | #ifdef WITH_NEXT_FRAMEWORK |
| 25 | #include <mach-o/dyld.h> |
| 26 | #endif |
| 27 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 28 | /* Search in some common locations for the associated Python libraries. |
| 29 | * |
| 30 | * Two directories must be found, the platform independent directory |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 31 | * (prefix), containing the common .py and .pyc files, and the platform |
| 32 | * dependent directory (exec_prefix), containing the shared library |
| 33 | * modules. Note that prefix and exec_prefix can be the same directory, |
| 34 | * but for some installations, they are different. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 35 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 36 | * Py_GetPath() carries out separate searches for prefix and exec_prefix. |
| 37 | * Each search tries a number of different locations until a ``landmark'' |
| 38 | * file or directory is found. If no prefix or exec_prefix is found, a |
| 39 | * warning message is issued and the preprocessor defined PREFIX and |
| 40 | * EXEC_PREFIX are used (even though they will not work); python carries on |
| 41 | * as best as is possible, but most imports will fail. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 42 | * |
| 43 | * Before any searches are done, the location of the executable is |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 44 | * determined. If argv[0] has one or more slashs in it, it is used |
| 45 | * unchanged. Otherwise, it must have been invoked from the shell's path, |
| 46 | * so we search $PATH for the named executable and use that. If the |
| 47 | * executable was not found on $PATH (or there was no $PATH environment |
| 48 | * variable), the original argv[0] string is used. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 49 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 50 | * Next, the executable location is examined to see if it is a symbolic |
| 51 | * link. If so, the link is chased (correctly interpreting a relative |
| 52 | * pathname if one is found) and the directory of the link target is used. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 53 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 54 | * Finally, argv0_path is set to the directory containing the executable |
| 55 | * (i.e. the last component is stripped). |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 56 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 57 | * With argv0_path in hand, we perform a number of steps. The same steps |
| 58 | * are performed for prefix and for exec_prefix, but with a different |
| 59 | * landmark. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 60 | * |
| 61 | * Step 1. Are we running python out of the build directory? This is |
| 62 | * checked by looking for a different kind of landmark relative to |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 63 | * argv0_path. For prefix, the landmark's path is derived from the VPATH |
| 64 | * preprocessor variable (taking into account that its value is almost, but |
| 65 | * not quite, what we need). For exec_prefix, the landmark is |
| 66 | * Modules/Setup. If the landmark is found, we're done. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 67 | * |
| 68 | * For the remaining steps, the prefix landmark will always be |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 69 | * lib/python$VERSION/os.py and the exec_prefix will always be |
Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 70 | * lib/python$VERSION/lib-dynload, where $VERSION is Python's version |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 71 | * number as supplied by the Makefile. Note that this means that no more |
| 72 | * build directory checking is performed; if the first step did not find |
| 73 | * the landmarks, the assumption is that python is running from an |
| 74 | * installed setup. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 75 | * |
| 76 | * Step 2. See if the $PYTHONHOME environment variable points to the |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 77 | * installed location of the Python libraries. If $PYTHONHOME is set, then |
| 78 | * it points to prefix and exec_prefix. $PYTHONHOME can be a single |
| 79 | * directory, which is used for both, or the prefix and exec_prefix |
| 80 | * directories separated by a colon. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 81 | * |
| 82 | * Step 3. Try to find prefix and exec_prefix relative to argv0_path, |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 83 | * backtracking up the path until it is exhausted. This is the most common |
| 84 | * step to succeed. Note that if prefix and exec_prefix are different, |
| 85 | * exec_prefix is more likely to be found; however if exec_prefix is a |
| 86 | * subdirectory of prefix, both will be found. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 87 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 88 | * Step 4. Search the directories pointed to by the preprocessor variables |
| 89 | * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be |
| 90 | * passed in as options to the configure script. |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 91 | * |
Barry Warsaw | 9012603 | 1997-04-11 20:27:03 +0000 | [diff] [blame] | 92 | * That's it! |
| 93 | * |
| 94 | * Well, almost. Once we have determined prefix and exec_prefix, the |
| 95 | * preprocesor variable PYTHONPATH is used to construct a path. Each |
| 96 | * relative path on PYTHONPATH is prefixed with prefix. Then the directory |
| 97 | * containing the shared library modules is appended. The environment |
| 98 | * variable $PYTHONPATH is inserted in front of it all. Finally, the |
| 99 | * prefix and exec_prefix globals are tweaked so they reflect the values |
| 100 | * expected by other code, by stripping the "lib/python$VERSION/..." stuff |
| 101 | * off. If either points to the build directory, the globals are reset to |
| 102 | * the corresponding preprocessor variables (so sys.prefix will reflect the |
| 103 | * installation location, even though sys.path points into the build |
| 104 | * directory). This seems to make more sense given that currently the only |
| 105 | * known use of sys.prefix and sys.exec_prefix is for the ILU installation |
| 106 | * process to find the installed Python tree. |
| 107 | */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 108 | |
| 109 | #ifndef VERSION |
| 110 | #define VERSION "1.5" |
| 111 | #endif |
| 112 | |
| 113 | #ifndef VPATH |
| 114 | #define VPATH "." |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 115 | #endif |
| 116 | |
Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 117 | #ifndef PREFIX |
| 118 | #define PREFIX "/usr/local" |
| 119 | #endif |
| 120 | |
| 121 | #ifndef EXEC_PREFIX |
Guido van Rossum | 6e12d56 | 1996-07-30 20:36:12 +0000 | [diff] [blame] | 122 | #define EXEC_PREFIX PREFIX |
Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 125 | #ifndef PYTHONPATH |
| 126 | /* I know this isn't K&R C, but the Makefile specifies it anyway */ |
| 127 | #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \ |
Guido van Rossum | 266033e | 1997-10-20 23:20:32 +0000 | [diff] [blame] | 128 | EXEC_PREFIX "/lib/python" VERSION "/lib-dynload" |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 129 | #endif |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 131 | #ifndef LANDMARK |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 132 | #define LANDMARK "os.py" |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 133 | #endif |
| 134 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 135 | static char prefix[MAXPATHLEN+1]; |
| 136 | static char exec_prefix[MAXPATHLEN+1]; |
Guido van Rossum | 7929c6f | 1997-05-20 22:38:21 +0000 | [diff] [blame] | 137 | static char progpath[MAXPATHLEN+1]; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 138 | static char *module_search_path = NULL; |
| 139 | static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */ |
| 140 | |
| 141 | static void |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 142 | reduce(char *dir) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 143 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 144 | size_t i = strlen(dir); |
| 145 | while (i > 0 && dir[i] != SEP) |
| 146 | --i; |
| 147 | dir[i] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 148 | } |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 149 | |
| 150 | |
| 151 | #ifndef S_ISREG |
| 152 | #define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) |
| 153 | #endif |
| 154 | |
| 155 | #ifndef S_ISDIR |
| 156 | #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR) |
| 157 | #endif |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 158 | |
| 159 | static int |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 160 | isfile(char *filename) /* Is file, not directory */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 161 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 162 | struct stat buf; |
| 163 | if (stat(filename, &buf) != 0) |
| 164 | return 0; |
| 165 | if (!S_ISREG(buf.st_mode)) |
| 166 | return 0; |
| 167 | return 1; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| 171 | static int |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 172 | ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 173 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 174 | if (isfile(filename)) |
| 175 | return 1; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 176 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 177 | /* Check for the compiled version of prefix. */ |
| 178 | if (strlen(filename) < MAXPATHLEN) { |
| 179 | strcat(filename, Py_OptimizeFlag ? "o" : "c"); |
| 180 | if (isfile(filename)) |
| 181 | return 1; |
| 182 | } |
| 183 | return 0; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
| 187 | static int |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 188 | isxfile(char *filename) /* Is executable file */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 189 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 190 | struct stat buf; |
| 191 | if (stat(filename, &buf) != 0) |
| 192 | return 0; |
| 193 | if (!S_ISREG(buf.st_mode)) |
| 194 | return 0; |
| 195 | if ((buf.st_mode & 0111) == 0) |
| 196 | return 0; |
| 197 | return 1; |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | |
| 201 | static int |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 202 | isdir(char *filename) /* Is directory */ |
Guido van Rossum | d29806c | 1998-01-19 22:06:22 +0000 | [diff] [blame] | 203 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 204 | struct stat buf; |
| 205 | if (stat(filename, &buf) != 0) |
| 206 | return 0; |
| 207 | if (!S_ISDIR(buf.st_mode)) |
| 208 | return 0; |
| 209 | return 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
| 213 | static void |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 214 | joinpath(char *buffer, char *stuff) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 215 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 216 | size_t n, k; |
| 217 | if (stuff[0] == SEP) |
| 218 | n = 0; |
| 219 | else { |
| 220 | n = strlen(buffer); |
| 221 | if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN) |
| 222 | buffer[n++] = SEP; |
| 223 | } |
| 224 | k = strlen(stuff); |
| 225 | if (n + k > MAXPATHLEN) |
| 226 | k = MAXPATHLEN - n; |
| 227 | strncpy(buffer+n, stuff, k); |
| 228 | buffer[n+k] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 232 | static int |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 233 | search_for_prefix(char *argv0_path, char *home) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 234 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 235 | size_t n; |
| 236 | char *vpath; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 237 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 238 | /* If PYTHONHOME is set, we believe it unconditionally */ |
| 239 | if (home) { |
| 240 | char *delim; |
| 241 | strcpy(prefix, home); |
| 242 | delim = strchr(prefix, DELIM); |
| 243 | if (delim) |
| 244 | *delim = '\0'; |
| 245 | joinpath(prefix, lib_python); |
| 246 | joinpath(prefix, LANDMARK); |
| 247 | return 1; |
| 248 | } |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 249 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 250 | /* Check to see if argv[0] is in the build directory */ |
| 251 | strcpy(prefix, argv0_path); |
| 252 | joinpath(prefix, "Modules/Setup"); |
| 253 | if (isfile(prefix)) { |
| 254 | /* Check VPATH to see if argv0_path is in the build directory. |
| 255 | * Complication: the VPATH passed in is relative to the |
| 256 | * Modules build directory and points to the Modules source |
| 257 | * directory; we need it relative to the build tree and |
| 258 | * pointing to the source tree. Solution: chop off a leading |
| 259 | * ".." (but only if it's there -- it could be an absolute |
| 260 | * path) and chop off the final component (assuming it's |
| 261 | * "Modules"). |
| 262 | */ |
| 263 | vpath = VPATH; |
| 264 | if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/') |
| 265 | vpath += 3; |
| 266 | strcpy(prefix, argv0_path); |
| 267 | joinpath(prefix, vpath); |
| 268 | reduce(prefix); |
| 269 | joinpath(prefix, "Lib"); |
| 270 | joinpath(prefix, LANDMARK); |
| 271 | if (ismodule(prefix)) |
| 272 | return -1; |
| 273 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 274 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 275 | /* Search from argv0_path, until root is found */ |
| 276 | strcpy(prefix, argv0_path); |
| 277 | do { |
| 278 | n = strlen(prefix); |
| 279 | joinpath(prefix, lib_python); |
| 280 | joinpath(prefix, LANDMARK); |
| 281 | if (ismodule(prefix)) |
| 282 | return 1; |
| 283 | prefix[n] = '\0'; |
| 284 | reduce(prefix); |
| 285 | } while (prefix[0]); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 286 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 287 | /* Look at configure's PREFIX */ |
| 288 | strcpy(prefix, PREFIX); |
| 289 | joinpath(prefix, lib_python); |
| 290 | joinpath(prefix, LANDMARK); |
| 291 | if (ismodule(prefix)) |
| 292 | return 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 293 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 294 | /* Fail */ |
| 295 | return 0; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | |
| 299 | static int |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 300 | search_for_exec_prefix(char *argv0_path, char *home) |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 301 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 302 | size_t n; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 303 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 304 | /* If PYTHONHOME is set, we believe it unconditionally */ |
| 305 | if (home) { |
| 306 | char *delim; |
| 307 | delim = strchr(home, DELIM); |
| 308 | if (delim) |
| 309 | strcpy(exec_prefix, delim+1); |
| 310 | else |
| 311 | strcpy(exec_prefix, home); |
| 312 | joinpath(exec_prefix, lib_python); |
| 313 | joinpath(exec_prefix, "lib-dynload"); |
| 314 | return 1; |
| 315 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 316 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 317 | /* Check to see if argv[0] is in the build directory */ |
| 318 | strcpy(exec_prefix, argv0_path); |
| 319 | joinpath(exec_prefix, "Modules/Setup"); |
| 320 | if (isfile(exec_prefix)) { |
| 321 | reduce(exec_prefix); |
| 322 | return -1; |
| 323 | } |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 324 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 325 | /* Search from argv0_path, until root is found */ |
| 326 | strcpy(exec_prefix, argv0_path); |
| 327 | do { |
| 328 | n = strlen(exec_prefix); |
| 329 | joinpath(exec_prefix, lib_python); |
| 330 | joinpath(exec_prefix, "lib-dynload"); |
| 331 | if (isdir(exec_prefix)) |
| 332 | return 1; |
| 333 | exec_prefix[n] = '\0'; |
| 334 | reduce(exec_prefix); |
| 335 | } while (exec_prefix[0]); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 336 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 337 | /* Look at configure's EXEC_PREFIX */ |
| 338 | strcpy(exec_prefix, EXEC_PREFIX); |
| 339 | joinpath(exec_prefix, lib_python); |
| 340 | joinpath(exec_prefix, "lib-dynload"); |
| 341 | if (isdir(exec_prefix)) |
| 342 | return 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 343 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 344 | /* Fail */ |
| 345 | return 0; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
| 349 | static void |
| 350 | calculate_path() |
| 351 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 352 | extern char *Py_GetProgramName(); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 353 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 354 | static char delimiter[2] = {DELIM, '\0'}; |
| 355 | static char separator[2] = {SEP, '\0'}; |
| 356 | char *pythonpath = PYTHONPATH; |
| 357 | char *rtpypath = getenv("PYTHONPATH"); |
| 358 | char *home = Py_GetPythonHome(); |
| 359 | char *path = getenv("PATH"); |
| 360 | char *prog = Py_GetProgramName(); |
| 361 | char argv0_path[MAXPATHLEN+1]; |
| 362 | int pfound, efound; /* 1 if found; -1 if found build directory */ |
| 363 | char *buf; |
| 364 | size_t bufsz; |
| 365 | size_t prefixsz; |
| 366 | char *defpath = pythonpath; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 367 | #ifdef WITH_NEXT_FRAMEWORK |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 368 | NSModule pythonModule; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 369 | #endif |
| 370 | |
| 371 | #ifdef WITH_NEXT_FRAMEWORK |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 372 | pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize")); |
| 373 | /* Use dylib functions to find out where the framework was loaded from */ |
| 374 | buf = NSLibraryNameForModule(pythonModule); |
| 375 | if (buf != NULL) { |
| 376 | /* We're in a framework. */ |
| 377 | strcpy(progpath, buf); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 378 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 379 | /* Frameworks have support for versioning */ |
| 380 | strcpy(lib_python, "lib"); |
| 381 | } |
| 382 | else { |
| 383 | /* If we're not in a framework, fall back to the old way |
| 384 | (even though NSNameOfModule() probably does the same thing.) */ |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 385 | #endif |
| 386 | |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 387 | /* Initialize this dynamically for K&R C */ |
| 388 | sprintf(lib_python, "lib/python%s", VERSION); |
| 389 | |
| 390 | /* If there is no slash in the argv0 path, then we have to |
| 391 | * assume python is on the user's $PATH, since there's no |
| 392 | * other way to find a directory to start the search from. If |
| 393 | * $PATH isn't exported, you lose. |
| 394 | */ |
| 395 | if (strchr(prog, SEP)) |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 396 | strcpy(progpath, prog); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 397 | else if (path) { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 398 | while (1) { |
| 399 | char *delim = strchr(path, DELIM); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 400 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 401 | if (delim) { |
| 402 | size_t len = delim - path; |
| 403 | strncpy(progpath, path, len); |
| 404 | *(progpath + len) = '\0'; |
| 405 | } |
| 406 | else |
| 407 | strcpy(progpath, path); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 408 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 409 | joinpath(progpath, prog); |
| 410 | if (isxfile(progpath)) |
| 411 | break; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 412 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 413 | if (!delim) { |
| 414 | progpath[0] = '\0'; |
| 415 | break; |
| 416 | } |
| 417 | path = delim + 1; |
| 418 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 419 | } |
| 420 | else |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 421 | progpath[0] = '\0'; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 422 | #ifdef WITH_NEXT_FRAMEWORK |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 423 | } |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 424 | #endif |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 425 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 426 | strcpy(argv0_path, progpath); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 427 | |
| 428 | #if HAVE_READLINK |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 429 | { |
| 430 | char tmpbuffer[MAXPATHLEN+1]; |
| 431 | int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN); |
| 432 | while (linklen != -1) { |
| 433 | /* It's not null terminated! */ |
| 434 | tmpbuffer[linklen] = '\0'; |
| 435 | if (tmpbuffer[0] == SEP) |
| 436 | strcpy(argv0_path, tmpbuffer); |
| 437 | else { |
| 438 | /* Interpret relative to progpath */ |
| 439 | reduce(argv0_path); |
| 440 | joinpath(argv0_path, tmpbuffer); |
| 441 | } |
| 442 | linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN); |
| 443 | } |
| 444 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 445 | #endif /* HAVE_READLINK */ |
| 446 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 447 | reduce(argv0_path); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 448 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 449 | if (!(pfound = search_for_prefix(argv0_path, home))) { |
| 450 | if (!Py_FrozenFlag) |
| 451 | fprintf(stderr, |
| 452 | "Could not find platform independent libraries <prefix>\n"); |
| 453 | strcpy(prefix, PREFIX); |
| 454 | joinpath(prefix, lib_python); |
| 455 | } |
| 456 | else |
| 457 | reduce(prefix); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 458 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 459 | if (!(efound = search_for_exec_prefix(argv0_path, home))) { |
| 460 | if (!Py_FrozenFlag) |
| 461 | fprintf(stderr, |
| 462 | "Could not find platform dependent libraries <exec_prefix>\n"); |
| 463 | strcpy(exec_prefix, EXEC_PREFIX); |
| 464 | joinpath(exec_prefix, "lib/lib-dynload"); |
| 465 | } |
| 466 | /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */ |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 467 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 468 | if ((!pfound || !efound) && !Py_FrozenFlag) |
| 469 | fprintf(stderr, |
| 470 | "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n"); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 471 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 472 | /* Calculate size of return buffer. |
| 473 | */ |
| 474 | bufsz = 0; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 475 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 476 | if (rtpypath) |
| 477 | bufsz += strlen(rtpypath) + 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 478 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 479 | prefixsz = strlen(prefix) + 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 480 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 481 | while (1) { |
| 482 | char *delim = strchr(defpath, DELIM); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 483 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 484 | if (defpath[0] != SEP) |
| 485 | /* Paths are relative to prefix */ |
| 486 | bufsz += prefixsz; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 487 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 488 | if (delim) |
| 489 | bufsz += delim - defpath + 1; |
| 490 | else { |
| 491 | bufsz += strlen(defpath) + 1; |
| 492 | break; |
| 493 | } |
| 494 | defpath = delim + 1; |
| 495 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 496 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 497 | bufsz += strlen(exec_prefix) + 1; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 498 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 499 | /* This is the only malloc call in this file */ |
| 500 | buf = PyMem_Malloc(bufsz); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 501 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 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 | else |
| 515 | buf[0] = '\0'; |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 516 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 517 | /* Next goes merge of compile-time $PYTHONPATH with |
| 518 | * dynamically located prefix. |
| 519 | */ |
| 520 | defpath = pythonpath; |
| 521 | while (1) { |
| 522 | char *delim = strchr(defpath, DELIM); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 523 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 524 | if (defpath[0] != SEP) { |
| 525 | strcat(buf, prefix); |
| 526 | strcat(buf, separator); |
| 527 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 528 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 529 | if (delim) { |
| 530 | size_t len = delim - defpath + 1; |
| 531 | size_t end = strlen(buf) + len; |
| 532 | strncat(buf, defpath, len); |
| 533 | *(buf + end) = '\0'; |
| 534 | } |
| 535 | else { |
| 536 | strcat(buf, defpath); |
| 537 | break; |
| 538 | } |
| 539 | defpath = delim + 1; |
| 540 | } |
| 541 | strcat(buf, delimiter); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 542 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 543 | /* Finally, on goes the directory for dynamic-load modules */ |
| 544 | strcat(buf, exec_prefix); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 545 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 546 | /* And publish the results */ |
| 547 | module_search_path = buf; |
| 548 | } |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 549 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 550 | /* Reduce prefix and exec_prefix to their essence, |
| 551 | * e.g. /usr/local/lib/python1.5 is reduced to /usr/local. |
| 552 | * If we're loading relative to the build directory, |
| 553 | * return the compiled-in defaults instead. |
| 554 | */ |
| 555 | if (pfound > 0) { |
| 556 | reduce(prefix); |
| 557 | reduce(prefix); |
| 558 | } |
| 559 | else |
| 560 | strcpy(prefix, PREFIX); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 561 | |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 562 | if (efound > 0) { |
| 563 | reduce(exec_prefix); |
| 564 | reduce(exec_prefix); |
| 565 | reduce(exec_prefix); |
| 566 | } |
| 567 | else |
| 568 | strcpy(exec_prefix, EXEC_PREFIX); |
Guido van Rossum | 305e5d0 | 1997-04-11 17:18:45 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | /* External interface */ |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 573 | |
| 574 | char * |
Guido van Rossum | 582646a | 1996-05-28 22:30:17 +0000 | [diff] [blame] | 575 | Py_GetPath() |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 576 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 577 | if (!module_search_path) |
| 578 | calculate_path(); |
| 579 | return module_search_path; |
Guido van Rossum | 667d704 | 1995-08-04 04:20:48 +0000 | [diff] [blame] | 580 | } |
Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 581 | |
Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 582 | char * |
| 583 | Py_GetPrefix() |
| 584 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 585 | if (!module_search_path) |
| 586 | calculate_path(); |
| 587 | return prefix; |
Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | char * |
| 591 | Py_GetExecPrefix() |
| 592 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 593 | if (!module_search_path) |
| 594 | calculate_path(); |
| 595 | return exec_prefix; |
Guido van Rossum | c34c9a5 | 1996-06-12 04:20:27 +0000 | [diff] [blame] | 596 | } |
Guido van Rossum | 7929c6f | 1997-05-20 22:38:21 +0000 | [diff] [blame] | 597 | |
| 598 | char * |
| 599 | Py_GetProgramFullPath() |
| 600 | { |
Fred Drake | edabdc1 | 2000-07-08 06:16:37 +0000 | [diff] [blame] | 601 | if (!module_search_path) |
| 602 | calculate_path(); |
| 603 | return progpath; |
Guido van Rossum | 7929c6f | 1997-05-20 22:38:21 +0000 | [diff] [blame] | 604 | } |