@@ -596,13 +596,34 @@ def pyc_source_file_paths() -> Generator[str, None, None]:
596
596
# Sorting installation paths makes it easier to reproduce and debug
597
597
# issues related to permissions on existing files.
598
598
for installed_path in sorted (set (installed .values ())):
599
+ if not installed_path .endswith (".py" ):
600
+ continue
601
+
599
602
full_installed_path = os .path .join (lib_dir , installed_path )
600
603
if not os .path .isfile (full_installed_path ):
601
604
continue
602
- if not full_installed_path .endswith (".py" ):
603
- continue
604
605
yield full_installed_path
605
606
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
+
606
627
def pyc_output_path (path : str ) -> str :
607
628
"""Return the path the pyc file would have been written to."""
608
629
return importlib .util .cache_from_source (path )
@@ -614,11 +635,16 @@ def pyc_output_path(path: str) -> str:
614
635
) as stdout :
615
636
with warnings .catch_warnings ():
616
637
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
617
645
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 ):
622
648
pyc_record_path = cast (
623
649
"RecordPath" , pyc_path .replace (os .path .sep , "/" )
624
650
)
0 commit comments