From 0ef0ff4c3fa5cd936ab05504dc7717af818a8dcc Mon Sep 17 00:00:00 2001 From: John Sirois Date: Sun, 3 Nov 2024 14:03:37 -0800 Subject: [PATCH] Continue to populate empty code caches for back compat. --- pex/layout.py | 23 ++++++++++++----------- pex/util.py | 7 ++----- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/pex/layout.py b/pex/layout.py index 514e0e205..30724e73b 100644 --- a/pex/layout.py +++ b/pex/layout.py @@ -339,6 +339,11 @@ def _ensure_installed( bootstrap_cache = BootstrapDir.create( pex_info.bootstrap_hash, pex_root=pex_info.pex_root ) + if pex_info.code_hash is None: + raise AssertionError( + "Expected code_hash to be populated for {}.".format(layout) + ) + code_cache = UserCodeDir.create(pex_info.code_hash, pex_root=pex_info.pex_root) with atomic_directory( bootstrap_cache, source=layout.bootstrap_strip_prefix() @@ -357,18 +362,14 @@ def _ensure_installed( install_to=install_to, ) - if pex_info.code_hash: - code_cache = UserCodeDir.create( - pex_info.code_hash, pex_root=pex_info.pex_root + with atomic_directory(code_cache) as code_chroot: + if not code_chroot.is_finalized(): + layout.extract_code(code_chroot.work_dir) + for path in os.listdir(code_cache): + os.symlink( + os.path.join(os.path.relpath(code_cache, install_to), path), + os.path.join(chroot.work_dir, path), ) - with atomic_directory(code_cache) as code_chroot: - if not code_chroot.is_finalized(): - layout.extract_code(code_chroot.work_dir) - for path in os.listdir(code_cache): - os.symlink( - os.path.join(os.path.relpath(code_cache, install_to), path), - os.path.join(chroot.work_dir, path), - ) layout.extract_pex_info(chroot.work_dir) layout.extract_main(chroot.work_dir) diff --git a/pex/util.py b/pex/util.py index 94d51ea9f..0f935a040 100644 --- a/pex/util.py +++ b/pex/util.py @@ -70,8 +70,6 @@ def access_zipped_assets(cls, static_module_name, static_path, dir_location=None class CacheHelper(object): - _EMPTY_CODE_HASH = hashlib.sha1().hexdigest() - @classmethod def hash(cls, path, digest=None, hasher=sha1): # type: (Text, Optional[Hasher], Callable[[], Hasher]) -> str @@ -88,7 +86,7 @@ def pex_code_hash( exclude_dirs=(), # type: Container[str] exclude_files=(), # type: Container[str] ): - # type: (...) -> Optional[str] + # type: (...) -> str """Return a reproducible hash of the user code of a loose PEX; excluding all `.pyc` files. If no code is found, `None` is returned. @@ -106,8 +104,7 @@ def pex_code_hash( ) ), ) - code_hash = digest.hexdigest() - return None if code_hash == cls._EMPTY_CODE_HASH else code_hash + return digest.hexdigest() @classmethod def dir_hash(cls, directory, digest=None, hasher=sha1):