Mawg
Mawg

Reputation: 40155

Python subprocess not returning

I want to call a Python script from Jenkins and have it build my app, FTP it to the target, and run it.

I am trying to build and the subprocess command fails. I have tried this with both subprocess.call() and subprocess.popen(), with the same result.

When I evaluate shellCommand and run it from the command line, the build succeeds.

Note that I have 3 shell commands: 1) remove work directory, 2) create a fresh, empty, work directory, then 3) build. The first two commands return from subprocess, but the third hangs (although it completes when run from the command line).

What am I doing wrongly? Or, what alternatives do I have for executing that command?

# +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
def ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand):
    try:
        process = subprocess.call(shellCommand, shell=True, stdout=subprocess.PIPE)
        #process.wait()
        return process #.returncode

    except KeyboardInterrupt, e: # Ctrl-C
        raise e
    except SystemExit, e: # sys.exit()
        raise e
    except Exception, e:
        print 'Exception while executing shell command : ' + shellCommand
        print str(e)
        traceback.print_exc()
        os._exit(1)

# +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
def BuildApplciation(arguments):
    # See http://gnuarmeclipse.github.io/advanced/headless-builds/

    jenkinsWorkspaceDirectory = arguments.eclipseworkspace + '/jenkins'

    shellCommand = 'rm -r ' + jenkinsWorkspaceDirectory
    ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand)

    shellCommand = 'mkdir ' + jenkinsWorkspaceDirectory
    if not ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand) == 0:
        print "Error making Jenkins work directory in Eclipse workspace : " + jenkinsWorkspaceDirectory
        return False

    application = 'org.eclipse.cdt.managedbuilder.core.headlessbuild'
    shellCommand = 'eclipse -nosplash -application ' + application + ' -import ' + arguments.buildRoot + '/../Project/ -build myAppApp/TargetRelease -cleanBuild    myAppApp/TargetRelease -data ' + jenkinsWorkspaceDirectory + ' -D DO_APPTEST'
    if not ExcecuteShellCommandAndGetReturnCode(arguments, shellCommand) == 0:
        print "Error in build"
        return False

Upvotes: 3

Views: 9727

Answers (1)

Mawg
Mawg

Reputation: 40155

I Googled further and found this page, which, at 1.2 says

One way of gaining access to the output of the executed command would be to use PIPE in the arguments stdout or stderr, but the child process will block if it generates enough output to a pipe to fill up the OS pipe buffer as the pipes are not being read from.

Sure enough, when I deleted the , stdout=subprocess.PIPE from the code above, it worked as expected.

As I only want the exit code from the subprocess, the above code is enough for me. Read the linked page if you want the output of the command.

Upvotes: 7

Related Questions