Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for existence of Python dev files from bash script

Tags:

python

bash

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.

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

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

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
like image 691
CarpeNoctem Avatar asked Jan 31 '11 07:01

CarpeNoctem


3 Answers

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:

# 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.

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.

like image 90
Shawn Chin Avatar answered Oct 01 '22 14:10

Shawn Chin


Imho your solutions works well.

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

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

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

like image 26
Enrico Carlesso Avatar answered Oct 01 '22 13:10

Enrico Carlesso


For those looking for a pure python solution that also works for python3:

python3 -c 'from distutils.sysconfig import get_makefile_filename as m; from os.path import isfile; import sys ; sys.exit(not isfile(m()))')

Or as a file script check-py-dev.py:

from distutils.sysconfig import get_makefile_filename as m
from os.path import isfile 
import sys 
sys.exit(not isfile(m()))

To get a string in bash, just use the exit output:

python3 check-py-dev.py && echo "Ok" || echo "Error: Python header files NOT found"
like image 38
Onilton Maciel Avatar answered Oct 01 '22 13:10

Onilton Maciel