更改.so文件的Cython命名规则 [英] Change Cython's naming rules for .so files

查看:442
本文介绍了更改.so文件的Cython命名规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cython从Python模块中生成共享对象。编译输出将写入 build / lib.linux-x86_64-3.5 /< Package> /< module> .cpython-35m-x86_64-linux-gnu.so 。是否可以更改命名规则?我希望将文件命名为< module> .so ,而无需解释器版本或拱形附录。

I'm using Cython to generate a shared object out of Python module. The compilation output is written to build/lib.linux-x86_64-3.5/<Package>/<module>.cpython-35m-x86_64-linux-gnu.so. Is there any option to change the naming rule? I want the file to be named <module>.so without the interpreter version or arch appendix.

推荐答案

类似于 setuptools 的选项,没有提供更改或完全消除后缀的选项。魔术发生在 distutils / command / build_ext.py 中:

Seems like setuptools provides no option to change or get rid of the suffix completely. The magic happens in distutils/command/build_ext.py:

def get_ext_filename(self, ext_name):
    from distutils.sysconfig import get_config_var
    ext_path = ext_name.split('.')
    ext_suffix = get_config_var('EXT_SUFFIX')
    return os.path.join(*ext_path) + ext_suffix

似乎我需要添加一个后构建重命名

Seems like I will need to add a post-build renaming action.

2016年8月12日更新:

好,我忘了实际发布解决方案。实际上,我通过重载内置的 install_lib 命令实现了重命名操作。逻辑如下:

Ok, I forgot to actually post the solution. Actually, I implemented a renaming action by overloading the built-in install_lib command. Here's the logic:

from distutils.command.install_lib import install_lib as _install_lib

def batch_rename(src, dst, src_dir_fd=None, dst_dir_fd=None):
    '''Same as os.rename, but returns the renaming result.'''
    os.rename(src, dst,
              src_dir_fd=src_dir_fd,
              dst_dir_fd=dst_dir_fd)
    return dst

class _CommandInstallCythonized(_install_lib):
    def __init__(self, *args, **kwargs):
        _install_lib.__init__(self, *args, **kwargs)

    def install(self):
        # let the distutils' install_lib do the hard work
        outfiles = _install_lib.install(self)
        # batch rename the outfiles:
        # for each file, match string between
        # second last and last dot and trim it
        matcher = re.compile('\.([^.]+)\.so$')
        return [batch_rename(file, re.sub(matcher, '.so', file))
                for file in outfiles]

现在所有要做的就是在 setup 函数中重载命令:

Now all you have to do is to overload the command in the setup function:

setup(
    ...
    cmdclass={
        'install_lib': _CommandInstallCythonized,
    },
    ...
)

不过,我对重载标准命令不满意;如果您找到更好的解决方案,请将其发布,我会接受您的回答。

Still, I'm not happy with overloading standard commands; if you find a better solution, post it and I will accept your answer.

这篇关于更改.so文件的Cython命名规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆