5

假设我有一个包,它在代码中的某处调用可执行文件(例如第三方 c/java 程序)。让我们进一步假设,应用程序足够小/微不足道,可以与包捆绑在一起。例如单个可执行文件 ( cfoo)。

我可以继续,将文件放入以下结构:

.
|-- foo
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- core.py
|   |-- corebin
|   |   `-- cfoo
|   `-- foomain.py
`-- setup.py

并准备setup.py如下:

from setuptools import setup

setup(
    name='foo',
    version='1.0',
    packages=['foo'],
    scripts=['foo/foomain.py'],
    package_data={'foo': ['corebin/*']},
    zip_safe=False
)

这将使我能够正确安装软件包。后来,在包代码中我可以这样做:

from subprocess import call

import pkg_resources as res

def main():
    fn = res.resource_filename('foo', 'corebin/cfoo')
    print "Resource located at:", fn
    call([fn])

不幸的是,可执行文件将在没有设置可执行标志的情况下安装。即使原始文件已设置。chmod在脚本末尾添加调用setup.py并不容易,因为需要先找出正确的安装路径。我尝试过,resource_filename但返回了本地文件(如“预安装”)。

如何解决这个问题?也virtualenv考虑到...

4

1 回答 1

2

I'm promoting my comment to an answer:

If you install it using the scripts keyword, it will get the correct mode (and get installed in an appropriate bin/ directory).

How would you execute something on files contained inside a package after install?

This question would appear to address the same situation, and it looks like it has a reasonable answer.

于 2011-11-08T16:57:09.070 回答