2011-09-13 3 views
3

Je souhaite créer un ensemble d'applications Mac simple qui appelle un simple script Python. Je veux faire ça en Python.Comment créer un ensemble d'applications Mac pour un script Python via Python

Existe-t-il un moyen facile?

J'ai essayé d'utiliser py2app mais échoue en quelque sorte, par exemple:

from setuptools import setup 
setup(app=["foo.py"], setup_requires=["py2app"]) 

donne:

--------------------------------------------------------------------------- 
SystemExit        Traceback (most recent call last) 
/Users/az/<ipython console> in <module>() 

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.pyc in setup(**attrs) 
    138   ok = dist.parse_command_line() 
    139  except DistutilsArgError, msg: 
--> 140   raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg 
    141 
    142  if DEBUG: 

SystemExit: usage: ipython [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] 
    or: ipython --help [cmd1 cmd2 ...] 
    or: ipython --help-commands 
    or: ipython cmd --help 

error: no commands supplied 
Type %exit or %quit to exit IPython (%Exit or %Quit do so unconditionally). 

J'ai aussi essayé:

import py2app.build_app 
py2app.build_app.py2app("foo.py") 

qui ne fonctionne pas aussi (TypeError: dist must be a Distribution instance) (Je ne sais pas vraiment comment utiliser py2app.build_app.py2app et aussi je n'ai pas vraiment trouvé beaucoup d'exemples/documentation à ce sujet).

Peut-être que setuptools/py2app ou quoi que ce soit sont trop compliqués pour mon cas d'utilisation. Je veux juste créer un simple paquet d'applications vide, y copier un script Python et configurer son Info.plist de telle sorte qu'il appelle le script Python.

+0

Avez-vous essayé suivant http://packages.python.org/py2app/tutorial.html#create-a-setup-py-file ? Parce que les étapes que vous définissez ne semblent pas refléter cela ou le tutoriel setuptools. – millimoose

+0

@Sii: Je ne veux pas créer un fichier setup.py. Je veux le faire depuis un autre script Python. – Albert

+0

Une option consiste à utiliser l'OSX Automator: http://stackoverflow.com/a/31638867/191246 – ccpizza

Répondre

10

C'est exactement ce que je voulais et fonctionne très bien:

#!/usr/bin/python 

import sys 
assert len(sys.argv) > 1 

apppath = sys.argv[1] 

import os, os.path 
assert os.path.splitext(apppath)[1] == ".app" 

os.makedirs(apppath + "/Contents/MacOS") 

version = "1.0.0" 
bundleName = "Test" 
bundleIdentifier = "org.test.test" 

f = open(apppath + "/Contents/Info.plist", "w") 
f.write("""<?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>CFBundleDevelopmentRegion</key> 
    <string>English</string> 
    <key>CFBundleExecutable</key> 
    <string>main.py</string> 
    <key>CFBundleGetInfoString</key> 
    <string>%s</string> 
    <key>CFBundleIconFile</key> 
    <string>app.icns</string> 
    <key>CFBundleIdentifier</key> 
    <string>%s</string> 
    <key>CFBundleInfoDictionaryVersion</key> 
    <string>6.0</string> 
    <key>CFBundleName</key> 
    <string>%s</string> 
    <key>CFBundlePackageType</key> 
    <string>APPL</string> 
    <key>CFBundleShortVersionString</key> 
    <string>%s</string> 
    <key>CFBundleSignature</key> 
    <string>????</string> 
    <key>CFBundleVersion</key> 
    <string>%s</string> 
    <key>NSAppleScriptEnabled</key> 
    <string>YES</string> 
    <key>NSMainNibFile</key> 
    <string>MainMenu</string> 
    <key>NSPrincipalClass</key> 
    <string>NSApplication</string> 
</dict> 
</plist> 
""" % (bundleName + " " + version, bundleIdentifier, bundleName, bundleName + " " + version, version)) 
f.close() 

f = open(apppath + "/Contents/PkgInfo", "w") 
f.write("APPL????") 
f.close() 

f = open(apppath + "/Contents/MacOS/main.py", "w") 
f.write("""#!/usr/bin/python 
print "Hi there" 
""") 
f.close() 

import stat 
oldmode = os.stat(apppath + "/Contents/MacOS/main.py").st_mode 
os.chmod(apppath + "/Contents/MacOS/main.py", oldmode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) 
+1

Vous pourriez jeter un oeil à Platypus, qui crée des enveloppes d'applications Mac à partir de scripts interprétés. http://sveinbjorn.org/platypus – svth

4

Découvrez PyInstaller. Vous lui donnez le chemin de votre script python et il analyse toutes vos importations de paquetages, en extrayant les fichiers binaires nécessaires et en les plaçant dans une archive. Je l'ai utilisé pour un programme python assez complexe, et cela a fonctionné pour moi.

+0

Je ne veux pas tout cela. Je veux juste créer un ensemble d'applications vide. – Albert

+0

Ensuite, votre application ne sera pas portable. – jterrace

+0

Pourquoi pas? Voir ma propre réponse. Cela devrait être portable. – Albert

Questions connexes