Forked from tddschn/pipx_post_upgrade_re_symlink_python_executables.py
Created
February 9, 2025 22:13
-
-
Save datavudeja/8d6133fefaf02200a8f3cc403ec565a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Author: Teddy Xinyuan Chen | |
Date: 2020-08-17 | |
""" | |
from functools import cache | |
from pathlib import Path | |
from subprocess import run | |
import re | |
PIPX_VIRTUALENV_DIR = Path('~/.local/pipx/venvs').expanduser() | |
@cache | |
def get_pipx_version() -> str: | |
version = run(['pipx', '--version'], capture_output=True).stdout.decode().strip() | |
return version | |
def re_symlink_python(src: Path) -> str: | |
pipx_version = get_pipx_version() | |
orig_target = str(src.readlink()) | |
new_target, subs_made = re.subn( | |
r'/pipx/[^/]+/', f'/pipx/{pipx_version}/', orig_target, count=1 | |
) | |
if subs_made == 1: | |
src.unlink() | |
src.symlink_to(new_target) | |
return new_target | |
else: | |
return orig_target | |
def main() -> None: | |
link_src = list(PIPX_VIRTUALENV_DIR.rglob('*/bin/python3.*')) | |
for src in link_src: | |
new_target = re_symlink_python(src) | |
print(f'symlinked {str(src)} to {new_target}') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment