[Python-checkins] CVS: python/dist/src/Mac/Python macmain.c,1.69,1.70

Jack Jansen jackjansen@users.sourceforge.net
Tue, 11 Sep 2001 06:08:12 -0700


Update of /cvsroot/python/python/dist/src/Mac/Python
In directory usw-pr-cvs1:/tmp/cvs-serv19343

Modified Files:
	macmain.c 
Log Message:
Mods by Donovan Preston (with changes by me to make them "go with the flow")
that will detect an __main__.py or __rawmain__.py in the application bundle.
This file is then exectued as the main script. We now have applets in
MachO Python!!!

The difference between __main__ and __rawmain__ is that the former gets a
complete simulated argv (so you can drop files on the applet and the script
sees them in sys.argv) while the latter skips the argv simulation and the
<option>key dialog. This keeps the AppleEvent that started the app intact,
as well as the funny "-psn_xxxx" argv[1] argument, so the script can do
with these what it wants.


Index: macmain.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/Python/macmain.c,v
retrieving revision 1.69
retrieving revision 1.70
diff -C2 -d -r1.69 -r1.70
*** macmain.c	2001/09/11 11:29:31	1.69
--- macmain.c	2001/09/11 13:08:10	1.70
***************
*** 82,86 ****
  PyMac_PrefRecord PyMac_options;
  
! static void Py_Main(int, char **); /* Forward */
  void PyMac_Exit(int); /* Forward */
  
--- 82,86 ----
  PyMac_PrefRecord PyMac_options;
  
! static void Py_Main(int, char **, char *); /* Forward */
  void PyMac_Exit(int); /* Forward */
  
***************
*** 489,503 ****
  
  #if TARGET_API_MAC_OSX
  int
  main(int argc, char **argv)
  {
! 	int i;
! 	printf("first argc=%d\n", argc);
! 	for(i=0; i<argc; i++) printf("first argv[%d] = \"%s\"\n", i, argv[i]);
! 	init_common(&argc, &argv, 0);
! 	printf("second argc=%d\n", argc);
! 	for(i=0; i<argc; i++) printf("second argv[%d] = \"%s\"\n", i, argv[i]);
! 	Py_Main(argc, argv);
! 	return 0;
  }
  
--- 489,573 ----
  
  #if TARGET_API_MAC_OSX
+ 
+ static int
+ locateResourcePy(char * resourceName, char * resourceURLCStr, int length) {
+     CFBundleRef mainBundle = NULL;
+     CFURLRef URL, absoluteURL;
+     CFStringRef filenameString, filepathString, rsrcString;
+     CFIndex size, i;
+     CFArrayRef arrayRef;
+     Boolean success = 0;
+ 
+     /* Create a CFString with the resource name in it */
+     rsrcString = CFStringCreateWithCString(0, resourceName, kCFStringEncodingMacRoman);
+ 
+     /* Get a reference to our main bundle */
+     mainBundle = CFBundleGetMainBundle();
+ 
+     /* Look for py files in the main bundle by type */
+     arrayRef = CFBundleCopyResourceURLsOfType( mainBundle, 
+             CFSTR("py"), 
+            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, rsrcString, 0) == kCFCompareEqualTo) {
+             /* We found a match, get the file's full path */
+             absoluteURL = CFURLCopyAbsoluteURL(URL);
+             filepathString = CFURLCopyFileSystemPath(absoluteURL, kCFURLPOSIXPathStyle);
+             CFRelease(absoluteURL);
+ 
+             /* Copy the full path into the caller's character buffer */
+             success = CFStringGetCString(filepathString, resourceURLCStr, length,
+                                         kCFStringEncodingMacRoman);
+ 
+             CFRelease(filepathString);
+         }
+         CFRelease(filenameString);
+     }
+     CFRelease(rsrcString);
+     CFRelease(arrayRef);
+ 
+     return success;
+ }
+ 
  int
  main(int argc, char **argv)
  {
!     int i;
!     static char scriptpath[1024];
!     char *script = NULL;
! 
! 	/* First we see whether we have __rawmain__.py and run that if it
! 	** is there
! 	*/
! 	if (locateResourcePy("__rawmain__.py", scriptpath, 1024)) {
! 		/* If we have a raw main we don't do AppleEvent processing.
! 		** Notice that this also means we keep the -psn.... argv[1]
! 		** value intact. Not sure whether that is important to someone,
! 		** but you never know...
! 		*/
! 		script = scriptpath;
! 	} else {
! 		/* Otherwise we look for __main__.py. Whether that is
! 		** found or not we also process AppleEvent arguments.
! 		*/
! 		if (locateResourcePy("__main__.py", scriptpath, 1024))
! 			script = scriptpath;
! 			
! 		printf("original argc=%d\n", argc);
! 		for(i=0; i<argc; i++) printf("original argv[%d] = \"%s\"\n", i, argv[i]);
! 
! 		init_common(&argc, &argv, 0);
! 
! 		printf("modified argc=%d\n", argc);
! 		for(i=0; i<argc; i++) printf("modified argv[%d] = \"%s\"\n", i, argv[i]);
! 	}
! 
! 	Py_Main(argc, argv, script);
!     return 0;
  }
  
***************
*** 534,538 ****
  		}
  	}
! 	Py_Main(argc, argv);
  }
  #endif /* TARGET_API_MAC_OSX */
--- 604,608 ----
  		}
  	}
! 	Py_Main(argc, argv, NULL);
  }
  #endif /* TARGET_API_MAC_OSX */
***************
*** 541,552 ****
  
  static void
! Py_Main(int argc, char **argv)
  {
  	int sts;
  	char *command = NULL;
- 	char *filename = NULL;
  	FILE *fp = stdin;
  
! 	filename = argv[1];
  
  	if (Py_VerboseFlag ||
--- 611,628 ----
  
  static void
! Py_Main(int argc, char **argv, char *filename)
  {
  	int sts;
  	char *command = NULL;
  	FILE *fp = stdin;
  
! 	if ( filename ) {
! 		/* Someone else has found our "script" already */
! 		argv[0] = filename;
! 	} else {
! 		filename = argv[1];
! 		argv++;
! 		argc--;
! 	}
  
  	if (Py_VerboseFlag ||
***************
*** 574,578 ****
  	PyMac_InstallNavServicesForSF();
  
! 	PySys_SetArgv(argc-1, argv+1);
  
  	if (filename == NULL && isatty((int)fileno(fp))) {
--- 650,654 ----
  	PyMac_InstallNavServicesForSF();
  
! 	PySys_SetArgv(argc, argv);
  
  	if (filename == NULL && isatty((int)fileno(fp))) {