Nick Parker
My ramblings on .NET...

Enumerating GAC Assemblies with IronPython

Sunday, 31 October 2004 13:35 by nickp

I haven't been able to play with IronPython for a while now, work has been rather busy and I have also just received a copy of Programming .NET Security from the folks at O'Reilly to read and review. So far I am rather impressed with the book. This morning when I woke up I decided to sit down and play around with IronPython again. I love the fact that I can simple write a script yet have the power of the .NET Framework available to me at the same time. With the following script we can enumerate all of the assemblies installed within the GAC (Global Assembly Cache).

 

from System import *
from System.IO import *
from System.Diagnostics import *
def EnumAssemblies(di):
dia = di.GetDirectories()
if dia != None:
for d in dia:
EnumAssemblies(d)
fia = d.GetFiles("*.dll")
if fia != None:
for file in fia:
version = FileVersionInfo.GetVersionInfo(file.FullName)
print String.Format("{0}, {1}", file.Name, version.FileVersion)
path = Environment.ExpandEnvironmentVariables("%WINDIR%")
path += "\\assembly\\gac"
di = DirectoryInfo(path)
EnumAssemblies(di)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   .NET | Software | IronPython
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Windows Groups and Python

Monday, 18 October 2004 01:34 by nickp

So because of my recent interests with IronPython, I decided I should learn Python to better understand everything. Python really appears to be a nice language (from the weekend that I spent with it). It feels slightly strange not using curly braces to scope methods though. Anyway, I wanted to come up with a nice example of how Python works within Windows to help me better understand the language. I was watching Crossing Jordan tonight with Megan when I though of the script I threw together on Friday at work and how it would be a great sample to test Python. After I downloaded the Python Win32 extensions, I was off and running. After a little fiddling, this is what I came up with, which is very close to my original script:

 

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
strName = shell.ExpandEnvironmentStrings("%USERNAME%")
strDomain = shell.ExpandEnvironmentStrings("%USERDOMAIN%")
objUser = win32com.client.GetObject("WinNT://" + strDomain + "/" + strName)
strMsg = strName + " belongs to the following groups:\n\n"
for group in objUser.Groups():
strMsg += group.Name + "\n"
shell.Popup(strMsg)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   IronPython
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

IronPython and Generics

Thursday, 14 October 2004 01:48 by nickp

So lately I have been going through the source in Jim Hugunin's release of IronPython. This has been a really great experience for me as programming language design has always peaked my interest. I just got to thinking as I went through some of his code, how could generics help out when writing this language. So for an example I took a look at the Slot class and just a random method from his CodeGen class. The Slot class looks something like this:

 

public abstract class Slot
{
public abstract void emitGet(CodeGen cg);
public abstract void emitGetAddr(CodeGen cg);

public virtual void emitSet(CodeGen cg, Slot val)
{
val.emitGet(cg);
emitSet(cg);
}

public virtual void emitSet(CodeGen cg)
{
Slot val = cg.getLocalTmp(typeof(object));
val.emitSet(cg);
emitSet(cg, val);
cg.freeLocalTmp(val);
}

public virtual void emitCheck(CodeGen cg)
{
if (Options.CHECK_UNINITIALIZED)
{
cg.ilg.Emit(OpCodes.Dup);
cg.emitCall(typeof(Ops), "CheckInitialized");
}
}

public virtual Type type
{
get
{
throw new NotImplementedException("type");
}
}
}

public class ModuleSlot : Slot
{
public readonly Type _type;

public ModuleSlot(Type type)
{
this._type = type;
}

public override void emitGet(CodeGen cg)
{
cg.emitModuleInstance();
}

public override void emitSet(CodeGen cg)
{
throw new NotSupportedException("setting a module slot");
}

public override void emitGetAddr(CodeGen cg)
{
throw new NotSupportedException("addr of a module slot");
}

public override Type type { get { return _type; } }
}

So obviously there are a series of other classes (i.e., ModuleSlot) that use Slot as their base class. The important part is within the CodeGen class. Jim uses a private ArrayList to maintain free slots. This is simply declaired as such:

 

private ArrayList freeSlots = new ArrayList();

and the CodeGen class has a method I selected at random that is implemented as such:

 

public Slot getLocalTmp(Type type)
{
for (int i=0; i < freeSlots.Count; i++)
{
Slot slot = (Slot)freeSlots[i];
if (slot.type == type)
{
freeSlots.RemoveAt(i);
return slot;
}
}
return new LocalSlot(ilg.DeclareLocal(type));
}

However, with generics, we could do the following:

private LinkedList<Slot> freeSlots = new LinkedList<Slot>();
//...

public Slot getLocalTmp(Type type)
{
LinkedList<Slot>.Enumerator en = freeSlots.GetEnumerator();
while(en.MoveNext())
{
Slot slot = en.Current;
if(slot.type == type)
{
freeSlots.Remove(en.Current);
return slot;
}
}
return new LocalSlot(ilg.DeclareLocal(type));
}

It will be interesting to see if generics gets added to IronPython as we get closer to a RTM version of generics.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Categories:   IronPython
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed