distutils如何确定要复制的二进制文件?

时间:2016-09-03 20:34:15

标签: python distutils py2app

我为distutils定义了这样的setup命令(对于Mac OS X使用py2app,如果重要的话):

setup(...,
      extensions=Extension('tracking_funcs',
                           ['tracking_funcs/tracking_funcs.pyx'],
                           include_dirs=[numpyincludedirs,]),
                 Extension('_psutil_osx',
                           sources = ['psutil/_psutil_osx.c',
                                      'psutil/_psutil_common.c',
                                      'psutil/arch/osx/process_info.c'],
                                      define_macros=[('PSUTIL_VERSION', int(get_psutilver().replace('.', '')))],
                                      extra_link_args=['-framework', 'CoreFoundation',
                                                       '-framework', 'IOKit']),
                 Extension('_psutil_posix',
                           sources = ['psutil/_psutil_posix.c'])],
      ...)

它正确构建了所有三个扩展:

...
building 'tracking_funcs' extension
creating build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/tracking_funcs
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/include -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c tracking_funcs/tracking_funcs.c -o build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/tracking_funcs/tracking_funcs.o
... (some compiler warnings) ...
42 warnings generated.
/usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/tracking_funcs/tracking_funcs.o -o build/bdist.macosx-10.6-x86_64/lib.macosx-10.6-x86_64-2.7/tracking_funcs.so
building '_psutil_osx' extension
creating build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/psutil
... (some compiler warnings) ...
2 warnings generated.
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DPSUTIL_VERSION=400 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c psutil/_psutil_common.c -o build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/psutil/_psutil_common.o
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -DPSUTIL_VERSION=400 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c psutil/arch/osx/process_info.c -o build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/psutil/arch/osx/process_info.o
/usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/psutil/_psutil_osx.o build/bdist.macosx-10.6-x86_64/lib.macosx-10.6-x86_64-2.7/_psutil_osx.so -framework CoreFoundation -framework IOKit
building '_psutil_posix' extension
/usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c psutil/_psutil_posix.c -o build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/psutil/_psutil_posix.o
/usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g build/bdist.macosx-10.6-x86_64/temp.macosx-10.6-x86_64-2.7/psutil/_psutil_posix.o -o build/dist.macosx-10.6-x86_64/lib.macosx-10.6-x86_64-2.7/_psutil_posix.so
...

然后它将构建的二进制文件复制到包目标中,但它只复制三个扩展中的两个:

...
copying file /.../build/bdist.macosx-10.6-x86_64/lib.macosx-10.6-x86_64-2.7/tracking_funcs.so -> /.../dist/my.app/Contents/Resources/lib/python2.7/lib-dynload/tracking_funcs.so
copying file /.../build/bdist.macosx-10.6-x86_64/lib.macosx-10.6-x86_64-2.7/_psutil_posix.so -> /.../dist/my.app/Contents/Resources/lib/python2.7/lib-dynload/_psutil_posix.so
...

然后我的应用程序崩溃,因为它无法在运行时找到第三个扩展名。

我该如何调试? distutils从哪里获取依赖树,如果不是我定义的扩展列表?也许这个bug出现在py2app而不是distutils本身?

1 个答案:

答案 0 :(得分:0)

Py2app uses modulegraph以递归方式构建一个源代码树,其中包含项目所有依赖项的所有依赖项。运行带有debug-modulegraph标志的py2app会将一堆调试信息打印到控制台并放入a breakpoint,以便您浏览模块图的内容:

$ python setup.py py2app --debug-modulegraph
...
(Pdb) for item in mf.flatten(): print item

这可能会在其输出中显示类似(MissingModule) psutil._psutil_osx的内容,这意味着modulegraph无法找到该扩展程序的导入路径。

Modulegraph公开了一个名为addPackagePath的公共函数,它允许您为其在特定包中查找文件的位置提供额外的提示。在这种情况下,在setup.py中添加类似的内容可以解决问题:

from modulegraph import modulegraph
modulegraph.addPackagePath('psutil', 'build/bdist.macosx-10.6-x86_64/lib.macosx-10.6-x86_64-2.7/psutil/')