Created
February 12, 2018 15:37
-
-
Save yrcjaya/decbe1764bcdf3e12d0f39a7923a5b08 to your computer and use it in GitHub Desktop.
Sanitizing project name base on path
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
""" | |
Sanitizing filename | |
from: https://github.com/pypa/pipenv/blob/master/pipenv/project.py | |
""" | |
def virtualenv_name(self): | |
# Replace dangerous characters into '_'. The length of the sanitized | |
# project name is limited as 42 because of the limit of linux kernel | |
# | |
# 42 = 127 - len('/home//.local/share/virtualenvs//bin/python2') - 32 - len('-HASHHASH') | |
# | |
# 127 : BINPRM_BUF_SIZE - 1 | |
# 32 : Maximum length of username | |
# | |
# References: | |
# https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html | |
# http://www.tldp.org/LDP/abs/html/special-chars.html#FIELDREF | |
# https://github.com/torvalds/linux/blob/2bfe01ef/include/uapi/linux/binfmts.h#L18 | |
sanitized = re.sub(r'[ $`!*@"\\\r\n\t]', '_', self.name)[0:42] | |
# Hash the full path of the pipfile | |
hash = hashlib.sha256(self.pipfile_location.encode()).digest()[:6] | |
encoded_hash = base64.urlsafe_b64encode(hash).decode() | |
# If the pipfile was located at '/home/user/MY_PROJECT/Pipfile', | |
# the name of its virtualenv will be 'my-project-wyUfYPqE' | |
return sanitized + '-' + encoded_hash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment