2

I am trying to build a program which starts a new command prompt and run a custom command defined by the user. Here is a snippet of my code:

if(fileName == "" or className == ""):
    tkMessageBox.showinfo("Error", "Please select a test class/test!")
else:
    command = ["start","/w","cmd","/c","ctetest"]
    if(verbose.get()):
        command.append("-v")
    if(xml.get()):
        command.append("-x")
    if(version.get()):
        command.append("-V")
    if(output.get()):
        command.append("-o")
    command.append("RegressionTest/" + folderName)
    command.append(fileName + "." + className + "." + methodName)
    processOutput = subprocess.check_output(command, shell = True)
    print processOutput

I am able to run the command in the new command prompt and then close it when it finishes but the problem is that I am not getting the output from my custom command but instead I am getting the output from the "start" command which is nothing. Is there a way to obtain the output from my custom command? Thanks in advance!

Ted
  • 365
  • 1
  • 7
  • 16
  • Do you really need to open a new CMD window and run the command there? You could run that command directly and check for any output repeatedly. – ForceBru Nov 09 '16 at 16:13
  • @ForceBru Yeah, because I might want to run multiple of these commands and monitor them while they are running. I agree that it would be simpler if I run this directly but that will make it really messy since sometimes my output can be very long that it clips and a chunk can be lost. – Ted Nov 09 '16 at 16:17

1 Answers1

0

You don't need multiple CMD windows, just have one.

If you want to run multiple commands at a time, use multi-threading.

If you fear for the returned data to get truncated, just output it to a file.

Like :

if(fileName == "" or className == ""):
    tkMessageBox.showinfo("Error", "Please select a test class/test!")
else:
    command = ["ctetest"]
    if(verbose.get()):
        command.append("-v")
    if(xml.get()):
        command.append("-x")
    if(version.get()):
        command.append("-V")
    if(output.get()):
        command.append("-o")
    command.append("RegressionTest/" + folderName)
    command.append(fileName + "." + className + "." + methodName)
    command.append('> %s_test_%s_%s_%s.txt' % (time.time(), fileName, className, methodName))
    subprocess.call(command, shell = True)
Loïc
  • 11,804
  • 1
  • 31
  • 49
  • But assuming that I do want a new window, there's really no way I can get the output? This is ultimately my next step, to output to a text file. But as of right now, my custom command is used to run a test script, inside this test script I send multiple commands to a server and monitor the response that I can get back. Sometimes the server will take a while or maybe not even respond. I want separate windows so that I can cleanly monitor the behavior/response time of the server. – Ted Nov 09 '16 at 16:36
  • I really have no idea how you could retrieve the output when you open a new window, if not by writing it to a file (or any database). But again using multithreading, you could write each command output to a csv file (along with the class / method being tested, the time it took, the eventual errors (timeouts, server unreachable...)) So by just one glance at that csv document, you can perfectly monitor what's going on. – Loïc Nov 09 '16 at 17:09