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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import sys
  6. import __builtin__
  7. import tempfile
  8. import operator
  9. _os = sys.modules[os.name]
  10. _open = open
  11. from distutils.errors import DistutilsError
  12. __all__ = [
  13.     'AbstractSandbox',
  14.     'DirectorySandbox',
  15.     'SandboxViolation',
  16.     'run_setup']
  17.  
  18. def run_setup(setup_script, args):
  19.     old_dir = os.getcwd()
  20.     save_argv = sys.argv[:]
  21.     save_path = sys.path[:]
  22.     setup_dir = os.path.abspath(os.path.dirname(setup_script))
  23.     temp_dir = os.path.join(setup_dir, 'temp')
  24.     if not os.path.isdir(temp_dir):
  25.         os.makedirs(temp_dir)
  26.     
  27.     save_tmp = tempfile.tempdir
  28.     
  29.     try:
  30.         tempfile.tempdir = temp_dir
  31.         os.chdir(setup_dir)
  32.         
  33.         try:
  34.             sys.argv[:] = [
  35.                 setup_script] + list(args)
  36.             sys.path.insert(0, setup_dir)
  37.             (DirectorySandbox(setup_dir).run,)((lambda : execfile('setup.py', {
  38. '__file__': setup_script,
  39. '__name__': '__main__' })))
  40.         except SystemExit:
  41.             v = None
  42.             if v.args and v.args[0]:
  43.                 raise 
  44.             v.args[0]
  45.  
  46.     finally:
  47.         os.chdir(old_dir)
  48.         sys.path[:] = save_path
  49.         sys.argv[:] = save_argv
  50.         tempfile.tempdir = save_tmp
  51.  
  52.  
  53.  
  54. class AbstractSandbox:
  55.     _active = False
  56.     
  57.     def __init__(self):
  58.         self._attrs = _[1]
  59.  
  60.     
  61.     def _copy(self, source):
  62.         for name in self._attrs:
  63.             setattr(os, name, getattr(source, name))
  64.         
  65.  
  66.     
  67.     def run(self, func):
  68.         
  69.         try:
  70.             self._copy(self)
  71.             __builtin__.open = __builtin__.file = self._open
  72.             self._active = True
  73.             return func()
  74.         finally:
  75.             self._active = False
  76.             __builtin__.open = __builtin__.file = _open
  77.             self._copy(_os)
  78.  
  79.  
  80.     
  81.     def _mk_dual_path_wrapper(name):
  82.         original = getattr(_os, name)
  83.         
  84.         def wrap(self, src, dst, *args, **kw):
  85.             if self._active:
  86.                 (src, dst) = self._remap_pair(name, src, dst, *args, **kw)
  87.             
  88.             return original(src, dst, *args, **kw)
  89.  
  90.         return wrap
  91.  
  92.     for name in [
  93.         'rename',
  94.         'link',
  95.         'symlink']:
  96.         if hasattr(_os, name):
  97.             locals()[name] = _mk_dual_path_wrapper(name)
  98.             continue
  99.     
  100.     
  101.     def _mk_single_path_wrapper(name, original = None):
  102.         if not original:
  103.             pass
  104.         original = getattr(_os, name)
  105.         
  106.         def wrap(self, path, *args, **kw):
  107.             if self._active:
  108.                 path = self._remap_input(name, path, *args, **kw)
  109.             
  110.             return original(path, *args, **kw)
  111.  
  112.         return wrap
  113.  
  114.     _open = _mk_single_path_wrapper('file', _open)
  115.     for name in [
  116.         'stat',
  117.         'listdir',
  118.         'chdir',
  119.         'open',
  120.         'chmod',
  121.         'chown',
  122.         'mkdir',
  123.         'remove',
  124.         'unlink',
  125.         'rmdir',
  126.         'utime',
  127.         'lchown',
  128.         'chroot',
  129.         'lstat',
  130.         'startfile',
  131.         'mkfifo',
  132.         'mknod',
  133.         'pathconf',
  134.         'access']:
  135.         if hasattr(_os, name):
  136.             locals()[name] = _mk_single_path_wrapper(name)
  137.             continue
  138.     
  139.     
  140.     def _mk_single_with_return(name):
  141.         original = getattr(_os, name)
  142.         
  143.         def wrap(self, path, *args, **kw):
  144.             if self._active:
  145.                 path = self._remap_input(name, path, *args, **kw)
  146.                 return self._remap_output(name, original(path, *args, **kw))
  147.             return original(path, *args, **kw)
  148.  
  149.         return wrap
  150.  
  151.     for name in [
  152.         'readlink',
  153.         'tempnam']:
  154.         if hasattr(_os, name):
  155.             locals()[name] = _mk_single_with_return(name)
  156.             continue
  157.     
  158.     
  159.     def _mk_query(name):
  160.         original = getattr(_os, name)
  161.         
  162.         def wrap(self, *args, **kw):
  163.             retval = original(*args, **kw)
  164.             if self._active:
  165.                 return self._remap_output(name, retval)
  166.             return retval
  167.  
  168.         return wrap
  169.  
  170.     for name in [
  171.         'getcwd',
  172.         'tmpnam']:
  173.         if hasattr(_os, name):
  174.             locals()[name] = _mk_query(name)
  175.             continue
  176.     
  177.     
  178.     def _validate_path(self, path):
  179.         return path
  180.  
  181.     
  182.     def _remap_input(self, operation, path, *args, **kw):
  183.         return self._validate_path(path)
  184.  
  185.     
  186.     def _remap_output(self, operation, path):
  187.         return self._validate_path(path)
  188.  
  189.     
  190.     def _remap_pair(self, operation, src, dst, *args, **kw):
  191.         return (self._remap_input(operation + '-from', src, *args, **kw), self._remap_input(operation + '-to', dst, *args, **kw))
  192.  
  193.  
  194.  
  195. class DirectorySandbox(AbstractSandbox):
  196.     write_ops = dict.fromkeys([
  197.         'open',
  198.         'chmod',
  199.         'chown',
  200.         'mkdir',
  201.         'remove',
  202.         'unlink',
  203.         'rmdir',
  204.         'utime',
  205.         'lchown',
  206.         'chroot',
  207.         'mkfifo',
  208.         'mknod',
  209.         'tempnam'])
  210.     
  211.     def __init__(self, sandbox):
  212.         self._sandbox = os.path.normcase(os.path.realpath(sandbox))
  213.         self._prefix = os.path.join(self._sandbox, '')
  214.         AbstractSandbox.__init__(self)
  215.  
  216.     
  217.     def _violation(self, operation, *args, **kw):
  218.         raise SandboxViolation(operation, args, kw)
  219.  
  220.     
  221.     def _open(self, path, mode = 'r', *args, **kw):
  222.         if mode not in ('r', 'rt', 'rb', 'rU', 'U') and not self._ok(path):
  223.             self._violation('open', path, mode, *args, **kw)
  224.         
  225.         return _open(path, mode, *args, **kw)
  226.  
  227.     
  228.     def tmpnam(self):
  229.         self._violation('tmpnam')
  230.  
  231.     
  232.     def _ok(self, path):
  233.         active = self._active
  234.         
  235.         try:
  236.             self._active = False
  237.             realpath = os.path.normcase(os.path.realpath(path))
  238.             if realpath == self._sandbox or realpath.startswith(self._prefix):
  239.                 return True
  240.         finally:
  241.             self._active = active
  242.  
  243.  
  244.     
  245.     def _remap_input(self, operation, path, *args, **kw):
  246.         if operation in self.write_ops and not self._ok(path):
  247.             self._violation(operation, os.path.realpath(path), *args, **kw)
  248.         
  249.         return path
  250.  
  251.     
  252.     def _remap_pair(self, operation, src, dst, *args, **kw):
  253.         if not self._ok(src) or not self._ok(dst):
  254.             self._violation(operation, src, dst, *args, **kw)
  255.         
  256.         return (src, dst)
  257.  
  258.     
  259.     def open(self, file, flags, mode = 511):
  260.         if flags & WRITE_FLAGS and not self._ok(file):
  261.             self._violation('os.open', file, flags, mode)
  262.         
  263.         return _os.open(file, flags, mode)
  264.  
  265.  
  266. WRITE_FLAGS = []([], [ getattr(_os, a, 0) for a in 'O_WRONLY O_RDWR O_APPEND O_CREAT O_TRUNC O_TEMPORARY'.split() ])
  267.  
  268. class SandboxViolation(DistutilsError):
  269.     
  270.     def __str__(self):
  271.         return "SandboxViolation: %s%r %s\n\nThe package setup script has attempted to modify files on your system\nthat are not within the EasyInstall build area, and has been aborted.\n\nThis package cannot be safely installed by EasyInstall, and may not\nsupport alternate installation locations even if you run its setup\nscript by hand.  Please inform the package's author and the EasyInstall\nmaintainers to find out if a fix or workaround is available." % self.args
  272.  
  273.  
  274.