2

I have a python script which builds a solution file using msbuild on windows system.I want to show the command prompt output when the build process is running.My code something looks like below

def build(self,projpath):
    if not os.path.isfile(self.msbuild):
        raise Exception('MSBuild.exe not found. path=' + self.msbuild)

    arg1 = '/t:Rebuild'
    arg2 = '/p:Configuration=Release'
    p = subprocess.call([self.msbuild,projpath,arg1,arg2])
    print p
    if p==1:
        return False
    return True

I am able to build the file, but i need to show the build status in the separate GUI(status window).I tried a lot to redirect the command prompt output to a file and then read from the file but, could not make it. I tried with below command,

subprocess.check_output('subprocess.call([self.msbuild,projpath,arg1,arg2])', shell=False) > 'C:\tmp\file.txt'

can anyone let me know how can i show all the outputs from a command prompt in a status window(a GUI using wxpython) when i run the script?

Aramanethota
  • 683
  • 2
  • 9
  • 23

1 Answers1

4

I did something like this when I wanted to capture traceroute and ping commands with wxPython. I wrote about it in this tutorial: http://www.blog.pythonlibrary.org/2010/06/05/python-running-ping-traceroute-and-more/

First of all you'll need to redirect stdout, which basically goes like this:

redir=RedirectText(log)
sys.stdout=redir

Where RedirectText is a special class that accepts a wx.TextCtrl as an argument. See below:

class RedirectText(object):
    def __init__(self,aWxTextCtrl):
        self.out=aWxTextCtrl

    def write(self,string):
        self.out.WriteText(string)

And here's an example of my ping command:

proc = subprocess.Popen("ping %s" % ip, shell=True, 
                        stdout=subprocess.PIPE) 
print
while True:
    line = proc.stdout.readline()                        
    wx.Yield()
    if line.strip() == "":
        pass
    else:
        print line.strip()
    if not line: break
proc.wait()

So you just need to run subprocess and use its readline function to grab the data as it is output. Then you print the output to stdout which is redirected to the text control. The wx.Yield() call will allow the text control to get updated in real time. Otherwise, it would get updated AFTER the subprocess is finished.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Thanks @Mike.Your code helped me displaying python command line output in a panel of a frame. But,i want windows command prompt output to be displayed in my panel when i call python commands! – Aramanethota Mar 06 '13 at 09:41