views:

546

answers:

4

The first batch file launches a command prompt, i need the second command to be in the ccontext of the first. how can I do this in python?

As is, it launches the batch, and blocks until the batch (with its command prompt context) terminates, and then executes devenv without the necessary context.

os.system(r'%comspec% /k ""C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86')
os.system(r'devenv asdf.sln /rebuild Debug /Out last-build.txt')

think of it as in i'm in bash, and i need to execute a command in a perl context, so i type perl -c 'asdf'. executing perl and asdf back to back won't work, i need to get the devenv inside of the perl context.

+2  A: 

You could append the devenv command onto the end of the original batch file like so:

'%comspec% /k "...vcvarsall.bat" x86 && devenv asdf.sln /rebuild ...'

(obviously I have shortened the commands for simplicity's sake)

+2  A: 

I these situations I use script that does it all. That way you can chain as much as you want. Sometimes I will generate the script on the fly.

compileit.cmd
  call C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat
  devenv $1.sln /rebuild Debug /Out last-build.txt
Sanjaya R
2nd vote for generating the script then running it. Cat the command lines into a file and then call it, then delete the file. Saves you another build artifact that has to be maintained outside your primary build script (ant, for us).
Niniki
FYI: devenv.exe will never get executed unless you use the "call" command to invoke vcvarsall.bat.
bk1e
devenv is not the proper way to do command line builds. You should use vcbuild for vc <=9.0 and msbuild for versions >=10.0.
Sorin Sbarnea
+1  A: 

I run my Python script from a batch file that sets the variables :-)

call ...\vcvarsall.bat
c:\python26\python.exe myscript.py

But Brett's solution sounds better.

orip
+3  A: 

I think that the proper way for achieving this would be running this command:

%comspec% /C "%VCINSTALLDIR%\vcvarsall.bat" x86 && vcbuild "project.sln"

Below you'll see the Python version of the same command:

os.system('%comspec% /C "%VCINSTALLDIR%\\vcvarsall.bat" x86 && vcbuild "project.sln"')

This should work with any Visual Studio so it would be a good idea to edit the question to make it more generic.

There is a small problem I found regarding the location of vcvarsall.bat - Because VCINSTALLDIR is not always set, you have to use the registry entries in order to detect the location where it is installer:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0]
"InstallDir"="c:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\IDE\\"

Add ..\..\VC\vcvarsall.bat to this path. Also is a good idea to test for other versions of Visual Studio.

Sorin Sbarnea