3

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
asked Oct 6, 2016 at 15:28

1 Answer 1

7

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.

mit
11.3k11 gold badges52 silver badges74 bronze badges
answered Oct 6, 2016 at 16:16
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.