Skip to content

Fails when the brew command is not available #158

Closed
@S-zebra

Description

@S-zebra

Distutils tries to get the output of brew --prefix, but it fails if Homebrew is not installed on the system, or when brew command is not available.

homebrew_prefix = subprocess.check_output(['brew', '--prefix'], text=True).strip()

Activity

dflems

dflems commented on Jul 13, 2022

@dflems
self-assigned this
on Jul 13, 2022
jaraco

jaraco commented on Jul 13, 2022

@jaraco
Member

At first, I was going to use this diff:

diff --git a/distutils/command/_framework_compat.py b/distutils/command/_framework_compat.py
index e032603..c2849a1 100644
--- a/distutils/command/_framework_compat.py
+++ b/distutils/command/_framework_compat.py
@@ -7,6 +7,7 @@ import sys
 import os
 import functools
 import subprocess
+import contextlib
 
 
 @functools.lru_cache()
@@ -39,7 +40,10 @@ schemes = dict(
 def vars():
     if not enabled():
         return {}
-    homebrew_prefix = subprocess.check_output(['brew', '--prefix'], text=True).strip()
+    with contextlib.suppress(FileNotFoundError):
+        homebrew_prefix = subprocess.check_output(
+            ['brew', '--prefix'], text=True
+        ).strip()
     return locals()

But then I realized that will only cause a different error because {homebrew_prefix} will be in the scheme but undefined. The enabled() function needs to detect whether the install is a homebrew one.

jaraco

jaraco commented on Jul 13, 2022

@jaraco
Member

Maybe the presence of "Cellar" in the projectbase config var is a good indicator?

>>> pprint.pprint({k:v for k, v in sysconfig.get_config_vars().items() if isinstance(v, str) and 'Cellar' in v})
{'projectbase': '/opt/homebrew/Cellar/python@3.10/3.10.5/Frameworks/Python.framework/Versions/3.10/bin',
 'srcdir': '/opt/homebrew/Cellar/python@3.10/3.10.5/Frameworks/Python.framework/Versions/3.10/lib/python3.10/config-3.10-darwin'}
jaraco

jaraco commented on Jul 13, 2022

@jaraco
Member

Fix is rolling out is 63.2.0.

1255 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @jaraco@dflems@S-zebra@carlocab

      Issue actions

        Fails when the `brew` command is not available · Issue #158 · pypa/distutils