Update icon and Add info

This commit is contained in:
Andros Fenollosa
2017-04-20 00:57:59 +02:00
parent 65f275850d
commit 014ffce8ed
182 changed files with 9338 additions and 10 deletions

View File

@ -0,0 +1,34 @@
"""
builds Mac OS X application bundles from Python scripts
New keywords for distutils' setup function specify what to build:
app
list of scripts to convert into gui app bundles
py2app options, to be specified in the options keyword to the setup function:
optimize - string or int (0, 1, or 2)
includes - list of module names to include
packages - list of packages to include with subpackages
ignores - list of modules to ignore if they are not found
excludes - list of module names to exclude
dylib_excludes - list of dylibs and/or frameworks to exclude
resources - list of additional files and folders to include
plist - Info.plist template file, dict, or plistlib.Plist
dist_dir - directory where to build the final files
Items in the macosx list can also be
dictionaries to further customize the build process. The following
keys in the dictionary are recognized, most are optional:
script (MACOSX) - list of python scripts (required)
dest_base - directory and basename for the executable
if a directory is contained, must be the same for all targets
"""
import pkg_resources
__version__ = pkg_resources.require('py2app')[0].version
# This makes the py2app command work in the distutils.core.setup() case
import setuptools

View File

@ -0,0 +1,2 @@
from . import setup
from . import plist_template

View File

@ -0,0 +1,19 @@
#!/bin/sh
#
# This is the default apptemplate error script
#
if ( test -n "$2" ) ; then
echo "$1 Error"
echo "An unexpected error has occurred during execution of the main script"
echo ""
echo "$2: $3"
echo ""
echo "See the Console for a detailed traceback."
else
echo "$1 Error"
# Usage: ERRORURL <anURL> <a button label>, this is used by the
# bundle runner to put up a dialog.
#echo "ERRORURL: http://www.python.org/ Visit the Python Website
# echo "ERRORURL: http://homepages.cwi.nl/~jack/macpython/index.html Visit the MacPython Website"
fi

View File

@ -0,0 +1,132 @@
"""
Append module search paths for third-party packages to sys.path.
This is stripped down and customized for use in py2app applications
"""
import sys
# os is actually in the zip, so we need to do this here.
# we can't call it python24.zip because zlib is not a built-in module (!)
_libdir = '/lib/python' + sys.version[:3]
_parent = '/'.join(__file__.split('/')[:-1])
if not _parent.endswith(_libdir):
_parent += _libdir
sys.path.append(_parent + '/site-packages.zip')
# Stuffit decompresses recursively by default, that can mess up py2app bundles,
# add the uncompressed site-packages to the path to compensate for that.
sys.path.append(_parent + '/site-packages')
USER_SITE=None
import os
try:
basestring
except NameError:
basestring = str
def makepath(*paths):
dir = os.path.abspath(os.path.join(*paths))
return dir, os.path.normcase(dir)
for m in sys.modules.values():
f = getattr(m, '__file__', None)
if isinstance(f, basestring) and os.path.exists(f):
m.__file__ = os.path.abspath(m.__file__)
del m
# This ensures that the initial path provided by the interpreter contains
# only absolute pathnames, even if we're running from the build directory.
L = []
_dirs_in_sys_path = {}
dir = dircase = None # sys.path may be empty at this point
for dir in sys.path:
# Filter out duplicate paths (on case-insensitive file systems also
# if they only differ in case); turn relative paths into absolute
# paths.
dir, dircase = makepath(dir)
if not dircase in _dirs_in_sys_path:
L.append(dir)
_dirs_in_sys_path[dircase] = 1
sys.path[:] = L
del dir, dircase, L
_dirs_in_sys_path = None
def _init_pathinfo():
global _dirs_in_sys_path
_dirs_in_sys_path = d = {}
for dir in sys.path:
if dir and not os.path.isdir(dir):
continue
dir, dircase = makepath(dir)
d[dircase] = 1
def addsitedir(sitedir):
global _dirs_in_sys_path
if _dirs_in_sys_path is None:
_init_pathinfo()
reset = 1
else:
reset = 0
sitedir, sitedircase = makepath(sitedir)
if not sitedircase in _dirs_in_sys_path:
sys.path.append(sitedir) # Add path component
try:
names = os.listdir(sitedir)
except os.error:
return
names.sort()
for name in names:
if name[-4:] == os.extsep + "pth":
addpackage(sitedir, name)
if reset:
_dirs_in_sys_path = None
def addpackage(sitedir, name):
global _dirs_in_sys_path
if _dirs_in_sys_path is None:
_init_pathinfo()
reset = 1
else:
reset = 0
fullname = os.path.join(sitedir, name)
try:
with open(fullname) as f:
while 1:
dir = f.readline()
if not dir:
break
if dir[0] == '#':
continue
if dir.startswith("import"):
exec(dir)
continue
if dir[-1] == '\n':
dir = dir[:-1]
dir, dircase = makepath(sitedir, dir)
if not dircase in _dirs_in_sys_path and os.path.exists(dir):
sys.path.append(dir)
_dirs_in_sys_path[dircase] = 1
except IOError:
return
if reset:
_dirs_in_sys_path = None
#sys.setdefaultencoding('utf-8')
#
# Run custom site specific code, if available.
#
try:
import sitecustomize
except ImportError:
pass
#
# Remove sys.setdefaultencoding() so that users cannot change the
# encoding after initialization. The test for presence is needed when
# this module is run as a script, because this code is executed twice.
#
if hasattr(sys, "setdefaultencoding"):
del sys.setdefaultencoding

View File

@ -0,0 +1,46 @@
import sys
import py2app
__all__ = ['infoPlistDict']
def infoPlistDict(CFBundleExecutable, plist={}):
CFBundleExecutable = CFBundleExecutable
version = sys.version[:3]
pdict = dict(
CFBundleDevelopmentRegion='English',
CFBundleDisplayName=plist.get('CFBundleName', CFBundleExecutable),
CFBundleExecutable=CFBundleExecutable,
CFBundleIconFile=CFBundleExecutable,
CFBundleIdentifier='org.pythonmac.unspecified.%s' % (''.join(CFBundleExecutable.split()),),
CFBundleInfoDictionaryVersion='6.0',
CFBundleName=CFBundleExecutable,
CFBundlePackageType='APPL',
CFBundleShortVersionString=plist.get('CFBundleVersion', '0.0'),
CFBundleSignature='????',
CFBundleVersion='0.0',
LSHasLocalizedDisplayName=False,
NSAppleScriptEnabled=False,
NSHumanReadableCopyright='Copyright not specified',
NSMainNibFile='MainMenu',
NSPrincipalClass='NSApplication',
PyMainFileNames=['__boot__'],
PyResourcePackages=[],
PyRuntimeLocations=[(s % version) for s in [
'@executable_path/../Frameworks/Python.framework/Versions/%s/Python',
'~/Library/Frameworks/Python.framework/Versions/%s/Python',
'/Library/Frameworks/Python.framework/Versions/%s/Python',
'/Network/Library/Frameworks/Python.framework/Versions/%s/Python',
'/System/Library/Frameworks/Python.framework/Versions/%s/Python',
]],
)
pdict.update(plist)
pythonInfo = pdict.setdefault('PythonInfoDict', {})
pythonInfo.update(dict(
PythonLongVersion=sys.version,
PythonShortVersion=sys.version[:3],
PythonExecutable=sys.executable,
))
py2appInfo = pythonInfo.setdefault('py2app', {}).update(dict(
version=py2app.__version__,
template='app',
))
return pdict

View File

@ -0,0 +1,129 @@
import os
import re
import sys
import distutils.sysconfig
import distutils.util
gPreBuildVariants = [
{
'name': 'main-universal',
'target': '10.5',
'cflags': '-g -isysroot /Developer/SDKs/MacOSX10.5.sdk -arch i386 -arch ppc -arch ppc64 -arch x86_64',
'cc': 'gcc-4.2',
},
{
'name': 'main-ppc64',
'target': '10.5',
'cflags': '-g -isysroot /Developer/SDKs/MacOSX10.5.sdk -arch ppc64',
'cc': 'gcc-4.2',
},
{
'name': 'main-x86_64',
'target': '10.5',
'cflags': '-g -arch x86_64',
'cc': '/usr/bin/clang',
},
{
'name': 'main-fat3',
'target': '10.5',
'cflags': '-g -isysroot / -arch i386 -arch ppc -arch x86_64',
'cc': 'gcc-4.2',
},
{
'name': 'main-intel',
'target': '10.5',
'cflags': '-g -arch i386 -arch x86_64 -fexceptions',
'cc': '/usr/bin/clang',
},
{
'name': 'main-i386',
'target': '10.4',
'cflags': '-g -arch i386',
'cc': '/usr/bin/clang',
},
{
'name': 'main-ppc',
'target': '10.3',
'cflags': '-g -isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch ppc',
'cc': 'gcc-4.0',
},
{
'name': 'main-fat',
'target': '10.3',
'cflags': '-g -isysroot /Developer/SDKs/MacOSX10.4u.sdk -arch i386 -arch ppc',
'cc': 'gcc-4.0',
},
]
def main(all=False, arch=None, secondary=False):
basepath = os.path.dirname(__file__)
builddir = os.path.join(basepath, 'prebuilt')
if not os.path.exists(builddir):
os.makedirs(builddir)
src = os.path.join(basepath, 'src', 'main.c')
cfg = distutils.sysconfig.get_config_vars()
BASE_CFLAGS = cfg['CFLAGS']
BASE_CFLAGS = BASE_CFLAGS.replace('-dynamic', '')
while True:
x = re.sub('-arch\s+\S+', '', BASE_CFLAGS)
if x == BASE_CFLAGS:
break
BASE_CFLAGS=x
while True:
x = re.sub('-isysroot\s+\S+', '', BASE_CFLAGS)
if x == BASE_CFLAGS:
break
BASE_CFLAGS=x
if arch is None:
arch = distutils.util.get_platform().split('-')[-1]
if sys.prefix.startswith('/System') and \
sys.version_info[:2] == (2,5):
arch = "fat"
name = 'main-' + arch
root = None
if all:
for entry in gPreBuildVariants:
dest = os.path.join(builddir, entry['name'])
for replace in (0, 1):
if replace:
dest = os.path.join(builddir, entry['name'].replace('main', 'secondary'))
if not os.path.exists(dest) or (
os.stat(dest).st_mtime < os.stat(src).st_mtime):
if root is None:
fp = os.popen('xcode-select -print-path', 'r')
root = fp.read().strip()
fp.close()
print ("rebuilding %s"%(os.path.basename(dest),))
CC=os.path.join(root, 'usr', 'bin', entry['cc'])
CFLAGS = BASE_CFLAGS + ' ' + entry['cflags'].replace('@@XCODE_ROOT@@', root)
if replace:
CFLAGS += " -DPY2APP_SECONDARY"
os.environ['MACOSX_DEPLOYMENT_TARGET'] = entry['target']
os.system('"%(CC)s" -o "%(dest)s" "%(src)s" %(CFLAGS)s -framework Cocoa' % locals())
if secondary:
name = 'secondary-'
else:
name = 'main-'
dest = os.path.join(
builddir,
name + arch
)
return dest
if __name__ == '__main__':
main(all=True)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
""" py2app bootstrap files """

View File

@ -0,0 +1,272 @@
"""
sys.argv emulation
This module starts a basic event loop to collect file- and url-open AppleEvents. Those get
converted to strings and stuffed into sys.argv. When that is done we continue starting
the application.
This is a workaround to convert scripts that expect filenames on the command-line to work
in a GUI environment. GUI applications should not use this feature.
NOTE: This module uses ctypes and not the Carbon modules in the stdlib because the latter
don't work in 64-bit mode and are also not available with python 3.x.
"""
import sys
import os
import time
import ctypes
import struct
class AEDesc (ctypes.Structure):
_fields_ = [
('descKey', ctypes.c_int),
('descContent', ctypes.c_void_p),
]
class EventTypeSpec (ctypes.Structure):
_fields_ = [
('eventClass', ctypes.c_int),
('eventKind', ctypes.c_uint),
]
def _ctypes_setup():
carbon = ctypes.CDLL('/System/Library/Carbon.framework/Carbon')
timer_func = ctypes.CFUNCTYPE(
None, ctypes.c_void_p, ctypes.c_long)
ae_callback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p,
ctypes.c_void_p, ctypes.c_void_p)
carbon.AEInstallEventHandler.argtypes = [
ctypes.c_int, ctypes.c_int, ae_callback,
ctypes.c_void_p, ctypes.c_char ]
carbon.AERemoveEventHandler.argtypes = [
ctypes.c_int, ctypes.c_int, ae_callback,
ctypes.c_char ]
carbon.AEProcessEvent.restype = ctypes.c_int
carbon.AEProcessEvent.argtypes = [ctypes.c_void_p]
carbon.ReceiveNextEvent.restype = ctypes.c_int
carbon.ReceiveNextEvent.argtypes = [
ctypes.c_long, ctypes.POINTER(EventTypeSpec),
ctypes.c_double, ctypes.c_char,
ctypes.POINTER(ctypes.c_void_p)
]
carbon.AEGetParamDesc.restype = ctypes.c_int
carbon.AEGetParamDesc.argtypes = [
ctypes.c_void_p, ctypes.c_int, ctypes.c_int,
ctypes.POINTER(AEDesc)]
carbon.AECountItems.restype = ctypes.c_int
carbon.AECountItems.argtypes = [ ctypes.POINTER(AEDesc),
ctypes.POINTER(ctypes.c_long) ]
carbon.AEGetNthDesc.restype = ctypes.c_int
carbon.AEGetNthDesc.argtypes = [
ctypes.c_void_p, ctypes.c_long, ctypes.c_int,
ctypes.c_void_p, ctypes.c_void_p ]
carbon.AEGetDescDataSize.restype = ctypes.c_int
carbon.AEGetDescDataSize.argtypes = [ ctypes.POINTER(AEDesc) ]
carbon.AEGetDescData.restype = ctypes.c_int
carbon.AEGetDescData.argtypes = [
ctypes.POINTER(AEDesc),
ctypes.c_void_p,
ctypes.c_int,
]
carbon.FSRefMakePath.restype = ctypes.c_int
carbon.FSRefMakePath.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint]
return carbon
def _run_argvemulator(timeout = 60):
# Configure ctypes
carbon = _ctypes_setup()
# Is the emulator running?
running = [True]
timeout = [timeout]
# Configure AppleEvent handlers
ae_callback = carbon.AEInstallEventHandler.argtypes[2]
kAEInternetSuite, = struct.unpack('>i', b'GURL')
kAEISGetURL, = struct.unpack('>i', b'GURL')
kCoreEventClass, = struct.unpack('>i', b'aevt')
kAEOpenApplication, = struct.unpack('>i', b'oapp')
kAEOpenDocuments, = struct.unpack('>i', b'odoc')
keyDirectObject, = struct.unpack('>i', b'----')
typeAEList, = struct.unpack('>i', b'list')
typeChar, = struct.unpack('>i', b'TEXT')
typeFSRef, = struct.unpack('>i', b'fsrf')
FALSE = b'\0'
TRUE = b'\1'
eventLoopTimedOutErr = -9875
kEventClassAppleEvent, = struct.unpack('>i', b'eppc')
kEventAppleEvent = 1
@ae_callback
def open_app_handler(message, reply, refcon):
# Got a kAEOpenApplication event, which means we can
# start up. On some OSX versions this event is even
# sent when an kAEOpenDocuments or kAEOpenURLs event
# is sent later on.
#
# Therefore don't set running to false, but reduce the
# timeout to at most two seconds beyond the current time.
timeout[0] = min(timeout[0], time.time() - start + 2)
#running[0] = False
return 0
carbon.AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
open_app_handler, 0, FALSE)
@ae_callback
def open_file_handler(message, reply, refcon):
listdesc = AEDesc()
sts = carbon.AEGetParamDesc(message, keyDirectObject, typeAEList,
ctypes.byref(listdesc))
if sts != 0:
print("argvemulator warning: cannot unpack open document event")
running[0] = False
return
item_count = ctypes.c_long()
sts = carbon.AECountItems(ctypes.byref(listdesc), ctypes.byref(item_count))
if sts != 0:
print("argvemulator warning: cannot unpack open document event")
running[0] = False
return
desc = AEDesc()
for i in range(item_count.value):
sts = carbon.AEGetNthDesc(ctypes.byref(listdesc), i+1, typeFSRef, 0, ctypes.byref(desc))
if sts != 0:
print("argvemulator warning: cannot unpack open document event")
running[0] = False
return
sz = carbon.AEGetDescDataSize(ctypes.byref(desc))
buf = ctypes.create_string_buffer(sz)
sts = carbon.AEGetDescData(ctypes.byref(desc), buf, sz)
if sts != 0:
print("argvemulator warning: cannot extract open document event")
continue
fsref = buf
buf = ctypes.create_string_buffer(1024)
sts = carbon.FSRefMakePath(ctypes.byref(fsref), buf, 1023)
if sts != 0:
print("argvemulator warning: cannot extract open document event")
continue
if sys.version_info[0] > 2:
sys.argv.append(buf.value.decode('utf-8'))
else:
sys.argv.append(buf.value)
running[0] = False
return 0
carbon.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
open_file_handler, 0, FALSE)
@ae_callback
def open_url_handler(message, reply, refcon):
listdesc = AEDesc()
ok = carbon.AEGetParamDesc(message, keyDirectObject, typeAEList,
ctypes.byref(listdesc))
if ok != 0:
print("argvemulator warning: cannot unpack open document event")
running[0] = False
return
item_count = ctypes.c_long()
sts = carbon.AECountItems(ctypes.byref(listdesc), ctypes.byref(item_count))
if sts != 0:
print("argvemulator warning: cannot unpack open url event")
running[0] = False
return
desc = AEDesc()
for i in range(item_count.value):
sts = carbon.AEGetNthDesc(ctypes.byref(listdesc), i+1, typeChar, 0, ctypes.byref(desc))
if sts != 0:
print("argvemulator warning: cannot unpack open URL event")
running[0] = False
return
sz = carbon.AEGetDescDataSize(ctypes.byref(desc))
buf = ctypes.create_string_buffer(sz)
sts = carbon.AEGetDescData(ctypes.byref(desc), buf, sz)
if sts != 0:
print("argvemulator warning: cannot extract open URL event")
else:
if sys.version_info[0] > 2:
sys.argv.append(buf.value.decode('utf-8'))
else:
sys.argv.append(buf.value)
running[0] = False
return 0
carbon.AEInstallEventHandler(kAEInternetSuite, kAEISGetURL,
open_url_handler, 0, FALSE)
# Remove the funny -psn_xxx_xxx argument
if len(sys.argv) > 1 and sys.argv[1].startswith('-psn_'):
del sys.argv[1]
start = time.time()
now = time.time()
eventType = EventTypeSpec()
eventType.eventClass = kEventClassAppleEvent
eventType.eventKind = kEventAppleEvent
while running[0] and now - start < timeout[0]:
event = ctypes.c_void_p()
sts = carbon.ReceiveNextEvent(1, ctypes.byref(eventType),
start + timeout[0] - now, TRUE, ctypes.byref(event))
if sts == eventLoopTimedOutErr:
break
elif sts != 0:
print("argvemulator warning: fetching events failed")
break
sts = carbon.AEProcessEvent(event)
if sts != 0:
print("argvemulator warning: processing events failed")
break
carbon.AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
open_app_handler, FALSE)
carbon.AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
open_file_handler, FALSE)
carbon.AERemoveEventHandler(kAEInternetSuite, kAEISGetURL,
open_url_handler, FALSE)
def _argv_emulation():
import sys, os
# only use if started by LaunchServices
if os.environ.get('_PY2APP_LAUNCHED_'):
_run_argvemulator()
_argv_emulation()

View File

@ -0,0 +1,7 @@
def _argv_inject(argv):
import sys
# only use if started by LaunchServices
if len(sys.argv) > 1 and sys.argv[1].startswith('-psn'):
sys.argv[1:2] = argv
else:
sys.argv[1:1] = argv

View File

@ -0,0 +1,42 @@
import re, sys
cookie_re = re.compile(b"coding[:=]\s*([-\w.]+)")
if sys.version_info[0] == 2:
default_encoding = 'ascii'
else:
default_encoding = 'utf-8'
def guess_encoding(fp):
for i in range(2):
ln = fp.readline()
m = cookie_re.search(ln)
if m is not None:
return m.group(1).decode('ascii')
return default_encoding
def _run():
global __file__
import os, site
sys.frozen = 'macosx_app'
argv0 = os.path.basename(os.environ['ARGVZERO'])
script = SCRIPT_MAP.get(argv0, DEFAULT_SCRIPT)
sys.argv[0] = __file__ = script
if sys.version_info[0] == 2:
with open(script, 'rU') as fp:
source = fp.read() + "\n"
else:
with open(script, 'rb') as fp:
encoding = guess_encoding(fp)
with open(script, 'r', encoding=encoding) as fp:
source = fp.read() + '\n'
BOM=b'\xef\xbb\xbf'.decode('utf-8')
if source.startswith(BOM):
source = source[1:]
exec(compile(source, script, 'exec'), globals(), globals())

View File

@ -0,0 +1,45 @@
import re, sys
cookie_re = re.compile(b"coding[:=]\s*([-\w.]+)")
if sys.version_info[0] == 2:
default_encoding = 'ascii'
else:
default_encoding = 'utf-8'
def guess_encoding(fp):
for i in range(2):
ln = fp.readline()
m = cookie_re.search(ln)
if m is not None:
return m.group(1).decode('ascii')
return default_encoding
def _run():
global __file__
import os, site
sys.frozen = 'macosx_plugin'
base = os.environ['RESOURCEPATH']
if 'ARGVZERO' in os.environ:
argv0 = os.path.basename(os.environ['ARGVZERO'])
else:
argv0 = None
script = SCRIPT_MAP.get(argv0, DEFAULT_SCRIPT)
sys.argv[0] = __file__ = path = os.path.join(base, script)
if sys.version_info[0] == 2:
with open(path, 'rU') as fp:
source = fp.read() + "\n"
else:
with open(path, 'rb') as fp:
encoding = guess_encoding(fp)
with open(path, 'r', encoding=encoding) as fp:
source = fp.read() + '\n'
BOM=b'\xef\xbb\xbf'.decode('utf-8')
if source.startswith(BOM):
source = source[1:]
exec(compile(source, script, 'exec'), globals(), globals())

View File

@ -0,0 +1,42 @@
import re, sys
cookie_re = re.compile(b"coding[:=]\s*([-\w.]+)")
if sys.version_info[0] == 2:
default_encoding = 'ascii'
else:
default_encoding = 'utf-8'
def guess_encoding(fp):
for i in range(2):
ln = fp.readline()
m = cookie_re.search(ln)
if m is not None:
return m.group(1).decode('ascii')
return default_encoding
def _run():
global __file__
import os, site
sys.frozen = 'macosx_app'
base = os.environ['RESOURCEPATH']
argv0 = os.path.basename(os.environ['ARGVZERO'])
script = SCRIPT_MAP.get(argv0, DEFAULT_SCRIPT)
path = os.path.join(base, script)
sys.argv[0] = __file__ = path
if sys.version_info[0] == 2:
with open(path, 'rU') as fp:
source = fp.read() + "\n"
else:
with open(path, 'rb') as fp:
encoding = guess_encoding(fp)
with open(path, 'r', encoding=encoding) as fp:
source = fp.read() + '\n'
BOM=b'\xef\xbb\xbf'.decode('utf-8')
if source.startswith(BOM):
source = source[1:]
exec(compile(source, path, 'exec'), globals(), globals())

View File

@ -0,0 +1,45 @@
import re, sys
cookie_re = re.compile(b"coding[:=]\s*([-\w.]+)")
if sys.version_info[0] == 2:
default_encoding = 'ascii'
else:
default_encoding = 'utf-8'
def guess_encoding(fp):
for i in range(2):
ln = fp.readline()
m = cookie_re.search(ln)
if m is not None:
return m.group(1).decode('ascii')
return default_encoding
def _run():
global __file__
import os, site
sys.frozen = 'macosx_plugin'
base = os.environ['RESOURCEPATH']
if 'ARGVZERO' in os.environ:
argv0 = os.path.basename(os.environ['ARGVZERO'])
else:
argv0 = None
script = SCRIPT_MAP.get(argv0, DEFAULT_SCRIPT)
__file__ = path = os.path.join(base, script)
if sys.version_info[0] == 2:
with open(path, 'rU') as fp:
source = fp.read() + "\n"
else:
with open(path, 'rb') as fp:
encoding = guess_encoding(fp)
with open(path, 'r', encoding=encoding) as fp:
source = fp.read() + '\n'
BOM=b'\xef\xbb\xbf'.decode('utf-8')
if source.startswith(BOM):
source = source[1:]
exec(compile(source, path, 'exec'), globals(), globals())

View File

@ -0,0 +1,4 @@
def _chdir_resource():
import os
os.chdir(os.environ['RESOURCEPATH'])
_chdir_resource()

View File

@ -0,0 +1,8 @@
def _setup_ctypes():
from ctypes.macholib import dyld
import os
frameworks = os.path.join(os.environ['RESOURCEPATH'], '..', 'Frameworks')
dyld.DEFAULT_FRAMEWORK_FALLBACK.insert(0, frameworks)
dyld.DEFAULT_LIBRARY_FALLBACK.insert(0, frameworks)
_setup_ctypes()

View File

@ -0,0 +1,7 @@
def _disable_linecache():
import linecache
def fake_getline(*args, **kwargs):
return ''
linecache.orig_getline = linecache.getline
linecache.getline = fake_getline
_disable_linecache()

View File

@ -0,0 +1,79 @@
def _emulate_shell_environ():
import os
import sys
import time
import subprocess
if sys.version_info[0] > 2:
env = os.environb
else:
env = os.environ
split_char = b'='
# Start 'login -qf $LOGIN' in a pseudo-tty. The pseudo-tty
# is required to get the right behavior from the shell, without
# a tty the shell won't properly initialize the environment.
#
# NOTE: The code is very carefull w.r.t. getting the login
# name, the application shouldn't crash when the shell information
# cannot be retrieved
master, slave = os.openpty()
pid = os.fork()
try:
login = os.getlogin()
except AttributeError:
try:
login = os.environ['LOGNAME']
except KeyError:
login = None
if login is not None:
if pid == 0:
# Child
os.close(master)
os.setsid()
os.dup2(slave, 0)
os.dup2(slave, 1)
os.dup2(slave, 2)
os.execv('/usr/bin/login', ['login', '-qf', login])
os._exit(42)
else:
# Parent
os.close(slave)
# Echo markers around the actual output of env, that makes it
# easier to find the real data between other data printed
# by the shell.
os.write(master, b'echo "---------";env;echo "-----------"\r\n')
os.write(master, b'exit\r\n')
time.sleep(1)
data = []
b = os.read(master, 2048)
while b:
data.append(b)
b = os.read(master, 2048)
data = b''.join(data)
os.waitpid(pid, 0)
in_data = False
for ln in data.splitlines():
if not in_data:
if ln.strip().startswith(b'--------'):
in_data = True
continue
if ln.startswith(b'--------'):
break
try:
key, value = ln.rstrip().split(split_char, 1)
except:
pass
else:
env[key] = value
_emulate_shell_environ()

View File

@ -0,0 +1,23 @@
def _import_encodings():
import os
import imp
import encodings
import pkgutil
import sys
del sys.path[:2]
import encodings.aliases
encodings.__path__ = pkgutil.extend_path(
encodings.__path__,
encodings.__name__)
#imp.reload(encodings)
import encodings.mac_roman
encodings.aliases.__file__ = os.path.join(
os.path.dirname(encodings.mac_roman.__file__),
'aliases.py' + encodings.mac_roman.__file__[:-1])
imp.reload(encodings.aliases)
imp.reload(encodings)
_import_encodings()

View File

@ -0,0 +1,3 @@
def _path_inject(paths):
import sys
sys.path[:0] = paths

View File

@ -0,0 +1,7 @@
def _reset_sys_path():
# Clear generic sys.path[0]
import sys, os
resources = os.environ['RESOURCEPATH']
while sys.path[0] == resources:
del sys.path[0]
_reset_sys_path()

View File

@ -0,0 +1,11 @@
def _update_path():
import os, sys
resources = os.environ['RESOURCEPATH']
sys.path.append(os.path.join(
resources, 'lib', 'python%d.%d'%(sys.version_info[:2]), 'lib-dynload'))
sys.path.append(os.path.join(
resources, 'lib', 'python%d.%d'%(sys.version_info[:2])))
sys.path.append(os.path.join(
resources, 'lib', 'python%d.%d'%(sys.version_info[:2]), 'site-packages.zip'))
_update_path()

View File

@ -0,0 +1,20 @@
def _included_subpackages(packages):
for pkg in packages:
pass
class Finder (object):
def find_module(self, fullname, path=None):
if fullname in _path_hooks:
return Loader()
class Loader (object):
def load_module(self, fullname):
import imp, os
pkg_dir = os.path.join(os.environ['RESOURCEPATH'],
'lib', 'python%d.%d'%(sys.version_info[:2]))
return imp.load_module(
fullname, None,
os.path.join(pkg_dir, fullname), ('', '', imp.PKG_DIRECTORY))
import sys
sys.meta_path.insert(0, Finder())

View File

@ -0,0 +1,13 @@
def _setup_pkgresources():
import pkg_resources
import os, plistlib
pl = plistlib.readPlist(os.path.join(
os.path.dirname(os.getenv('RESOURCEPATH')), "Info.plist"))
appname = pl.get('CFBundleIdentifier')
if appname is None:
appname = pl['CFBundleDisplayName']
path = os.path.expanduser('~/Library/Caches/%s/python-eggs'%(appname,))
pkg_resources.set_extraction_path(path)
_setup_pkgresources()

View File

@ -0,0 +1,24 @@
def _site_packages():
import site, sys, os
paths = []
prefixes = [sys.prefix]
if sys.exec_prefix != sys.prefix:
prefixes.append(sys.exec_prefix)
for prefix in prefixes:
paths.append(os.path.join(prefix, 'lib', 'python' + sys.version[:3],
'site-packages'))
if os.path.join('.framework', '') in os.path.join(sys.prefix, ''):
home = os.environ.get('HOME')
if home:
paths.append(os.path.join(home, 'Library', 'Python',
sys.version[:3], 'site-packages'))
# Work around for a misfeature in setuptools: easy_install.pth places
# site-packages way to early on sys.path and that breaks py2app bundles.
# NOTE: this is hacks into an undocumented feature of setuptools and
# might stop to work without warning.
sys.__egginsert = len(sys.path)
for path in paths:
site.addsitedir(path)
_site_packages()

View File

@ -0,0 +1,9 @@
""" Add Apple's additional packages to sys.path """
def add_system_python_extras():
import site, sys
ver = '%s.%s'%(sys.version_info[:2])
site.addsitedir('/System/Library/Frameworks/Python.framework/Versions/%s/Extras/lib/python'%(ver,))
add_system_python_extras()

View File

@ -0,0 +1,33 @@
def _fixup_virtualenv(real_prefix):
import sys, os
sys.real_prefix = real_prefix
# NOTE: The adjustment code is based from logic in the site.py
# installed by virtualenv 1.8.2 (but simplified by removing support
# for platforms that aren't supported by py2app)
paths = [os.path.join(sys.real_prefix, 'lib', 'python'+sys.version[:3])]
hardcoded_relative_dirs = paths[:]
plat_path = os.path.join(sys.real_prefix, 'lib', 'python'+sys.version[:3],
'plat-%s' % sys.platform)
if os.path.exists(plat_path):
paths.append(plat_path)
# This is hardcoded in the Python executable, but
# relative to sys.prefix, so we have to fix up:
for path in list(paths):
tk_dir = os.path.join(path, 'lib-tk')
if os.path.exists(tk_dir):
paths.append(tk_dir)
# These are hardcoded in the Apple's Python executable,
# but relative to sys.prefix, so we have to fix them up:
hardcoded_paths = [os.path.join(relative_dir, module)
for relative_dir in hardcoded_relative_dirs
for module in ('plat-darwin', 'plat-mac', 'plat-mac/lib-scriptpackages')]
for path in hardcoded_paths:
if os.path.exists(path):
paths.append(path)
sys.path.extend(paths)

View File

@ -0,0 +1,32 @@
def _site_packages(prefix, real_prefix, global_site_packages):
import site, sys, os
paths = []
prefixes = [sys.prefix]
paths.append(os.path.join(prefix, 'lib', 'python' + sys.version[:3],
'site-packages'))
if os.path.join('.framework', '') in os.path.join(prefix, ''):
home = os.environ.get('HOME')
if home:
paths.append(os.path.join(home, 'Library', 'Python',
sys.version[:3], 'site-packages'))
# Work around for a misfeature in setuptools: easy_install.pth places
# site-packages way to early on sys.path and that breaks py2app bundles.
# NOTE: this is hacks into an undocumented feature of setuptools and
# might stop to work without warning.
sys.__egginsert = len(sys.path)
for path in paths:
site.addsitedir(path)
# Ensure that the global site packages get placed on sys.path after
# the site packages from the virtual environment (this functionality
# is also in virtualenv)
sys.__egginsert = len(sys.path)
if global_site_packages:
site.addsitedir(os.path.join(real_prefix, 'lib', 'python' + sys.version[:3],
'site-packages'))

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
from . import setup
from . import plist_template

View File

@ -0,0 +1,12 @@
#!/bin/sh
#
# This is the default bundletemplate error script
# Note that this DOES NOT present a GUI dialog, because
# it has no output on stdout, and has a return value of 0.
#
if ( test -n "$2" ) ; then
echo "[$1] Unexpected Exception:" 1>&2
echo "$2: $3" 1>&2
else
echo "[$1] Could not find a suitable Python runtime" 1>&2
fi

View File

@ -0,0 +1,132 @@
"""
Append module search paths for third-party packages to sys.path.
This is stripped down and customized for use in py2app applications
"""
import sys
# os is actually in the zip, so we need to do this here.
# we can't call it python24.zip because zlib is not a built-in module (!)
_libdir = '/lib/python' + sys.version[:3]
_parent = '/'.join(__file__.split('/')[:-1])
if not _parent.endswith(_libdir):
_parent += _libdir
sys.path.append(_parent + '/site-packages.zip')
# Stuffit decompresses recursively by default, that can mess up py2app bundles,
# add the uncompressed site-packages to the path to compensate for that.
sys.path.append(_parent + '/site-packages')
USER_SITE=None
import os
try:
basestring
except NameError:
basestring = str
def makepath(*paths):
dir = os.path.abspath(os.path.join(*paths))
return dir, os.path.normcase(dir)
for m in sys.modules.values():
f = getattr(m, '__file__', None)
if isinstance(f, basestring) and os.path.exists(f):
m.__file__ = os.path.abspath(m.__file__)
del m
# This ensures that the initial path provided by the interpreter contains
# only absolute pathnames, even if we're running from the build directory.
L = []
_dirs_in_sys_path = {}
dir = dircase = None # sys.path may be empty at this point
for dir in sys.path:
# Filter out duplicate paths (on case-insensitive file systems also
# if they only differ in case); turn relative paths into absolute
# paths.
dir, dircase = makepath(dir)
if not dircase in _dirs_in_sys_path:
L.append(dir)
_dirs_in_sys_path[dircase] = 1
sys.path[:] = L
del dir, dircase, L
_dirs_in_sys_path = None
def _init_pathinfo():
global _dirs_in_sys_path
_dirs_in_sys_path = d = {}
for dir in sys.path:
if dir and not os.path.isdir(dir):
continue
dir, dircase = makepath(dir)
d[dircase] = 1
def addsitedir(sitedir):
global _dirs_in_sys_path
if _dirs_in_sys_path is None:
_init_pathinfo()
reset = 1
else:
reset = 0
sitedir, sitedircase = makepath(sitedir)
if not sitedircase in _dirs_in_sys_path:
sys.path.append(sitedir) # Add path component
try:
names = os.listdir(sitedir)
except os.error:
return
names.sort()
for name in names:
if name[-4:] == os.extsep + "pth":
addpackage(sitedir, name)
if reset:
_dirs_in_sys_path = None
def addpackage(sitedir, name):
global _dirs_in_sys_path
if _dirs_in_sys_path is None:
_init_pathinfo()
reset = 1
else:
reset = 0
fullname = os.path.join(sitedir, name)
try:
with open(fullname) as f:
while 1:
dir = f.readline()
if not dir:
break
if dir[0] == '#':
continue
if dir.startswith("import"):
exec(dir)
continue
if dir[-1] == '\n':
dir = dir[:-1]
dir, dircase = makepath(sitedir, dir)
if not dircase in _dirs_in_sys_path and os.path.exists(dir):
sys.path.append(dir)
_dirs_in_sys_path[dircase] = 1
except IOError:
return
if reset:
_dirs_in_sys_path = None
#sys.setdefaultencoding('utf-8')
#
# Run custom site specific code, if available.
#
try:
import sitecustomize
except ImportError:
pass
#
# Remove sys.setdefaultencoding() so that users cannot change the
# encoding after initialization. The test for presence is needed when
# this module is run as a script, because this code is executed twice.
#
if hasattr(sys, "setdefaultencoding"):
del sys.setdefaultencoding

View File

@ -0,0 +1,51 @@
import sys
import py2app
__all__ = ['infoPlistDict']
def infoPlistDict(CFBundleExecutable, plist={}):
CFBundleExecutable = CFBundleExecutable
NSPrincipalClass = ''.join(CFBundleExecutable.split())
version = sys.version[:3]
pdict = dict(
CFBundleDevelopmentRegion='English',
CFBundleDisplayName=plist.get('CFBundleName', CFBundleExecutable),
CFBundleExecutable=CFBundleExecutable,
CFBundleIconFile=CFBundleExecutable,
CFBundleIdentifier='org.pythonmac.unspecified.%s' % (NSPrincipalClass,),
CFBundleInfoDictionaryVersion='6.0',
CFBundleName=CFBundleExecutable,
CFBundlePackageType='BNDL',
CFBundleShortVersionString=plist.get('CFBundleVersion', '0.0'),
CFBundleSignature='????',
CFBundleVersion='0.0',
LSHasLocalizedDisplayName=False,
NSAppleScriptEnabled=False,
NSHumanReadableCopyright='Copyright not specified',
NSMainNibFile='MainMen',
NSPrincipalClass=NSPrincipalClass,
PyMainFileNames=['__boot__'],
PyResourcePackages=[ (s % version) for s in [
'lib/python%s',
'lib/python%s/lib-dynload',
'lib/python%s/site-packages.zip',
]] + [ 'lib/python%s.zip' % version.replace('.', '') ],
PyRuntimeLocations=[(s % version) for s in [
'@executable_path/../Frameworks/Python.framework/Versions/%s/Python',
'~/Library/Frameworks/Python.framework/Versions/%s/Python',
'/Library/Frameworks/Python.framework/Versions/%s/Python',
'/Network/Library/Frameworks/Python.framework/Versions/%s/Python',
'/System/Library/Frameworks/Python.framework/Versions/%s/Python',
]],
)
pdict.update(plist)
pythonInfo = pdict.setdefault('PythonInfoDict', {})
pythonInfo.update(dict(
PythonLongVersion=sys.version,
PythonShortVersion=sys.version[:3],
PythonExecutable=sys.executable,
))
py2appInfo = pythonInfo.setdefault('py2app', {}).update(dict(
version=py2app.__version__,
template='bundle',
))
return pdict

View File

@ -0,0 +1,122 @@
import os
import re
import sys
import distutils.sysconfig
import distutils.util
gPreBuildVariants = [
{
'name': 'main-universal',
'target': '10.5',
'cflags': '-isysroot @@XCODE_ROOT@@/SDKs/MacOSX10.5.sdk -arch i386 -arch ppc -arch ppc64 -arch x86_64',
'cc': 'gcc-4.2',
},
{
'name': 'main-ppc64',
'target': '10.5',
'cflags': '-isysroot @@XCODE_ROOT@@/SDKs/MacOSX10.5.sdk -arch ppc64',
'cc': 'gcc-4.2',
},
{
'name': 'main-x86_64',
'target': '10.5',
'cflags': '-isysroot / -arch x86_64',
'cc': 'clang',
},
{
'name': 'main-fat3',
'target': '10.5',
'cflags': '-isysroot / -arch i386 -arch ppc -arch x86_64',
'cc': 'gcc-4.2',
},
{
'name': 'main-intel',
'target': '10.5',
'cflags': '-isysroot / -arch i386 -arch x86_64',
'cc': 'clang',
},
{
'name': 'main-i386',
'target': '10.3',
#'cflags': '-isysroot @@XCODE_ROOT@@/SDKs/MacOSX10.4u.sdk -arch i386',
'cflags': '-arch i386 -isysroot /',
'cc': 'clang',
},
{
'name': 'main-ppc',
'target': '10.3',
'cflags': '-isysroot @@XCODE_ROOT@@/SDKs/MacOSX10.4u.sdk -arch ppc',
'cc': 'gcc-4.0',
},
{
'name': 'main-fat',
'target': '10.3',
'cflags': '-isysroot @@XCODE_ROOT@@/SDKs/MacOSX10.4u.sdk -arch i386 -arch ppc',
'cc': 'gcc-4.0',
},
]
def main(all=False, arch=None):
basepath = os.path.dirname(__file__)
builddir = os.path.join(basepath, 'prebuilt')
if not os.path.exists(builddir):
os.makedirs(builddir)
src = os.path.join(basepath, 'src', 'main.m')
cfg = distutils.sysconfig.get_config_vars()
BASE_CFLAGS = cfg['CFLAGS']
BASE_CFLAGS = BASE_CFLAGS.replace('-dynamic', '')
BASE_CFLAGS += ' -bundle -framework Foundation -framework AppKit'
while True:
x = re.sub('-arch\s+\S+', '', BASE_CFLAGS)
if x == BASE_CFLAGS:
break
BASE_CFLAGS=x
while True:
x = re.sub('-isysroot\s+\S+', '', BASE_CFLAGS)
if x == BASE_CFLAGS:
break
BASE_CFLAGS=x
if arch is None:
arch = distutils.util.get_platform().split('-')[-1]
if sys.prefix.startswith('/System') and \
sys.version_info[:2] == (2,5):
arch = "fat"
name = 'main-' + arch
root = None
if all:
for entry in gPreBuildVariants:
if (not all) and entry['name'] != name: continue
dest = os.path.join(builddir, entry['name'])
if not os.path.exists(dest) or (
os.stat(dest).st_mtime < os.stat(src).st_mtime):
if root is None:
fp = os.popen('xcode-select -print-path', 'r')
root = fp.read().strip()
fp.close()
print ("rebuilding %s"%(entry['name']))
#CC=os.path.join(root, 'usr', 'bin', entry['cc'])
CC=entry['cc']
CFLAGS = BASE_CFLAGS + ' ' + entry['cflags'].replace('@@XCODE_ROOT@@', root)
os.environ['MACOSX_DEPLOYMENT_TARGET'] = entry['target']
os.system('"%(CC)s" -o "%(dest)s" "%(src)s" %(CFLAGS)s' % locals())
dest = os.path.join(
builddir,
'main-' + arch
)
return dest
if __name__ == '__main__':
main(all=True)

View File

@ -0,0 +1,692 @@
//
// main.m
// apptemplate
//
// Created by Bob Ippolito on Mon September 20 2004.
// Copyright (c) 2004 Bob Ippolito. All rights reserved.
//
#import <Foundation/Foundation.h>
#include <mach-o/dyld.h>
#include <mach-o/loader.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syslimits.h>
#include <crt_externs.h>
#include <wchar.h>
#include <locale.h>
#include <langinfo.h>
#include <objc/objc-class.h>
//
// Constants
//
NSString *ERR_CANNOT_SAVE_LOCALE = @"Cannot save locale information";
NSString *ERR_REALLYBADTITLE = @"The bundle could not be launched.";
NSString *ERR_TITLEFORMAT = @"%@ has encountered a fatal error, and will now terminate.";
NSString *ERR_PYRUNTIMELOCATIONS = @"The Info.plist file must have a PyRuntimeLocations array containing string values for preferred Python runtime locations. These strings should be \"otool -L\" style mach ids; \"@executable_stub\" and \"~\" prefixes will be translated accordingly.";
NSString *ERR_NOPYTHONRUNTIME = @"A Python runtime could be located. You may need to install a framework build of Python, or edit the PyRuntimeLocations array in this bundle's Info.plist file.\rThese runtime locations were attempted:\r\r";
NSString *ERR_NOPYTHONSCRIPT = @"A main script could not be located in the Resources folder.\rThese files were tried:\r\r";
NSString *ERR_LINKERRFMT = @"An internal error occurred while attempting to link with:\r\r%s\r\rSee the Console for a detailed dyld error message";
NSString *ERR_PYTHONEXCEPTION = @"An uncaught exception was raised during execution of the main script:\r\r%@: %@\r\rThis may mean that an unexpected error has occurred, or that you do not have all of the dependencies for this bundle.";
NSString *ERR_COLONPATH = @"Python bundles can not currently run from paths containing a '/' (or ':' from the Terminal).";
#define PYMACAPP_NSIMAGEFLAGS (NSADDIMAGE_OPTION_RETURN_ON_ERROR | NSADDIMAGE_OPTION_WITH_SEARCHING)
#define PYMACAPP_NSLOOKUPSYMBOLINIMAGEFLAGS (NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR)
//
// Typedefs
//
#define Py_file_input 257
typedef int PyObject;
typedef int PyThreadState;
typedef enum {PyGILState_LOCKED, PyGILState_UNLOCKED} PyGILState_STATE;
typedef PyGILState_STATE (*PyGILState_EnsurePtr)(void);
typedef void (*PyGILState_ReleasePtr)(PyGILState_STATE);
typedef PyThreadState *(*PyThreadState_SwapPtr)(PyThreadState *);
typedef void (*PyEval_ReleaseLockPtr)(void);
typedef void (*PyErr_ClearPtr)(void);
typedef void (*PyErr_PrintPtr)(void);
typedef int (*PyErr_OccurredPtr)(void);
typedef PyObject *(*PyBytes_FromStringPtr)(const char *);
typedef int (*PyList_InsertPtr)(PyObject *, int, PyObject *);
typedef void (*Py_DecRefPtr)(PyObject *);
typedef void (*Py_SetProgramNamePtr)(const wchar_t *);
typedef int (*Py_IsInitializedPtr)(void);
typedef void (*Py_InitializePtr)(void);
typedef void (*PyEval_InitThreadsPtr)(void);
typedef PyObject *(*PyRun_FilePtr)(FILE *, const char *, int, PyObject *, PyObject *);
typedef PyObject *(*PySys_GetObjectPtr)(const char *);
typedef int *(*PySys_SetArgvPtr)(int argc, char **argv);
typedef PyObject *(*PyObject_StrPtr)(PyObject *);
typedef const char *(*PyBytes_AsStringPtr)(PyObject *);
typedef PyObject *(*PyObject_GetAttrStringPtr)(PyObject *, const char *);
typedef PyObject *(*PyObject_CallMethodPtr)(PyObject *, const char *, const char *, ...);
typedef PyObject *(*PyImport_ImportModulePtr)(char *);
typedef PyObject *(*PyImport_AddModulePtr)(char *);
typedef PyObject *(*PyModule_AddStringConstantPtr)(PyObject *, char *, char *);
typedef PyObject *(*PyModule_AddObjectPtr)(PyObject *, char *, PyObject *);
typedef PyObject *(*PyModule_GetDictPtr)(PyObject *);
typedef void (*PyObject_SetItemPtr)(PyObject *, PyObject *, PyObject *);
typedef wchar_t* (*_Py_DecodeUTF8_surrogateescapePtr)(const char *s, ssize_t size);
//
// Signatures
//
static void DefaultDecRef(PyObject *op);
static int report_error(NSString *err);
static int report_linkEdit_error(const char* name);
static int report_script_error(NSString *err, NSString *errClassName, NSString *errName);
static NSString *pyStandardizePath(NSString *pyLocation);
static BOOL doesPathExist(NSString *path);
static NSString *getBundleName(void);
static NSString *getErrorTitle(NSString *bundleName);
static const char *bundlePath(void);
static NSBundle *bundleBundle(void);
static int pyobjc_main(int argc, char * const *argv, char * const *envp);
//
// Mach-O Constructor
//
static void __attribute__ ((constructor)) _py2app_bundle_load(void);
//
// Implementation
//
static
const char *bundlePath(void) {
int i;
const struct mach_header *myHeader = _dyld_get_image_header_containing_address(&bundlePath);
int count = _dyld_image_count();
for (i = 0; i < count; i++) {
if (_dyld_get_image_header(i) == myHeader) {
return _dyld_get_image_name(i);
}
}
abort();
return NULL;
}
static
NSBundle *bundleBundle(void) {
static NSBundle *myBundle = NULL;
if (!myBundle) {
int i;
NSString *path = [NSString stringWithUTF8String:bundlePath()];
// strip Contents/MacOS/App
for (i = 0; i < 3; i++) {
path = [path stringByDeletingLastPathComponent];
}
myBundle = [[NSBundle alloc] initWithPath:path];
}
return myBundle;
}
//
// THIS WILL NOT WORK WITH Py_TRACE_REFS / Py_DEBUG ON UNLESS USING 2.4 OR LATER!
//
static
void DefaultDecRef(PyObject *op) {
if (op != NULL) {
--(*op);
}
}
static
int report_script_error(NSString *err, NSString *errClassName, NSString *errName) {
return report_error(err);
}
static
int report_error(NSString *err) {
NSLog(@"%@", getErrorTitle(getBundleName()));
NSLog(@"%@", err);
return -1;
}
static
int report_linkEdit_error(const char* name) {
NSLinkEditErrors errorClass;
int errorNumber;
const char *fileName;
const char *errorString;
NSLinkEditError(&errorClass, &errorNumber, &fileName, &errorString);
NSLog(@"%s: %s", name, errorString);
printf("<<<py2app>>>> %s: %s\n", name, errorString);
return report_error([NSString stringWithFormat:ERR_LINKERRFMT, fileName]);
}
static
NSString *pyStandardizePath(NSString *pyLocation) {
if ([pyLocation hasPrefix:@"@executable_path/"]) {
NSMutableArray *newComponents = [[pyLocation pathComponents] mutableCopy];
[newComponents replaceObjectAtIndex:0 withObject:[bundleBundle() privateFrameworksPath]];
pyLocation = [NSString pathWithComponents: newComponents];
}
return [pyLocation stringByStandardizingPath];
};
static
BOOL doesPathExist(NSString *path) {
struct stat sb;
return (stat([path fileSystemRepresentation], &sb) == -1) ? NO : YES;
}
static
NSString *getBundleName(void) {
NSDictionary *infoDictionary = [bundleBundle() infoDictionary];
NSString *bundleName = [infoDictionary objectForKey:@"CFBundleName"];
if (!bundleName) {
bundleName = [infoDictionary objectForKey:@"CFBundleExecutable"];
}
return bundleName;
}
static
NSString *getErrorTitle(NSString *bundleName) {
if (!bundleName) {
return ERR_REALLYBADTITLE;
}
return [NSString stringWithFormat:ERR_TITLEFORMAT,bundleName];
}
static
NSString *getPythonLocation(NSArray *pyLocations) {
// get the runtime locations from the Info.plist
// *does not* inspect DYLD environment variables for overrides, fallbacks, suffixes, etc.
// I don't really consider that a very bad thing, as it makes this search extremely deterministic.
// Note that I use the env variables when the image is actually linked, so what you find here
// may not be what gets linked. If this is the case, you deserve it :)
// find a Python runtime
NSString *pyLocation;
NSEnumerator *pyLocationsEnumerator = [pyLocations objectEnumerator];
while ((pyLocation = [pyLocationsEnumerator nextObject])) {
pyLocation = pyStandardizePath(pyLocation);
if (doesPathExist(pyLocation)) {
return pyLocation;
}
}
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];
NSArray *pyResourcePackages = [infoDictionary objectForKey:@"PyResourcePackages"];
if (pyResourcePackages != nil) {
NSString *pkg;
NSEnumerator *pyResourcePackageEnumerator = [pyResourcePackages objectEnumerator];
while ((pkg = [pyResourcePackageEnumerator nextObject])) {
pkg = [pkg stringByExpandingTildeInPath];
if (![@"/" isEqualToString: [pkg substringToIndex:1]]) {
pkg = [resourcePath stringByAppendingPathComponent:pkg];
}
[pythonPathArray addObject:pkg];
}
}
return pythonPathArray;
}
static
NSString *getMainPyPath(NSDictionary *infoDictionary) {
NSArray *possibleMains = [infoDictionary objectForKey:@"PyMainFileNames"];
if ( !possibleMains )
possibleMains = [NSArray array];
// find main python file. __main__.py seems to be a standard, so we'll go ahead and add defaults.
possibleMains = [possibleMains arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:
@"__main__",
@"__realmain__",
@"Main",
nil]];
NSEnumerator *possibleMainsEnumerator = [possibleMains objectEnumerator];
NSString *mainPyPath = nil;
NSString *nextFileName = nil;
NSString *nextExtension = nil;
NSArray *extensions = [NSArray arrayWithObjects:@".py", @".pyc", @".pyo", @"", nil];
NSMutableArray *runtimeAttempts = [NSMutableArray array];
while ((nextFileName = [possibleMainsEnumerator nextObject])) {
NSEnumerator *nextExtensionEnumerator = [extensions objectEnumerator];
while ((nextExtension = [nextExtensionEnumerator nextObject])) {
[runtimeAttempts addObject:[nextFileName stringByAppendingString:nextExtension]];
}
}
possibleMainsEnumerator = [runtimeAttempts objectEnumerator];
while ((nextFileName = [possibleMainsEnumerator nextObject]) && !mainPyPath) {
mainPyPath = [bundleBundle() pathForResource:nextFileName ofType:nil];
}
if (!mainPyPath) {
NSString *components = [runtimeAttempts componentsJoinedByString:@"\r"];
report_error([ERR_NOPYTHONSCRIPT stringByAppendingString:components]);
}
return mainPyPath;
}
int pyobjc_main(int argc, char * const *argv, char * const *envp) {
char* curenv;
char* curlocale;
NSDictionary *infoDictionary = [bundleBundle() infoDictionary];
if (getenv("PYTHONOPTIMIZE") != NULL) {
unsetenv("PYTHONOPTIMIZE");
}
if (getenv("PYTHONDEBUG") != NULL) {
unsetenv("PYTHONDEBUG");
}
if (getenv("PYTHONDONTWRITEBYTECODE") != NULL) {
unsetenv("PYTHONDONTWRITEBYTECODE");
}
if (getenv("PYTHONIOENCODING") != NULL) {
unsetenv("PYTHONIOENCODING");
}
if (getenv("PYTHONDUMPREFS") != NULL) {
unsetenv("PYTHONDUMPREFS");
}
if (getenv("PYTHONMALLOCSTATS") != NULL) {
unsetenv("PYTHONMALLOCSTATS");
}
/* Disable writing of bytecode files */
setenv("PYTHONDONTWRITEBYTECODE", "1", 1);
NSString *pyLocation = nil;
while (NSIsSymbolNameDefined("_Py_Initialize")) {
// Python is already in-process
NSSymbol sym = NSLookupAndBindSymbol("_Py_Initialize");
if (!sym) {
break;
}
NSModule mod = NSModuleForSymbol(sym);
if (!mod) {
break;
}
const char *python_dylib_path = NSLibraryNameForModule(mod);
if (python_dylib_path) {
pyLocation = [NSString stringWithUTF8String:python_dylib_path];
}
break;
}
NSArray *pyLocations = [infoDictionary objectForKey:@"PyRuntimeLocations"];
if (!pyLocation) {
// Python is not in-process
if (!pyLocations) {
return report_error(ERR_PYRUNTIMELOCATIONS);
}
pyLocation = getPythonLocation(pyLocations);
}
if (!pyLocation) {
NSString *components = [pyLocations componentsJoinedByString:@"\r\r"];
return report_script_error([ERR_NOPYTHONRUNTIME stringByAppendingString:components], nil, nil);
}
// Find our resource path and possible PYTHONPATH
NSString *resourcePath = [bundleBundle() resourcePath];
NSArray *pythonPathArray = getPythonPathArray(infoDictionary, resourcePath);
// find the main script
NSString *mainPyPath = getMainPyPath(infoDictionary);
if (!mainPyPath) {
// error already reported
return -1;
}
// Load the Python dylib (may have already been loaded, that is OK)
const struct mach_header *py_dylib = NSAddImage([pyLocation fileSystemRepresentation], PYMACAPP_NSIMAGEFLAGS);
if (!py_dylib) {
return report_linkEdit_error([pyLocation fileSystemRepresentation]);
}
// Load the symbols we need from Python. We avoid lookups of unicode methods because their
// names are mangled by Python (because of UCS2/UCS4 stuff) and looking them up reliably is
// problematic.
NSSymbol tmpSymbol;
#define LOOKUP_SYMBOL(NAME) \
tmpSymbol = NSLookupSymbolInImage(py_dylib, "_" #NAME, PYMACAPP_NSLOOKUPSYMBOLINIMAGEFLAGS)
#define LOOKUP_DEFINEADDRESS(NAME, ADDRESS) \
NAME ## Ptr NAME = (NAME ## Ptr)ADDRESS
#define LOOKUP_DEFINE(NAME) \
LOOKUP_DEFINEADDRESS(NAME, NSAddressOfSymbol(tmpSymbol))
#define LOOKUP(NAME) \
LOOKUP_SYMBOL(NAME); \
if ( !tmpSymbol ) \
return report_linkEdit_error(#NAME); \
LOOKUP_DEFINE(NAME)
#define OPT_LOOKUP(NAME) \
LOOKUP_SYMBOL(NAME); \
NAME ## Ptr NAME = NULL; \
if (tmpSymbol) { \
NAME = (NAME ## Ptr)NSAddressOfSymbol(tmpSymbol); \
}
LOOKUP_SYMBOL(Py_DecRef);
LOOKUP_DEFINEADDRESS(Py_DecRef, (tmpSymbol ? NSAddressOfSymbol(tmpSymbol) : &DefaultDecRef));
LOOKUP(Py_SetProgramName);
LOOKUP(Py_IsInitialized);
LOOKUP(Py_Initialize);
LOOKUP(PyErr_Clear);
LOOKUP(PyErr_Print);
LOOKUP(PyErr_Occurred);
LOOKUP(PyEval_ReleaseLock);
LOOKUP(PyGILState_Ensure);
LOOKUP(PyGILState_Release);
LOOKUP(PyEval_InitThreads);
LOOKUP(PyRun_File);
LOOKUP(PySys_GetObject);
LOOKUP(PySys_SetArgv);
LOOKUP(PyObject_Str);
LOOKUP(PyList_Insert);
LOOKUP(PyObject_GetAttrString);
LOOKUP(PyObject_CallMethod);
LOOKUP(PyImport_ImportModule);
LOOKUP(PyImport_AddModule);
LOOKUP(PyObject_SetItem);
LOOKUP(PyModule_AddStringConstant);
LOOKUP(PyModule_AddObject);
LOOKUP(PyModule_GetDict);
LOOKUP(PyThreadState_Swap);
OPT_LOOKUP(_Py_DecodeUTF8_surrogateescape);
/* PyBytes / PyString lookups depend of if we're on py3k or not */
PyBytes_AsStringPtr PyBytes_AsString = NULL;
PyBytes_FromStringPtr PyBytes_FromString = NULL;
LOOKUP_SYMBOL(PyBytes_AsString);
int isPy3k = tmpSymbol != NULL;
if (isPy3k) {
PyBytes_AsString = (PyBytes_AsStringPtr)NSAddressOfSymbol(tmpSymbol);
LOOKUP_SYMBOL(PyBytes_FromString);
PyBytes_FromString = (PyBytes_FromStringPtr)NSAddressOfSymbol(tmpSymbol);
}
else {
LOOKUP_SYMBOL(PyString_AsString);
PyBytes_AsString = (PyBytes_AsStringPtr)NSAddressOfSymbol(tmpSymbol);
LOOKUP_SYMBOL(PyString_FromString);
PyBytes_FromString = (PyBytes_FromStringPtr)NSAddressOfSymbol(tmpSymbol);
}
#undef LOOKUP
#undef LOOKUP_DEFINE
#undef LOOKUP_DEFINEADDRESS
#undef LOOKUP_SYMBOL
int was_initialized = Py_IsInitialized();
/*
* When apps are started from the Finder (or anywhere
* except from the terminal), the LANG and LC_* variables
* aren't set in the environment. This confuses Py_Initialize
* when it tries to import the codec for UTF-8,
* therefore explicitly set the locale.
*
* Also set the LC_CTYPE environment variable because Py_Initialize
* reset the locale information using the environment :-(
*/
if (isPy3k) {
curlocale = setlocale(LC_ALL, NULL);
if (curlocale != NULL) {
curlocale = strdup(curlocale);
if (curlocale == NULL) {
(void)report_error(ERR_CANNOT_SAVE_LOCALE);
return -1;
}
}
setlocale(LC_ALL, "en_US.UTF-8");
curenv = getenv("LC_CTYPE");
if (!curenv) {
setenv("LC_CTYPE", "en_US.UTF-8", 1);
}
}
// Set up the environment variables to be transferred
NSMutableDictionary *newEnviron = [NSMutableDictionary dictionary];
[newEnviron setObject:[NSString stringWithFormat:@"%p", bundleBundle()] forKey:@"PYOBJC_BUNDLE_ADDRESS"];
[newEnviron setObject:[NSString stringWithFormat:@"%p", bundleBundle()] forKey:[NSString
stringWithFormat:@"PYOBJC_BUNDLE_ADDRESS%ld", (long)getpid()]];
[newEnviron setObject:resourcePath forKey:@"RESOURCEPATH"];
NSMutableDictionary *oldEnviron = [NSMutableDictionary dictionary];
// bootstrap Python with information about how to find what it needs
// if it is not already initialized
if (!was_initialized) {
// $PREFIX/Python -> $PREFIX
NSString *pythonProgramName = [pyLocation stringByDeletingLastPathComponent];
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);
}
NSString *pyExecutableName = [infoDictionary objectForKey:@"PyExecutableName"];
if ( !pyExecutableName ) {
pyExecutableName = @"python";
}
pythonProgramName = [[pythonProgramName stringByAppendingPathComponent:@"bin"] stringByAppendingPathComponent:pyExecutableName];
wchar_t wPythonName[PATH_MAX+1];
if (isPy3k) {
mbstowcs(wPythonName, [pythonProgramName fileSystemRepresentation], PATH_MAX+1);
}
else {
const char *cPythonName = [pythonProgramName fileSystemRepresentation];
memcpy(wPythonName, cPythonName, strlen(cPythonName));
}
Py_SetProgramName(wPythonName);
}
// Set new environment variables and save older ones (for nested plugin loading)
NSEnumerator *envEnumerator = [newEnviron keyEnumerator];
NSString *envKey;
while ((envKey = [envEnumerator nextObject])) {
char *keyString = (char *)[envKey UTF8String];
char *oldValue = getenv(keyString);
if (oldValue) {
[oldEnviron setObject:[NSString stringWithUTF8String:oldValue] forKey:envKey];
}
setenv(keyString, (char *)[[newEnviron objectForKey:envKey] UTF8String], 1);
}
int rval = 0;
FILE *mainPyFile = NULL;
Py_Initialize();
PyEval_InitThreads();
if (isPy3k) {
/*
* Reset the environment and locale information
*/
setlocale(LC_CTYPE, curlocale);
free(curlocale);
if (!curenv) {
unsetenv("LC_CTYPE");
}
}
PyGILState_STATE gilState = PyGILState_Ensure();
if (was_initialized) {
// transfer path into existing Python process
PyObject *path = PySys_GetObject("path");
NSEnumerator *pathEnumerator = [pythonPathArray reverseObjectEnumerator];
NSString *curPath;
while ((curPath = [pathEnumerator nextObject])) {
PyObject *b = PyBytes_FromString([curPath UTF8String]);
PyObject *s = PyObject_CallMethod(b, "decode", "s", "utf-8");
PyList_Insert(path, 0, s);
Py_DecRef(b);Py_DecRef(s);
}
// transfer environment variables into existing Python process
PyObject *osModule = PyImport_ImportModule("os");
PyObject *pyenv = PyObject_GetAttrString(osModule, "environ");
Py_DecRef(osModule);
envEnumerator = [newEnviron keyEnumerator];
while ((envKey = [envEnumerator nextObject])) {
char *keyString = (char *)[envKey UTF8String];
PyObject *b_key = PyBytes_FromString(keyString);
PyObject *b_value = PyBytes_FromString(getenv(keyString));
PyObject *key = PyObject_CallMethod(b_key, "decode", "s", "utf-8");
PyObject *value = PyObject_CallMethod(b_value, "decode", "s", "utf-8");
PyObject_SetItem(pyenv, key, value);
Py_DecRef(b_key);Py_DecRef(key);
Py_DecRef(b_value);Py_DecRef(value);
}
Py_DecRef(pyenv);
}
char *c_mainPyPath = (char *)[mainPyPath fileSystemRepresentation];
mainPyFile = fopen(c_mainPyPath, "r");
if (!mainPyFile) {
rval = report_error([NSString stringWithFormat:@"Could not open main script %@",mainPyPath]);
goto cleanup;
}
if (!was_initialized) {
int i;
NSMutableData *data_argv = [NSMutableData dataWithCapacity:(sizeof(char *) * argc)];
char **argv_new = [data_argv mutableBytes];
if (isPy3k) {
argv_new[0] = (char*)_Py_DecodeUTF8_surrogateescape(c_mainPyPath, strlen(c_mainPyPath));
} else {
argv_new[0] = c_mainPyPath;
}
for (i = 1; i < argc; i++) {
if (isPy3k) {
argv_new[i] = (char*)_Py_DecodeUTF8_surrogateescape(argv[i], strlen(argv[i]));
} else {
argv_new[i] = argv[i];
}
}
argv_new[argc] = NULL;
PySys_SetArgv(argc, argv_new);
}
// create a unique moduleName by CFBundleIdentifier replacing . with _ and prepending __main__
NSString *moduleName = [NSString stringWithFormat:@"__main__%@", [[[infoDictionary objectForKey:@"CFBundleIdentifier"] componentsSeparatedByString:@"."] componentsJoinedByString:@"_"]];
PyObject *module = PyImport_AddModule((char *)[moduleName UTF8String]);
if (!module) {
rval = report_error([NSString stringWithFormat:@"Could not create module '%@'",moduleName]);
goto cleanup;
}
PyModule_AddStringConstant(module, "__file__", c_mainPyPath);
char * builtinsName = isPy3k ? "builtins" : "__builtin__";
PyObject *builtins = PyImport_ImportModule(builtinsName);
PyModule_AddObject(module, "__builtins__", builtins);
PyObject *module_dict = PyModule_GetDict(module);
if (PyErr_Occurred()) {
goto cleanup;
}
PyObject *res = PyRun_File(mainPyFile, c_mainPyPath, Py_file_input, module_dict, module_dict);
if (res) {
Py_DecRef(res);
}
cleanup:
// un-transfer the environment variables
envEnumerator = [newEnviron keyEnumerator];
while ((envKey = [envEnumerator nextObject])) {
char *keyString = (char *)[envKey UTF8String];
NSString *newValue = [oldEnviron objectForKey:envKey];
if (newValue) {
setenv(keyString, [newValue UTF8String], 1);
} else {
unsetenv(keyString);
}
}
if (mainPyFile) {
fclose(mainPyFile);
}
if (PyErr_Occurred()) {
rval = -1;
PyErr_Print();
}
while ( rval ) {
PyObject *exc = PySys_GetObject("last_type");
if ( !exc ) {
rval = report_error([NSString stringWithFormat:ERR_PYTHONEXCEPTION,"<<PyMacAppException>>","The exception went away?"]);
break;
}
PyObject *exceptionClassName = PyObject_GetAttrString(exc, "__name__");
if ( !exceptionClassName ) {
rval = report_error([NSString stringWithFormat:ERR_PYTHONEXCEPTION,"<<PyMacAppException>>","Could not get exception class name?"]);
break;
}
PyObject *v = PySys_GetObject("last_value");
PyObject *exceptionName = NULL;
if ( v )
exceptionName = PyObject_Str(v);
PyObject *b = PyObject_CallMethod(exceptionClassName, "encode", "s", "utf-8");
NSString *nsExceptionClassName = [NSString stringWithCString:PyBytes_AsString(b) encoding:NSUTF8StringEncoding];
Py_DecRef(exceptionClassName);Py_DecRef(b);
NSString *nsExceptionName;
if ( exceptionName ) {
PyObject *b = PyObject_CallMethod(exceptionName, "encode", "s", "utf-8");
nsExceptionName = [NSString stringWithCString:PyBytes_AsString(b) encoding:NSUTF8StringEncoding];
Py_DecRef(exceptionName);Py_DecRef(b);
} else {
nsExceptionName = @"";
}
rval = report_script_error([NSString stringWithFormat:ERR_PYTHONEXCEPTION, nsExceptionClassName, nsExceptionName], nsExceptionClassName, nsExceptionName);
break;
}
PyErr_Clear();
PyGILState_Release(gilState);
if (gilState == PyGILState_LOCKED) {
PyThreadState_Swap(NULL);
PyEval_ReleaseLock();
}
return rval;
}
static
void _py2app_bundle_load(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int argc = 1;
char * const argv[] = { (char *)bundlePath(), NULL };
char * const *envp = *_NSGetEnviron();
(void)pyobjc_main(argc, argv, envp);
[pool release];
}

View File

@ -0,0 +1,3 @@
"""
Resource data converters
"""

View File

@ -0,0 +1,24 @@
"""
Automatic compilation of CoreData model files
"""
import subprocess, os
from py2app.decorators import converts
from py2app.util import momc, mapc
@converts(suffix=".xcdatamodel")
def convert_datamodel(source, destination, dry_run=0):
destination = os.path.splitext(destination)[0] + ".mom"
if dry_run:
return
momc(source, destination)
@converts(suffix=".xcmappingmodel")
def convert_mappingmodel(source, destination, dry_run=0):
destination = destination[:-4] + ".cdm"
if dry_run:
return
mapc(source, destination)

View File

@ -0,0 +1,72 @@
"""
Automatic compilation of XIB files
"""
from __future__ import print_function
import subprocess, os
from py2app.decorators import converts
from py2app.util import check_output
import time
# XXX: _run_nibtool is an experiment while researching an odd
# failure of py2app: when _run_nibtool is None py2app will often
# (but for from everytime) fail when there are NIB files in the
# project. The failure is very odd: writing to sys.stderr fails
# with EGAIN as the errno, and subsequently the interpreter basicly
# crashes.
#
# This workaround seems to fix that issue for now.
#
def _run_nibtool(source, destination):
pid = os.fork()
if pid == 0:
os.setsid()
xit = subprocess.call([_get_ibtool(), '--compile', destination, source])
os._exit(xit)
else:
pid, status = os.waitpid(pid, 0)
if os.WEXITSTATUS(status) != 0:
raise RuntimeError("ibtool failed (%r -> %r)"%(source, destination))
#_run_nibtool = None
gTool = None
def _get_ibtool():
global gTool
if gTool is None:
if os.path.exists('/usr/bin/xcrun'):
try:
gTool = check_output(['/usr/bin/xcrun', '-find', 'ibtool'])[:-1]
except subprocess.CalledProcessError:
raise IOError("Tool 'ibtool' not found")
else:
gTool = 'ibtool'
return gTool
@converts(suffix=".xib")
def convert_xib(source, destination, dry_run=0):
destination = destination[:-4] + ".nib"
print("compile %s -> %s"%(source, destination))
if dry_run:
return
if _run_nibtool is None:
subprocess.check_call([_get_ibtool(), '--compile', destination, source])
else:
_run_nibtool(source, destination)
#time.sleep(1)
@converts(suffix=".nib")
def convert_nib(source, destination, dry_run=0):
destination = destination[:-4] + ".nib"
print("compile %s -> %s"%(source, destination))
if dry_run:
return
if _run_nibtool is None:
subprocess.check_call([_get_ibtool, '--compile', destination, source])
else:
_run_nibtool(source, destination)
#time.sleep(1)

View File

@ -0,0 +1,55 @@
import os
import plistlib
import shutil
import sys
from pkg_resources import resource_filename
import py2app.apptemplate
from py2app.util import makedirs, mergecopy, mergetree, skipscm, make_exec
def create_appbundle(destdir, name, extension='.app', module=py2app.apptemplate,
platform='MacOS', copy=mergecopy, mergetree=mergetree,
condition=skipscm, plist={}, arch=None, redirect_stdout=False):
kw = module.plist_template.infoPlistDict(
plist.get('CFBundleExecutable', name), plist)
app = os.path.join(destdir, kw['CFBundleName'] + extension)
if os.path.exists(app):
# Remove any existing build artifacts to ensure that
# we're getting a clean build
shutil.rmtree(app)
contents = os.path.join(app, 'Contents')
resources = os.path.join(contents, 'Resources')
platdir = os.path.join(contents, platform)
dirs = [contents, resources, platdir]
plist = plistlib.Plist()
plist.update(kw)
plistPath = os.path.join(contents, 'Info.plist')
if os.path.exists(plistPath):
if plist != plistlib.Plist.fromFile(plistPath):
for d in dirs:
shutil.rmtree(d, ignore_errors=True)
for d in dirs:
makedirs(d)
plist.write(plistPath)
srcmain = module.setup.main(arch=arch, secondary=not redirect_stdout)
if sys.version_info[0] == 2 and isinstance(kw['CFBundleExecutable'], unicode):
destmain = os.path.join(platdir, kw['CFBundleExecutable'].encode('utf-8'))
else:
destmain = os.path.join(platdir, kw['CFBundleExecutable'])
with open(os.path.join(contents, 'PkgInfo'), 'w') as fp:
fp.write(
kw['CFBundlePackageType'] + kw['CFBundleSignature']
)
copy(srcmain, destmain)
make_exec(destmain)
mergetree(
resource_filename(module.__name__, 'lib'),
resources,
condition=condition,
copyfn=copy,
)
return app, plist
if __name__ == '__main__':
import sys
create_appbundle('build', sys.argv[1])

View File

@ -0,0 +1,55 @@
import os
import plistlib
import shutil
import sys
from pkg_resources import resource_filename
import py2app.bundletemplate
from py2app.util import makedirs, mergecopy, mergetree, skipscm, make_exec
def create_pluginbundle(destdir, name, extension='.plugin', module=py2app.bundletemplate,
platform='MacOS', copy=mergecopy, mergetree=mergetree,
condition=skipscm, plist={}, arch=None):
kw = module.plist_template.infoPlistDict(
plist.get('CFBundleExecutable', name), plist)
plugin = os.path.join(destdir, kw['CFBundleName'] + extension)
if os.path.exists(plugin):
# Remove any existing build artifacts to ensure
# we're getting a clean build
shutil.rmtree(plugin)
contents = os.path.join(plugin, 'Contents')
resources = os.path.join(contents, 'Resources')
platdir = os.path.join(contents, platform)
dirs = [contents, resources, platdir]
plist = plistlib.Plist()
plist.update(kw)
plistPath = os.path.join(contents, 'Info.plist')
if os.path.exists(plistPath):
if plist != plistlib.Plist.fromFile(plistPath):
for d in dirs:
shutil.rmtree(d, ignore_errors=True)
for d in dirs:
makedirs(d)
plist.write(plistPath)
srcmain = module.setup.main(arch=arch)
if sys.version_info[0] == 2 and isinstance(kw['CFBundleExecutable'], unicode):
destmain = os.path.join(platdir, kw['CFBundleExecutable'].encode('utf-8'))
else:
destmain = os.path.join(platdir, kw['CFBundleExecutable'])
with open(os.path.join(contents, 'PkgInfo'), 'w') as fp:
fp.write(
kw['CFBundlePackageType'] + kw['CFBundleSignature']
)
copy(srcmain, destmain)
make_exec(destmain)
mergetree(
resource_filename(module.__name__, 'lib'),
resources,
condition=condition,
copyfn=copy,
)
return plugin, plist
if __name__ == '__main__':
import sys
create_pluginbundle('build', sys.argv[1])

View File

@ -0,0 +1,14 @@
def converts(suffix):
"""
Use the following convention when writing a file converter::
from py2app.decorators import converts
@converts(suffix=".png")
def convert_png(source_file, destination_file):
pass
"""
def wrapper(func):
func.py2app_suffix=suffix
return func
return wrapper

View File

@ -0,0 +1,61 @@
import os
import sys
from modulegraph import modulegraph
from macholib.util import in_system_path
def has_filename_filter(module):
if isinstance(module, modulegraph.MissingModule):
return True
return getattr(module, 'filename', None) is not None
def not_stdlib_filter(module, prefix=None):
"""
Return False if the module is located in the standard library
"""
if prefix is None:
prefix = sys.prefix
if module.filename is None:
return True
prefix = os.path.join(os.path.realpath(prefix), '')
rp = os.path.realpath(module.filename)
if rp.startswith(prefix):
rest = rp[len(prefix):]
if '/site-python/' in rest:
return True
elif '/site-packages/' in rest:
return True
else:
return False
if os.path.exists(os.path.join(prefix, ".Python")):
# Virtualenv
fn = os.path.join(prefix, "lib", "python%d.%d"%(sys.version_info[:2]), "orig-prefix.txt")
if os.path.exists(fn):
with open(fn, 'rU') as fp:
prefix = fp.read().strip()
if rp.startswith(prefix):
rest = rp[len(prefix):]
if '/site-python/' in rest:
return True
elif '/site-packages/' in rest:
return True
else:
return False
return True
def not_system_filter(module):
"""
Return False if the module is located in a system directory
"""
return not in_system_path(module.filename)
def bundle_or_dylib_filter(module):
"""
Return False if the module does not have a filetype attribute
corresponding to a Mach-O bundle or dylib
"""
return getattr(module, 'filetype', None) in ('bundle', 'dylib')

View File

@ -0,0 +1,87 @@
from py2app.util import imp_find_module
import os, sys, glob
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
try:
set
except NameError:
from sets import Set as set
try:
basestring
except NameError:
basestring = str
def check(cmd, mf):
m = mf.findNode('Image') or mf.findNode('PIL.Image')
if m is None or m.filename is None:
return None
if mf.findNode('PIL.Image'):
have_PIL = True
else:
have_PIL = False
plugins = set()
visited = set()
for folder in sys.path:
if not isinstance(folder, basestring):
continue
for extra in ('', 'PIL'):
folder = os.path.realpath(os.path.join(folder, extra))
if (not os.path.isdir(folder)) or (folder in visited):
continue
for fn in os.listdir(folder):
if not fn.endswith('ImagePlugin.py'):
continue
mod, ext = os.path.splitext(fn)
try:
sys.path.insert(0, folder)
imp_find_module(mod)
del sys.path[0]
except ImportError:
pass
else:
plugins.add(mod)
visited.add(folder)
s = StringIO('_recipes_pil_prescript(%r)\n' % list(plugins))
for plugin in plugins:
if have_PIL:
mf.implyNodeReference(m, 'PIL.' + plugin)
else:
mf.implyNodeReference(m, plugin)
mf.removeReference(m, 'FixTk')
# Since Imaging-1.1.5, SpiderImagePlugin imports ImageTk conditionally.
# This is not ever used unless the user is explicitly using Tk elsewhere.
sip = mf.findNode('SpiderImagePlugin')
if sip is not None:
mf.removeReference(sip, 'ImageTk')
# The ImageQt plugin should only be usefull when using PyQt, which
# would then be explicitly imported.
# Note: this code doesn't have the right side-effect at the moment
# due to the way the PyQt5 recipe is structured.
sip = mf.findNode('PIL.ImageQt')
if sip is not None:
mf.removeReference(sip, 'PyQt5')
mf.removeReference(sip, 'PyQt5.QtGui')
mf.removeReference(sip, 'PyQt5.QtCore')
mf.removeReference(sip, 'PyQt4')
mf.removeReference(sip, 'PyQt4.QtGui')
mf.removeReference(sip, 'PyQt4.QtCore')
pass
return dict(
prescripts = ['py2app.recipes.PIL.prescript', s],
include = "PIL.JpegPresets", # Dodgy import from PIL.JpegPlugin in Pillow 2.0
flatpackages = [os.path.dirname(m.filename)],
)

View File

@ -0,0 +1,43 @@
def _recipes_pil_prescript(plugins):
try:
import Image
have_PIL = False
except ImportError:
from PIL import Image
have_PIL = True
import sys
def init():
if Image._initialized >= 2:
return
if have_PIL:
try:
import PIL.JpegPresets
sys.modules['JpegPresets'] = PIL.JpegPresets
except ImportError:
pass
for plugin in plugins:
try:
if have_PIL:
try:
# First try absolute import through PIL (for Pillow support)
# only then try relative imports
m = __import__('PIL.' + plugin, globals(), locals(), [])
m = getattr(m, plugin)
sys.modules[plugin] = m
continue
except ImportError:
pass
__import__(plugin, globals(), locals(), [])
except ImportError:
if Image.DEBUG:
print('Image: failed to import')
if Image.OPEN or Image.SAVE:
Image._initialized = 2
return 1
Image.init = init

View File

@ -0,0 +1,26 @@
from . import PIL
from . import ctypes
from . import docutils
from . import ftplib
from . import importlib
from . import lxml
from . import matplotlib
from . import mimetypes
from . import numpy
from . import os_module
from . import pydoc
from . import pyenchant
from . import pygame
from . import pyopengl
from . import pyside
from . import pyzmq
from . import qt5
from . import re
from . import scipy
from . import sip
from . import subprocess
from . import virtualenv
from . import wx
from . import xml
from . import setuptools
from . import uuid

View File

@ -0,0 +1,7 @@
def check(cmd, mf):
name = 'cjkcodecs'
m = mf.findNode(name)
if m is None or m.filename is None:
return None
mf.import_hook(name, m, ['*'])
return dict()

View File

@ -0,0 +1,9 @@
import os
def check(cmd, mf):
m = mf.findNode('ctypes')
if m is None or m.filename is None:
return None
return dict(
prescripts=['py2app.bootstrap.ctypes_setup'],
)

View File

@ -0,0 +1,9 @@
def check(cmd, mf):
m = mf.findNode('docutils')
if m is None or m.filename is None:
return None
for pkg in [
'languages', 'parsers', 'readers', 'writers',
'parsers.rst.directives', 'parsers.rst.languages']:
mf.import_hook('docutils.' + pkg, m, ['*'])
return dict()

View File

@ -0,0 +1,14 @@
import sys
from modulegraph.modulegraph import MissingModule
def check(cmd, mf):
if sys.version_info[0] != 2: return {}
# ftplib has an optional dependency on an external (and likely
# non-existing) SOCKS module.
f = mf.findNode('ftplib')
m = mf.findNode('SOCKS')
if m is not None and f is not None and isinstance(m, MissingModule):
mf.removeReference(f, m)
return {}

View File

@ -0,0 +1,7 @@
def check(cmd, mf):
m = mf.findNode('importlib')
if m:
return dict(expected_missing_imports=set(
['_frozen_importlib_external']))
return None

View File

@ -0,0 +1,38 @@
#
# LXML uses imports from C code (or actually Cython code)
# and those cannot be detected by modulegraph.
# The check function adds the hidden imports to the graph
#
# The dependency list was extracted from lxml 3.0.2
import sys
def check(cmd, mf):
m = mf.findNode('lxml.etree')
if m is not None and m.filename is not None:
mf.import_hook('lxml._elementpath', m)
mf.import_hook('os.path', m)
mf.import_hook('re', m)
mf.import_hook('gzip', m)
if sys.version_info[0] == 2:
mf.import_hook('StringIO', m)
else:
mf.import_hook('io', m)
m = mf.findNode('lxml.objectify')
if m is not None and m.filename is not None:
if sys.version_info[0] == 2:
mf.import_hook('copy_reg', m)
else:
mf.import_hook('copyreg', m)
m = mf.findNode('lxml.isoschematron')
if m is not None and m.filename is not None:
# Not zip-safe (see issue 118)
return dict(packages=['lxml'])
if mf.findNode('lxml') is None:
return None
return {}

View File

@ -0,0 +1,27 @@
import os
def check(cmd, mf):
m = mf.findNode('matplotlib')
if m is None or m.filename is None:
return None
if cmd.matplotlib_backends:
backends = {}
for backend in cmd.matplotlib_backends:
if backend == '-':
pass
elif backend == '*':
mf.import_hook('matplotlib.backends', m, ['*'])
else:
mf.import_hook('matplotlib.backends.backend_%s'%(backend,), m)
else:
backends = {'packages': ['matplotlib']}
return dict(
prescripts=['py2app.recipes.matplotlib_prescript'],
resources=[os.path.join(os.path.dirname(m.filename), 'mpl-data')],
**backends
)

View File

@ -0,0 +1,2 @@
import os
os.environ['MATPLOTLIBDATA'] = os.path.join(os.environ['RESOURCEPATH'], 'mpl-data')

View File

@ -0,0 +1,4 @@
def check(cmd, mf):
m = mf.findNode('mimetypes')
if m:
return dict(expected_missing_imports=set(['winreg']))

View File

@ -0,0 +1,7 @@
def check(cmd, mf):
m = mf.findNode('numpy')
if m is None or m.filename is None:
return None
return dict(
packages = ['numpy']
)

View File

@ -0,0 +1,4 @@
def check(cmd, mf):
m = mf.findNode('os')
if m:
return dict(expected_missing_imports=set(['nt']))

View File

@ -0,0 +1,12 @@
import sys
def check(cmd, mf):
m = mf.findNode('pydoc')
if m is None or m.filename is None:
return None
refs = [
'Tkinter', 'tty', 'BaseHTTPServer', 'mimetools', 'select',
'threading', 'ic', 'getopt', 'tkinter', 'win32',
]
for ref in refs:
mf.removeReference(m, ref)
return dict()

View File

@ -0,0 +1,28 @@
"""
Recipe for pyEnchant <http://pypi.python.org/pypi/pyenchant>
PyEnchant is a python library that wraps a C library
using ctypes, hence the usual way to find the library
won't work.
"""
import os
def check(cmd, mf):
m = mf.findNode('enchant')
if m is None or m.filename is None:
return None
if 'PYENCHANT_LIBRARY_PATH' in os.environ:
libpath = os.environ['PYENCHANT_LIBRARY_PATH']
print("WARNING: using pyEnchant without embedding")
print("WARNING: this is not supported at the moment")
else:
path = os.path.dirname(m.filename)
if not os.path.exists(os.path.join(path, "lib", "libenchant.1.dylib")):
print("WARNING: using pyEnchant without embedding")
print("WARNING: this is not supported at the moment")
# Include the entire package outside of site-packages.zip,
# mostly to avoid trying to extract the C code from the package
return dict(packages=['enchant'])

View File

@ -0,0 +1,14 @@
import os
def check(cmd, mf):
m = mf.findNode('pygame')
if m is None or m.filename is None:
return None
def addpath(f):
return os.path.join(os.path.dirname(m.filename), f)
RESOURCES = ['freesansbold.ttf', 'pygame_icon.tiff', 'pygame_icon.icns']
result = dict(
loader_files = [
('pygame', map(addpath, RESOURCES)),
],
)
return result

View File

@ -0,0 +1,16 @@
from __future__ import absolute_import
import os
def check(cmd, mf):
m = mf.findNode('OpenGL')
if m is None or m.filename is None:
return None
p = os.path.splitext(m.filename)[0] + '.py'
# check to see if it's a patched version that doesn't suck
if os.path.exists(p):
for line in open(p, 'rU'):
if line.startswith('__version__ = '):
return dict()
# otherwise include the whole damned thing
return dict(
packages = ['OpenGL'],
)

View File

@ -0,0 +1,48 @@
# since pkg_resources.py lives next to this file, we need to disambiguate the import
from __future__ import absolute_import
import glob
import os
import pkg_resources
def check(cmd, mf):
name = 'PySide'
m = mf.findNode(name)
if m is None or m.filename is None:
return None
try:
from PySide import QtCore
except ImportError:
print("WARNING: macholib found PySide, but cannot import")
return {}
plugin_dir = QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PluginsPath)
resources = [pkg_resources.resource_filename('py2app', 'recipes/qt.conf')]
for item in cmd.qt_plugins:
if '/' not in item:
item = item + '/*'
if '*' in item:
for path in glob.glob(os.path.join(plugin_dir, item)):
resources.append((os.path.dirname('qt_plugins' + path[len(plugin_dir):]), [path]))
else:
resources.append((os.path.dirname(os.path.join('qt_plugins', item)), [os.path.join(plugin_dir, item)]))
# PySide dumps some of its shared files
# into /usr/lib, which is a system location
# and those files are therefore not included
# into the app bundle by default.
from macholib.util import NOT_SYSTEM_FILES
import sys
for fn in os.listdir('/usr/lib'):
add=False
if fn.startswith('libpyside-python'):
add=True
elif fn.startswith('libshiboken-python'):
add=True
if add:
NOT_SYSTEM_FILES.append(os.path.join('/usr/lib', fn))
return dict(resources=resources)

View File

@ -0,0 +1,15 @@
def check(cmd, mf):
m = mf.findNode('zmq')
if m is None or m.filename is None:
return None
# PyZMQ is a package that contains
# a shared library. This recipe
# is mostly a workaround for a bug
# in py2app: it copies the dylib into
# the site-packages zipfile and builds
# a non-functionaly application.
return dict(
packages=['zmq']
)

View File

@ -0,0 +1,3 @@
; Qt Configuration file
[Paths]
Plugins = Resources/qt_plugins

View File

@ -0,0 +1,28 @@
import sys
from modulegraph.modulegraph import MissingModule
def check(cmd, mf):
m = mf.findNode('PyQt5')
if m and not isinstance(m, MissingModule):
try:
# PyQt5 with sipconfig module, handled
# by sip recipe
import sipconfig
return None
except ImportError:
pass
# All imports are done from C code, hence not visible
# for modulegraph
# 1. Use of 'sip'
# 2. Use of other modules, datafiles and C libraries
# in the PyQt5 package.
mf.import_hook('sip', m)
if sys.version[0] != 2:
return dict(packages=['PyQt5'],
expected_missing_imports=set(['copy_reg', 'cStringIO', 'StringIO']))
else:
return dict(packages=['PyQt5'])
return None

View File

@ -0,0 +1,4 @@
def check(cmd, mf):
m = mf.findNode('re')
if m:
return dict(expected_missing_imports=set(['sys.getwindowsversion']))

View File

@ -0,0 +1,7 @@
def check(cmd, mf):
m = mf.findNode('scipy')
if m is None or m.filename is None:
return None
return dict(
packages = ['scipy', 'numpy']
)

View File

@ -0,0 +1,18 @@
import sys
def check(cmd, mf):
m = mf.findNode('pkg_resources')
if m is None or m.filename is None:
return None
for pkg in [
'packaging', 'pyparsing', 'six', 'appdirs' ]:
mf.import_hook('pkg_resources._vendor.' + pkg, m, ['*'])
expected_missing_imports=['__main__.__requires__',
'pkg_resources.extern.pyparsing',
'pkg_resources.extern.six',
'pkg_resources._vendor.appdirs']
if sys.version[0] != 2:
expected_missing_imports.append( '__builtin__' )
return dict(expected_missing_imports=set(expected_missing_imports))

View File

@ -0,0 +1,148 @@
"""
Py2app support for project using sip, which basicly means PyQt and wrappers
for other Qt-based libraries.
This will include all C modules that might be used when you import a package
using sip because we have no way to fine-tune this.
The problem with SIP is that all inter-module depedencies (for example from
PyQt4.Qt to PyQt4.QtCore) are handled in C code and therefore cannot be
detected by the python code in py2app).
"""
# since pkg_resources.py lives next to this file, we need to disambiguate the import
from __future__ import absolute_import
import sys
import glob
import os
import pkg_resources
class Sip(object):
def __init__(self):
self.packages = None
self.plugin_dir = None
def config(self):
if self.packages is not None:
print("packages", self.packages)
return self.packages
import sipconfig, os
try:
from PyQt4 import pyqtconfig
cfg = pyqtconfig.Configuration()
qtdir = cfg.qt_lib_dir
sipdir = os.path.dirname(cfg.pyqt_mod_dir)
self.plugin_dir = os.path.join(cfg.qt_dir, 'plugins')
except ImportError:
from PyQt5.QtCore import QLibraryInfo
qtdir = QLibraryInfo.location(QLibraryInfo.LibrariesPath)
self.plugin_dir = QLibraryInfo.location(QLibraryInfo.PluginsPath)
sipdir = os.path.dirname(sipconfig.__file__)
if not os.path.exists(qtdir):
print("sip: Qtdir %r does not exist"%(qtdir))
# half-broken installation? ignore.
raise ImportError
# Qt is GHETTO!
dyld_library_path = os.environ.get('DYLD_LIBRARY_PATH', '').split(':')
if qtdir not in dyld_library_path:
dyld_library_path.insert(0, qtdir)
os.environ['DYLD_LIBRARY_PATH'] = ':'.join(dyld_library_path)
self.packages = set()
for fn in os.listdir(sipdir):
fullpath = os.path.join(sipdir, fn)
if os.path.isdir(fullpath):
self.packages.add(fn)
if fn in ('PyQt4', 'PyQt5'):
# PyQt4 and later has a nested structure, also import
# subpackage to ensure everything get seen.
for sub in os.listdir(fullpath):
if ".py" not in sub:
self.packages.add('%s.%s'%(fn, sub.replace(".so","")))
# Causes a python3-related syntax error (metaclass keyword),
# and you probably don't need it:
#if "PyQt4.uic" in self.packages and sys.version_info.major != 3:
# print("WARNING: PyQt uic module found.")
# print("avoid python3 metaclass syntax errors by adding 'PyQt4.uic' to your excludes option.")
print("sip: packages: %s"%(self.packages,))
return self.packages
def check(self, cmd, mf):
try:
packages = self.config()
except ImportError:
return None
if 'PyQt4.uic' in packages:
# PyQt4.uic contains subpackages with python 2 and python 3
# support. Exclude the variant that won't be ussed, this avoids
# compilation errors on Python 2 (because some of the Python 3
# code is not valid Python 2 code)
if sys.version_info[0] == 2:
ref = 'PyQt4.uic.port_v3'
else:
ref = 'PyQt4.uic.port_v2'
if 'PyQt5.uic' in packages:
# ditto
if sys.version_info[0] == 2:
ref = 'PyQt5.uic.port_v3'
else:
ref = 'PyQt5.uic.port_v2'
# Exclude...
mf.lazynodes[ref] = None
for pkg in packages:
m = mf.findNode(pkg)
if m is not None and m.filename is not None:
break
else:
print("sip: No sip package used in application")
return None
mf.import_hook('sip', m)
m = mf.findNode('sip')
# naive inclusion of ALL sip packages
# stupid C modules.. hate hate hate
for pkg in packages:
try:
mf.import_hook(pkg, m)
except ImportError as exc:
print("WARNING: ImportError in sip recipe ignored: %s"%(exc,))
print(mf.findNode("PyQt4"))
print(mf.findNode("PyQt5"))
if mf.findNode('PyQt4') is not None or mf.findNode('PyQt5') is not None:
resources = [pkg_resources.resource_filename('py2app', 'recipes/qt.conf')]
for item in cmd.qt_plugins:
if '/' not in item:
item = item + '/*'
if '*' in item:
for path in glob.glob(os.path.join(self.plugin_dir, item)):
resources.append((os.path.dirname('qt_plugins' + path[len(self.plugin_dir):]), [path]))
else:
resources.append((os.path.dirname(os.path.join('qt_plugins', item)), [os.path.join(self.plugin_dir, item)]))
print("PyQt resources", resources)
return dict(resources=resources)
print("Return {}")
return dict()
check = Sip().check

View File

@ -0,0 +1,4 @@
def check(cmd, mf):
m = mf.findNode('subprocess')
if m:
return dict(expected_missing_imports=set(['_winapi']))

View File

@ -0,0 +1,4 @@
def check(cmd, mf):
m = mf.findNode('uuid')
if m:
return dict(expected_missing_imports=set(['netbios', 'win32wnet']))

View File

@ -0,0 +1,106 @@
"""
virtualenv installs a wrapper for the real distutils into the
virtual environment. Ignore that wrapper, but go for the real
distutils instead
This recipe is rather compilicated and definitely not a
good model for other recipes!!!
"""
from __future__ import absolute_import
import sys, os, imp
from modulegraph.modulegraph import MissingModule, \
Package, SourceModule, CompiledModule, find_module
def retry_import(mf, m):
"""
Try to reimport 'm', which should be a MissingModule
"""
if '.' in m.identifier:
pname, partname = m.identifier.rsplit('.', 1)
parent = mf.findNode(pname)
else:
parent = None
partname = m.identifier
# This is basicly mf.find_module inlined and with a
# check disabled.
def fmod(name, path, parent):
if path is None:
if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN))
path = mf.path
fp, buf, stuff = find_module(name, path)
if buf:
buf = os.path.realpath(buf)
return (fp, buf, stuff)
try:
fp, pathname, stuff = fmod(partname,
parent and parent.packagepath, parent)
except ImportError as e:
return
if stuff[-1] == imp.PKG_DIRECTORY:
m.__class__ = Package
elif stuff[-1] == imp.PY_SOURCE:
m.__class__ = SourceModule
else:
m.__class__ = CompiledModule
#making this match the code later on that checks whether scan_code needs a leading _
if hasattr(mf, 'load_module'):
m = mf.load_module(m.identifier, fp, pathname, stuff)
else:
m = mf._load_module(m.identifier, fp, pathname, stuff)
if parent:
mf.createReference(m, parent)
parent[partname] = m
return m
def check(cmd, mf):
m = mf.findNode('distutils')
if m is None or m.filename is None:
return None
with open(m.filename, 'rU') as fp:
contents = fp.read()
if 'virtualenv' in contents:
# This is the virtualenv version
mos = mf.findNode('os')
if mos is None or mos.filename is None:
raise ValueError("Where is the os module")
m.filename = os.path.join(os.path.dirname(mos.filename), 'distutils', '__init__.py')
with open(m.filename) as fp:
source = fp.read() + '\n'
m.code = co = compile(source, m.filename, 'exec')
m.packagepath = [os.path.dirname(m.filename)]
if mf.replace_paths:
co = mf.replace_paths_in_code(co)
# XXX: Recent versions of modulegraph made scan_code private,
# temporarily call the private version.
if hasattr(mf, 'scan_code'):
mf.scan_code(co, m)
else:
mf._scan_code(co, m)
# That's not all there is to this, we need to look for
# MissingModules in the distutils namespace as well and
# try to import these again.
for m in mf.flatten():
if isinstance(m, MissingModule):
if m.identifier.startswith('distutils.'):
# A missing distutils package, retry
# importing it.
#
retry_import(mf, m)
return dict()

View File

@ -0,0 +1,20 @@
def check(cmd, mf):
# wx.lib.pubsub tries to be too smart w.r.t.
# the __path__ it uses, include all of it when
# found.
m = mf.findNode('wx.lib.pubsub')
if m is None or m.filename is None:
return None
include_packages = [
'wx.lib.pubsub.*',
'wx.lib.pubsub.core.*',
'wx.lib.pubsub.core.arg1.*',
'wx.lib.pubsub.core.kwargs.*',
'wx.lib.pubsub.pubsub1.*',
'wx.lib.pubsub.pubsub2.*',
'wx.lib.pubsub.utils.*',
]
return dict(
includes = include_packages
)

View File

@ -0,0 +1,13 @@
import sys
from modulegraph.modulegraph import MissingModule
def check(cmd, mf):
if sys.version_info[0] != 2: return {}
# Optional dependency on XML+ package in the stdlib, ignore
# this when the package isn't present.
m = mf.findNode('_xmlplus')
if m is not None and isinstance(m, MissingModule):
mf.removeReference(mf.findNode('xml'), m)
return {}

View File

@ -0,0 +1,205 @@
"""
Create an applet from a Python script.
You can drag in packages, Info.plist files, icons, etc.
It's expected that only one Python script is dragged in.
"""
from __future__ import print_function
import os, sys
from distutils.core import setup
from plistlib import Plist
import py2app
import tempfile
import shutil
import imp
import pprint
from py2app.util import copy_tree
from py2app import build_app
try:
set
except NameError:
from sets import Set as set
if sys.version_info[0] == 3:
raw_input = input
HELP_TEXT = """
usage: py2applet --make-setup [options...] script.py [data files...]
or: py2applet [options...] script.py [data files...]
or: py2applet --help
"""
SETUP_TEMPLATE = '''"""
This is a setup.py script generated by py2applet
Usage:
python setup.py py2app
"""
from setuptools import setup
APP = %s
DATA_FILES = %s
OPTIONS = %s
setup(
app=APP,
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
'''
def get_option_map():
optmap = {}
for option in build_app.py2app.user_options:
opt_long, opt_short = option[:2]
if opt_short:
optmap['-' + opt_short] = opt_long.rstrip('=')
return optmap
def get_cmd_options():
options = set()
for option in build_app.py2app.user_options:
opt_long, opt_short = option[:2]
if opt_long.endswith('=') and opt_short:
options.add('-' + opt_short)
return options
def main():
if not sys.argv[1:]:
print(HELP_TEXT)
return
scripts = []
data_files = []
packages = []
args = []
plist = {}
iconfile = None
parsing_options = True
next_is_option = False
cmd_options = get_cmd_options()
is_make_setup = False
for fn in sys.argv[1:]:
if parsing_options:
if next_is_option:
args.append(fn)
next_is_option = False
continue
elif fn == '--make-setup':
is_make_setup = True
continue
elif fn.startswith('-'):
args.append(fn)
if fn in cmd_options:
next_is_option = True
continue
parsing_options = False
if not is_make_setup:
fn = os.path.abspath(fn)
if fn.endswith('.py'):
if scripts:
data_files.append(fn)
else:
scripts.append(fn)
elif os.path.basename(fn) == 'Info.plist':
plist = Plist.fromFile(fn)
elif fn.endswith('.icns') and not iconfile:
iconfile = os.path.abspath(fn)
elif os.path.isdir(fn):
sys.path.insert(0, [os.path.dirname(fn)])
try:
path = imp.find_module(os.path.basename(fn))[0]
except ImportError:
path = ''
del sys.path[0]
if os.path.realpath(path) == os.path.realpath(fn):
packages.append(os.path.basename(fn))
else:
data_files.append(fn)
else:
data_files.append(fn)
options = dict(
packages=packages,
plist=plist,
iconfile=iconfile,
)
for k,v in list(options.items()):
if not v:
del options[k]
if is_make_setup:
make_setup(args, scripts, data_files, options)
else:
build(args, scripts, data_files, options)
def make_setup(args, scripts, data_files, options):
optmap = get_option_map()
cmd_options = get_cmd_options()
while args:
cmd = args.pop(0)
if cmd in cmd_options:
cmd = optmap[cmd]
options[cmd.replace('-', '_')] = args.pop(0)
elif '=' in cmd:
cmd, val = cmd.split('=', 1)
options[cmd.lstrip('-').replace('-', '_')] = val
else:
cmd = optmap.get(cmd, cmd)
options[cmd.lstrip('-').replace('-', '_')] = True
if os.path.exists('setup.py'):
res = ''
while res.lower() not in ('y', 'n'):
res = raw_input('Existing setup.py detected, replace? [Y/n] ')
if not res:
break
if res == 'n':
print('aborted!')
return
f = open('setup.py', 'w')
tvars = tuple(map(pprint.pformat, (scripts, data_files, options)))
f.write(SETUP_TEMPLATE % tvars)
f.flush()
f.close()
print('Wrote setup.py')
def build(args, scripts, data_files, options):
old_argv = sys.argv
sys.argv = [sys.argv[0], 'py2app'] + args
old_path = sys.path
path_insert = set()
for script in scripts:
path_insert.add(os.path.dirname(script))
sys.path = list(path_insert) + old_path
old_dir = os.getcwd()
tempdir = tempfile.mkdtemp()
os.chdir(tempdir)
try:
d = setup(
app=scripts,
data_files=data_files,
options={'py2app': options},
)
for target in d.app:
copy_tree(
target.appdir,
os.path.join(
os.path.dirname(target.script),
os.path.basename(target.appdir),
),
preserve_symlinks=True,
)
finally:
os.chdir(old_dir)
shutil.rmtree(tempdir, ignore_errors=True)
sys.argv = old_argv
sys.path = old_path
if __name__ == '__main__':
main()

View File

@ -0,0 +1,653 @@
from __future__ import print_function
import os, sys, imp, time, errno, stat, filecmp
from modulegraph.find_modules import PY_SUFFIXES, C_SUFFIXES
from modulegraph.util import *
from modulegraph import zipio
import macholib.util
import warnings
from distutils import log
import pkg_resources
import subprocess
# Deprecated functionality
def os_path_islink(path):
warnings.warn("Use zipio.islink instead of os_path_islink",
DeprecationWarning)
return zipio.islink(path)
def os_path_isdir(path):
warnings.warn("Use zipio.isdir instead of os_path_isdir",
DeprecationWarning)
return zipio.islink(path)
def os_readlink(path):
warnings.warn("Use zipio.readlink instead of os_readlink",
DeprecationWarning)
return zipio.islink(path)
import zipfile
def get_zip_data(path_to_zip, path_in_zip):
warnings.warn("Use zipio.open instead of get_zip_data",
DeprecationWarning)
zf = zipfile.ZipFile(path_to_zip)
return zf.read(path_in_zip)
def path_to_zip(path):
"""
Returns (pathtozip, pathinzip). If path isn't in a zipfile pathtozip
will be None
"""
warnings.warn("Don't use this function", DeprecationWarning)
zf = zipfile.ZipFile(path_to_zip)
orig_path = path
from distutils.errors import DistutilsFileError
if os.path.exists(path):
return (None, path)
else:
rest = ''
while not os.path.exists(path):
path, r = os.path.split(path)
if not path:
raise DistutilsFileError("File doesn't exist: %s"%(orig_path,))
rest = os.path.join(r, rest)
if not os.path.isfile(path):
# Directory really doesn't exist
raise DistutilsFileError("File doesn't exist: %s"%(orig_path,))
try:
zf = zipfile.ZipFile(path)
except zipfile.BadZipfile:
raise DistutilsFileError("File doesn't exist: %s"%(orig_path,))
if rest.endswith('/'):
rest = rest[:-1]
return path, rest
def get_mtime(path, mustExist=True):
"""
Get mtime of a path, even if it is inside a zipfile
"""
warnings.warn("Don't use this function", DeprecationWarning)
try:
return zipio.getmtime(target)
except IOError:
if not mustExist:
return -1
raise
# End deprecated functionality
gConverterTab = {}
def find_converter(source):
if not gConverterTab:
for ep in pkg_resources.iter_entry_points('py2app.converter'):
function = ep.load()
if not hasattr(function, "py2app_suffix"):
print("WARNING: %s does not have 'py2app_suffix' attribute"%(function))
continue
gConverterTab[function.py2app_suffix] = function
basename, suffix = os.path.splitext(source)
try:
return gConverterTab[suffix]
except KeyError:
return None
def copy_resource(source, destination, dry_run=0, symlink=0):
"""
Copy a resource file into the application bundle
"""
import py2app.converters as conv
converter = find_converter(source)
if converter is not None:
converter(source, destination, dry_run=dry_run)
return
if os.path.isdir(source):
if not dry_run:
if not os.path.exists(destination):
os.mkdir(destination)
for fn in zipio.listdir(source):
copy_resource(os.path.join(source, fn),
os.path.join(destination, fn), dry_run=dry_run, symlink=symlink)
else:
if symlink:
if not dry_run:
make_symlink(os.path.abspath(source), destination)
else:
copy_file(source, destination, dry_run=dry_run, preserve_mode=True)
def copy_file(source, destination, preserve_mode=False, preserve_times=False, update=False, dry_run=0):
while True:
try:
_copy_file(source, destination, preserve_mode, preserve_times, update, dry_run)
return
except IOError as exc:
if exc.errno != errno.EAGAIN:
raise
log.info("copying file %s failed due to spurious EAGAIN, retrying in 2 seconds", source)
time.sleep(2)
def _copy_file(source, destination, preserve_mode=False, preserve_times=False, update=False, dry_run=0):
log.info("copying file %s -> %s", source, destination)
with zipio.open(source, 'rb') as fp_in:
if not dry_run:
if os.path.isdir(destination):
destination = os.path.join(destination, os.path.basename(source))
if os.path.exists(destination):
os.unlink(destination)
with open(destination, 'wb') as fp_out:
data = fp_in.read()
fp_out.write(data)
if preserve_mode:
mode = None
if hasattr(zipio, 'getmode'):
mode = zipio.getmode(source)
elif os.path.isfile(source):
mode = os.stat(source).st_mode
if mode is not None:
os.chmod(destination, mode)
if preserve_times:
mtime = zipio.getmtime(source)
os.utime(destination, (mtime, mtime))
def make_symlink(source, target):
if os.path.islink(target):
os.unlink(target)
os.symlink(source, target)
def newer(source, target):
"""
distutils.dep_utils.newer with zipfile support
"""
try:
return zipio.getmtime(source) > zipio.getmtime(target)
except IOError:
return True
def find_version(fn):
"""
Try to find a __version__ assignment in a source file
"""
return "0.0.0"
import compiler
from compiler.ast import Module, Stmt, Assign, AssName, Const
ast = compiler.parseFile(fn)
if not isinstance(ast, Module):
raise ValueError("expecting Module")
statements = ast.getChildNodes()
if not (len(statements) == 1 and isinstance(statements[0], Stmt)):
raise ValueError("expecting one Stmt")
for node in statements[0].getChildNodes():
if not isinstance(node, Assign):
continue
if not len(node.nodes) == 1:
continue
assName = node.nodes[0]
if not (
isinstance(assName, AssName) and
isinstance(node.expr, Const) and
assName.flags == 'OP_ASSIGN' and
assName.name == '__version__'
):
continue
return node.expr.value
else:
raise ValueError("Version not found")
def in_system_path(filename):
"""
Return True if the file is in a system path
"""
return macholib.util.in_system_path(filename)
if sys.version_info[0] == 2:
def fsencoding(s, encoding=sys.getfilesystemencoding()):
return macholib.util.fsencoding(s, encoding=encoding)
else:
def fsencoding(s, encoding=sys.getfilesystemencoding()):
return s
def make_exec(path):
mask = os.umask(0)
os.umask(mask)
os.chmod(path, os.stat(path).st_mode | (0o111 & ~mask))
def makedirs(path):
path = fsencoding(path)
if not os.path.exists(path):
os.makedirs(path)
def mergecopy(src, dest):
return macholib.util.mergecopy(src, dest)
def mergetree(src, dst, condition=None, copyfn=mergecopy):
"""Recursively merge a directory tree using mergecopy()."""
return macholib.util.mergetree(src, dst, condition=condition, copyfn=copyfn)
def move(src, dst):
return macholib.util.move(src, dst)
def copy2(src, dst):
return macholib.util.copy2(src, dst)
def fancy_split(s, sep=","):
# a split which also strips whitespace from the items
# passing a list or tuple will return it unchanged
if s is None:
return []
if hasattr(s, "split"):
return [item.strip() for item in s.split(sep)]
return s
class FileSet(object):
# A case insensitive but case preserving set of files
def __init__(self, iterable=None):
self._dict = {}
if iterable is not None:
for arg in iterable:
self.add(arg)
def __repr__(self):
return "<FileSet %s at %x>" % (self._dict.values(), id(self))
def add(self, fname):
self._dict[fname.upper()] = fname
def remove(self, fname):
del self._dict[fname.upper()]
def __contains__(self, fname):
return fname.upper() in self._dict.keys()
def __getitem__(self, index):
key = self._dict.keys()[index]
return self._dict[key]
def __len__(self):
return len(self._dict)
def copy(self):
res = FileSet()
res._dict.update(self._dict)
return res
LOADER = """
def __load():
import imp, os, sys
ext = %r
for path in sys.path:
if not path.endswith('lib-dynload'):
continue
ext_path = os.path.join(path, ext)
if os.path.exists(ext_path):
mod = imp.load_dynamic(__name__, ext_path)
break
else:
raise ImportError(repr(ext) + " not found")
__load()
del __load
"""
def make_loader(fn):
return LOADER % fn
def byte_compile(py_files, optimize=0, force=0,
target_dir=None, verbose=1, dry_run=0,
direct=None):
if direct is None:
direct = (__debug__ and optimize == 0)
# "Indirect" byte-compilation: write a temporary script and then
# run it with the appropriate flags.
if not direct:
from tempfile import mktemp
from distutils.util import execute, spawn
script_name = mktemp(".py")
if verbose:
print("writing byte-compilation script '%s'" % script_name)
if not dry_run:
with open(script_name, "w") as script:
script.write("""
from py2app.util import byte_compile
from modulegraph.modulegraph import *
files = [
""")
for f in py_files:
script.write(repr(f) + ",\n")
script.write("]\n")
script.write("""
byte_compile(files, optimize=%r, force=%r,
target_dir=%r,
verbose=%r, dry_run=0,
direct=1)
""" % (optimize, force, target_dir, verbose))
# Ensure that py2app is on PYTHONPATH, this ensures that
# py2app.util can be found even when we're running from
# an .egg that was downloaded by setuptools
import py2app
pp = os.path.dirname(os.path.dirname(py2app.__file__))
if 'PYTHONPATH' in os.environ:
pp = '%s:%s'%(pp, os.environ['PYTHONPATH'])
cmd = ['/usr/bin/env', 'PYTHONPATH=%s'%(pp,), sys.executable, script_name]
if optimize == 1:
cmd.insert(3, "-O")
elif optimize == 2:
cmd.insert(3, "-OO")
spawn(cmd, verbose=verbose, dry_run=dry_run)
execute(os.remove, (script_name,), "removing %s" % script_name,
verbose=verbose, dry_run=dry_run)
else:
from py_compile import compile
from distutils.dir_util import mkpath
for mod in py_files:
# Terminology from the py_compile module:
# cfile - byte-compiled file
# dfile - purported source filename (same as 'file' by default)
if mod.filename == mod.identifier:
cfile = os.path.basename(mod.filename)
dfile = cfile + (__debug__ and 'c' or 'o')
else:
cfile = mod.identifier.replace('.', os.sep)
if sys.version_info[:2] <= (3, 4):
if mod.packagepath:
dfile = cfile + os.sep + '__init__.py' + (__debug__ and 'c' or 'o')
else:
dfile = cfile + '.py' + (__debug__ and 'c' or 'o')
else:
if mod.packagepath:
dfile = cfile + os.sep + '__init__.pyc'
else:
dfile = cfile + '.pyc'
if target_dir:
cfile = os.path.join(target_dir, dfile)
if force or newer(mod.filename, cfile):
if verbose:
print("byte-compiling %s to %s" % (mod.filename, dfile))
if not dry_run:
mkpath(os.path.dirname(cfile))
suffix = os.path.splitext(mod.filename)[1]
if suffix in ('.py', '.pyw'):
fn = cfile + '.py'
with zipio.open(mod.filename, 'rb') as fp_in:
with open(fn, 'wb') as fp_out:
fp_out.write(fp_in.read())
compile(fn, cfile, dfile)
os.unlink(fn)
elif suffix in PY_SUFFIXES:
# Minor problem: This will happily copy a file
# <mod>.pyo to <mod>.pyc or <mod>.pyc to
# <mod>.pyo, but it does seem to work.
copy_file(mod.filename, cfile, preserve_times=True)
else:
raise RuntimeError \
("Don't know how to handle %r" % mod.filename)
else:
if verbose:
print("skipping byte-compilation of %s to %s" %
(mod.filename, dfile))
SCMDIRS = ['CVS', '.svn', '.hg', '.git']
def skipscm(ofn):
ofn = fsencoding(ofn)
fn = os.path.basename(ofn)
if fn in SCMDIRS:
return False
return True
def skipfunc(junk=(), junk_exts=(), chain=()):
junk = set(junk)
junk_exts = set(junk_exts)
chain = tuple(chain)
def _skipfunc(fn):
if os.path.basename(fn) in junk:
return False
elif os.path.splitext(fn)[1] in junk_exts:
return False
for func in chain:
if not func(fn):
return False
else:
return True
return _skipfunc
JUNK = ['.DS_Store', '.gdb_history', 'build', 'dist'] + SCMDIRS
JUNK_EXTS = ['.pbxuser', '.pyc', '.pyo', '.swp']
skipjunk = skipfunc(JUNK, JUNK_EXTS)
def get_magic(platform=sys.platform):
if platform == 'darwin':
import struct
import macholib.mach_o
return [
struct.pack('!L', macholib.mach_o.MH_MAGIC),
struct.pack('!L', macholib.mach_o.MH_CIGAM),
struct.pack('!L', macholib.mach_o.MH_MAGIC_64),
struct.pack('!L', macholib.mach_o.MH_CIGAM_64),
struct.pack('!L', macholib.mach_o.FAT_MAGIC),
]
elif platform == 'linux2':
return ['\x7fELF']
elif platform == 'win32':
return ['MZ']
return None
def iter_platform_files(path, is_platform_file=macholib.util.is_platform_file):
"""
Iterate over all of the platform files in a directory
"""
for root, dirs, files in os.walk(path):
for fn in files:
fn = os.path.join(root, fn)
if is_platform_file(fn):
yield fn
def strip_files(files, dry_run=0, verbose=0):
"""
Strip the given set of files
"""
if dry_run:
return
return macholib.util.strip_files(files)
def copy_tree(src, dst,
preserve_mode=1,
preserve_times=1,
preserve_symlinks=0,
update=0,
verbose=0,
dry_run=0,
condition=None):
"""
Copy an entire directory tree 'src' to a new location 'dst'. Both
'src' and 'dst' must be directory names. If 'src' is not a
directory, raise DistutilsFileError. If 'dst' does not exist, it is
created with 'mkpath()'. The end result of the copy is that every
file in 'src' is copied to 'dst', and directories under 'src' are
recursively copied to 'dst'. Return the list of files that were
copied or might have been copied, using their output name. The
return value is unaffected by 'update' or 'dry_run': it is simply
the list of all files under 'src', with the names changed to be
under 'dst'.
'preserve_mode' and 'preserve_times' are the same as for
'copy_file'; note that they only apply to regular files, not to
directories. If 'preserve_symlinks' is true, symlinks will be
copied as symlinks (on platforms that support them!); otherwise
(the default), the destination of the symlink will be copied.
'update' and 'verbose' are the same as for 'copy_file'.
"""
assert isinstance(src, (str, unicode)), repr(src)
assert isinstance(dst, (str, unicode)), repr(dst)
from distutils.dir_util import mkpath
from distutils.dep_util import newer
from distutils.errors import DistutilsFileError
from distutils import log
src = fsencoding(src)
dst = fsencoding(dst)
if condition is None:
condition = skipscm
if not dry_run and not zipio.isdir(src):
raise DistutilsFileError(
"cannot copy tree '%s': not a directory" % src)
try:
names = zipio.listdir(src)
except os.error as exc:
(errno, errstr) = exc.args
if dry_run:
names = []
else:
raise DistutilsFileError(
"error listing files in '%s': %s" % (src, errstr))
if not dry_run:
mkpath(dst)
outputs = []
for n in names:
src_name = os.path.join(src, n)
dst_name = os.path.join(dst, n)
if (condition is not None) and (not condition(src_name)):
continue
# Note: using zipio's internal _locate function throws an IOError on
# dead symlinks, so handle it here.
if os.path.islink(src_name) and not os.path.exists(os.readlink(src_name)):
continue
if preserve_symlinks and zipio.islink(src_name):
link_dest = zipio.readlink(src_name)
log.info("linking %s -> %s", dst_name, link_dest)
if not dry_run:
if update and not newer(src, dst_name):
pass
else:
make_symlink(link_dest, dst_name)
outputs.append(dst_name)
elif zipio.isdir(src_name) and not os.path.isfile(src_name):
# ^^^ this odd tests ensures that resource files that
# happen to be a zipfile won't get extracted.
# XXX: need API in zipio to clean up this code
outputs.extend(
copy_tree(src_name, dst_name, preserve_mode,
preserve_times, preserve_symlinks, update,
dry_run=dry_run, condition=condition))
else:
copy_file(src_name, dst_name, preserve_mode,
preserve_times, update, dry_run=dry_run)
outputs.append(dst_name)
return outputs
def walk_files(path):
for root, dirs, files in os.walk(path):
for f in files:
yield f
def find_app(app):
dpath = os.path.realpath(app)
if os.path.exists(dpath):
return dpath
if os.path.isabs(app):
return None
for path in os.environ.get('PATH', '').split(':'):
dpath = os.path.realpath(os.path.join(path, app))
if os.path.exists(dpath):
return dpath
return None
def check_output(command_line):
p = subprocess.Popen(command_line,
stdout=subprocess.PIPE)
stdout, _ = p.communicate()
xit = p.wait()
if xit != 0:
raise subprocess.CalledProcessError(xit, command_line)
return stdout
_tools = {}
def _get_tool(toolname):
if toolname not in _tools:
if os.path.exists('/usr/bin/xcrun'):
try:
_tools[toolname] = check_output(['/usr/bin/xcrun', '-find', toolname])[:-1]
except subprocess.CalledProcessError:
raise IOError("Tool %r not found"%(toolname,))
else:
# Support for Xcode 3.x and earlier
if toolname == 'momc':
choices = [
'/Library/Application Support/Apple/Developer Tools/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc',
'/Developer/Library/Xcode/Plug-ins/XDCoreDataModel.xdplugin/Contents/Resources/momc',
'/Developer/usr/bin/momc',
]
elif toolname == 'mapc':
choices = [
'/Developer/Library/Xcode/Plug-ins/XDMappingModel.xdplugin/Contents/Resources/mapc'
'/Developer/usr/bin/mapc'
]
else:
raise IOError("Tool %r not found"%(toolname,))
for fn in choices:
if os.path.exists(fn):
_tools[toolname] = fn
break
else:
raise IOError("Tool %r not found"%(toolname,))
return _tools[toolname]
def momc(src, dst):
subprocess.check_call([_get_tool('momc'), src, dst])
def mapc(src, dst):
subprocess.check_call([_get_tool('mapc'), src, dst])