hoe*_*ing 11 python setuptools cython
我正在使用Cython从Python模块生成共享对象.编译输出被写入build/lib.linux-x86_64-3.5/<Package>/<module>.cpython-35m-x86_64-linux-gnu.so
.有没有更改命名规则的选项?我希望在<module>.so
没有解释器版本或arch附录的情况下命名文件.
似乎setuptools
没有提供完全更改或删除后缀的选项.神奇的事情发生在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
Run Code Online (Sandbox Code Playgroud)
好像我需要添加一个post-build重命名动作.
2016年12月8日更新:
好的,我忘了实际发布解决方案.实际上,我通过重载内置install_lib
命令实现了重命名操作.这是逻辑:
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]
Run Code Online (Sandbox Code Playgroud)
现在你要做的就是在setup
函数中重载命令:
setup(
...
cmdclass={
'install_lib': _CommandInstallCythonized,
},
...
)
Run Code Online (Sandbox Code Playgroud)
不过,我对重载标准命令不满意; 如果你找到更好的解决方案,发布它,我会接受你的答案.
此行为已在 distutils 包中定义。distutils 使用 sysconfig 和“EXT_SUFFIX”配置变量:
# Lib\distutils\command\build_ext.py
def get_ext_filename(self, ext_name):
r"""Convert the name of an extension (eg. "foo.bar") into the name
of the file from which it will be loaded (eg. "foo/bar.so", or
"foo\bar.pyd").
"""
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
Run Code Online (Sandbox Code Playgroud)
从 Python 3.5 开始,“EXT_SUFFIX”变量包含平台信息,例如“.cp35-win_amd64”。
我编写了以下函数:
def get_ext_filename_without_platform_suffix(filename):
name, ext = os.path.splitext(filename)
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
if ext_suffix == ext:
return filename
ext_suffix = ext_suffix.replace(ext, '')
idx = name.find(ext_suffix)
if idx == -1:
return filename
else:
return name[:idx] + ext
Run Code Online (Sandbox Code Playgroud)
和自定义build_ext命令:
from Cython.Distutils import build_ext
class BuildExtWithoutPlatformSuffix(build_ext):
def get_ext_filename(self, ext_name):
filename = super().get_ext_filename(ext_name)
return get_ext_filename_without_platform_suffix(filename)
Run Code Online (Sandbox Code Playgroud)
用法:
setup(
...
cmdclass={'build_ext': BuildExtWithoutPlatformSuffix},
...
)
Run Code Online (Sandbox Code Playgroud)