检查是否存在来自bash脚本的Python开发文件

时间:2022-06-21 00:14:32

I am creating a simple bash script to download and install a python Nagios plugin. On some older servers the script may need to install the subprocess module and as a result I need to make sure the correct python-devel files are installed.

我正在创建一个简单的bash脚本来下载和安装python Nagios插件。在一些较老的服务器上,脚本可能需要安装子过程模块,因此我需要确保安装了正确的python-devel文件。

What is an appropriate and cross platform method of checking for these files. Would like to stay away from rpm or apt.

检查这些文件的合适的跨平台方法是什么?想要远离rpm或apt。

If you can tell me how to do the check from within python that would work. Thanks!

如果您能告诉我如何从python中进行检查,那将会起作用。谢谢!

Update:

更新:

This is the best I have come up with. Anyone know a better or more conclusive method?

这是我想出的最好的办法。有人知道一个更好或更有说服力的方法吗?

if [ ! -e $(python -c 'from distutils.sysconfig import get_makefile_filename as m; print m()') ]; then echo "Sorry"; fi

2 个解决方案

#1


5  

That would be pretty much how I would go about doing it. Seems reasonable simple.

这就是我要做的。似乎是合理的简单。

However, if I need to be really sure that python-devel files are installed for the current version of Python, I would look for the relevant Python.h file. Something along the lines of:

但是,如果我确实需要确保Python -devel文件是为当前版本的Python安装的,我将查找相关的Python。h文件。一些类似的东西:

# first, makes sure distutils.sysconfig usable
if ! $(python -c "import distutils.sysconfig.get_config_vars" &> /dev/null); then
    echo "ERROR: distutils.sysconfig not usable" >&2
    exit 2
fi

# get include path for this python version
INCLUDE_PY=$(python -c "from distutils import sysconfig as s; print s.get_config_vars()['INCLUDEPY']")
if [ ! -f "${INCLUDE_PY}/Python.h" ]; then
    echo "ERROR: python-devel not installed" >&2
    exit 3
fi

Note: distutils.sysconfig may not be supported on all platforms so not the most portable solution, but still better than trying to cater for variations in apt, rpm and the likes.

注意:distutils。sysconfig可能不支持所有平台,因此不是最可移植的解决方案,但仍然比尝试满足apt、rpm之类的变化要好。

If you really need to support all platforms, it might be worth exploring what is done in the AX_PYTHON_DEVEL m4 module. This module can be used in a configure.ac script to incorporate checks for python-devel during the ./configure stage of an autotools-based build.

如果您真的需要支持所有的平台,那么在AX_PYTHON_DEVEL m4模块中做些什么可能是值得的。此模块可用于配置。在基于自动工具的构建的./配置阶段合并检查python-devel的ac脚本。

#2


1  

Imho your solutions works well.

你的解决方案很有效。

Otherwise, a more "elegant" solution would be to use a tiny script like:

否则,一个更“优雅”的解决方案就是使用一个小脚本,比如:

testimport.py

testimport.py

#!/usr/bin/env python2

import sys

try:
  __import__(sys.argv[1])
  print "Sucessfully import", sys.argv[1]
except:
  print "Error!"
  sys.exit(4)

sys.exit(0)

And call it with testimport.sh distutils.sysconfig

用testimport。sh distutils.sysconfig

You can adapt it to check for internal function if needed...

如果需要,您可以调整它来检查内部功能……

#1


5  

That would be pretty much how I would go about doing it. Seems reasonable simple.

这就是我要做的。似乎是合理的简单。

However, if I need to be really sure that python-devel files are installed for the current version of Python, I would look for the relevant Python.h file. Something along the lines of:

但是,如果我确实需要确保Python -devel文件是为当前版本的Python安装的,我将查找相关的Python。h文件。一些类似的东西:

# first, makes sure distutils.sysconfig usable
if ! $(python -c "import distutils.sysconfig.get_config_vars" &> /dev/null); then
    echo "ERROR: distutils.sysconfig not usable" >&2
    exit 2
fi

# get include path for this python version
INCLUDE_PY=$(python -c "from distutils import sysconfig as s; print s.get_config_vars()['INCLUDEPY']")
if [ ! -f "${INCLUDE_PY}/Python.h" ]; then
    echo "ERROR: python-devel not installed" >&2
    exit 3
fi

Note: distutils.sysconfig may not be supported on all platforms so not the most portable solution, but still better than trying to cater for variations in apt, rpm and the likes.

注意:distutils。sysconfig可能不支持所有平台,因此不是最可移植的解决方案,但仍然比尝试满足apt、rpm之类的变化要好。

If you really need to support all platforms, it might be worth exploring what is done in the AX_PYTHON_DEVEL m4 module. This module can be used in a configure.ac script to incorporate checks for python-devel during the ./configure stage of an autotools-based build.

如果您真的需要支持所有的平台,那么在AX_PYTHON_DEVEL m4模块中做些什么可能是值得的。此模块可用于配置。在基于自动工具的构建的./配置阶段合并检查python-devel的ac脚本。

#2


1  

Imho your solutions works well.

你的解决方案很有效。

Otherwise, a more "elegant" solution would be to use a tiny script like:

否则,一个更“优雅”的解决方案就是使用一个小脚本,比如:

testimport.py

testimport.py

#!/usr/bin/env python2

import sys

try:
  __import__(sys.argv[1])
  print "Sucessfully import", sys.argv[1]
except:
  print "Error!"
  sys.exit(4)

sys.exit(0)

And call it with testimport.sh distutils.sysconfig

用testimport。sh distutils.sysconfig

You can adapt it to check for internal function if needed...

如果需要,您可以调整它来检查内部功能……