home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2404 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  16.9 KB  |  481 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. import os
  6. import marshal
  7. from setuptools import Command
  8. from distutils.dir_util import remove_tree, mkpath
  9. from distutils.sysconfig import get_python_version, get_python_lib
  10. from distutils import log
  11. from distutils.errors import DistutilsSetupError
  12. from pkg_resources import get_build_platform, Distribution, ensure_directory
  13. from pkg_resources import EntryPoint
  14. from types import CodeType
  15. from setuptools.extension import Library
  16.  
  17. def strip_module(filename):
  18.     if '.' in filename:
  19.         filename = os.path.splitext(filename)[0]
  20.     
  21.     if filename.endswith('module'):
  22.         filename = filename[:-6]
  23.     
  24.     return filename
  25.  
  26.  
  27. def write_stub(resource, pyfile):
  28.     f = open(pyfile, 'w')
  29.     f.write('\n'.join([
  30.         'def __bootstrap__():',
  31.         '   global __bootstrap__, __loader__, __file__',
  32.         '   import sys, pkg_resources, imp',
  33.         '   __file__ = pkg_resources.resource_filename(__name__,%r)' % resource,
  34.         '   __loader__ = None; del __bootstrap__, __loader__',
  35.         '   imp.load_dynamic(__name__,__file__)',
  36.         '__bootstrap__()',
  37.         '']))
  38.     f.close()
  39.  
  40. NS_PKG_STUB = '__import__("pkg_resources").declare_namespace(__name__)'
  41.  
  42. class bdist_egg(Command):
  43.     description = 'create an "egg" distribution'
  44.     user_options = [
  45.         ('bdist-dir=', 'b', 'temporary directory for creating the distribution'),
  46.         ('plat-name=', 'p', 'platform name to embed in generated filenames (default: %s)' % get_build_platform()),
  47.         ('exclude-source-files', None, 'remove all .py files from the generated egg'),
  48.         ('keep-temp', 'k', 'keep the pseudo-installation tree around after ' + 'creating the distribution archive'),
  49.         ('dist-dir=', 'd', 'directory to put final built distributions in'),
  50.         ('skip-build', None, 'skip rebuilding everything (for testing/debugging)')]
  51.     boolean_options = [
  52.         'keep-temp',
  53.         'skip-build',
  54.         'exclude-source-files']
  55.     
  56.     def initialize_options(self):
  57.         self.bdist_dir = None
  58.         self.plat_name = None
  59.         self.keep_temp = 0
  60.         self.dist_dir = None
  61.         self.skip_build = 0
  62.         self.egg_output = None
  63.         self.exclude_source_files = None
  64.  
  65.     
  66.     def finalize_options(self):
  67.         ei_cmd = self.ei_cmd = self.get_finalized_command('egg_info')
  68.         self.egg_info = ei_cmd.egg_info
  69.         if self.bdist_dir is None:
  70.             bdist_base = self.get_finalized_command('bdist').bdist_base
  71.             self.bdist_dir = os.path.join(bdist_base, 'egg')
  72.         
  73.         if self.plat_name is None:
  74.             self.plat_name = get_build_platform()
  75.         
  76.         self.set_undefined_options('bdist', ('dist_dir', 'dist_dir'))
  77.         if self.egg_output is None:
  78.             if self.distribution.has_ext_modules():
  79.                 pass
  80.             basename = Distribution(None, None, ei_cmd.egg_name, ei_cmd.egg_version, get_python_version(), self.plat_name).egg_name()
  81.             self.egg_output = os.path.join(self.dist_dir, basename + '.egg')
  82.         
  83.  
  84.     
  85.     def do_install_data(self):
  86.         self.get_finalized_command('install').install_lib = self.bdist_dir
  87.         site_packages = os.path.normcase(os.path.realpath(get_python_lib()))
  88.         old = self.distribution.data_files
  89.         self.distribution.data_files = []
  90.         for item in old:
  91.             if isinstance(item, tuple) and len(item) == 2:
  92.                 if os.path.isabs(item[0]):
  93.                     realpath = os.path.realpath(item[0])
  94.                     normalized = os.path.normcase(realpath)
  95.                     if normalized == site_packages or normalized.startswith(site_packages + os.sep):
  96.                         item = (realpath[len(site_packages) + 1:], item[1])
  97.                     
  98.                 
  99.             
  100.             self.distribution.data_files.append(item)
  101.         
  102.         
  103.         try:
  104.             log.info('installing package data to %s' % self.bdist_dir)
  105.             self.call_command('install_data', force = 0, root = None)
  106.         finally:
  107.             self.distribution.data_files = old
  108.  
  109.  
  110.     
  111.     def get_outputs(self):
  112.         return [
  113.             self.egg_output]
  114.  
  115.     
  116.     def call_command(self, cmdname, **kw):
  117.         for dirname in INSTALL_DIRECTORY_ATTRS:
  118.             kw.setdefault(dirname, self.bdist_dir)
  119.         
  120.         kw.setdefault('skip_build', self.skip_build)
  121.         kw.setdefault('dry_run', self.dry_run)
  122.         cmd = self.reinitialize_command(cmdname, **kw)
  123.         self.run_command(cmdname)
  124.         return cmd
  125.  
  126.     
  127.     def run(self):
  128.         self.run_command('egg_info')
  129.         log.info('installing library code to %s' % self.bdist_dir)
  130.         instcmd = self.get_finalized_command('install')
  131.         old_root = instcmd.root
  132.         instcmd.root = None
  133.         cmd = self.call_command('install_lib', warn_dir = 0)
  134.         instcmd.root = old_root
  135.         (all_outputs, ext_outputs) = self.get_ext_outputs()
  136.         self.stubs = []
  137.         to_compile = []
  138.         for p, ext_name in enumerate(ext_outputs):
  139.             (filename, ext) = os.path.splitext(ext_name)
  140.             pyfile = os.path.join(self.bdist_dir, strip_module(filename) + '.py')
  141.             self.stubs.append(pyfile)
  142.             log.info('creating stub loader for %s' % ext_name)
  143.             if not self.dry_run:
  144.                 write_stub(os.path.basename(ext_name), pyfile)
  145.             
  146.             to_compile.append(pyfile)
  147.             ext_outputs[p] = ext_name.replace(os.sep, '/')
  148.         
  149.         to_compile.extend(self.make_init_files())
  150.         if to_compile:
  151.             cmd.byte_compile(to_compile)
  152.         
  153.         if self.distribution.data_files:
  154.             self.do_install_data()
  155.         
  156.         archive_root = self.bdist_dir
  157.         egg_info = os.path.join(archive_root, 'EGG-INFO')
  158.         self.mkpath(egg_info)
  159.         if self.distribution.scripts:
  160.             script_dir = os.path.join(egg_info, 'scripts')
  161.             log.info('installing scripts to %s' % script_dir)
  162.             self.call_command('install_scripts', install_dir = script_dir, no_ep = 1)
  163.         
  164.         self.copy_metadata_to(egg_info)
  165.         native_libs = os.path.join(egg_info, 'native_libs.txt')
  166.         if all_outputs:
  167.             log.info('writing %s' % native_libs)
  168.             if not self.dry_run:
  169.                 ensure_directory(native_libs)
  170.                 libs_file = open(native_libs, 'wt')
  171.                 libs_file.write('\n'.join(all_outputs))
  172.                 libs_file.write('\n')
  173.                 libs_file.close()
  174.             
  175.         elif os.path.isfile(native_libs):
  176.             log.info('removing %s' % native_libs)
  177.             if not self.dry_run:
  178.                 os.unlink(native_libs)
  179.             
  180.         
  181.         write_safety_flag(os.path.join(archive_root, 'EGG-INFO'), self.zip_safe())
  182.         if os.path.exists(os.path.join(self.egg_info, 'depends.txt')):
  183.             log.warn("WARNING: 'depends.txt' will not be used by setuptools 0.6!\nUse the install_requires/extras_require setup() args instead.")
  184.         
  185.         if self.exclude_source_files:
  186.             self.zap_pyfiles()
  187.         
  188.         make_zipfile(self.egg_output, archive_root, verbose = self.verbose, dry_run = self.dry_run, mode = self.gen_header())
  189.         if not self.keep_temp:
  190.             remove_tree(self.bdist_dir, dry_run = self.dry_run)
  191.         
  192.         getattr(self.distribution, 'dist_files', []).append(('bdist_egg', get_python_version(), self.egg_output))
  193.  
  194.     
  195.     def zap_pyfiles(self):
  196.         log.info('Removing .py files from temporary directory')
  197.         for base, dirs, files in walk_egg(self.bdist_dir):
  198.             for name in files:
  199.                 if name.endswith('.py'):
  200.                     path = os.path.join(base, name)
  201.                     log.debug('Deleting %s', path)
  202.                     os.unlink(path)
  203.                     continue
  204.             
  205.         
  206.  
  207.     
  208.     def zip_safe(self):
  209.         safe = getattr(self.distribution, 'zip_safe', None)
  210.         if safe is not None:
  211.             return safe
  212.         log.warn('zip_safe flag not set; analyzing archive contents...')
  213.         return analyze_egg(self.bdist_dir, self.stubs)
  214.  
  215.     
  216.     def make_init_files(self):
  217.         init_files = []
  218.         for base, dirs, files in walk_egg(self.bdist_dir):
  219.             if base == self.bdist_dir:
  220.                 continue
  221.             
  222.             for name in files:
  223.                 if name.endswith('.py'):
  224.                     if '__init__.py' not in files:
  225.                         pkg = base[len(self.bdist_dir) + 1:].replace(os.sep, '.')
  226.                         if self.distribution.has_contents_for(pkg):
  227.                             log.warn('Creating missing __init__.py for %s', pkg)
  228.                             filename = os.path.join(base, '__init__.py')
  229.                             if not self.dry_run:
  230.                                 f = open(filename, 'w')
  231.                                 f.write(NS_PKG_STUB)
  232.                                 f.close()
  233.                             
  234.                             init_files.append(filename)
  235.                         
  236.                     
  237.                     break
  238.                     continue
  239.             else:
  240.                 dirs[:] = []
  241.         
  242.         return init_files
  243.  
  244.     
  245.     def gen_header(self):
  246.         if not self.distribution.entry_points:
  247.             pass
  248.         epm = EntryPoint.parse_map('')
  249.         ep = epm.get('setuptools.installation', { }).get('eggsecutable')
  250.         if ep is None:
  251.             return 'w'
  252.         if not (ep.attrs) or ep.extras:
  253.             raise DistutilsSetupError("eggsecutable entry point (%r) cannot have 'extras' or refer to a module" % (ep,))
  254.         ep.extras
  255.         pyver = sys.version[:3]
  256.         pkg = ep.module_name
  257.         full = '.'.join(ep.attrs)
  258.         base = ep.attrs[0]
  259.         basename = os.path.basename(self.egg_output)
  260.         header = '#!/bin/sh\nif [ `basename $0` = "%(basename)s" ]\nthen exec python%(pyver)s -c "import sys, os; sys.path.insert(0, os.path.abspath(\'$0\')); from %(pkg)s import %(base)s; sys.exit(%(full)s())" "$@"\nelse\n  echo $0 is not the correct name for this egg file.\n  echo Please rename it back to %(basename)s and try again.\n  exec false\nfi\n' % locals()
  261.         if not self.dry_run:
  262.             mkpath(os.path.dirname(self.egg_output), dry_run = self.dry_run)
  263.             f = open(self.egg_output, 'w')
  264.             f.write(header)
  265.             f.close()
  266.         
  267.         return 'a'
  268.  
  269.     
  270.     def copy_metadata_to(self, target_dir):
  271.         prefix = os.path.join(self.egg_info, '')
  272.         for path in self.ei_cmd.filelist.files:
  273.             if path.startswith(prefix):
  274.                 target = os.path.join(target_dir, path[len(prefix):])
  275.                 ensure_directory(target)
  276.                 self.copy_file(path, target)
  277.                 continue
  278.         
  279.  
  280.     
  281.     def get_ext_outputs(self):
  282.         all_outputs = []
  283.         ext_outputs = []
  284.         paths = {
  285.             self.bdist_dir: '' }
  286.         for base, dirs, files in os.walk(self.bdist_dir):
  287.             for filename in files:
  288.                 if os.path.splitext(filename)[1].lower() in NATIVE_EXTENSIONS:
  289.                     all_outputs.append(paths[base] + filename)
  290.                     continue
  291.             
  292.             for filename in dirs:
  293.                 paths[os.path.join(base, filename)] = paths[base] + filename + '/'
  294.             
  295.         
  296.         if self.distribution.has_ext_modules():
  297.             build_cmd = self.get_finalized_command('build_ext')
  298.             for ext in build_cmd.extensions:
  299.                 if isinstance(ext, Library):
  300.                     continue
  301.                 
  302.                 fullname = build_cmd.get_ext_fullname(ext.name)
  303.                 filename = build_cmd.get_ext_filename(fullname)
  304.                 if not os.path.basename(filename).startswith('dl-'):
  305.                     if os.path.exists(os.path.join(self.bdist_dir, filename)):
  306.                         ext_outputs.append(filename)
  307.                     
  308.                 os.path.exists(os.path.join(self.bdist_dir, filename))
  309.             
  310.         
  311.         return (all_outputs, ext_outputs)
  312.  
  313.  
  314. NATIVE_EXTENSIONS = dict.fromkeys('.dll .so .dylib .pyd'.split())
  315.  
  316. def walk_egg(egg_dir):
  317.     walker = os.walk(egg_dir)
  318.     (base, dirs, files) = walker.next()
  319.     if 'EGG-INFO' in dirs:
  320.         dirs.remove('EGG-INFO')
  321.     
  322.     yield (base, dirs, files)
  323.     for bdf in walker:
  324.         yield bdf
  325.     
  326.  
  327.  
  328. def analyze_egg(egg_dir, stubs):
  329.     for flag, fn in safety_flags.items():
  330.         if os.path.exists(os.path.join(egg_dir, 'EGG-INFO', fn)):
  331.             return flag
  332.     
  333.     if not can_scan():
  334.         return False
  335.     safe = True
  336.     for base, dirs, files in walk_egg(egg_dir):
  337.         for name in files:
  338.             if name.endswith('.py') or name.endswith('.pyw'):
  339.                 continue
  340.                 continue
  341.             can_scan()
  342.             if name.endswith('.pyc') or name.endswith('.pyo'):
  343.                 if scan_module(egg_dir, base, name, stubs):
  344.                     pass
  345.                 safe = safe
  346.                 continue
  347.             os.path.exists(os.path.join(egg_dir, 'EGG-INFO', fn))
  348.         
  349.     
  350.     return safe
  351.  
  352.  
  353. def write_safety_flag(egg_dir, safe):
  354.     for flag, fn in safety_flags.items():
  355.         fn = os.path.join(egg_dir, fn)
  356.         if os.path.exists(fn):
  357.             if safe is None or bool(safe) != flag:
  358.                 os.unlink(fn)
  359.             
  360.         bool(safe) != flag
  361.         if safe is not None and bool(safe) == flag:
  362.             f = open(fn, 'wb')
  363.             f.write('\n')
  364.             f.close()
  365.             continue
  366.     
  367.  
  368. safety_flags = {
  369.     True: 'zip-safe',
  370.     False: 'not-zip-safe' }
  371.  
  372. def scan_module(egg_dir, base, name, stubs):
  373.     filename = os.path.join(base, name)
  374.     if filename[:-1] in stubs:
  375.         return True
  376.     pkg = base[len(egg_dir) + 1:].replace(os.sep, '.')
  377.     if not pkg or '.':
  378.         pass
  379.     module = pkg + '' + os.path.splitext(name)[0]
  380.     f = open(filename, 'rb')
  381.     f.read(8)
  382.     code = marshal.load(f)
  383.     f.close()
  384.     safe = True
  385.     symbols = dict.fromkeys(iter_symbols(code))
  386.     for bad in [
  387.         '__file__',
  388.         '__path__']:
  389.         if bad in symbols:
  390.             log.warn('%s: module references %s', module, bad)
  391.             safe = False
  392.             continue
  393.         filename[:-1] in stubs
  394.     
  395.     if 'inspect' in symbols:
  396.         for bad in [
  397.             'getsource',
  398.             'getabsfile',
  399.             'getsourcefile',
  400.             'getfilegetsourcelines',
  401.             'findsource',
  402.             'getcomments',
  403.             'getframeinfo',
  404.             'getinnerframes',
  405.             'getouterframes',
  406.             'stack',
  407.             'trace']:
  408.             if bad in symbols:
  409.                 log.warn('%s: module MAY be using inspect.%s', module, bad)
  410.                 safe = False
  411.                 continue
  412.         
  413.     
  414.     if '__name__' in symbols and '__main__' in symbols and '.' not in module:
  415.         if sys.version[:3] == '2.4':
  416.             log.warn("%s: top-level module may be 'python -m' script", module)
  417.             safe = False
  418.         
  419.     
  420.     return safe
  421.  
  422.  
  423. def iter_symbols(code):
  424.     for name in code.co_names:
  425.         yield name
  426.     
  427.     for const in code.co_consts:
  428.         if isinstance(const, basestring):
  429.             yield const
  430.             continue
  431.         if isinstance(const, CodeType):
  432.             for name in iter_symbols(const):
  433.                 yield name
  434.             
  435.     
  436.  
  437.  
  438. def can_scan():
  439.     if not sys.platform.startswith('java') and sys.platform != 'cli':
  440.         return True
  441.     log.warn('Unable to analyze compiled code on this platform.')
  442.     log.warn("Please ask the author to include a 'zip_safe' setting (either True or False) in the package's setup.py")
  443.  
  444. INSTALL_DIRECTORY_ATTRS = [
  445.     'install_lib',
  446.     'install_dir',
  447.     'install_data',
  448.     'install_base']
  449.  
  450. def make_zipfile(zip_filename, base_dir, verbose = 0, dry_run = 0, compress = None, mode = 'w'):
  451.     import zipfile
  452.     mkpath(os.path.dirname(zip_filename), dry_run = dry_run)
  453.     log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir)
  454.     
  455.     def visit(z, dirname, names):
  456.         for name in names:
  457.             path = os.path.normpath(os.path.join(dirname, name))
  458.             if os.path.isfile(path):
  459.                 p = path[len(base_dir) + 1:]
  460.                 if not dry_run:
  461.                     z.write(path, p)
  462.                 
  463.                 log.debug("adding '%s'" % p)
  464.                 continue
  465.         
  466.  
  467.     if compress is None:
  468.         compress = sys.version >= '2.4'
  469.     
  470.     compression = [
  471.         zipfile.ZIP_STORED,
  472.         zipfile.ZIP_DEFLATED][bool(compress)]
  473.     if not dry_run:
  474.         z = zipfile.ZipFile(zip_filename, mode, compression = compression)
  475.         os.path.walk(base_dir, visit, z)
  476.         z.close()
  477.     else:
  478.         os.path.walk(base_dir, visit, None)
  479.     return zip_filename
  480.  
  481.