[Python-checkins] CVS: python/dist/src/Tools/freeze freeze.py,1.39,1.40 makefreeze.py,1.11,1.12 modulefinder.py,1.17,1.18

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 18 Oct 2001 12:15:34 -0700


Update of /cvsroot/python/python/dist/src/Tools/freeze
In directory usw-pr-cvs1:/tmp/cvs-serv16030

Modified Files:
	freeze.py makefreeze.py modulefinder.py 
Log Message:
Part 2/2 of SF patch #416704: More robust freeze, by Toby Dickenson.

(With slight cosmetic improvements to shorten lines and a grammar fix
to a docstring.)

This addes -X and -E options to freeze.  From the docstring:

-X module     Like -x, except the module can never be imported by
              the frozen binary.

-E:           Freeze will fail if any modules can't be found (that
              were not excluded using -x or -X).



Index: freeze.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/freeze/freeze.py,v
retrieving revision 1.39
retrieving revision 1.40
diff -C2 -d -r1.39 -r1.40
*** freeze.py	2001/06/02 06:16:02	1.39
--- freeze.py	2001/10/18 19:15:31	1.40
***************
*** 43,48 ****
  -h:           Print this help message.
  
! -x module     Exclude the specified module.
  
  -i filename:  Include a file with additional command line options.  Used
                to prevent command lines growing beyond the capabilities of
--- 43,55 ----
  -h:           Print this help message.
  
! -x module     Exclude the specified module. It will still be imported
!               by the frozen binary if it exists on the host system.
! 
! -X module     Like -x, except the module can never be imported by
!               the frozen binary.
  
+ -E:           Freeze will fail if any modules can't be found (that
+               were not excluded using -x or -X).
+ 
  -i filename:  Include a file with additional command line options.  Used
                to prevent command lines growing beyond the capabilities of
***************
*** 115,123 ****
      win = sys.platform[:3] == 'win'
      replace_paths = []                  # settable with -r option
  
      # default the exclude list for each platform
      if win: exclude = exclude + [
!         'dos', 'dospath', 'mac', 'macpath', 'macfs', 'MACFS', 'posix', 'os2', 'ce']
  
      # modules that are imported by the Python runtime
      implicits = ["site", "exceptions"]
--- 122,135 ----
      win = sys.platform[:3] == 'win'
      replace_paths = []                  # settable with -r option
+     error_if_any_missing = 0
  
      # default the exclude list for each platform
      if win: exclude = exclude + [
!         'dos', 'dospath', 'mac', 'macpath', 'macfs', 'MACFS', 'posix',
!         'os2', 'ce', 'riscos', 'riscosenviron', 'riscospath',
!         ]
  
+     fail_import = exclude[:]
+ 
      # modules that are imported by the Python runtime
      implicits = ["site", "exceptions"]
***************
*** 130,141 ****
      subsystem = 'console'
  
!     # parse command line by first replacing any "-i" options with the file contents.
      pos = 1
!     while pos < len(sys.argv)-1: # last option can not be "-i", so this ensures "pos+1" is in range!
          if sys.argv[pos] == '-i':
              try:
                  options = string.split(open(sys.argv[pos+1]).read())
              except IOError, why:
!                 usage("File name '%s' specified with the -i option can not be read - %s" % (sys.argv[pos+1], why) )
              # Replace the '-i' and the filename with the read params.
              sys.argv[pos:pos+2] = options
--- 142,156 ----
      subsystem = 'console'
  
!     # parse command line by first replacing any "-i" options with the
!     # file contents.
      pos = 1
!     while pos < len(sys.argv)-1:
!         # last option can not be "-i", so this ensures "pos+1" is in range!
          if sys.argv[pos] == '-i':
              try:
                  options = string.split(open(sys.argv[pos+1]).read())
              except IOError, why:
!                 usage("File name '%s' specified with the -i option "
!                       "can not be read - %s" % (sys.argv[pos+1], why) )
              # Replace the '-i' and the filename with the read params.
              sys.argv[pos:pos+2] = options
***************
*** 145,149 ****
      # Now parse the command line with the extras inserted.
      try:
!         opts, args = getopt.getopt(sys.argv[1:], 'r:a:de:hmo:p:P:qs:wx:l:')
      except getopt.error, msg:
          usage('getopt error: ' + str(msg))
--- 160,164 ----
      # Now parse the command line with the extras inserted.
      try:
!         opts, args = getopt.getopt(sys.argv[1:], 'r:a:dEe:hmo:p:P:qs:wX:x:l:')
      except getopt.error, msg:
          usage('getopt error: ' + str(msg))
***************
*** 176,179 ****
--- 191,199 ----
          if o == '-x':
              exclude.append(a)
+         if o == '-X':
+             exclude.append(a)
+             fail_import.append(a)
+         if o == '-E':
+             error_if_any_missing = 1
          if o == '-l':
              addn_link.append(a)
***************
*** 226,230 ****
      # sanity check of directories and files
      check_dirs = [prefix, exec_prefix, binlib, incldir]
!     if not win: check_dirs = check_dirs + extensions # These are not directories on Windows.
      for dir in check_dirs:
          if not os.path.exists(dir):
--- 246,252 ----
      # sanity check of directories and files
      check_dirs = [prefix, exec_prefix, binlib, incldir]
!     if not win:
!         # These are not directories on Windows.
!         check_dirs = check_dirs + extensions
      for dir in check_dirs:
          if not os.path.exists(dir):
***************
*** 351,356 ****
      dict = mf.modules
  
      # generate output for frozen modules
!     files = makefreeze.makefreeze(base, dict, debug, custom_entry_point)
  
      # look for unfrozen modules (builtin and of unknown origin)
--- 373,384 ----
      dict = mf.modules
  
+     if error_if_any_missing:
+         missing = mf.any_missing()
+         if missing:
+             sys.exit("There are some missing modules: %r" % missing)
+ 
      # generate output for frozen modules
!     files = makefreeze.makefreeze(base, dict, debug, custom_entry_point,
!                                   fail_import)
  
      # look for unfrozen modules (builtin and of unknown origin)

Index: makefreeze.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/freeze/makefreeze.py,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -d -r1.11 -r1.12
*** makefreeze.py	2000/07/09 03:09:57	1.11
--- makefreeze.py	2001/10/18 19:15:32	1.12
***************
*** 33,37 ****
  """
  
! def makefreeze(base, dict, debug=0, entry_point = None):
      if entry_point is None: entry_point = default_entry_point
      done = []
--- 33,37 ----
  """
  
! def makefreeze(base, dict, debug=0, entry_point=None, fail_import=()):
      if entry_point is None: entry_point = default_entry_point
      done = []
***************
*** 64,67 ****
--- 64,74 ----
      for mod, mangled, size in done:
          outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
+     outfp.write('\n')
+     # The following modules have a NULL code pointer, indicating
+     # that the prozen program should not search for them on the host
+     # system. Importing them will *always* raise an ImportError.
+     # The zero value size is never used.
+     for mod in fail_import:
+         outfp.write('\t{"%s", NULL, 0},\n' % (mod,))
      outfp.write(trailer)
      outfp.write(entry_point)

Index: modulefinder.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Tools/freeze/modulefinder.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** modulefinder.py	2001/09/05 23:42:36	1.17
--- modulefinder.py	2001/10/18 19:15:32	1.18
***************
*** 357,362 ****
  
      def find_module(self, name, path):
!         if name in self.excludes:
!             self.msgout(3, "find_module -> Excluded")
              raise ImportError, name
  
--- 357,366 ----
  
      def find_module(self, name, path):
!         if path:
!             fullname = '.'.join(path)+'.'+name
!         else:
!             fullname = name
!         if fullname in self.excludes:
!             self.msgout(3, "find_module -> Excluded", fullname)
              raise ImportError, name
  
***************
*** 397,400 ****
--- 401,413 ----
                  mods.sort()
                  print "?", key, "from", string.join(mods, ', ')
+ 
+     def any_missing(self):
+         keys = self.badmodules.keys()
+         missing = []
+         for key in keys:
+             if key not in self.excludes:
+                 # Missing, and its not supposed to be
+                 missing.append(key)
+         return missing
  
      def replace_paths_in_code(self, co):