utils: fix dist package check

Older versions of virtualenv include their own "site" module
that does not have the "getsitepackages" method.  Add
a check to verify its presence  before calling it.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-03-05 16:41:21 -05:00
parent c40a3474f4
commit 80920dd872
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 10 additions and 9 deletions

View File

@ -28,16 +28,17 @@ def is_git_repo(src_path: Optional[pathlib.Path] = None) -> bool:
def is_dist_package(src_path: Optional[pathlib.Path] = None) -> bool: def is_dist_package(src_path: Optional[pathlib.Path] = None) -> bool:
if src_path is None: if src_path is None:
# Fetch dist info for moonraker. This should be the most reliable # Check Moonraker's source path
# method
src_path = source_path() src_path = source_path()
site_dirs = site.getsitepackages() if hasattr(site, "getsitepackages"):
return str(src_path) in site_dirs # The site module is present, get site packages for Moonraker's venv.
else: # This is more "correct" than the fallback method.
# Make an assumption based on the source path. If its name is site_dirs = site.getsitepackages()
# site-packages or dist-packages then presumably it is an return str(src_path) in site_dirs
# installed package # Make an assumption based on the source path. If its name is
return src_path.name in ["dist-packages", "site-packages"] # site-packages or dist-packages then presumably it is an
# installed package
return src_path.name in ["dist-packages", "site-packages"]
def package_version() -> Optional[str]: def package_version() -> Optional[str]:
try: try: