From fb049d75391412fbdc38d441d5f3afea177525ac Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Sat, 4 Jul 2020 11:44:02 -0400 Subject: [PATCH] Use compileall.compile_file instead of compileall.compile_dir We want to move towards having more control over the generation of pyc files, which will allow us to provide deterministic installs and generate pyc files without relying on an already-extracted wheel. To that end, here we are stripping away one layer of abstraction, `compileall.compile_dir`. `compileall.compile_dir` essentially recurses through the provided directories and passes the files and args verbatim to `compileall.compile_file`, so removing that layer means that we directly call `compileall.compile_file`. We make the assumption that we can successfully walk over the source file tree, since we just wrote it, and omit the per-directory traversal error handling done by `compileall.compile_dir`. --- src/pip/_internal/operations/install/wheel.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/pip/_internal/operations/install/wheel.py b/src/pip/_internal/operations/install/wheel.py index eb3a07c684b..faaf4aab53e 100644 --- a/src/pip/_internal/operations/install/wheel.py +++ b/src/pip/_internal/operations/install/wheel.py @@ -451,12 +451,24 @@ def install_unpacked_wheel( changed = set() # type: Set[RecordPath] generated = [] # type: List[str] + def pyc_source_file_paths(): + # type: () -> Iterator[str] + for dir_path, subdir_paths, files in os.walk(source): + subdir_paths[:] = [ + p for p in subdir_paths if p != '__pycache__' + ] + for path in files: + yield os.path.join(dir_path, path) + # Compile all of the pyc files that we're going to be installing if pycompile: with captured_stdout() as stdout: with warnings.catch_warnings(): warnings.filterwarnings('ignore') - compileall.compile_dir(source, force=True, quiet=True) + for path in pyc_source_file_paths(): + compileall.compile_file( + path, force=True, quiet=True + ) logger.debug(stdout.getvalue()) def record_installed(srcfile, destfile, modified=False):