Skip to content

Commit

Permalink
If installed in /usr/local , ramalama libs cannot be found
Browse files Browse the repository at this point in the history
This adds the /usr/local paths

Signed-off-by: Eric Curtin <[email protected]>
  • Loading branch information
ericcurtin committed Oct 19, 2024
1 parent 07a53c4 commit 4ff5d7c
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions bin/ramalama
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,41 @@ def add_pipx_venvs_bin_to_path():
if os.path.exists(pipx_bin_path):
os.environ["PATH"] += ":" + pipx_bin_path

def add_pipx_venvs_to_syspath():

def add_site_packages_to_syspath(base_path):
"""
Adds all available pipx virtual environments' site-packages directories to sys.path.
This function looks for any venv in ~/.local/pipx/venvs and appends their site-packages directory to sys.path.
Adds site-packages directories from a given base path to sys.path.
The function searches for directories matching the pattern:
{base_path}/lib/python{python_version}/site-packages or
{base_path}/lib/python*/site-packages and adds them to sys.path.
:param base_path: The base directory to search for site-packages.
"""
python_version = f'{sys.version_info.major}.{sys.version_info.minor}'
pipx_venvs_path = os.path.expanduser(f'~/.local/pipx/venvs/*/lib/python{python_version}/site-packages')
matched_paths = glob.glob(pipx_venvs_path)
search_pattern = os.path.expanduser(f'{base_path}/lib/python{python_version}/site-packages')
matched_paths = glob.glob(search_pattern)

if matched_paths:
for path in matched_paths:
sys.path.insert(0, path)

return

pipx_venvs_path = os.path.expanduser('~/.local/pipx/venvs/*/lib/python*/site-packages')
matched_paths = glob.glob(pipx_venvs_path)
if not matched_paths:
return

for path in matched_paths:
sys.path.insert(0, path)
# Fallback to a more general pattern if the specific version doesn't match
search_pattern = os.path.expanduser(f'{base_path}/lib/python*/site-packages')
matched_paths = glob.glob(search_pattern)

if matched_paths:
for path in matched_paths:
sys.path.insert(0, path)

def main(args):
sharedirs = ["./", "/opt/homebrew/share/ramalama", "/usr/local/share/ramalama", "/usr/share/ramalama"]
syspath = next((d for d in sharedirs if os.path.exists(d+"/ramalama/cli.py")), None)
if syspath:
sys.path.insert(0, syspath)

add_pipx_venvs_to_syspath()
add_site_packages_to_syspath('~/.local/pipx/venvs')
add_site_packages_to_syspath('/usr/local')
add_pipx_venvs_bin_to_path()
try:
import ramalama
Expand Down

0 comments on commit 4ff5d7c

Please sign in to comment.