Skip to content

Commit 2c68c14

Browse files
committed
cephadm: fix zip_root_entries population in version command
The 'cephadm version --verbose' command was returning an empty zip_root_entries list because it relied on the private '_files' attribute of zipimport.zipimporter, which is not reliably populated across Python versions. This commit fixes the issue by using the zipfile module to properly read the archive contents via the loader.archive path. This ensures that zip_root_entries is correctly populated with the root-level directories in the zipapp. This fix is necessary for the cephadm build tests to properly validate that all expected packages and modules are included in the built zipapp. Signed-off-by: Kefu Chai <[email protected]>
1 parent 2fadd34 commit 2c68c14

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/cephadm/cephadm.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,7 @@ def command_version(ctx):
16941694
# type: (CephadmContext) -> int
16951695
import importlib
16961696
import zipimport
1697+
import zipfile
16971698
import types
16981699

16991700
vmod: Optional[types.ModuleType]
@@ -1750,10 +1751,17 @@ def command_version(ctx):
17501751
out['bundled_packages'] = deps_info
17511752
except OSError:
17521753
pass
1753-
files = getattr(loader, '_files', {})
1754-
out['zip_root_entries'] = sorted(
1755-
{p.split('/')[0] for p in files.keys()}
1756-
)
1754+
# Use zipfile module to properly read the archive contents
1755+
# loader.archive contains the path to the zip file
1756+
try:
1757+
with zipfile.ZipFile(loader.archive, 'r') as zf:
1758+
files = zf.namelist()
1759+
out['zip_root_entries'] = sorted(
1760+
{p.split('/')[0] for p in files if p}
1761+
)
1762+
except (OSError, zipfile.BadZipFile):
1763+
# Fallback to empty list if we can't read the zip
1764+
out['zip_root_entries'] = []
17571765

17581766
json.dump(out, sys.stdout, indent=2)
17591767
print()

0 commit comments

Comments
 (0)