def test_running_from_zip(self):
        # Test assumes that it runs from a normal checkout, not a zip.
        self.assertFalse(zip_package.is_zipped_module(sys.modules[__name__]))
        self.assertIsNone(
            zip_package.get_module_zip_archive(sys.modules[__name__]))
        self.assertTrue(
            os.path.abspath(zip_package.get_main_script_path()).startswith(
                test_env.CLIENT_DIR))

        # Build executable zip that calls same functions.
        pkg = zip_package.ZipPackage(self.temp_dir)
        pkg.add_directory(os.path.join(test_env.CLIENT_DIR, 'utils'), 'utils')
        pkg.add_buffer(
            '__main__.py', b'\n'.join([
                b'import sys',
                b'',
                b'from utils import zip_package',
                b'',
                b'print(zip_package.is_zipped_module(sys.modules[__name__]))',
                b'print(zip_package.get_module_zip_archive(sys.modules[__name__]))',
                b'print(zip_package.get_main_script_path())',
            ]))
        zip_file = os.path.join(self.temp_dir, 'out.zip')
        pkg.zip_into_file(zip_file)

        # Run the zip, validate results.
        actual = check_output([sys.executable, zip_file]).strip().splitlines()
        self.assertEqual(['True', zip_file, zip_file], actual)
  def test_running_from_zip(self):
    # Test assumes that it runs from a normal checkout, not a zip.
    self.assertFalse(zip_package.is_zipped_module(sys.modules[__name__]))
    self.assertIsNone(zip_package.get_module_zip_archive(sys.modules[__name__]))
    self.assertTrue(os.path.abspath(
        zip_package.get_main_script_path()).startswith(ROOT_DIR))

    # Build executable zip that calls same functions.
    pkg = zip_package.ZipPackage(self.temp_dir)
    pkg.add_directory(os.path.join(ROOT_DIR, 'utils'), 'utils')
    pkg.add_buffer('__main__.py', '\n'.join([
      'import sys',
      '',
      'from utils import zip_package',
      '',
      'print zip_package.is_zipped_module(sys.modules[__name__])',
      'print zip_package.get_module_zip_archive(sys.modules[__name__])',
      'print zip_package.get_main_script_path()',
    ]))
    zip_file = os.path.join(self.temp_dir, 'out.zip')
    pkg.zip_into_file(zip_file)

    # Run the zip, validate results.
    actual = check_output([sys.executable, zip_file]).strip().splitlines()
    self.assertEqual(['True', zip_file, zip_file], actual)
Example #3
0
  def test_running_from_zip(self):
    # Test assumes that it runs from a normal checkout, not a zip.
    self.assertFalse(zip_package.is_zipped_module(sys.modules[__name__]))
    self.assertIsNone(zip_package.get_module_zip_archive(sys.modules[__name__]))
    self.assertTrue(os.path.abspath(
        zip_package.get_main_script_path()).startswith(ROOT_DIR))

    # Build executable zip that calls same functions.
    pkg = zip_package.ZipPackage(self.temp_dir)
    pkg.add_directory(os.path.join(ROOT_DIR, 'utils'), 'utils')
    pkg.add_buffer('__main__.py', '\n'.join([
      'import sys',
      '',
      'from utils import zip_package',
      '',
      'print zip_package.is_zipped_module(sys.modules[__name__])',
      'print zip_package.get_module_zip_archive(sys.modules[__name__])',
      'print zip_package.get_main_script_path()',
    ]))
    zip_file = os.path.join(self.temp_dir, 'out.zip')
    pkg.zip_into_file(zip_file)

    # Run the zip, validate results.
    proc = subprocess.Popen(
        [sys.executable, zip_file],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE)
    out, err = proc.communicate()

    actual = out.strip().splitlines()
    expected = ['True', zip_file, zip_file,]
    self.assertEqual(err, '')
    self.assertEqual(actual, expected)
Example #4
0
def get_as_zip_package(executable=True):
  """Returns ZipPackage with this module and all its dependencies.

  If |executable| is True will store run_isolated.py as __main__.py so that
  zip package is directly executable be python.
  """
  # Building a zip package when running from another zip package is
  # unsupported and probably unneeded.
  assert not zip_package.is_zipped_module(sys.modules[__name__])
  assert THIS_FILE_PATH
  assert BASE_DIR
  package = zip_package.ZipPackage(root=BASE_DIR)
  package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None)
  package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py'))
  package.add_directory(os.path.join(BASE_DIR, 'third_party'))
  package.add_directory(os.path.join(BASE_DIR, 'utils'))
  return package
def get_as_zip_package(executable=True):
  """Returns ZipPackage with this module and all its dependencies.

  If |executable| is True will store run_isolated.py as __main__.py so that
  zip package is directly executable be python.
  """
  # Building a zip package when running from another zip package is
  # unsupported and probably unneeded.
  assert not zip_package.is_zipped_module(sys.modules[__name__])
  assert THIS_FILE_PATH
  assert BASE_DIR
  package = zip_package.ZipPackage(root=BASE_DIR)
  package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None)
  package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py'))
  package.add_python_file(os.path.join(BASE_DIR, 'auth.py'))
  package.add_directory(os.path.join(BASE_DIR, 'third_party'))
  package.add_directory(os.path.join(BASE_DIR, 'utils'))
  return package