使用“setup.py”安装软件包后运行“chmod”

发布于 2024-12-13 20:15:34 字数 1016 浏览 0 评论 0原文

假设我有一个包,它在代码中的某个位置调用可执行文件(例如第三方 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])

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

如何解决这个问题呢?还要考虑到virtualenv...

Let's assume I have a package which calls an executable file somewhere in the code (for example a third-party c/java-program). Let's further assume, the application is small/trivial enough to bundle with the package. For example a single executable file (cfoo).

I could go ahead, and put the files into the following structure:

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

And prepare a setup.py as follows:

from setuptools import setup

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

This will allow me to properly install the package. Later, in the package-code I could do this:

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])

Unfortunately, the executable file will be installed without executable flag set. Even if the original file had it set. Adding a chmod call at the end of the setup.py script is not as easy, as one would need to figure out the proper installation path first. I tried with resource_filename but that returned the local file (as in "pre-installation").

How can this problem be solved? Also with virtualenv in mind...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水水月牙 2024-12-20 20:15:34

我将我的评论推广为答案:

如果您使用 scripts 关键字安装它,它将获得正确的模式(并安装在适当的 bin/ 目录中)。

安装后如何对包内包含的文件执行某些操作?

此问题会出现针对同样的情况,看起来它有一个合理的答案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文