/Modules/getpath.c

http://unladen-swallow.googlecode.com/ · C · 694 lines · 442 code · 81 blank · 171 comment · 103 complexity · 5a8e01a89a7bbebd1bf15e24fcd77cd0 MD5 · raw file

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