Exemplo n.º 1
0
  def _prepare_bootstrap(self):
    """
      Write enough of distribute and pip into the .pex .bootstrap directory so that
      we can be fully self-contained.
    """
    bare_env = pkg_resources.Environment()

    pip_req = pkg_resources.Requirement.parse('pip>=1.1')
    distribute_req = pkg_resources.Requirement.parse('distribute>=0.6.24')
    pip_dist = distribute_dist = None

    for dist in DistributionHelper.all_distributions(sys.path):
      if dist in pip_req and bare_env.can_add(dist):
        pip_dist = dist
      if dist in distribute_req and bare_env.can_add(dist):
        distribute_dist = dist
      if pip_dist and distribute_dist:
        break
    if not pip_dist:
      raise DistributionNotFound('Could not find pip!')
    if not distribute_dist:
      raise DistributionNotFound('Could not find distribute!')

    PEX.debug('Writing .bootstrap library.')
    for fn, content in DistributionHelper.walk_data(pip_dist):
      if fn.startswith('pip/'):
        # PEX.debug('BOOTSTRAP: Writing %s' % fn)
        self._chroot.write(content, os.path.join(self.BOOTSTRAP_DIR, fn), 'resource')
    for fn, content in DistributionHelper.walk_data(distribute_dist):
      if fn.startswith('pkg_resources.py') or fn.startswith('setuptools'):
        # PEX.debug('BOOTSTRAP: Writing %s' % fn)
        self._chroot.write(content, os.path.join(self.BOOTSTRAP_DIR, fn), 'resource')
    libraries = (
      'twitter.common.dirutil',
      'twitter.common.collections',
      'twitter.common.contextutil',
      'twitter.common.lang',
      'twitter.common.python'
    )
    for name in libraries:
      dirname = name.replace('.', '/')
      provider = pkg_resources.get_provider(name)
      if not isinstance(provider, pkg_resources.DefaultProvider):
        mod = __import__(name, fromlist=['wutttt'])
        provider = pkg_resources.ZipProvider(mod)
      for fn in provider.resource_listdir(''):
        if fn.endswith('.py'):
          # PEX.debug('BOOTSTRAP: Writing %s' % os.path.join(dirname, fn))
          self._chroot.write(provider.get_resource_string(name, fn),
            os.path.join(self.BOOTSTRAP_DIR, dirname, fn), 'resource')
    for initdir in ('twitter', 'twitter/common'):
      self._chroot.write(
        b"__import__('pkg_resources').declare_namespace(__name__)",
        os.path.join(self.BOOTSTRAP_DIR, initdir, '__init__.py'),
        'resource')
Exemplo n.º 2
0
    def test_resource_listdir(self):
        import mod
        zp = pkg_resources.ZipProvider(mod)

        expected_root = ['data.dat', 'mod.py', 'subdir']
        assert sorted(zp.resource_listdir('')) == expected_root

        expected_subdir = ['data2.dat', 'mod2.py']
        assert sorted(zp.resource_listdir('subdir')) == expected_subdir
        assert sorted(zp.resource_listdir('subdir/')) == expected_subdir

        assert zp.resource_listdir('nonexistent') == []
        assert zp.resource_listdir('nonexistent/') == []

        import mod2
        zp2 = pkg_resources.ZipProvider(mod2)

        assert sorted(zp2.resource_listdir('')) == expected_subdir

        assert zp2.resource_listdir('subdir') == []
        assert zp2.resource_listdir('subdir/') == []
    def test_resource_filename_rewrites_on_change(self):
        """
		If a previous call to get_resource_filename has saved the file, but
		the file has been subsequently mutated with different file of the
		same size and modification time, it should not be overwritten on a
		subsequent call to get_resource_filename.
		"""
        import mod
        manager = pkg_resources.ResourceManager()
        zp = pkg_resources.ZipProvider(mod)
        filename = zp.get_resource_filename(manager, 'data.dat')
        assert os.stat(filename).st_mtime == 1368379500
        f = open(filename, 'wb')
        f.write('hello, world?')
        f.close()
        os.utime(filename, (1368379500, 1368379500))
        filename = zp.get_resource_filename(manager, 'data.dat')
        f = open(filename)
        assert f.read() == 'hello, world!'
        manager.cleanup_resources()