Created
November 27, 2019 17:55
-
-
Save yurenchen000/e7b0068d0826f2745d63d3e962736f99 to your computer and use it in GitHub Desktop.
pipenv calcate virtualenv name
This file contains 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
# 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