Created
September 14, 2018 21:54
-
-
Save akiross/140890c31abedfc670e896832b983b2e to your computer and use it in GitHub Desktop.
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
"""Given a root directory, search for Pipfiles in it and | |
produce the corresponding pipenv names. | |
This script can be useful to find lost project directories. | |
Python 3.6 is required (for f-strings). | |
This code is public domain. | |
""" | |
import os | |
import sys | |
import base64 | |
import hashlib | |
from pathlib import Path | |
def get_virtualenv_name(pipfile_path): | |
dir_name = Path(pipfile_path).parts[-2] | |
sha = hashlib.sha256(pipfile_path.encode()).digest()[:6] | |
enc_hash = base64.urlsafe_b64encode(sha).decode()[:8] | |
return f'{dir_name}-{enc_hash}' | |
def find(name, path): | |
for root, dirs, files in os.walk(path): | |
if name in files: | |
yield os.path.join(root, name) | |
if __name__ == '__main__': | |
for path in find('Pipfile', sys.argv[1]): | |
print(get_virtualenv_name(path), path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment