I am trying to write a python script that checks out a revision from a mercurial repo for a visual studio 2010 native c++ project, builds it, and then runs a program under various scenarios. Then I will compare results from other builds, etc. So I am just getting started with a prototype, and I have:
from subprocess import call
import os
import tempfile
import sys
def main():
temp_repo_name = 'temprepo'
d = tempfile.mkdtemp()
os.chdir(os.path.normpath(d))
command1 = ['hg', 'clone', r'C:\temp\1\jxg_hcr', temp_repo_name]
devnull = open(os.devnull,'w')
rc1 = call(command1,stdout=devnull, stderr=devnull)
if rc1 != 0:
print('could not clone repo into temporary directory. Terminating Program')
sys.exit(1)
devnull.close()
devnull = open(os.devnull,'w')
os.chdir(temp_repo_name)
command2 = [r'msbuild', r'hcr_dll.sln', r'/t:Rebuild',r'/p:Configuration=Release']
rc2 = call(command2,stdout=devnull, stderr=devnull)
print rc2
if rc2 != 0:
print('could not build repo. Terminating Program')
sys.exit(1)
devnull.close()
if __name__ == '__main__':
main()
When I run this, I get the following output on my console:
C:\programming\eclipse_workspace\hcr_cli_build>python hcr_cli_build.py
0
1
could not build repo. Terminating Program
But when I change the line for command2 to
rc2 = call(command2)
I get
0
<bunch of build output>
0
and it builds successfully. I do not know why it would fail when I redirect.
Any Ideas? Maybe just a silly error I cannot see?
NOTE: I don’t think I really need to close and re-open devnull each time, but it was just something I was trying out when I was trying to figure out the issue. I get the same results when I just keep it open the whole time and close it at the very end.
EDIT1: This also fails from the command line when I tried it per the suggestion of David Hess.
EDIT2: I have also verified this same issue occurs when I create an empty C# console application with the boiler plate Main function. It will build via the gui and on the command line when msbuild is not redirected to NUL, but when I do redirect, it returns 1 and does not build.
Bounty Edit: I am most interested in why this is happening. Obviously, I would also like to be able to succeed silently and just output a message saying “Build went okay” if the return code was 0, so if nobody can tell me why it is happening, the bounty will go to the best solution.
I also tried a bash script on mintty.exe:
#!/bin/bash
for i in 1 2 3 4
do
echo "doing $i"
msbuild.exe /c/temp/$i/jxg_hcr/hcr_dll.sln //t:Rebuild //p:Configuration=Release
echo $?
done
works (it prints all the output and builds successfully and then prints 0 to the console), but
#!/bin/bash
for i in 1 2 3 4
do
echo "doing $i"
msbuild.exe /c/temp/$i/jxg_hcr/hcr_dll.sln //t:Rebuild //p:Configuration=Release > /dev/null
echo $?
done
does not perform the build and just returns 1 to the console.
Apparently this is a bug in msbuild:
http://connect.microsoft.com/VisualStudio/feedback/details/633122/msbuild-fails-with-1-when-redirecting-the-output-1-nul-2-anywhere