Resolving parts of #688907:
authorJust van Rossum <just@letterror.com>
Tue, 25 Feb 2003 20:15:40 +0000 (20:15 +0000)
committerJust van Rossum <just@letterror.com>
Tue, 25 Feb 2003 20:15:40 +0000 (20:15 +0000)
- Replaced bootstrap shell script with Python script. This means
  standalone apps built with bundlebuilder will not work on MacOS < 10.1,
  since we depend (again) on an installed Python.
- Add a hack to set sys.executable; the bootstrap script does os.execve()
  with an argv[0] that's different from the actual Python executable
  (it has to match the CFBundleExecutable entry in the Info.plist to make
  the app work both from the Finder and the command line, and it has to be
  the bootstrap script), yet a proper sys.executable is needed to spawn
  auxiliary processes.

Lib/plat-mac/bundlebuilder.py

index fd19cbb0ed6de496ed5d55d813498218e24d7b4f..4c33e64a298b351a0dbc419f8e5c69b427c3113a 100755 (executable)
@@ -230,7 +230,17 @@ if USE_ZIPIMPORT:
                path = fullname.replace(".", os.sep) + PYC_EXT
                return path, MAGIC + '\0\0\0\0' + marshal.dumps(code)
 
+SITECUSTOMIZE_PY = """\
+import sys, os
+executable = os.getenv("PYTHONEXECUTABLE")
+if executable is not None:
+    sys.executable = executable
+"""
+
+SITE_PY += SITECUSTOMIZE_PY
 SITE_CO = compile(SITE_PY, "<-bundlebuilder.py->", "exec")
+SITECUSTOMIZE_CO = compile(SITECUSTOMIZE_PY, "<-bundlebuilder.py->", "exec")
+
 
 EXT_LOADER = """\
 def __load():
@@ -255,15 +265,18 @@ MAYMISS_MODULES = ['mac', 'os2', 'nt', 'ntpath', 'dos', 'dospath',
 STRIP_EXEC = "/usr/bin/strip"
 
 BOOTSTRAP_SCRIPT = """\
-#!/bin/sh
-
-execdir=$(dirname "${0}")
-executable="${execdir}/%(executable)s"
-resdir=$(dirname "${execdir}")/Resources
-main="${resdir}/%(mainprogram)s"
-PYTHONPATH="$resdir"
-export PYTHONPATH
-exec "${executable}" "${main}" "$@"
+#!/usr/bin/env python
+
+import sys, os
+execdir = os.path.dirname(sys.argv[0])
+executable = os.path.join(execdir, "%(executable)s")
+resdir = os.path.join(os.path.dirname(execdir), "Resources")
+mainprogram = os.path.join(resdir, "%(mainprogram)s")
+
+sys.argv.insert(1, mainprogram)
+os.environ["PYTHONPATH"] = resdir
+os.environ["PYTHONEXECUTABLE"] = executable
+os.execve(executable, sys.argv, os.environ)
 """
 
 ARGVEMULATOR="""\
@@ -415,6 +428,10 @@ class AppBuilder(BundleBuilder):
        def postProcess(self):
                if self.standalone:
                        self.addPythonModules()
+               else:
+                       sitecustomizepath = pathjoin(self.bundlepath, "Contents", "Resources",
+                                       "sitecustomize" + PYC_EXT)
+                       writePyc(SITECUSTOMIZE_CO, sitecustomizepath)
                if self.strip and not self.symlink:
                        self.stripBinaries()
 
@@ -487,6 +504,9 @@ class AppBuilder(BundleBuilder):
                site.__code__ = SITE_CO
                mf.scan_code(SITE_CO, site)
 
+               # warnings.py gets imported implicitly from C
+               mf.import_hook("warnings")
+
                includeModules = self.includeModules[:]
                for name in self.includePackages:
                        includeModules.extend(findPackageContents(name).keys())