home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / build_py.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  11.4 KB  |  335 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.command.build_py
  5.  
  6. Implements the Distutils 'build_py' command."""
  7. __revision__ = '$Id: build_py.py 65742 2008-08-17 04:16:04Z brett.cannon $'
  8. import string
  9. import os
  10. from types import *
  11. from glob import glob
  12. from distutils.core import Command
  13. from distutils.errors import *
  14. from distutils.util import convert_path
  15. from distutils import log
  16.  
  17. class build_py(Command):
  18.     description = '"build" pure Python modules (copy to build directory)'
  19.     user_options = [
  20.         ('build-lib=', 'd', 'directory to "build" (copy) to'),
  21.         ('compile', 'c', 'compile .py to .pyc'),
  22.         ('no-compile', None, "don't compile .py files [default]"),
  23.         ('optimize=', 'O', 'also compile with optimization: -O1 for "python -O", -O2 for "python -OO", and -O0 to disable [default: -O0]'),
  24.         ('force', 'f', 'forcibly build everything (ignore file timestamps)')]
  25.     boolean_options = [
  26.         'compile',
  27.         'force']
  28.     negative_opt = {
  29.         'no-compile': 'compile' }
  30.     
  31.     def initialize_options(self):
  32.         self.build_lib = None
  33.         self.py_modules = None
  34.         self.package = None
  35.         self.package_data = None
  36.         self.package_dir = None
  37.         self.compile = 0
  38.         self.optimize = 0
  39.         self.force = None
  40.  
  41.     
  42.     def finalize_options(self):
  43.         self.set_undefined_options('build', ('build_lib', 'build_lib'), ('force', 'force'))
  44.         self.packages = self.distribution.packages
  45.         self.py_modules = self.distribution.py_modules
  46.         self.package_data = self.distribution.package_data
  47.         self.package_dir = { }
  48.         if self.distribution.package_dir:
  49.             for name, path in self.distribution.package_dir.items():
  50.                 self.package_dir[name] = convert_path(path)
  51.             
  52.         
  53.         self.data_files = self.get_data_files()
  54.         if type(self.optimize) is not IntType:
  55.             
  56.             try:
  57.                 self.optimize = int(self.optimize)
  58.                 if self.optimize <= self.optimize:
  59.                     pass
  60.                 elif not self.optimize <= 2:
  61.                     raise AssertionError
  62.             except (ValueError, AssertionError):
  63.                 raise DistutilsOptionError, 'optimize must be 0, 1, or 2'
  64.             except:
  65.                 None<EXCEPTION MATCH>(ValueError, AssertionError)
  66.             
  67.  
  68.         None<EXCEPTION MATCH>(ValueError, AssertionError)
  69.  
  70.     
  71.     def run(self):
  72.         if self.py_modules:
  73.             self.build_modules()
  74.         
  75.         if self.packages:
  76.             self.build_packages()
  77.             self.build_package_data()
  78.         
  79.         self.byte_compile(self.get_outputs(include_bytecode = 0))
  80.  
  81.     
  82.     def get_data_files(self):
  83.         """Generate list of '(package,src_dir,build_dir,filenames)' tuples"""
  84.         data = []
  85.         if not self.packages:
  86.             return data
  87.         for package in self.packages:
  88.             src_dir = self.get_package_dir(package)
  89.             build_dir = os.path.join(*[
  90.                 self.build_lib] + package.split('.'))
  91.             plen = 0
  92.             if src_dir:
  93.                 plen = len(src_dir) + 1
  94.             
  95.             filenames = [ file[plen:] for file in self.find_data_files(package, src_dir) ]
  96.             data.append((package, src_dir, build_dir, filenames))
  97.         
  98.         return data
  99.  
  100.     
  101.     def find_data_files(self, package, src_dir):
  102.         """Return filenames for package's data files in 'src_dir'"""
  103.         globs = self.package_data.get('', []) + self.package_data.get(package, [])
  104.         files = []
  105.         for pattern in globs:
  106.             filelist = glob(os.path.join(src_dir, convert_path(pattern)))
  107.             [](_[1])
  108.         
  109.         return files
  110.  
  111.     
  112.     def build_package_data(self):
  113.         '''Copy data files into build directory'''
  114.         lastdir = None
  115.         for package, src_dir, build_dir, filenames in self.data_files:
  116.             for filename in filenames:
  117.                 target = os.path.join(build_dir, filename)
  118.                 self.mkpath(os.path.dirname(target))
  119.                 self.copy_file(os.path.join(src_dir, filename), target, preserve_mode = False)
  120.             
  121.         
  122.  
  123.     
  124.     def get_package_dir(self, package):
  125.         """Return the directory, relative to the top of the source
  126.            distribution, where package 'package' should be found
  127.            (at least according to the 'package_dir' option, if any)."""
  128.         path = string.split(package, '.')
  129.         if not self.package_dir:
  130.             if path:
  131.                 return apply(os.path.join, path)
  132.             return ''
  133.         self.package_dir
  134.         tail = []
  135.         while path:
  136.             
  137.             try:
  138.                 pdir = self.package_dir[string.join(path, '.')]
  139.             except KeyError:
  140.                 tail.insert(0, path[-1])
  141.                 del path[-1]
  142.                 continue
  143.  
  144.             tail.insert(0, pdir)
  145.             return os.path.join(*tail)
  146.         pdir = self.package_dir.get('')
  147.         if pdir is not None:
  148.             tail.insert(0, pdir)
  149.         
  150.         if tail:
  151.             return apply(os.path.join, tail)
  152.         return ''
  153.  
  154.     
  155.     def check_package(self, package, package_dir):
  156.         if package_dir != '':
  157.             if not os.path.exists(package_dir):
  158.                 raise DistutilsFileError, "package directory '%s' does not exist" % package_dir
  159.             os.path.exists(package_dir)
  160.             if not os.path.isdir(package_dir):
  161.                 raise DistutilsFileError, ("supposed package directory '%s' exists, " + 'but is not a directory') % package_dir
  162.             os.path.isdir(package_dir)
  163.         
  164.         if package:
  165.             init_py = os.path.join(package_dir, '__init__.py')
  166.             if os.path.isfile(init_py):
  167.                 return init_py
  168.             log.warn("package init file '%s' not found " + '(or not a regular file)', init_py)
  169.         
  170.  
  171.     
  172.     def check_module(self, module, module_file):
  173.         if not os.path.isfile(module_file):
  174.             log.warn('file %s (for module %s) not found', module_file, module)
  175.             return 0
  176.         return 1
  177.  
  178.     
  179.     def find_package_modules(self, package, package_dir):
  180.         self.check_package(package, package_dir)
  181.         module_files = glob(os.path.join(package_dir, '*.py'))
  182.         modules = []
  183.         setup_script = os.path.abspath(self.distribution.script_name)
  184.         for f in module_files:
  185.             abs_f = os.path.abspath(f)
  186.             if abs_f != setup_script:
  187.                 module = os.path.splitext(os.path.basename(f))[0]
  188.                 modules.append((package, module, f))
  189.                 continue
  190.             self.debug_print('excluding %s' % setup_script)
  191.         
  192.         return modules
  193.  
  194.     
  195.     def find_modules(self):
  196.         '''Finds individually-specified Python modules, ie. those listed by
  197.         module name in \'self.py_modules\'.  Returns a list of tuples (package,
  198.         module_base, filename): \'package\' is a tuple of the path through
  199.         package-space to the module; \'module_base\' is the bare (no
  200.         packages, no dots) module name, and \'filename\' is the path to the
  201.         ".py" file (relative to the distribution root) that implements the
  202.         module.
  203.         '''
  204.         packages = { }
  205.         modules = []
  206.         for module in self.py_modules:
  207.             path = string.split(module, '.')
  208.             package = string.join(path[0:-1], '.')
  209.             module_base = path[-1]
  210.             
  211.             try:
  212.                 (package_dir, checked) = packages[package]
  213.             except KeyError:
  214.                 package_dir = self.get_package_dir(package)
  215.                 checked = 0
  216.  
  217.             if not checked:
  218.                 init_py = self.check_package(package, package_dir)
  219.                 packages[package] = (package_dir, 1)
  220.                 if init_py:
  221.                     modules.append((package, '__init__', init_py))
  222.                 
  223.             
  224.             module_file = os.path.join(package_dir, module_base + '.py')
  225.             if not self.check_module(module, module_file):
  226.                 continue
  227.             
  228.             modules.append((package, module_base, module_file))
  229.         
  230.         return modules
  231.  
  232.     
  233.     def find_all_modules(self):
  234.         """Compute the list of all modules that will be built, whether
  235.         they are specified one-module-at-a-time ('self.py_modules') or
  236.         by whole packages ('self.packages').  Return a list of tuples
  237.         (package, module, module_file), just like 'find_modules()' and
  238.         'find_package_modules()' do."""
  239.         modules = []
  240.         if self.py_modules:
  241.             modules.extend(self.find_modules())
  242.         
  243.         if self.packages:
  244.             for package in self.packages:
  245.                 package_dir = self.get_package_dir(package)
  246.                 m = self.find_package_modules(package, package_dir)
  247.                 modules.extend(m)
  248.             
  249.         
  250.         return modules
  251.  
  252.     
  253.     def get_source_files(self):
  254.         modules = self.find_all_modules()
  255.         filenames = []
  256.         for module in modules:
  257.             filenames.append(module[-1])
  258.         
  259.         return filenames
  260.  
  261.     
  262.     def get_module_outfile(self, build_dir, package, module):
  263.         outfile_path = [
  264.             build_dir] + list(package) + [
  265.             module + '.py']
  266.         return os.path.join(*outfile_path)
  267.  
  268.     
  269.     def get_outputs(self, include_bytecode = 1):
  270.         modules = self.find_all_modules()
  271.         outputs = []
  272.         for package, module, module_file in modules:
  273.             package = string.split(package, '.')
  274.             filename = self.get_module_outfile(self.build_lib, package, module)
  275.             outputs.append(filename)
  276.             if include_bytecode:
  277.                 if self.compile:
  278.                     outputs.append(filename + 'c')
  279.                 
  280.                 if self.optimize > 0:
  281.                     outputs.append(filename + 'o')
  282.                 
  283.             self.optimize > 0
  284.         
  285.         [] += [ os.path.join(build_dir, filename) for package, src_dir, build_dir, filenames in self.data_files for filename in filenames ]
  286.         return outputs
  287.  
  288.     
  289.     def build_module(self, module, module_file, package):
  290.         if type(package) is StringType:
  291.             package = string.split(package, '.')
  292.         elif type(package) not in (ListType, TupleType):
  293.             raise TypeError, "'package' must be a string (dot-separated), list, or tuple"
  294.         
  295.         outfile = self.get_module_outfile(self.build_lib, package, module)
  296.         dir = os.path.dirname(outfile)
  297.         self.mkpath(dir)
  298.         return self.copy_file(module_file, outfile, preserve_mode = 0)
  299.  
  300.     
  301.     def build_modules(self):
  302.         modules = self.find_modules()
  303.         for package, module, module_file in modules:
  304.             self.build_module(module, module_file, package)
  305.         
  306.  
  307.     
  308.     def build_packages(self):
  309.         for package in self.packages:
  310.             package_dir = self.get_package_dir(package)
  311.             modules = self.find_package_modules(package, package_dir)
  312.             for package_, module, module_file in modules:
  313.                 if not package == package_:
  314.                     raise AssertionError
  315.                 self.build_module(module, module_file, package)
  316.             
  317.         
  318.  
  319.     
  320.     def byte_compile(self, files):
  321.         byte_compile = byte_compile
  322.         import distutils.util
  323.         prefix = self.build_lib
  324.         if prefix[-1] != os.sep:
  325.             prefix = prefix + os.sep
  326.         
  327.         if self.compile:
  328.             byte_compile(files, optimize = 0, force = self.force, prefix = prefix, dry_run = self.dry_run)
  329.         
  330.         if self.optimize > 0:
  331.             byte_compile(files, optimize = self.optimize, force = self.force, prefix = prefix, dry_run = self.dry_run)
  332.         
  333.  
  334.  
  335.