Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Python Module Dump

By: a guest on May 3rd, 2011  |  syntax: Python  |  size: 1.31 KB  |  views: 85  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import sys, re, os
  2. import django
  3.  
  4. def ModuleList():
  5.     ret = []
  6.     dir_project = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  7.     project_name = os.path.basename(dir_project)
  8.  
  9.     for k,v in sys.modules.items():
  10.  
  11.         x = str(v)
  12.         if 'built-in' in x:
  13.             ret.append((k, 'built-in'))
  14.             continue
  15.  
  16.         m = re.search(r"^.*?'(?P<module>.*?)' from '(?P<file>.*?)'.*$", x)
  17.         if m:
  18.             d = m.groupdict()
  19.             f = d['file']
  20.             f = re.sub(r'/usr/.*?/lib/python[.0-9]*/site-packages/django/', 'system django >> ', f)
  21.             f = re.sub(r'/usr/.*?/lib/python[.0-9]*/site-packages/', 'site-packages >> ', f)
  22.             f = re.sub(r'/usr/.*?/lib/python[.0-9]*/', 'system python >> ', f)
  23.             f = re.sub(dir_project+'.*python/', 'local python >> ', f)
  24.             f = re.sub(dir_project+'.*django/', 'local django >> ', f)
  25.             f = re.sub(dir_project+r'(/\.\./)?', project_name + ' >> ', f)
  26.             ret.append((d['module'], f))
  27.     ret.sort( lambda a,b: cmp(a[0].lower(), b[0].lower()) )
  28.     ret.insert(0, ('Python version', sys.version) )
  29.     ret.insert(0, ('Django version', django.get_version()) )
  30.  
  31.     return ret
  32. # ModuleList
  33.  
  34. if __name__ == "__main__":
  35.     for x in ModuleList():
  36.         print "%s\t%s" % (x[0], x[1])