def test_compile_dir_pathlike(self): self.assertFalse(os.path.isfile(self.bc_path)) with support.captured_stdout() as stdout: compileall.compile_dir(pathlib.Path(self.directory)) line = stdout.getvalue().splitlines()[0] self.assertRegex(line, r'Listing ([^WindowsPath|PosixPath].*)') self.assertTrue(os.path.isfile(self.bc_path))
def test_error(self): try: orig_stdout = sys.stdout sys.stdout = io.TextIOWrapper(io.BytesIO(),encoding='ascii') compileall.compile_dir(self.directory) finally: sys.stdout = orig_stdout
def test_compile_files(self): # Test compiling a single file, and complete directory for fn in (self.bc_path, self.bc_path2): try: os.unlink(fn) except: pass self.assertTrue( compileall.compile_file(self.source_path, force=False, quiet=True)) self.assertTrue( os.path.isfile(self.bc_path) and not os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) self.assertTrue( compileall.compile_dir(self.directory, force=False, quiet=True)) self.assertTrue( os.path.isfile(self.bc_path) and os.path.isfile(self.bc_path2)) os.unlink(self.bc_path) os.unlink(self.bc_path2) # Test against bad files self.add_bad_source_file() self.assertFalse( compileall.compile_file(self.bad_source_path, force=False, quiet=2)) self.assertFalse( compileall.compile_dir(self.directory, force=False, quiet=2))
def test_strip_prepend_and_ddir(self): fullpath = ["test", "build", "real", "path", "ddir"] path = os.path.join(self.directory, *fullpath) os.makedirs(path) script_helper.make_script(path, "test", "1 / 0") with self.assertRaises(ValueError): compileall.compile_dir(path, quiet=True, ddir="/bar", stripdir="/foo", prependdir="/bar")
def text_compile_dir_maxlevels(self): # Test the actual impact of maxlevels attr compileall.compile_dir(os.path.join(self.directory, "long"), maxlevels=10, quiet=True) self.assertFalse(os.path.isfile(self.bc_path_long)) compileall.compile_dir(os.path.join(self.directory, "long"), maxlevels=110, quiet=True) self.assertTrue(os.path.isfile(self.bc_path_long))
def test_append_only(self): fullpath = ["test", "build", "real", "path"] path = os.path.join(self.directory, *fullpath) os.makedirs(path) script = script_helper.make_script(path, "test", "1 / 0") bc = importlib.util.cache_from_source(script) appenddir = "/foo" compileall.compile_dir(path, quiet=True, appenddir=appenddir) rc, out, err = script_helper.assert_python_failure(bc) expected_in = os.path.join(appenddir, self.directory, *fullpath) self.assertIn(expected_in, str(err, encoding=sys.getdefaultencoding()))
def test_optimize(self): # make sure compiling with different optimization settings than the # interpreter's creates the correct file names optimize, opt = (1, 1) if __debug__ else (0, '') opt_kwarg = compileall.optimization_kwarg(opt) compileall.compile_dir(self.directory, quiet=True, optimize=optimize) cached = importlib.util.cache_from_source(self.source_path, **opt_kwarg) self.assertTrue(os.path.isfile(cached)) cached2 = importlib.util.cache_from_source(self.source_path2, **opt_kwarg) self.assertTrue(os.path.isfile(cached2)) cached3 = importlib.util.cache_from_source(self.source_path3, **opt_kwarg) self.assertTrue(os.path.isfile(cached3))
def recreation_check(self, metadata): """Check that compileall recreates bytecode when the new metadata is used.""" if os.environ.get('SOURCE_DATE_EPOCH'): raise unittest.SkipTest('SOURCE_DATE_EPOCH is set') py_compile.compile(self.source_path) self.assertEqual(*self.timestamp_metadata()) with open(self.bc_path, 'rb') as file: bc = file.read()[len(metadata):] with open(self.bc_path, 'wb') as file: file.write(metadata) file.write(bc) self.assertNotEqual(*self.timestamp_metadata()) compileall.compile_dir(self.directory, force=False, quiet=True) self.assertTrue(*self.timestamp_metadata())
def test_compile_dir_maxlevels(self): # Test the actual impact of maxlevels parameter depth = 3 path = self.directory for i in range(1, depth + 1): path = os.path.join(path, "dir_{}".format(i)) source = os.path.join(path, 'script.py') os.mkdir(path) shutil.copyfile(self.source_path, source) pyc_filename = importlib.util.cache_from_source(source) compileall.compile_dir(self.directory, quiet=True, maxlevels=depth - 1) self.assertFalse(os.path.isfile(pyc_filename)) compileall.compile_dir(self.directory, quiet=True, maxlevels=depth) self.assertTrue(os.path.isfile(pyc_filename))
def test_ignore_symlink_destination(self): # Create folders for allowed files, symlinks and prohibited area allowed_path = os.path.join(self.directory, "test", "dir", "allowed") symlinks_path = os.path.join(self.directory, "test", "dir", "symlinks") prohibited_path = os.path.join(self.directory, "test", "dir", "prohibited") os.makedirs(allowed_path) os.makedirs(symlinks_path) os.makedirs(prohibited_path) # Create scripts and symlinks and remember their byte-compiled versions allowed_script = script_helper.make_script(allowed_path, "test_allowed", "a = 0") prohibited_script = script_helper.make_script(prohibited_path, "test_prohibited", "a = 0") allowed_symlink = os.path.join(symlinks_path, "test_allowed.py") prohibited_symlink = os.path.join(symlinks_path, "test_prohibited.py") os.symlink(allowed_script, allowed_symlink) os.symlink(prohibited_script, prohibited_symlink) allowed_bc = importlib.util.cache_from_source(allowed_symlink) prohibited_bc = importlib.util.cache_from_source(prohibited_symlink) compileall.compile_dir(symlinks_path, quiet=True, limit_sl_dest=allowed_path) self.assertTrue(os.path.isfile(allowed_bc)) self.assertFalse(os.path.isfile(prohibited_bc))
import compileall2 compileall2.compile_dir(".", force=1)
def test_compile_missing_multiprocessing(self, compile_file_mock): compileall.compile_dir(self.directory, quiet=True, workers=5) self.assertTrue(compile_file_mock.called)
def test_compile_one_worker(self, compile_file_mock, pool_mock): compileall.compile_dir(self.directory, quiet=True) self.assertFalse(pool_mock.called) self.assertTrue(compile_file_mock.called)
def test_compile_workers_cpu_count(self, pool_mock): compileall.compile_dir(self.directory, quiet=True, workers=0) self.assertEqual(pool_mock.call_args[1]['max_workers'], None)
def test_compile_workers_non_positive(self): with self.assertRaisesRegex(ValueError, "workers must be greater or equal to 0"): compileall.compile_dir(self.directory, workers=-1)
def test_compile_pool_called(self, pool_mock): compileall.compile_dir(self.directory, quiet=True, workers=5) self.assertTrue(pool_mock.called)