使用package_data将构建的框架复制到Python包中

时间:2017-11-15 04:49:42

标签: python setuptools

我试图改进PyBluez在macOS上的安装方式。 Here是我今天在更改之前的setup.py代码。

简而言之,在macOS上安装了一个名为lightblue的额外包,这取决于一个名为LightAquaBlue.framework的框架。今天,它调用xcodebuild构建框架并将其安装到/Library/Frameworks,但我想更改它以将框架嵌入到Python包中。

这就是我所做的:

packages.append('lightblue')
package_dir['lightblue'] = 'osx'
install_requires += ['pyobjc-core>=3.1', 'pyobjc-framework-Cocoa>=3.1']

# Add the LightAquaBlue framework to the package data as an 'eager resource'
# so that we can extract the whole framework at runtime
package_data['lightblue'] = [ 'LightAquaBlue.framework' ]
eager_resources.append('LightAquaBlue.framework')

# FIXME: This is inelegant, how can we cover the cases?
if 'install' in sys.argv or 'bdist' in sys.argv or 'bdist_egg' in sys.argv:
    # Build the framework into osx/
    import subprocess
    subprocess.check_call([
        'xcodebuild', 'install',
        '-project', 'osx/LightAquaBlue/LightAquaBlue.xcodeproj',
        '-scheme', 'LightAquaBlue',
        'DSTROOT=' + os.path.join(os.getcwd(), 'osx'),
        'INSTALL_PATH=/',
        'DEPLOYMENT_LOCATION=YES',
    ])

这将在osx/lightblue包的目录)中构建LightAquaBlue.framework,然后将其作为package_data传递给setuptools。但是,当我运行pip install --upgrade -v ./pybluez/时,LightAquaBlue.framework 被复制:

creating build/lib/lightblue
copying osx/_bluetoothsockets.py -> build/lib/lightblue
copying osx/_LightAquaBlue.py -> build/lib/lightblue
copying osx/_obexcommon.py -> build/lib/lightblue
copying osx/_IOBluetoothUI.py -> build/lib/lightblue
copying osx/__init__.py -> build/lib/lightblue
copying osx/_IOBluetooth.py -> build/lib/lightblue
copying osx/_obex.py -> build/lib/lightblue
copying osx/_lightblue.py -> build/lib/lightblue
copying osx/obex.py -> build/lib/lightblue
copying osx/_macutil.py -> build/lib/lightblue
copying osx/_lightbluecommon.py -> build/lib/lightblue

如果我有setup.py在osx/内创建一个虚拟文件,并将其添加到package_data,则 会被复制。这表明我对路径没有混淆。

如果我添加os.system('ls osx/'),它还会向我显示LightAquaBlue.framework与我的虚拟文件位于同一位置。

    LightAquaBlue
--> LightAquaBlue.framework
    DUMMY_FILE_THAT_WORKS
    _IOBluetooth.py
    _IOBluetoothUI.py
    _LightAquaBlue.py
    __init__.py
    _bluetoothsockets.py
    _lightblue.py
    _lightbluecommon.py
    _macutil.py
    _obex.py
    _obexcommon.py
   obex.py

为什么框架没有正确复制?

1 个答案:

答案 0 :(得分:0)

看起来像setuptools的文档contradicts the implementation,实际上您无法指定要包含在package_data 中的目录。

它不是很优雅,但我用过:

# We can't seem to list a directory as package_data, so we will
# recursively add all all files we find
package_data['lightblue'] = []
for path, _, files in os.walk('osx/LightAquaBlue.framework'):
    for f in files:
        include = os.path.join(path, f)[4:]  # trim off osx/
        package_data['lightblue'].append(include)