【问题标题】:Creating self contained wheel创建独立的轮子
【发布时间】:2018-12-31 00:53:20
【问题描述】:

我尝试为 pyRFC 创建一个独立的轮子:http://sap.github.io/PyRFC/install.html

我有所需的(封闭源代码)库和头文件。

如果我将库和头文件复制到 $VIRTUAL_ENV/lib 和 $VIRTUAL_ENV/include,安装 pyRFC 就可以了。

现在我尝试创建一个包含封闭源库文件的轮子。

但我失败了。

如果我解压缩轮子,它只包含这个:

(pypi)pypi@pypiserver:~> unzip -l packages/pyrfc-1.9.91-cp27-cp27mu-linux_x86_64.whl
Archive:  packages/pyrfc-1.9.91-cp27-cp27mu-linux_x86_64.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
     5366  2018-07-23 13:38   pyrfc/_exception.py
     1045  2018-07-23 13:38   pyrfc/__init__.py
  1610216  2018-07-23 14:10   pyrfc/_pyrfc.so
     3835  2018-07-23 14:10   pyrfc-1.9.91.dist-info/DESCRIPTION.rst
      990  2018-07-23 14:10   pyrfc-1.9.91.dist-info/metadata.json
        6  2018-07-23 14:10   pyrfc-1.9.91.dist-info/top_level.txt
      105  2018-07-23 14:10   pyrfc-1.9.91.dist-info/WHEEL
     4666  2018-07-23 14:10   pyrfc-1.9.91.dist-info/METADATA
      715  2018-07-23 14:10   pyrfc-1.9.91.dist-info/RECORD
---------                     -------
  1626944                     9 files

extra_objects(见下文)丢失。

如何修改 pyRFC 的 setup.py 以使轮子包含来自 $VIRTUAL_ENV/lib 的库?

这里是 setup.py:https://github.com/SAP/PyRFC/blob/master/setup.py

我为 setup.py 尝试了这个补丁

@@ -48,7 +49,9 @@ PYRFC_EXT = Extension(
     , libraries=LIBS
     , define_macros=MACROS
     , extra_compile_args=COMPILE_ARGS
-    , extra_link_args=LINK_ARGS
+    , extra_link_args=LINK_ARGS,
+      library_dirs=['lib'],
+      extra_objects = ['lib/libicudata.so.50', 'lib/libsapnwrfc.so', 'lib/libicui18n.so.50', 'lib/libicuuc.so.50', 'lib/libicudecnumber.so', 'lib/libsapucum.so'],
 )

如果我在没有 libsapnwrfc.so 的情况下安装并运行库,我会收到此错误:

Traceback (most recent call last):
  File "test-pyrfc.py", line 1, in <module>
    from pyrfc import Connection
  File "/home/other/lib/python2.7/site-packages/pyrfc/__init__.py", line 22, in <module>
    from pyrfc._pyrfc import get_nwrfclib_version, Connection, TypeDescription, FunctionDescription, Server
ImportError: libsapnwrfc.so: cannot open shared object file: No such file or directory

如果我跟踪打开的调用,我发现它只查看库的根级别。该库在 virtualenv(即 /home/other)中没有得到搜索:

strace python test-pyrfc.py 2>&1 | grep libsapnwrfc.so
open("/lib64/tls/x86_64/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib64/tls/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib64/x86_64/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/lib64/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/usr/lib64/tls/x86_64/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/usr/lib64/tls/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/usr/lib64/x86_64/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("/usr/lib64/libsapnwrfc.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
write(2, "libsapnwrfc.so: cannot open shar"..., 73libsapnwrfc.so: cannot open shared object file: No such file or directory) = 73

我想通过 wheel 将“libsapnwrfc.so”安装到 virtualenv 中,因为这使我能够拥有多个独立的环境。我可以通过 RPM 或配置管理安装它,但我想避免它。

【问题讨论】:

  • 如果你使用绝对路径会发生什么?
  • @AzatIbrakov 你的意思是extra_objects 的绝对路径?我阅读了文档。这个 kwarg 用于链接。那不是问题。我在这里走错了路。我明天再看看这个。
  • 我建议使用audtiwheel 在轮子中包含所有必要的库。查看问题How to build and distribute a Python/Cython package that depends on third party libFoo.so;用auditwheel修复车轮有一个很好的答案。
  • @hoefling auditwheel 看起来不错。如果你把你的评论写成答案,我会投赞成票。

标签: python sap setup.py python-wheel pyrfc


【解决方案1】:

extra_objects 仅在链接扩展库时使用,不捆绑在轮子中。 From the docs:

extra_objects:要链接的额外文件列表(例如,“源”未暗示的目标文件、必须明确指定的静态库、二进制资源文件等)

要将库捆绑到轮子中,请使用 auditwheel 用于 Linux 轮子,或使用 delocate 用于 MacOS 轮子。 SO 已经有一个很好的问题:How to build and distribute a Python/Cython package that depends on third party libFoo.so,它涵盖了使用auditwheeldelocate 的整个车轮修复过程。在这里,我只会总结必要的命令。这两种工具的过程非常相似:

$ pip install auditwheel  # you may also need to install patchelf
$ python setup.py bdist_wheel
$ auditwheel show dist/*_linux_x86_64.whl  # will show the libs to be bundled
$ auditwheel repair dist/*_linux_x86_64.whl

这将创建一个新目录dist/wheelhouse,其中包含带有捆绑库的新轮子。

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2012-05-05
    • 2011-06-22
    相关资源
    最近更新 更多