Skip to content

Instantly share code, notes, and snippets.

@yurenchen000
Created November 27, 2019 17:55
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 yurenchen000/e7b0068d0826f2745d63d3e962736f99 to your computer and use it in GitHub Desktop.
Save yurenchen000/e7b0068d0826f2745d63d3e962736f99 to your computer and use it in GitHub Desktop.
pipenv calcate virtualenv name
# https://github.com/pypa/pipenv/issues/1049
# https://github.com/pypa/pipenv/blob/a4aca53b5d02a4168214a487714d9836c9415a0c/pipenv/project.py#L116
# How pipenv calc virtualenv name in `~/.local/share/virtualenvs/`
import os, re, base64, hashlib
def virtualenv_name(path):
sanitized = re.sub(r'[ $`!*@"\\\r\n\t]', '_', os.path.basename(path))[0:42]
hash = hashlib.sha256((path+'/Pipfile').encode()).digest()[:6]
encoded_hash = base64.urlsafe_b64encode(hash).decode()
return sanitized + '-' + encoded_hash
# >>> virtualenv_name('/home/chen/haibss')
# 'haibss-9rO3TUH9'
# $ ls -lh ~/.local/share/virtualenvs/
# drwxrwxr-x 5 chen chen 4.0K Nov 28 00:24 haibss-9rO3TUH9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment