Commits

Ronald Oussoren committed dc73a58

Make sure bundle template works with system python

The bugfix for issue #10 broke creating bundles with a system
python ('/usr/bin/python'), and --semi-standalone builds. This
patch rectifies that situation.

Comments (0)

Files changed (2)

doc/changelog.rst

   it ever since switching to mercurial, I've added a MANIFEST.in 
   file rather than relying on setuptool's autoguessing of files to include.
 
+- Bundle template works again with semi-standalone builds (such as
+  when using a system python), this rewrites the fix for issue #10
+  mentioned earlier.
+
 py2app 0.5.2
 ------------
 

py2app/bundletemplate/src/main.m

     return nil;
 }
 
+static NSString* getPythonInterpreter(NSString* pyLocation) {
+    NSBundle* bndl;
+    NSString* auxName;
+
+    bndl = bundleBundle();
+
+    auxName = [[bndl infoDictionary] objectForKey:@"PyExecutableName"];
+    if (!auxName) {
+       auxName = @"python";
+    }
+    return [bndl pathForAuxiliaryExecutable:auxName];
+}
+
+
 static
 NSArray *getPythonPathArray(NSDictionary *infoDictionary, NSString *resourcePath) {
     NSMutableArray *pythonPathArray = [NSMutableArray arrayWithObject: resourcePath];
     return mainPyPath;
 }
 
-static
-BOOL getPyOption(NSDictionary *infoDictionary, NSString *optionName) {
-    NSDictionary *pyOptions = [infoDictionary objectForKey:@"PyOptions"];
-    NSNumber *optionAsNumber = [pyOptions objectForKey:optionName];
-    if (optionAsNumber == nil) {
-        return NO;
-    }
-    return [optionAsNumber boolValue];
-}
 
 int pyobjc_main(int argc, char * const *argv, char * const *envp) {
     char* curenv;
     if (!was_initialized) {
         // $PREFIX/Python -> $PREFIX
         NSString *pythonProgramName = [pyLocation stringByDeletingLastPathComponent];
-        if (!getPyOption(infoDictionary, @"alias")) {
+	NSString* interpreter = getPythonInterpreter(pyLocation);
+	struct stat sb;
+
+	if (lstat([interpreter fileSystemRepresentation], &sb) == 0) {
+	    if(!((sb.st_mode & S_IFLNK) == S_IFLNK)) {
+            	setenv("PYTHONHOME", [resourcePath fileSystemRepresentation], 1);
+	    }
+	} else {
             setenv("PYTHONHOME", [resourcePath fileSystemRepresentation], 1);
         }
         
     }
 
     PyObject *res = PyRun_File(mainPyFile, c_mainPyPath, Py_file_input, module_dict, module_dict);
-    Py_DecRef(res);
+    if (res) {
+	    Py_DecRef(res);
+    }
 
 cleanup:
     // un-transfer the environment variables