Skip to content

Commit

Permalink
Fixes of requirements with git requirements
Browse files Browse the repository at this point in the history
Solved by using existing convert_deps_to_pip function.

Fix #5076.

Original implementation by @fraser-langton. This also
solves the export of packages with editable mode, as suggested
by @hoyaaaa (#5071).
  • Loading branch information
oz123 committed Apr 25, 2022
1 parent 347ce47 commit 6655598
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
1 change: 1 addition & 0 deletions news/5076.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes issue of requirements command where git requirements cause the command to fail, solved by using existing convert_deps_to_pip function.
32 changes: 16 additions & 16 deletions pipenv/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,24 +760,24 @@ def requirements(state, dev=False, dev_only=False, hash=False):
for i, package_index in enumerate(lockfile["_meta"]["sources"]):
prefix = "-i" if i == 0 else "--extra-index-url"
echo(" ".join([prefix, package_index["url"]]))

from pipenv.utils.dependencies import convert_deps_to_pip

lockfile = state.project.lockfile_content
deps = {}

if not dev_only:
for req_name, value in lockfile["default"].items():
if value.get("editable", False):
echo("-e " + value["path"])
elif hash:
hashes = [f" \\\n --hash={h}" for h in value.get("hashes", [])]
echo("".join([req_name, value["version"], *hashes]))
else:
echo("".join([req_name, value["version"]]))
deps.update(lockfile["default"])
if dev or dev_only:
for req_name, value in lockfile["develop"].items():
if value.get("editable", False):
echo("-e " + value["path"])
elif hash:
hashes = [f" \\\n --hash={h}" for h in value.get("hashes", [])]
echo("".join([req_name, value["version"], *hashes]))
else:
echo("".join([req_name, value["version"]]))
deps.update(lockfile["develop"])

pip_deps = convert_deps_to_pip(
deps, r=False, include_index=False, include_hashes=hash, include_markers=False
)

for d in pip_deps:
echo(d)

sys.exit(0)


Expand Down
16 changes: 14 additions & 2 deletions pipenv/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,14 @@ def is_pinned_requirement(ireq):
return spec.operator in {"==", "==="} and not spec.version.endswith(".*")


def convert_deps_to_pip(deps, project=None, r=True, include_index=True):
def convert_deps_to_pip(
deps,
project=None,
r=True,
include_index=True,
include_hashes=True,
include_markers=True,
):
""" "Converts a Pipfile-formatted dependency to a pip-formatted one."""
from pipenv.vendor.requirementslib.models.requirements import Requirement

Expand All @@ -255,7 +262,12 @@ def convert_deps_to_pip(deps, project=None, r=True, include_index=True):
new_dep = Requirement.from_pipfile(dep_name, dep)
if new_dep.index:
include_index = True
req = new_dep.as_line(sources=indexes if include_index else None).strip()
sources = indexes if include_index else None
req = new_dep.as_line(
sources=sources,
include_hashes=include_hashes,
include_markers=include_markers,
).strip()
dependencies.append(req)
if not r:
return dependencies
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/test_requirements.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import pytest


Expand Down Expand Up @@ -66,3 +67,27 @@ def test_requirements_generates_requirements_from_lockfile_multiple_sources(Pipe

assert '-i https://pypi.org/simple' in c.stdout
assert '--extra-index-url https://some_other_source.org' in c.stdout

@pytest.mark.requirements
def test_requirements_with_git_requirements(PipenvInstance):
req_name, req_hash = 'example-repo', 'cc858e89f19bc0dbd70983f86b811ab625dc9292'
lockfile = {
"_meta": {"sources": []},
"default": {
req_name: {
"editable": True,
"git": F"ssh://git@bitbucket.org/code/{req_name}.git",
"ref": req_hash
}
},
"develop": {}
}

with PipenvInstance(chdir=True) as p:
with open(p.lockfile_path, 'w') as f:
json.dump(lockfile, f)

c = p.pipenv('requirements')
assert c.returncode == 0
assert req_name in c.stdout
assert req_hash in c.stdout

0 comments on commit 6655598

Please sign in to comment.