Skip to content

Commit be8a084

Browse files
DasaniToz123
DasaniT
authored andcommitted
Add --categories option to work with requirements txt files
1 parent 5416f80 commit be8a084

File tree

6 files changed

+79
-7
lines changed

6 files changed

+79
-7
lines changed

news/5722.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The ``--categories`` option now works with requirements.txt file.

pipenv/routines/install.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def do_install(
6666
skip_requirements=skip_requirements,
6767
pypi_mirror=pypi_mirror,
6868
site_packages=site_packages,
69+
categories=categories,
6970
)
7071
# Don't attempt to install develop and default packages if Pipfile is missing
7172
if not project.pipfile_exists and not (package_args or dev):
@@ -131,7 +132,12 @@ def do_install(
131132
err=True,
132133
)
133134
try:
134-
import_requirements(project, r=project.path_to(requirementstxt), dev=dev)
135+
import_requirements(
136+
project,
137+
r=project.path_to(requirementstxt),
138+
dev=dev,
139+
categories=categories,
140+
)
135141
except (UnicodeDecodeError, PipError) as e:
136142
# Don't print the temp file path if remote since it will be deleted.
137143
req_path = requirements_url if remote else project.path_to(requirementstxt)

pipenv/utils/pipfile.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def find_pipfile(max_depth=3):
5050
raise RuntimeError("No Pipfile found!")
5151

5252

53-
def ensure_pipfile(project, validate=True, skip_requirements=False, system=False):
53+
def ensure_pipfile(
54+
project, validate=True, skip_requirements=False, system=False, categories=None
55+
):
5456
"""Creates a Pipfile for the project, if it doesn't exist."""
5557

5658
# Assert Pipfile exists.
@@ -81,7 +83,7 @@ def ensure_pipfile(project, validate=True, skip_requirements=False, system=False
8183
) as st:
8284
# Import requirements.txt.
8385
try:
84-
import_requirements(project)
86+
import_requirements(project, categories=categories)
8587
except Exception:
8688
err.print(environments.PIPENV_SPINNER_FAIL_TEXT.format("Failed..."))
8789
else:

pipenv/utils/project.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def ensure_project(
1919
skip_requirements=False,
2020
pypi_mirror=None,
2121
clear=False,
22+
categories=None,
2223
):
2324
"""Ensures both Pipfile and virtualenv exist for the project."""
2425

@@ -78,5 +79,6 @@ def ensure_project(
7879
validate=validate,
7980
skip_requirements=skip_requirements,
8081
system=system,
82+
categories=categories,
8183
)
8284
os.environ["PIP_PYTHON_PATH"] = project.python(system=system)

pipenv/utils/requirements.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pipenv.utils.pip import get_trusted_hosts
1313

1414

15-
def import_requirements(project, r=None, dev=False):
15+
def import_requirements(project, r=None, dev=False, categories=None):
1616
# Parse requirements.txt file with Pip's parser.
1717
# Pip requires a `PipSession` which is a subclass of requests.Session.
1818
# Since we're not making any network calls, it's initialized to nothing.
@@ -23,6 +23,8 @@ def import_requirements(project, r=None, dev=False):
2323
r = project.requirements_location
2424
with open(r) as f:
2525
contents = f.read()
26+
if categories is None:
27+
categories = []
2628
indexes = []
2729
trusted_hosts = []
2830
# Find and add extra indexes.
@@ -56,9 +58,22 @@ def import_requirements(project, r=None, dev=False):
5658
package_string = str(package.link._url)
5759
else:
5860
package_string = str(package.link)
59-
project.add_package_to_pipfile(package_string, dev=dev)
61+
if categories:
62+
for category in categories:
63+
project.add_package_to_pipfile(
64+
package_string, dev=dev, category=category
65+
)
66+
else:
67+
project.add_package_to_pipfile(package_string, dev=dev)
6068
else:
61-
project.add_package_to_pipfile(str(package.req), dev=dev)
69+
if categories:
70+
for category in categories:
71+
project.add_package_to_pipfile(
72+
str(package.req), dev=dev, category=category
73+
)
74+
else:
75+
project.add_package_to_pipfile(str(package.req), dev=dev)
76+
6277
for index in indexes:
6378
add_index_to_pipfile(project, index, trusted_hosts)
6479
project.recase_pipfile()

tests/integration/test_install_categories.py

+47-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,29 @@ def test_basic_category_install(pipenv_instance_private_pypi):
1818

1919
@pytest.mark.categories
2020
@pytest.mark.install
21-
@pytest.mark.parametrize('categories', ["prereq other", "prereq, other"])
21+
@pytest.mark.requirements
22+
def test_basic_category_install_from_requirements(pipenv_instance_private_pypi):
23+
with pipenv_instance_private_pypi(pipfile=False) as p:
24+
# Write a requirements file
25+
with open("requirements.txt", "w") as f:
26+
f.write(f"six==1.16.0")
27+
28+
c = p.pipenv("install --categories prereq")
29+
assert c.returncode == 0
30+
os.unlink("requirements.txt")
31+
print(c.stdout)
32+
print(c.stderr)
33+
# assert stuff in pipfile
34+
assert c.returncode == 0
35+
assert "six" not in p.pipfile["packages"]
36+
assert "six" not in p.lockfile["default"]
37+
assert "six" in p.pipfile["prereq"]
38+
assert "six" in p.lockfile["prereq"]
39+
40+
41+
@pytest.mark.categories
42+
@pytest.mark.install
43+
@pytest.mark.parametrize("categories", ["prereq other", "prereq, other"])
2244
def test_multiple_category_install(pipenv_instance_private_pypi, categories):
2345
with pipenv_instance_private_pypi() as p:
2446
c = p.pipenv('install six --categories="prereq other"')
@@ -31,6 +53,30 @@ def test_multiple_category_install(pipenv_instance_private_pypi, categories):
3153
assert "six" in p.lockfile["other"]
3254

3355

56+
@pytest.mark.categories
57+
@pytest.mark.install
58+
@pytest.mark.requirements
59+
def test_multiple_category_install_from_requirements(pipenv_instance_private_pypi):
60+
with pipenv_instance_private_pypi(pipfile=False) as p:
61+
# Write a requirements file
62+
with open("requirements.txt", "w") as f:
63+
f.write("six==1.16.0")
64+
65+
c = p.pipenv('install --categories="prereq other"')
66+
assert c.returncode == 0
67+
os.unlink("requirements.txt")
68+
print(c.stdout)
69+
print(c.stderr)
70+
# assert stuff in pipfile
71+
assert c.returncode == 0
72+
assert "six" not in p.pipfile["packages"]
73+
assert "six" not in p.lockfile["default"]
74+
assert "six" in p.pipfile["prereq"]
75+
assert "six" in p.lockfile["prereq"]
76+
assert "six" in p.pipfile["other"]
77+
assert "six" in p.lockfile["other"]
78+
79+
3480
@pytest.mark.extras
3581
@pytest.mark.install
3682
@pytest.mark.local

0 commit comments

Comments
 (0)