Can executables made with py2app include other terminal scripts and run them?
Asked Answered
M

2

13

So I have a nice python app for OS X that is working great. It runs an external terminal script and I would like to include that in with my python app. Ideally, I'd be able to run py2app and have this script bundled up with it into the executable and then be able to include it and run it in the python portion of my code. Is this possible?

Thanks in advance!

Extra Edit: The script that I am using is compiled. I can't just look inside and paste it.

Mickimickie answered 6/7, 2012 at 21:40 Comment(2)
What exactly is a 'terminal script'? Normally I'd assume you meant a shell script, but you say that it's compiled, so that can't be right.Transversal
Sorry, this is an old Mac Unix Executable file (PowerPC). (Script was probably NOT the best word to use.)Mickimickie
T
10

See http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html#option-reference and look at the --resources parameter. For example:

python setup.py py2app --resources foo

If this were a shell script, that would be a perfectly valid thing to do. For a binary executable, it's a bit more hacky. First, p2app's documentation clearly says "not for code!". Second, the OS X documentation says not to put executables in the Resources directory. The main reason for this is code signing: the default settings "seal" everything in Resources as part of the main app's signature, but separate executables are not supposed to be sealed that way, they're supposed to be signed separately.

However, all that being said, it will still work. Except that it probably won't end up with +x permissions, so after your py2app step, you'll have to "chmod +x MyApp.app/Contents/Resources/foo" to make it runnable.

You can also use the distutils package_data, data_files, and/or MANIFEST stuff to add arbitrary files with arbitrary relative paths, which might be a better solution, but it's more complicated.

Either way, in your script, you should use the bundle-related path, which you can easily access via PyObjC. Given that you're using a PowerPC executable, you may need so much backward compatibility that you can't rely on that, in which case you may have to make do with just "../Resources/foo", but otherwise, it looks like this:

import Foundation
# ...
bundle = Foundation.NSBundle.mainBundle()
path = bundle.pathForResource_ofType_('foo', None)

You can then launch it with NSTask, or subprocess,Popen, os.system, etc.

Transversal answered 7/7, 2012 at 0:14 Comment(2)
This worked fairly well for me except that the unix executable was changed so that it was not recognized as an executable--But all that I needed to do was add it in myself after creating the app. Now it is there and once it is installed with the other applications on a Mac, it has a consistent file path. Thanks for your help!Mickimickie
Actually, that makes sense; I don't know whether it uses install, cp, or ditto to copy resources, but whatever it does, it presumably doesn't preserve privileges, but rather set them to what they should be for resource files. Which might be part of why it says "not for code!" Edited the answer to mention that. Anyway, as long as a post-py2app step is acceptable for you, glad to see that it works.Transversal
N
0

You can always include the os module and call os.system(script);. It executes the given argument in the terminal/command line. For example:

import os
script = " a big long bash script "
os.system(script);
Nordin answered 6/7, 2012 at 21:51 Comment(3)
Unfortunately I'm not very familiar with compiling Python files, so I don't know if it's even possible to include a file like that. I think your best bet is to use os.system() to just run the script that you might keep in the same directory as the app.Nordin
So far that's what I am doing. I just was hoping to bundle it all up to make a nice deliverable for my client.Mickimickie
This doesn't answer his question at all. He knows how to run the script, but he doesn't know how to get it bundled with his py2app bundle, so telling him how to run the script isn't all that helpful.Transversal

© 2022 - 2024 — McMap. All rights reserved.