3

I'm trying to add a try/except to my subprocess.

        try:
            mountCmd = 'mount /dev/%s %s%s' % (splitDevice, homeDir, splitDevice)
            dev = '/dev/%s' % splitDevice
            subprocess.check_call(mountCmd, shell=True)
        except subprocess.CalledProcessError:
            continue

The above snipet works, but if the host executes the code on a Python version below 2.5 the code will fail since CalledProcessError was introduced in Python version 2.5.

Does anybody know of a substitute I can use for the CalledProcessError module?

EDIT: This is how I solved my issue

        mountCmd = 'mount /dev/%s %s%s' % (splitDevice, homeDir, splitDevice)
        dev = '/dev/%s' % splitDevice
        returnCode = 0
        #CalledProcessError module was introduced in version 2.5 of python. If older version do the following.
        if sys.hexversion < 0x02050000: 
            try:
                p3 = subprocess.Popen(mountCmd, shell=True, stdout=subprocess.PIPE)
                output = p3.communicate()[0]
                returnCode = p3.returncode
            except:
                pass
            if returnCode != 0:
                continue
        else: #If version of python is newer than 2.5 use CalledProcessError.
            try:
                subprocess.check_call(mountCmd, shell=True)
            except subprocess.CalledProcessError, e:
                continue
Adilicious
  • 1,643
  • 4
  • 18
  • 22

1 Answers1

1

exception subprocess.CalledProcessError

Exception raised when a process run by check_call() or check_output() returns a non-zero exit status.

returncode

    Exit status of the child process.

cmd

    Command that was used to spawn the child process.

output

    Output of the child process if this exception is raised by check_output(). Otherwise, None.

Source. This means that you need to check whether the process ran by check_all or check_output has a non-zero output.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • Should I use Popen then instead of check_call()? The bellow is in relation to check_call: 'Note Do not use stdout=PIPE or stderr=PIPE with this function as that can deadlock based on the child process output volume. Use Popen with the communicate() method when you need pipes.' – Adilicious Feb 26 '15 at 15:57
  • That sounds like an idea. Any strategy which enables you to get the result of the process should more-or-less solve the problem, but if I were you, I would only apply this code for old Python. The new Python should use all the juicy features possible. – Lajos Arpad Feb 26 '15 at 16:04
  • The issue is that I won't know what version of python will be in use but I agree I might add something that first checks if python version is 2.5 or above and then chose what to use. Thanks I'll update my post when I get a chance to test this. – Adilicious Feb 26 '15 at 16:18
  • 1
    You can determine the version in use. Check out this post: http://stackoverflow.com/questions/1093322/how-do-i-check-what-version-of-python-is-running-my-script – Lajos Arpad Feb 26 '15 at 16:38
  • Thanks for the help @lajos I've implemented the version check as well. – Adilicious Feb 27 '15 at 11:23
  • Out of curiosity: what kind of system is it that requires to use Python 2.4? :) – Dr. Jan-Philip Gehrcke Feb 27 '15 at 11:24