findmodulefiles.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Unsupported » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Unsupported » findmodulefiles.py
"""Findmodulefiles - Find out where modules are loaded from.
Run findmodulefiles() after running a program with "python -i". The
resulting file list can be given to mkfrozenresources or to a
(non-existent) freeze-like application.

findmodulefiles will create a file listing all modules and where they have
been imported from.

findunusedbuiltins takes a list of (modules, file) and prints all builtin modules
that are _not_ in the list. The list is created by opening the findmodulefiles
output, reading it and eval()ing that.

mkpycresource takes a list of (module, file) and creates a resourcefile with all those
modules and (optionally) a main module.
"""

def findmodulefiles(output=None):
  """Produce a file containing a list of (modulename, filename-or-None)
  tuples mapping module names to source files"""
  # Immedeately grab the names
  import sys
  module_names = sys.modules.keys()[:]
  import os
  if not output:
    if os.name == 'mac':
      import macfs
      
      output, ok = macfs.StandardPutFile('Module file listing output:')
      if not ok: sys.exit(0)
      output = output.as_pathname()
  if not output:
    output = sys.stdout
  elif type(output) == type(''):
    output = open(output, 'w')
  output.write('[\n')
  for name in module_names:
    try:
      source = sys.modules[name].__file__
    except AttributeError:
      source = None
    else:
      source = `source`
    output.write('\t(%s,\t%s),\n' % (`name`, source))
  output.write(']\n')
  del output
  
def findunusedbuiltins(list):
  """Produce (on stdout) a list of unused builtin modules"""
  import sys
  dict = {}
  for name, location in list:
    if location == None:
      dict[name] = 1
  for name in sys.builtin_module_names:
    if not dict.has_key(name):
      print 'Unused builtin module:', name
      

def mkpycresourcefile(list, main='', dst=None):
  """Copy list-of-modules to resource file dst."""
  import py_resource
  import Res
  import sys
  
  if dst == None:
    import macfs
    fss, ok = macfs.StandardPutFile("PYC Resource output file")
    if not ok: sys.exit(0)
    dst = fss.as_pathname()
  if main == '':
    import macfs
    fss, ok = macfs.PromptGetFile("Main program:", "TEXT")
    if ok:
      main = fss.as_pathname()
  
  fsid = py_resource.create(dst)

  if main:
    id, name = py_resource.frompyfile(main, '__main__', preload=1)
    print '%5d\t%s\t%s'%(id, name, main)
  for name, location in list:
    if not location: continue
    if location[-4:] == '.pyc':
      # Attempt corresponding .py
      location = location[:-1]
    if location[-3:] != '.py':
      print '*** skipping', location
      continue
    id, name = py_resource.frompyfile(location, name, preload=1)
    print '%5d\t%s\t%s'%(id, name, location)

  Res.CloseResFile(fsid)

      
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.