home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / distutils / command / build_py.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  9.8 KB  |  313 lines

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