[docs]classPipenvExportFailedError(RunwayError):"""Pipenv export failed to produce a ``requirements.txt`` file."""
[docs]def__init__(self,*args:Any,**kwargs:Any)->None:"""Instantiate class. All args/kwargs are passed to parent method."""self.message=("pipenv lock to requirements.txt format failed; review pipenv's"" output above to troubleshoot")super().__init__(*args,**kwargs)
[docs]classPipenvNotFoundError(RunwayError):"""Pipenv not installed or found in $PATH."""
[docs]def__init__(self,*args:Any,**kwargs:Any)->None:"""Instantiate class. All args/kwargs are passed to parent method."""self.message=("pipenv not installed or not in PATH! ""Install it according to pipenv docs (https://pipenv.pypa.io/en/latest/) ""and ensure it is available in PATH.")super().__init__(*args,**kwargs)
[docs]classPipenv(DependencyManager):"""Pipenv dependency manager."""CONFIG_FILES:ClassVar[tuple[str,...]]=("Pipfile","Pipfile.lock",)"""Configuration files used by pipenv."""EXECUTABLE:ClassVar[str]="pipenv""""CLI executable."""@cached_propertydefversion(self)->Version:"""pipenv version."""cmd_output=self._run_command([self.EXECUTABLE,"--version"])match=re.search(r"^pipenv, version (?P<version>\S*)",cmd_output)ifnotmatch:LOGGER.warning("unable to parse pipenv version from output:\n%s",cmd_output)returnVersion("0.0.0")returnVersion(match.group("version"))
[docs]@classmethoddefdir_is_project(cls,directory:StrPath,**__kwargs:Any)->bool:"""Determine if the directory contains a project for this dependency manager. Args: directory: Directory to check. """dir_path=Path(directory)ifnot(dir_path/Pipenv.CONFIG_FILES[0]).is_file():returnFalseifnot(dir_path/Pipenv.CONFIG_FILES[1]).is_file():LOGGER.warning("%s not found",Pipenv.CONFIG_FILES[1])returnTrue
[docs]defexport(self,*,dev:bool=False,output:StrPath)->Path:"""Export the lock file to other formats (requirements.txt only). The underlying command being executed by this method is ``pipenv requirements``. Args: dev: Include development dependencies. output: Path to the output file. """environ=self.ctx.env.vars.copy()environ["PIPENV_IGNORE_VIRTUALENVS"]="1"output=Path(output)ifnot(self.cwd/"Pipfile.lock").is_file():LOGGER.warning("Pipfile.lock does not exist! creating it...")self._run_command(self.generate_command("lock"),env=environ)try:result=self._run_command(self.generate_command("requirements",dev=dev),env=environ,suppress_output=True,)exceptsubprocess.CalledProcessErrorasexc:raisePipenvExportFailedErrorfromexcoutput.parent.mkdir(exist_ok=True,parents=True)# ensure directory existsoutput.write_text(str(result),encoding=locale.getpreferredencoding(do_setlocale=False))returnoutput