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`.
This commit is contained in:
Chris Hunt 2020-07-04 11:44:02 -04:00
parent feb2a24f14
commit 0a3a558e38
1 changed files with 21 additions and 1 deletions

View File

@ -451,12 +451,32 @@ def install_unpacked_wheel(
changed = set() # type: Set[RecordPath]
generated = [] # type: List[str]
def pyc_source_file_paths():
# type: () -> Iterator[text_type]
decoded_source = ensure_text(
source, encoding=sys.getfilesystemencoding()
)
for dir_path, subdir_paths, files in os.walk(decoded_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():
# Python 2's `compileall.compile_file` requires a str in
# error cases, so we must convert to the native type.
path_arg = ensure_str(
path, encoding=sys.getfilesystemencoding()
)
compileall.compile_file(
path_arg, force=True, quiet=True
)
logger.debug(stdout.getvalue())
def record_installed(srcfile, destfile, modified=False):