ronaldoussoren / py2app

py2app is a Python setuptools command which will allow you to make standalone Mac OS X application bundles and plugins from Python scripts.
Other
359 stars 36 forks source link

sandboxing interaction issue #85

Closed ronaldoussoren closed 10 years ago

ronaldoussoren commented 11 years ago

Original report by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


Sandboxing (as introduced in OSX 10.7) doesn't like py2app's executable stubs, in particular the fact that the stub is not linked to the AppKit framework.

This can be seen bij creating a GUI application (using any GUI toolkit) with a file dialog (open/save), the application will crash when trying to show the dialog.

There is an easy workaround for the crash: recompile the executable stub and explicitly link with AppKit.framework.

That is a suboptimal solution though:

1) the stub code uses dynamic loading tricks to avoid linking to AppKit, those tricks could be removed

2) the dynamic loading tricks are there for a reason: without those tricks the application will trigger compatibility modes in system framework (that is, some frameworks behave slightly differently when the binary is linked with older SDKs).

The solution for that would be to recompile the stubs with every new OSX release. That should only be necessary for the 'intel', 'i386' and 'x86_64' targets (the other ones include PPC code)

ronaldoussoren commented 11 years ago

Original comment by Falkor Systems, Inc. (Bitbucket: FalkorSystems, GitHub: FalkorSystems).


Hello. I am not familiar with mac development, but I'm trying to use py2app to build a Qt Python app for the Mac App store. Can you point me to a reference that will help me implement the above workaround?

Thanks.

ronaldoussoren commented 11 years ago

Original comment by Falkor Systems, Inc. (Bitbucket: FalkorSystems, GitHub: FalkorSystems).


I tried to do what was recommended here:

http://python.6.n6.nabble.com/How-to-make-py2app-app-stub-link-to-Cocoa-framework-td2157525.html

But that did not work. These are my errors:

#!console

3/30/13 7:04:01.840 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/__init__.pyc
3/30/13 7:04:01.843 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/error.pyc
3/30/13 7:04:01.846 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/tokens.pyc
3/30/13 7:04:01.848 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/events.pyc
3/30/13 7:04:01.851 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/nodes.pyc
3/30/13 7:04:01.853 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/loader.pyc
3/30/13 7:04:01.855 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/reader.pyc
3/30/13 7:04:01.891 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/scanner.pyc
3/30/13 7:04:01.897 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/parser.pyc
3/30/13 7:04:01.899 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/composer.pyc
3/30/13 7:04:01.905 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/constructor.pyc
3/30/13 7:04:01.915 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/resolver.pyc
3/30/13 7:04:01.923 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/dumper.pyc
3/30/13 7:04:01.933 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/emitter.pyc
3/30/13 7:04:01.935 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/serializer.pyc
3/30/13 7:04:01.940 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/representer.pyc
3/30/13 7:04:01.942 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-unlink /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/yaml/cyaml.pyc
3/30/13 7:04:01.979 PM sandboxd: ([11451]) DeFisheye(11451) deny file-write-create /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/PySide/__init__.pyc
3/30/13 7:04:02.049 PM sandboxd: ([11451]) DeFisheye(11451) deny file-read-data /usr/X11/lib/libpng15.15.dylib
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye: argvemulator warning: fetching events failed
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye: Traceback (most recent call last):
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:   File "/Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/__boot__.py", line 352, in <module>
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:     _run()
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:   File "/Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/__boot__.py", line 347, in _run
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:     exec(compile(source, path, 'exec'), globals(), globals())
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:   File "/Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/defisheye", line 43, in <module>
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:     import cv2
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye: ImportError: dlopen(/Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/Resources/lib/python2.7/lib-dynload/cv2.so, 2): Library not loaded: /usr/X11/lib/libpng15.15.dylib
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:   Referenced from: /Users/sameer/Documents/Falkor Systems/gopro_calibration/DeFisheye/dist/DeFisheye.app/Contents/MacOS/../Frameworks/libopencv_highgui.2.4.4.dylib
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:   Reason: no suitable image found.  Did find:
3/30/13 7:04:02.050 PM [0x0-0xb15b15].com.falkorsystems.defisheye:  /usr/X11/lib/libpng15.15.dylib: open() failed with errno=1
3/30/13 7:04:02.119 PM DeFisheye: DeFisheye Error
3/30/13 7:04:03.683 PM lsboxd: @AE relay 61657674:72617070
3/30/13 7:04:03.703 PM com.apple.launchd.peruser.501: ([0x0-0xb15b15].com.falkorsystems.defisheye[11451]) Exited with code: 255
ronaldoussoren commented 11 years ago

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


Deny-file-write-unlink errors are caused by python trying to byte-compile source, see issue #101.

The other problem is that your application seems to reference shared libraries that aren't in the application bundle, and that shouldn't happen and appears to indicate a problem with py2app.

Do you have a way to reproduce the problem (a simple script that you can share that also has the import error for opencv)?

ronaldoussoren commented 11 years ago

Original comment by James Kruth (Bitbucket: artlogic, GitHub: artlogic).


We're experiencing intermittent crashing from very simple sandboxed Python apps on dialog close (the dialogs show just fine). I'm wondering if this issue is related?

ronaldoussoren commented 11 years ago

Original comment by Falkor Systems, Inc. (Bitbucket: FalkorSystems, GitHub: FalkorSystems).


I'm happy to share with you on github if you want, we're going to open source this once it is ready for release anyway.

But in the meantime here a simple repro, without the deny-file-write-unlink error:

#!python

import cv2

(Most of the below I got from George's posting here of course =)

Here's my entitlements file

#!xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>com.apple.security.app-sandbox</key>   <true/>
</dict>
</plist>

My build script

#!bash

python setup.py py2app 

cd dist

# Sign
codesign --force --verify --verbose \
    --sign "3rd Party Mac Developer Application" \
    --entitlements ../DeFisheye.entitlements \
    DeFisheye.app

codesign --force --verify --verbose \
  --sign "3rd Party Mac Developer Application" \
  --entitlements ../DeFisheye.entitlements \
  DeFisheye.app/Contents/Frameworks/Python.framework/Versions/2.7

codesign --force --verify --verbose \
  --sign "3rd Party Mac Developer Application" \
  --entitlements ../DeFisheye.entitlements \
  DeFisheye.app/Contents/MacOS/python

codesign -vvv -d DeFisheye.app

And the relevant snippet of my setup.py:

#!python

if (platform.system() == "Darwin"):
    from setuptools import setup

    APP = ['defisheye']
    DATA_FILES = ['config']
    OPTIONS = {'argv_emulation': True,
               'packages': 'PySide,yaml',
               'includes': 'cv,cv2',
               'argv_inject': '--gui',
               'iconfile': 'pixmaps/defisheye.icns',
               'plist': {
                   'CFBundleIdentifier': 'com.falkorsystems.defisheye',
                   'CFBundleName': 'DeFisheye',
                   'CFBundleVersion': '0.1.0',
                   'CFBundleShortVersionString': '0.1.0',
                   'CFBundleDocumentTypes': [ { 'CFBundleTypeName': 'MP4' } ],
                   'LSApplicationCategoryType': 'public.app-category.video',
               }
    }

    setup(
        name='DeFisheye',
        app=APP,
        data_files=DATA_FILES,
        options={'py2app': OPTIONS},
        setup_requires=['py2app'],
    )

Thanks!

ronaldoussoren commented 11 years ago

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


James: The crash is likely caused by this issue.

ronaldoussoren commented 11 years ago

Original comment by Falkor Systems, Inc. (Bitbucket: FalkorSystems, GitHub: FalkorSystems).


The app is now in a public repo at

https://github.com/FalkorSystems/DeFisheye

ronaldoussoren commented 10 years ago

Original comment by William McBrine (Bitbucket: wmcbrine, GitHub: wmcbrine).


-nvm-

ronaldoussoren commented 10 years ago

Original comment by William McBrine (Bitbucket: wmcbrine, GitHub: wmcbrine).


-pls delete-

ronaldoussoren commented 10 years ago

Original comment by Ronald Oussoren (Bitbucket: ronaldoussoren, GitHub: ronaldoussoren).


Fixed in the next release