Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcinzona/ff2ea2702e14d96e67271a75140b411d to your computer and use it in GitHub Desktop.
Save dcinzona/ff2ea2702e14d96e67271a75140b411d to your computer and use it in GitHub Desktop.
#!/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