def export_requirements(session: Session) -> Path: """Export a requirements file from Poetry. This function uses `poetry export`_ to generate a `requirements file`_ containing the project dependencies at the versions specified in ``poetry.lock``. The requirements file includes both core and development dependencies. The requirements file is stored in a per-session temporary directory, together with a hash digest over ``poetry.lock`` to avoid generating the file when the dependencies have not changed since the last run. .. _poetry export: https://python-poetry.org/docs/cli/#export .. _requirements file: https://pip.pypa.io/en/stable/user_guide/#requirements-files Args: session: The ``Session`` object. Returns: The path to the requirements file. """ tmpdir = Path(session.create_tmp()) path = tmpdir / "requirements.txt" hashfile = tmpdir / f"{path.name}.hash" lockdata = Path("poetry.lock").read_bytes() digest = hashlib.blake2b(lockdata).hexdigest() if not hashfile.is_file() or hashfile.read_text() != digest: Poetry(session).export(path) hashfile.write_text(digest) return path
def export_requirements(session: Session, *, dev: bool) -> Path: """Export the lock file to requirements format. Args: session: The Session object. dev: If True, include development dependencies. Returns: The path to the requirements file. """ tmpdir = Path(session.create_tmp()) name = "dev-requirements.txt" if dev else "requirements.txt" path = tmpdir / name hashfile = tmpdir / f"{name}.hash" lockdata = Path("poetry.lock").read_bytes() digest = hashlib.blake2b(lockdata).hexdigest() if not hashfile.is_file() or hashfile.read_text() != digest: Poetry(session).export(path, dev=dev) hashfile.write_text(digest) return path