@property def virtualenv_location(self): # if VIRTUAL_ENV is set, use that. if PIPENV_VIRTUALENV: return PIPENV_VIRTUALENV
if not self._virtualenv_location: # Use cached version, if available. assert self.project_directory, "project not created" self._virtualenv_location = self.get_location_for_virtualenv() return self._virtualenv_location
def get_location_for_virtualenv(self): if self.is_venv_in_project(): return os.path.join(self.project_directory, ".venv")
name = self.virtualenv_name if self.project_directory: venv_path = os.path.join(self.project_directory, ".venv") if os.path.exists(venv_path) and not os.path.isdir(".venv"): with io.open(venv_path, "r") as f: name = f.read().strip() # Assume file's contents is a path if it contains slashes. if looks_like_dir(name): return vistir.compat.Path(name).absolute().as_posix() return str(get_workon_home().joinpath(name))
@property def virtualenv_name(self): sanitized, encoded_hash = self._get_virtualenv_hash(self.name) suffix = "-{0}".format(PIPENV_PYTHON) if PIPENV_PYTHON else "" # 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 + suffix
def _get_virtualenv_hash(self, name): """Get the name of the virtualenv adjusted for windows if needed
# This should work most of the time for # Case-sensitive filesystems, # In-project venv # "Proper" path casing (on non-case-sensitive filesystems). if ( fnmatch.fnmatch("A", "a") or self.is_venv_in_project() or get_workon_home().joinpath(venv_name).exists() ): return clean_name, encoded_hash
# Check for different capitalization of the same project. for path in get_workon_home().iterdir(): if not is_virtual_environment(path): continue try: env_name, hash_ = path.name.rsplit("-", 1) except ValueError: continue if len(hash_) != 8 or env_name.lower() != name.lower(): continue return get_name(env_name, self.pipfile_location.replace(name, env_name))
# Use the default if no matching env exists. return clean_name, encoded_hash
@property def pipfile_location(self): if PIPENV_PIPFILE: return PIPENV_PIPFILE
if self._pipfile_location is None: try: loc = pipfile.Pipfile.find(max_depth=PIPENV_MAX_DEPTH) except RuntimeError: loc = None self._pipfile_location = _normalized(loc) return self._pipfile_location
在pipenv/patched/pipfile/api.py中找到了find方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Pipfile(object): @staticmethod def find(max_depth=3): """Returns the path of a Pipfile in parent directories.""" i = 0 for c, d, f in walk_up(os.getcwd()): i += 1
if i < max_depth: if 'Pipfile': p = os.path.join(c, 'Pipfile') if os.path.isfile(p): return p raise RuntimeError('No Pipfile found!')