[help] Driving Visual Studio From Python

Roger Burnham rburnham at cri-inc.com
Fri Dec 14 16:35:12 CET 2001


This might get you started:

---------------------file MakeVCProjs.py---------------------

import sys
import win32ui
import win32com
from win32com.client import gencache
import string

projects = {
    r'00 C:\DevSrc\Python\Cri\CRILicenseDLL\CRILicenseDLL.dsw':

    r'01 C:\DevSrc\CRI\ColorEditToolRes\ColorEditToolRes.dsw':

    r'03 C:\DevSrc\CRI\FPAssistantRes\FPAssistantRes.dsw':
    r'04 C:\DevSrc\CRI\LCCalibRes\LCCalibRes.dsw':

    r'04 C:\DevSrc\CRI\LCDiodeCalibRes\LCDiodeCalibRes.dsw':

    #...many elided...

def DoBuild(app, workSpace, projectList, configSuffix, force):
    if app is None:
        app = win32com.client.Dispatch('MSDev.Application')
        app.Visible = 1
    docs = app.Documents

    print 'Open workspace', workSpace
    docs.Open(workSpace, 'Auto', 1)
    tailLen = len(configSuffix)

    for projectName in projectList:
        print '   looking for', projectName, '...',
        projects = app.Projects
        for project in projects:
            projFound = 0
            if str(project.Name) == projectName:
                projFound = 1
                print '   looking for config', configSuffix, '...',
                for config in project.Configurations:
                    confFound = 0
                    name = str(config.Name)
                    if name[-tailLen:] == configSuffix:
                        confFound = 1
                        if force:
                            print '   Forcing build ', name, '...'
                            print '   Updating build ', name, '...'
                        if app.Errors > 0:
                            win32ui.MessageBox('Error building %s' % name)
                            raise SystemExit
                if not confFound:
                    print '-- NOT Found'
        if not projFound:
            print '-- NOT Found'
def main(config='Release', force=0):
    mod = gencache.GetModuleForProgID('MSDev.Application')
    if mod is None:
        win32ui.MessageBox('''Could not import MSDev.Application:
        You probably need to run the COM makepy utility to
        regenerate the VC++ Shared Objects (6.0) interface''')
        raise ImportError
    app = win32com.client.Dispatch('MSDev.Application')
    app.Visible = 1
    projectList = projects.keys()
    for workSpaceID in projectList:
        num, workSpace = string.split(workSpaceID)
        DoBuild(app, workSpace, projects[workSpaceID], config, force)

if __name__ == '__main__':
    import sys
    config = 'Release'
    force = 0
    for arg in sys.argv[1:]:
        if arg in ('Release', 'Debug'):
            config = arg
        if arg in ('Force',):
            force = 1
    main(config, force)

---------------------file MakeVCProjs.py---------------------

On Tue, 27 Nov 2001 01:29:36 -0500, Michael Schneider
<Michael.L.Schneider at eds.com> wrote:

>This is a multi-part message in MIME format.
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>Hello  All,
>I am an old unix developer finally moving to Windows Visual C++.  I have
>been using python on Unix since 96,
>and have been really happy with python as a scripting/tools language.
>I need to drive visual studio to create projects, add configurations,
>and add files to projects.
>The COM extensions  make it look like it is possible to do this, but I
>am having trouble getting
>it going.
>I have ran makepy and added the visual studio type libs.
>I copied the following code from a post
>import win32com.client
>app = win32com.client.Dispatch( 'MSDev.Application')
>wnd = app.ActiveWindow
>if wnd.Type == 'Text':
>    print wnd.Selection
>app = None
>This prints on the selected test.
>I can't seem to get the syntax to obtain and print out project
>properties, or to add a project
>Could someone help my out by giving me the python code to try to get
>this to go, I am missing something
>very basic here.
>PS.  I added the MSDEV links to the visual studio docs
>DeveStudio Link to add project
>Link to Visual Studio Application Object
>Content-Type: text/x-vcard; charset=us-ascii;
> name="Michael.L.Schneider.vcf"
>Content-Transfer-Encoding: 7bit
>Content-Description: Card for Michael Schneider
>Content-Disposition: attachment;
> filename="Michael.L.Schneider.vcf"
>email;internet:Michael.L.Schneider at eds.com
>fn:Michael L Schneider

Roger Burnham
rburnham at blennylips.com

More information about the Python-list mailing list