home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / distutils / command / build_py.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  9.8 KB  |  307 lines

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