Tuesday, July 29, 2008

What jar is that class in?

So, I'm a pretty big fan of python now.  Just about a convert, at least for some tasks...

One of the first things I did with python was to find a script that would recursively look for your jar files given a directory and then open them up and look for the class you're looking for. 

The script I found was very complicated using generator and all kinds of things that I'm just not familiar with.  I don't know if generators don't translate to Java very well or what but I just couldn't seem to wrap my head around that script.

So I wrote my own.  Here it is (I have the root directory and class file I'm looking for hard coded) but my version does just about everything the other version did with just about the same number of lines but it is way simpler.  Let me know what you think:


import os
import fnmatch
import re

jarExe = '/Progra~1/Java/jdk1.6.0_03/bin/jar.exe'
rootOfSearch = '/RAD7/SDP70/runtimes/base_v61/''
lookingForClass = 'com/ibm/ws/util/ImplFactory.class'

def showDirectory (directory):
  for aFile in directory[2]:
    if (fnmatch.fnmatch(aFile, '*.jar')):
      print 'searching file: %s' % aFile
      myclasses = os.popen('/Progra~1/Java/jdk1.6.0_03/bin/jar.exe -tf %s/%s' % (directory[0], aFile))
      for aClass in myclasses:
        if fnmatch.fnmatch(aClass, lookingForClass):
          print 'found: %s in %s/%s' % (aClass, rootOfSearch, aFile)
          return True

def findJars ():
  tree = os.walk(rootOfSearch)
  found = False
  subDirs = ''
  for directory in tree:
    subDirs = directory[1]
    found = showDirectory(directory)
      print 'found class in: %s' % directory[0]
      return True
  print 'Not found'
if __name__ == "__main__":



No comments: