Пример #1
0
    def setUpClass(cls):
        # Create zipfile for tests to read
        tmpdir = test_utils.mkdtemp()
        zipfile_name = os.path.join(tmpdir, '_support_test_sample.par')

        z = zipfile.ZipFile(zipfile_name, 'w')
        entry_name = '_support_test_helper_script.py'
        entry_data = b'print("Hello world")'
        z.writestr(entry_name, entry_data)

        z.close()

        cls.zipfile_name = zipfile_name
        cls.entry_name = entry_name
        cls.entry_data = entry_data

        # Create mock loader object
        class MockLoader(object):
            pass
        mock_loader = MockLoader()
        mock_loader.archive = zipfile_name
        mock_loader.prefix = ''
        main = sys.modules.get('__main__')
        old_loader = getattr(main, '__loader__', None)
        main.__loader__ = mock_loader

        cls.mock_loader = mock_loader
        cls.old_loader = old_loader
Пример #2
0
    def setUpClass(cls):
        # Create zipfile for tests to read
        tmpdir = test_utils.mkdtemp()
        zipfile_name = os.path.join(tmpdir, '_support_test_sample.par')

        z = zipfile.ZipFile(zipfile_name, 'w')
        entry_name = '_support_test_helper_script.py'
        entry_data = b'print("Hello world")'
        z.writestr(entry_name, entry_data)

        z.close()

        cls.zipfile_name = zipfile_name
        cls.entry_name = entry_name
        cls.entry_data = entry_data

        # Create mock loader object
        class MockLoader(object):
            pass
        mock_loader = MockLoader()
        mock_loader.archive = zipfile_name
        mock_loader.prefix = ''
        main = sys.modules.get('__main__')
        old_loader = getattr(main, '__loader__', None)
        main.__loader__ = mock_loader

        cls.mock_loader = mock_loader
        cls.old_loader = old_loader
Пример #3
0
 def test_remove_if_present(self):
     tmpdir = test_utils.mkdtemp()
     filename = os.path.join(tmpdir, 'afile')
     with open(filename, 'wb') as f:
         f.write(b'dontcare')
     # File exists
     self.assertTrue(os.path.exists(filename))
     python_archive.remove_if_present(filename)
     self.assertFalse(os.path.exists(filename))
     # File doesn't exist
     python_archive.remove_if_present(filename)
     self.assertFalse(os.path.exists(filename))
Пример #4
0
 def test_remove_if_present(self):
     tmpdir = test_utils.mkdtemp()
     filename = os.path.join(tmpdir, 'afile')
     with open(filename, 'wb') as f:
         f.write(b'dontcare')
     # File exists
     self.assertTrue(os.path.exists(filename))
     python_archive.remove_if_present(filename)
     self.assertFalse(os.path.exists(filename))
     # File doesn't exist
     python_archive.remove_if_present(filename)
     self.assertFalse(os.path.exists(filename))
Пример #5
0
    def _write_and_check(self, resource, name, expected_content):
        # Write zipfile
        tmpdir = test_utils.mkdtemp()
        zipfile_name = os.path.join(tmpdir, 'baz.zip')
        z = zipfile.ZipFile(zipfile_name, 'w')
        resource.store(z)
        z.close()

        # Read and validate zipfile
        z = zipfile.ZipFile(zipfile_name, 'r')
        self.assertEqual(z.namelist(), [name])
        with z.open(name) as infile:
            actual_content = infile.read()
        self.assertEqual(expected_content, actual_content)
        z.close()
Пример #6
0
    def _write_and_check(self, resource, name, expected_content):
        # Write zipfile
        tmpdir = test_utils.mkdtemp()
        zipfile_name = os.path.join(tmpdir, 'baz.zip')
        z = zipfile.ZipFile(zipfile_name, 'w')
        resource.store(z)
        z.close()

        # Read and validate zipfile
        z = zipfile.ZipFile(zipfile_name, 'r')
        self.assertEqual(z.namelist(), [name])
        with z.open(name) as infile:
            actual_content = infile.read()
        self.assertEqual(expected_content, actual_content)
        z.close()
Пример #7
0
 def setUp(self):
     # Setup directory structure and files
     self.tmpdir = test_utils.mkdtemp()
     self.input_dir = os.path.join(self.tmpdir, 'input')
     if not os.path.exists(self.input_dir):
         os.makedirs(self.input_dir)
     self.manifest_filename = os.path.join(self.input_dir, 'manifest')
     self.main_file = test_utils.temp_file(b'print("Hello World!")',
                                           suffix='.py')
     manifest_content = '%s %s\n' % (
         os.path.basename(self.main_file.name), self.main_file.name)
     self.manifest_file = test_utils.temp_file(
         manifest_content.encode('utf8'))
     self.output_dir = os.path.join(self.tmpdir, 'output')
     if not os.path.exists(self.output_dir):
         os.makedirs(self.output_dir)
     self.output_filename = os.path.join(self.output_dir, 'output.par')
     self.interpreter = '/usr/bin/python2'
     self.import_roots = []
Пример #8
0
 def setUp(self):
     # Setup directory structure and files
     self.tmpdir = test_utils.mkdtemp()
     self.input_dir = os.path.join(self.tmpdir, 'input')
     if not os.path.exists(self.input_dir):
         os.makedirs(self.input_dir)
     self.manifest_filename = os.path.join(self.input_dir, 'manifest')
     self.main_file = test_utils.temp_file(b'print("Hello World!")',
                                           suffix='.py')
     manifest_content = '%s %s\n' % (os.path.basename(
         self.main_file.name), self.main_file.name)
     self.manifest_file = test_utils.temp_file(
         manifest_content.encode('utf8'))
     self.output_dir = os.path.join(self.tmpdir, 'output')
     if not os.path.exists(self.output_dir):
         os.makedirs(self.output_dir)
     self.output_filename = os.path.join(self.output_dir, 'output.par')
     self.interpreter = '/usr/bin/python2'
     self.import_roots = []
Пример #9
0
 def setUp(self):
     # Setup directory structure and files
     self.tmpdir = test_utils.mkdtemp()
     self.input_dir = os.path.join(self.tmpdir, 'input')
     if not os.path.exists(self.input_dir):
         os.makedirs(self.input_dir)
     self.manifest_filename = os.path.join(self.input_dir, 'manifest')
     self.main_file = test_utils.temp_file(b'print("Hello World!")',
                                           suffix='.py')
     manifest_content = '%s %s\n' % (os.path.basename(
         self.main_file.name), self.main_file.name)
     self.manifest_file = test_utils.temp_file(
         manifest_content.encode('utf8'))
     self.output_dir = os.path.join(self.tmpdir, 'output')
     if not os.path.exists(self.output_dir):
         os.makedirs(self.output_dir)
     self.output_filename = os.path.join(self.output_dir, 'output.par')
     self.interpreter = sys.executable
     self.import_roots = []
     self.date_time_tuple = (1980, 1, 1, 0, 0, 0)
     self.timestamp = 315532800
     self.zip_safe = True
Пример #10
0
 def setUp(self):
     # Setup directory structure and files
     self.tmpdir = test_utils.mkdtemp()
     self.input_dir = os.path.join(self.tmpdir, 'input')
     if not os.path.exists(self.input_dir):
         os.makedirs(self.input_dir)
     self.manifest_filename = os.path.join(self.input_dir, 'manifest')
     self.main_file = test_utils.temp_file(b'print("Hello World!")',
                                           suffix='.py')
     manifest_content = '%s %s\n' % (
         os.path.basename(self.main_file.name), self.main_file.name)
     self.manifest_file = test_utils.temp_file(
         manifest_content.encode('utf8'))
     self.output_dir = os.path.join(self.tmpdir, 'output')
     if not os.path.exists(self.output_dir):
         os.makedirs(self.output_dir)
     self.output_filename = os.path.join(self.output_dir, 'output.par')
     self.interpreter = sys.executable
     self.import_roots = []
     self.date_time_tuple = (1980, 1, 1, 0, 0, 0)
     self.timestamp = 315532800
     self.zip_safe = True