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")