IronPython for the Daily Grind (TFS sample included!)

IronPython is quickly becoming my scripting language of choice. I really like it. It’s easy to work with and I am getting work done with it. And, in spite of my best efforts, I am even starting to learn Python (a major 2009 goal).

What kind of work am I doing with it? Grunt work. Anywhere I used to use a batch file or a one-off utility (that I wrote in C# most likely), is getting replaced with an IronPython script. Generally, I am using it to automate repetitive tasks in my development cycle. In the future, I will probably use it for all my build scripts, too.

On my current project, we are using a product called EntitySpaces for data access. Out of the box ES generates 4 files that go into 2 different locations for each table. We have modified ES’s templates a bit, so there are actually 5 files that end up in 3 different locations. Hunting down these files and checking them out (Subversion I miss you so much) is such a PITA.

I decided to write a script in IronPython that would take one or more table names as command line arguments, and check out all of the appropriate files for me.

Initially, I ended up with humongous script. But then I factored out the TFS stuff into it’s own file:

import clr
clr.AddReferenceByPartialName ("Microsoft.TeamFoundation.Client")
clr.AddReferenceByPartialName ("Microsoft.TeamFoundation.VersionControl.Client")

from System import Environment
from System.IO import Path
from Microsoft.TeamFoundation.Client import *
from Microsoft.TeamFoundation.VersionControl.Client import *

SERVER = "MyTfsServer"
WORKSPACE_NAME = "$/MyWorkSpace"

def LoginToTFS():
    creds = UICredentialsProvider()
    tfs = TeamFoundationServerFactory.GetServer(SERVER, creds)
    tfs.EnsureAuthenticated()
    return tfs

def CheckOut(fileName):
    file = Path.Combine(projectDir, fileName)
    print "Checking out file: " + Path.GetFileName(file)
    workspace.PendEdit(file);

def FindWorkSpace():
    tfs = LoginToTFS()
    vcs = tfs.GetService(VersionControlServer)
    workspaces = vcs.QueryWorkspaces(None, tfs.AuthenticatedUserName, Environment.MachineName);
    for workspace in workspaces:
	    for folder in workspace.Folders:
	        if folder.ServerItem.ToString() == WORKSPACE_NAME:
	            return workspace, folder.LocalItem

workspace, projectDir = FindWorkSpace()

 

I can easily import the stuff from tfs.py into any script I am writing that needs to access TFS. Over time, I will just add functions to this module as they are needed. With tfs.py in place, I was able to write my escheckout.py script that handles the specifics of what I need to do. Note the swank UI:

import sys
from tfs import *
from System import Console

tables = sys.argv[1:]
print "Checkout the following entities:"
for table in tables:
    print table
print "(c)ontinue, E(x)it"
if Console.ReadKey() != "c":
    exit
print ""

custom = "Project\\DataAccess\\Custom\\"
generated = "Project\\DataAccess\\Generated\\"
interfaces = "Project\\DataAccess\\Entities\\"

for table in tables:
    CheckOut(custom + table + ".cs")
    CheckOut(custom + table + "Query.cs")
    CheckOut(custom + table + "Collection.cs")
    CheckOut(generated + table + ".cs")
    CheckOut(interfaces + "I" + table + ".cs")
December 23, 2008 08:21 by JP
E-mail | Permalink | Comments (2) | Comment RSSRSS comment feed

Related posts

Comments

December 25. 2008 17:59

Steve Gilham

IronPython is indeed an excellent language for dropping into build scripts -- it's where I have been making most use of it in the day-job since you can XCOPY install the necessary assemblies onto any machine with .Net 2.0 and have it "just work".

As such, it's something that can just be checked in alongside your source and used in a build without having to get it placed onto build machines as part of an official set up. The XCOPY-installable quality is where it scores a big win over the seemingly more obvious choice of PowerShell as a .Net-aware scripting language to replace contorted bits of extreme .cmd file coding, and helper C# projects, to perform intermediate tasks.

Other places where it can be very useful are in test automation -- IroPython can be used in Python based environments such as the PyFit module for FitNesse, with only minimal work -- and in exploratory coding (the prototype you know you're going to have to discard, getting to grips with an unfamiliar API).

Steve Gilham

January 19. 2009 13:06

Busby SEO Test

that's cool!!I was agree about this post!!thanks for sharing!!great one

Busby SEO Test

Comments are closed