[Tutor] Running Java process doesn't return to subprocess.call

Jerome Jabson jjabson at yahoo.com
Mon May 15 18:16:47 CEST 2006


Hello,

I'm running a Java process from my python script using
the subprocess module. But there seems to be some
problem with the return code to subprocess and the
Java program is just hanging waiting for something. If
I run the Java program from shell it terminates
normally and if run the same program through a shell
script it does the same thing. Is there something
different I need to handle when running a Java program
vs other programs? Here's my code:

def proc(cmd_in):
   cmd = cmd_in
   if isinstance(cmd, types.StringTypes):
      cmd = cmd.split(' ')
   print cmd
   os.chdir("/home/qauser/jerome")
   outFile = os.path.join(os.curdir, "output.log")
   outptr = file(outFile, "w")
   errFile = os.path.join(os.curdir, "error.log")
   errptr = file(errFile, "w")
   retval = subprocess.call(cmd, 0, None, None,
outptr, None)
   errptr.close()
   outptr.close()
   if retval != 0:
      errptr = file(errFile, "r")
      errData = errptr.read()
      errptr.close()
      raise Exception("Error executing command: " +
repr(errData))

def setClass():
   print "=== Setting Classpath ==="
   src = "/home/qauser/automation/setClass.sh"
   dst = "/home/qauser/jerome/setClass.sh"
   os.chdir("/home/qauser/jerome")
   shutil.copyfile(src, dst)
   os.chmod(dst, 0777)
   proc("/home/qauser/jerome/setClass.sh")
   classFile = open("/home/qauser/jerome/output.log",
"r")
   CP = classFile.read()
   CP = CP.rstrip('\n')
   print "=== Complete Classpath Setup ==="
   return CP

def run_junit(classpath):
   file_in =
open("/home/qauser/automation/testdata/junit_master_file",
"r")
   match = re.compile('#+')
   work_list = []
   for line in file_in:
      work = line
      work = work.rstrip('\n')
      if match.search(work):
         found = False
      else:
         found = True
      if found == True:
         work_list = work.split(',')
         arg1 = work_list[0]
         arg2 = work_list[1]
         arg3 = work_list[2]
         arg4 = work_list[3]
         os.chdir("/home/qauser/jerome")
         cmd = "java -cp %s
com.redsealsys.srm.server.analysis.NetmapTest %s %s %s
%s"
         cmdstr = cmd %
(classpath,arg1,arg2,arg3,arg4)
         print "=== Starting JUnit Run ==="
         proc(cmdstr)
         print "=== JUnit Run Complete ==="

####################
######  Main  ######
####################

cp = setClass()
run_junit(cp)

The junit_master_file is just a comma separted file:

netmap_func,/home/qauser/junit/static_nat/simple_nat,10.1.1.0,172.16.200.0

which has the prameters that I need to pass into the
Java program. I would appreciate any insight or help
in this matter.

Much Thanks,
Jerome

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list