Skip to content

Commit 3762ddc

Browse files
committed
Use compile_dir to compile
1 parent e98cc5c commit 3762ddc

File tree

1 file changed

+32
-6
lines changed
  • src/pip/_internal/operations/install

1 file changed

+32
-6
lines changed

src/pip/_internal/operations/install/wheel.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,13 +596,34 @@ def pyc_source_file_paths() -> Generator[str, None, None]:
596596
# Sorting installation paths makes it easier to reproduce and debug
597597
# issues related to permissions on existing files.
598598
for installed_path in sorted(set(installed.values())):
599+
if not installed_path.endswith(".py"):
600+
continue
601+
599602
full_installed_path = os.path.join(lib_dir, installed_path)
600603
if not os.path.isfile(full_installed_path):
601604
continue
602-
if not full_installed_path.endswith(".py"):
603-
continue
604605
yield full_installed_path
605606

607+
def pyc_source_root_dirs() -> Generator[str, None, None]:
608+
# All root source directories of the install (i.e. those that
609+
# are direct children of lib_dir) that the source Python files
610+
# are located within. Typically this is just one directory.
611+
source_root_dirs = set()
612+
for installed_path in installed.values():
613+
if not installed_path.endswith(".py"):
614+
continue
615+
616+
full_installed_path = os.path.join(lib_dir, installed_path)
617+
if not os.path.isfile(full_installed_path):
618+
continue
619+
620+
parent_full_installed_path = os.path.dirname(full_installed_path)
621+
if os.path.dirname(parent_full_installed_path) == lib_dir:
622+
if parent_full_installed_path in source_root_dirs:
623+
continue
624+
source_root_dirs.add(parent_full_installed_path)
625+
yield parent_full_installed_path
626+
606627
def pyc_output_path(path: str) -> str:
607628
"""Return the path the pyc file would have been written to."""
608629
return importlib.util.cache_from_source(path)
@@ -614,11 +635,16 @@ def pyc_output_path(path: str) -> str:
614635
) as stdout:
615636
with warnings.catch_warnings():
616637
warnings.filterwarnings("ignore")
638+
# Use compile_dir on the root directories in the package,
639+
# as compile_dir uses multiprocess this will be quicker
640+
# than using compile_file on each file.
641+
for dir in pyc_source_root_dirs():
642+
compileall.compile_dir(dir, force=True, quiet=2, workers=0)
643+
644+
# Now check individual files were created
617645
for path in pyc_source_file_paths():
618-
success = compileall.compile_file(path, force=True, quiet=True)
619-
if success:
620-
pyc_path = pyc_output_path(path)
621-
assert os.path.exists(pyc_path)
646+
pyc_path = pyc_output_path(path)
647+
if os.path.exists(pyc_path):
622648
pyc_record_path = cast(
623649
"RecordPath", pyc_path.replace(os.path.sep, "/")
624650
)

0 commit comments

Comments
 (0)