From 21ed16acbe0a8870f6833b7976f8b7e79452d21c Mon Sep 17 00:00:00 2001 From: Jack Jansen Date: Fri, 2 Aug 2002 14:11:24 +0000 Subject: [PATCH] Added one call to Py_Main(), for OSX framework builds only, that will get the actual script to run in case we are running from an applet. If we are indeed running an applet we skip the normal option processing leaving it all to the applet code. This allows us to get use the normal python binary in the Python.app bundle, giving us all the normal command line options through PythonLauncher while still allowing Python.app to be used as the template for building applets. Consequently, pythonforbundle is gone, and Mac/Python/macmain.c isn't used on OSX anymore. --- Include/pymactoolbox.h | 4 +- Mac/OSX/Makefile | 18 +-------- Mac/OSX/README | 49 +++++++++++------------ Modules/main.c | 18 ++++++++- Python/mactoolboxglue.c | 88 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 45 deletions(-) diff --git a/Include/pymactoolbox.h b/Include/pymactoolbox.h index 5313d91282bf69..5e2bb34ea32d3f 100644 --- a/Include/pymactoolbox.h +++ b/Include/pymactoolbox.h @@ -34,7 +34,9 @@ char *PyMac_StrError(int); /* strerror with mac errors */ PyObject *PyErr_Mac(PyObject *, int); /* Exception with a mac error */ PyObject *PyMac_Error(OSErr); /* Uses PyMac_GetOSErrException */ extern OSErr PyMac_GetFullPathname(FSSpec *, char *, int); /* convert fsspec->path */ - +#ifdef WITH_NEXT_FRAMEWORK +extern char *PyMac_GetAppletScriptFile(void); /* Return applet script file or NULL */ +#endif /* ** These conversion routines are defined in mactoolboxglue.c itself. */ diff --git a/Mac/OSX/Makefile b/Mac/OSX/Makefile index daf9dfe6da758f..f505bc5f50bf7b 100644 --- a/Mac/OSX/Makefile +++ b/Mac/OSX/Makefile @@ -37,9 +37,6 @@ LD=cc REZ=/Developer/Tools/Rez DEREZ=/Developer/Tools/DeRez -OBJECTS=$(builddir)/Mac/Python/macmain.o \ - $(builddir)/Mac/Python/macgetargv.o - PYTHON=$(builddir)/python.exe APPTEMPLATE=$(srcdir)/Mac/OSXResources/app APPSUBDIRS=MacOS Resources Resources/English.lproj @@ -53,7 +50,7 @@ install_PythonLauncher: cd $(srcdir)/Mac/OSX/PythonLauncher/PythonLauncher.pbproj ; \ pbxbuild -target PythonLauncher -buildstyle Deployment DSTROOT=/ install -install_Python: pythonforbundle +install_Python: $(PYTHON) @for i in $(PYTHONAPPSDIR) $(APPINSTALLDIR) $(APPINSTALLDIR)/Contents; do \ if test ! -d $$i; then \ echo "Creating directory $$i"; \ @@ -91,7 +88,7 @@ install_Python: pythonforbundle esac; \ done; \ done - $(INSTALL_PROGRAM) $(STRIPFLAG) pythonforbundle $(APPINSTALLDIR)/Contents/MacOS/python + $(INSTALL_PROGRAM) $(STRIPFLAG) $(PYTHON) $(APPINSTALLDIR)/Contents/MacOS/python # Create a temporary version of the resources here $(PYTHON) $(RFCONVERTER) -r $(RESOURCEDIR)/dialogs.rsrc dialogs.rsrc $(PYTHON) $(RFCONVERTER) -r $(RESOURCEDIR)/errors.rsrc errors.rsrc @@ -231,14 +228,3 @@ dontinstallmacsubtree: l=`cd $(srcdir)/Mac/Lib; pwd`; \ echo $$l > $(prefix)/lib/python$(VERSION)/site-packages/Mac.pth ; \ echo $$l/lib-scriptpackages >> $(prefix)/lib/python$(VERSION)/site-packages/Mac.pth - -pythonforbundle: $(OBJECTS) - $(LD) $(LDFLAGS) $(OBJECTS) -o pythonforbundle - -# Rules to build each file in OBJECTS - is there a better way? -$(builddir)/Mac/Python/macmain.o: $(srcdir)/Mac/Python/macmain.c - $(CC) $(CFLAGS) -c $(srcdir)/Mac/Python/macmain.c -o $@ - -$(builddir)/Mac/Python/macgetargv.o: $(srcdir)/Mac/Python/macgetargv.c - $(CC) $(CFLAGS) -c $(srcdir)/Mac/Python/macgetargv.c -o $@ - diff --git a/Mac/OSX/README b/Mac/OSX/README index 4c8dc0a1d49775..822a9d9fdc7754 100644 --- a/Mac/OSX/README +++ b/Mac/OSX/README @@ -1,34 +1,29 @@ -This directory contains a Makefile that will create a proof-of-concept -Mac OS X application for Python. The process is far from streamlined, -and it will definitely change in future releases of Python, but I wanted to -include this in the current distribution so people could play with it. +This directory contains a Makefile that will create a couple of python-related +applications (fullblown OSX .app applications, that is) in /Applications/Python, +and a hidden helper application Python.app inside the Python.framework. In addition +it has a target "installmacsubtree" that installs the relevant portions of the +Mac subtree into the Python.framework. -To create a fullblown Python.app proceed as follows. +It is normally invoked indirectly through the main Makefile, as the last step +in the sequence +1. configure --enable-framework +2. make +3. make frameworkinstall +4. make osxapps -1. In the main Python source directory configure python with - configure --enable-framework -2. Do a "make clean" if you did a previous build, then "make". -3. Install this as a framework with "make frameworkinstall". This puts a Python - framework into /Library/Frameworks. -4. Come back here (Mac/OSX) and build and install the application, - with "make install". -5. It is probably a good idea to add the Mac-specific modules to the framework, - with "make installmacsubtree". This puts a MacPython lib directory into - sys.prefix/Mac/Lib. Again, this is a temporary measure. -6. To actually find the Lib directory installed in step 5 you add a line - to your site.py file (the one in /Library/Frameworks/....): - sys.path.append(os.path.join(sys.prefix, 'Mac/Lib')) +The interesting targets in the makefile are: +installmacsubtree - explained above, +dontinstallmacsubtree - Put only a .pth file into the framework (pointing to this + sourcetree), which may be easier for development, +install_all - install all three .app applications, +install_Python - install the hidden interpreter .app into the framework, +install_PythonLauncher - install the user-visible script launch helper +install_IDE - install the IDE +installunixprograms - install symlinks/scripts mimicking normal unix Python into + /usr/local. -You are now done. In your Applications you should have a "Python", with the icon -being a falling 16 Ton weight with a shadow under it. You can drop Python scripts -on this and the will be run, in a full-windowing environment. Note that you -do not get sys.stdin, and that sys.stdout goes to the console (Use -Applications/Utilities/Console to see it). - -For some reason the application only accepts files with TEXT type, not straight unix -typeless files. Something to take note of is that the ".rsrc" files in the distribution are not actually resource files, they're AppleSingle encoded resource files. - Jack Jansen, jack@oratrix.com, 11-Sep-01. \ No newline at end of file + Jack Jansen, jack@oratrix.com, 02-Aug-02 \ No newline at end of file diff --git a/Modules/main.c b/Modules/main.c index cebb17b90d1b36..9e467171b0e5df 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -135,6 +135,22 @@ Py_Main(int argc, char **argv) PySys_ResetWarnOptions(); +#if defined(WITH_NEXT_FRAMEWORK) + /* If we are running from a framework it could be that we are actually + ** the main program for an applet. If so, the next call will return the + ** filename that we are supposed to run. + */ + filename = PyMac_GetAppletScriptFile(); + if (filename != NULL) { + if ((fp = fopen(filename, "r")) == NULL) { + fprintf(stderr, "%s: can't open file '%s'\n", + argv[0], filename); + exit(2); + } + } + /* Skip option-processing if we are an applet */ + if (filename == NULL) +#endif while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) { if (c == 'c') { /* -c is the last option; following arguments @@ -261,7 +277,7 @@ Py_Main(int argc, char **argv) (p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') unbuffered = 1; - if (command == NULL && _PyOS_optind < argc && + if (command == NULL && filename == NULL && _PyOS_optind < argc && strcmp(argv[_PyOS_optind], "-") != 0) { filename = argv[_PyOS_optind]; diff --git a/Python/mactoolboxglue.c b/Python/mactoolboxglue.c index 2852fde2e4c0c9..68876eb08fbe20 100644 --- a/Python/mactoolboxglue.c +++ b/Python/mactoolboxglue.c @@ -175,6 +175,94 @@ PyMac_GetFullPathname(FSSpec *fss, char *path, int len) } #endif /* TARGET_API_MAC_OSX */ + +#ifdef WITH_NEXT_FRAMEWORK +/* +** In a bundle, find a file "resourceName" of type "resourceType". Return the +** full pathname in "resourceURLCstr". +*/ +static int +locateResourcePy(CFStringRef resourceType, CFStringRef resourceName, char *resourceURLCStr, int length) +{ + CFBundleRef mainBundle = NULL; + CFURLRef URL, absoluteURL; + CFStringRef filenameString, filepathString; + CFIndex size, i; + CFArrayRef arrayRef = NULL; + int success = 0; + +#if TARGET_API_MAC_OSX + CFURLPathStyle thePathStyle = kCFURLPOSIXPathStyle; +#else + CFURLPathStyle thePathStyle = kCFURLHFSPathStyle; +#endif + + /* Get a reference to our main bundle */ + mainBundle = CFBundleGetMainBundle(); + + /* If we are running inside a bundle, look through it. Otherwise, do nothing. */ + if (mainBundle) { + + /* Look for py files in the main bundle by type */ + arrayRef = CFBundleCopyResourceURLsOfType( mainBundle, + resourceType, + NULL ); + + /* See if there are any filename matches */ + size = CFArrayGetCount(arrayRef); + for (i = 0; i < size; i++) { + URL = CFArrayGetValueAtIndex(arrayRef, i); + filenameString = CFURLCopyLastPathComponent(URL); + if (CFStringCompare(filenameString, resourceName, 0) == kCFCompareEqualTo) { + /* We found a match, get the file's full path */ + absoluteURL = CFURLCopyAbsoluteURL(URL); + filepathString = CFURLCopyFileSystemPath(absoluteURL, thePathStyle); + CFRelease(absoluteURL); + + /* Copy the full path into the caller's character buffer */ + success = CFStringGetCString(filepathString, resourceURLCStr, length, + kCFStringEncodingMacRoman); + + CFRelease(filepathString); + } + CFRelease(filenameString); + } + CFRelease(arrayRef); + } + return success; +} + +/* +** iff we are running in a .app framework then we could be +** the main program for an applet. In that case, return the +** script filename for the applet. +** Otherwise return NULL. +*/ +char * +PyMac_GetAppletScriptFile(void) +{ + static char scriptpath[1024]; + + /* First we see whether we have __rawmain__.py and run that if it + ** is there. This is used for applets that want sys.argv to be + ** unix-like: __rawmain__ will construct it (from the initial appleevent) + ** and then call __main__.py. + */ + if (locateResourcePy(CFSTR("py"), CFSTR("__rawmain__.py"), scriptpath, 1024)) { + return scriptpath; + } else if (locateResourcePy(CFSTR("pyc"), CFSTR("__rawmain__.pyc"), scriptpath, 1024)) { + return scriptpath; + } else if (locateResourcePy(CFSTR("py"), CFSTR("__main__.py"), scriptpath, 1024)) { + return scriptpath; + } else if (locateResourcePy(CFSTR("pyc"), CFSTR("__main__.pyc"), scriptpath, 1024)) { + return scriptpath; + } + return NULL; +} + +#endif + + /* Convert a 4-char string object argument to an OSType value */ int PyMac_GetOSType(PyObject *v, OSType *pr)