Skip to content

Commit 457bd52

Browse files
matteiusoz123
authored andcommitted
More ruff fixing and convert install routines to rich console
1 parent 816502e commit 457bd52

File tree

2 files changed

+41
-86
lines changed

2 files changed

+41
-86
lines changed

pipenv/routines/install.py

Lines changed: 37 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77

88
from pipenv import environments, exceptions
99
from pipenv.patched.pip._internal.exceptions import PipError
10-
from pipenv.patched.pip._vendor import rich
1110
from pipenv.routines.lock import do_lock
12-
from pipenv.utils import fileutils
11+
from pipenv.utils import console, err, fileutils
1312
from pipenv.utils.dependencies import (
1413
expansive_install_req_from_line,
1514
get_lockfile_section_using_pipfile_category,
@@ -25,10 +24,6 @@
2524
from pipenv.utils.requirements import add_index_to_pipfile, import_requirements
2625
from pipenv.utils.shell import temp_environ
2726
from pipenv.utils.virtualenv import cleanup_virtualenv, do_create_virtualenv
28-
from pipenv.vendor import click
29-
30-
console = rich.console.Console()
31-
err = rich.console.Console(stderr=True)
3227

3328

3429
def do_install(
@@ -100,10 +95,9 @@ def do_install(
10095
os.environ["PIPENV_USE_SYSTEM"] = "1"
10196
# Check if the file is remote or not
10297
if remote:
103-
click.secho(
98+
err.print(
10499
"Remote requirements file provided! Downloading...",
105-
bold=True,
106-
err=True,
100+
style="bold",
107101
)
108102
fd = NamedTemporaryFile(
109103
prefix="pipenv-", suffix="-requirement.txt", dir=requirements_directory
@@ -116,10 +110,9 @@ def do_install(
116110
except OSError:
117111
fd.close()
118112
os.unlink(temp_reqs)
119-
click.secho(
113+
err.print(
120114
f"Unable to find requirements file at {requirements_url}.",
121-
fg="red",
122-
err=True,
115+
style="red",
123116
)
124117
sys.exit(1)
125118
finally:
@@ -128,10 +121,9 @@ def do_install(
128121
requirementstxt = temp_reqs
129122
if requirementstxt:
130123
error, traceback = None, None
131-
click.secho(
124+
err.print(
132125
"Requirements file provided! Importing into Pipfile...",
133-
bold=True,
134-
err=True,
126+
style="bold",
135127
)
136128
try:
137129
import_requirements(
@@ -143,10 +135,7 @@ def do_install(
143135
except (UnicodeDecodeError, PipError) as e:
144136
# Don't print the temp file path if remote since it will be deleted.
145137
req_path = project.path_to(requirementstxt)
146-
error = (
147-
"Unexpected syntax in {}. Are you sure this is a "
148-
"requirements.txt style file?".format(req_path)
149-
)
138+
error = f"Unexpected syntax in {req_path}. Are you sure this is a requirements.txt style file?"
150139
traceback = e
151140
except AssertionError as e:
152141
error = (
@@ -156,8 +145,8 @@ def do_install(
156145
traceback = e
157146
finally:
158147
if error and traceback:
159-
click.secho(error, fg="red")
160-
click.secho(str(traceback), fg="yellow", err=True)
148+
console.print(error, sytle="red")
149+
err.print(str(traceback), style="yellow")
161150
sys.exit(1)
162151

163152
# Allow more than one package to be provided.
@@ -202,10 +191,9 @@ def do_install(
202191
)
203192

204193
for pkg_line in pkg_list:
205-
click.secho(
194+
console.print(
206195
f"Installing {pkg_line}...",
207-
fg="green",
208-
bold=True,
196+
style="bold green",
209197
)
210198
# pip install:
211199
with temp_environ(), console.status(
@@ -221,30 +209,14 @@ def do_install(
221209
pkg_line, expand_env=True
222210
)
223211
except ValueError as e:
224-
err.print("{}: {}".format(click.style("WARNING", fg="red"), e))
212+
err.print(f"[red]WARNING[/red]: {e}")
225213
err.print(
226214
environments.PIPENV_SPINNER_FAIL_TEXT.format(
227215
"Installation Failed"
228216
)
229217
)
230218
sys.exit(1)
231219
st.update(f"Installing {pkg_requirement.name}...")
232-
# Warn if --editable wasn't passed.
233-
if (
234-
pkg_requirement.link
235-
and pkg_requirement.link.is_vcs
236-
and not pkg_requirement.editable
237-
and not project.s.PIPENV_RESOLVE_VCS
238-
):
239-
err.print(
240-
"{}: You installed a VCS dependency in non-editable mode. "
241-
"This will work fine, but sub-dependencies will not be resolved by {}."
242-
"\n To enable this sub-dependency functionality, specify that this dependency is editable."
243-
"".format(
244-
click.style("Warning", fg="red", bold=True),
245-
click.style("$ pipenv lock", fg="yellow"),
246-
)
247-
)
248220
if categories:
249221
pipfile_sections = ""
250222
for c in categories:
@@ -288,12 +260,7 @@ def do_install(
288260
except ValueError:
289261
import traceback
290262

291-
err.print(
292-
"{} {}".format(
293-
click.style("Error:", fg="red", bold=True),
294-
traceback.format_exc(),
295-
)
296-
)
263+
err.print(f"[bold][red]Error:[/red][/bold] {traceback.format_exc()}")
297264
err.print(
298265
environments.PIPENV_SPINNER_FAIL_TEXT.format(
299266
"Failed adding package to Pipfile"
@@ -377,7 +344,7 @@ def do_sync(
377344
categories=categories,
378345
)
379346
if not bare:
380-
click.echo(click.style("All dependencies are now up-to-date!", fg="green"))
347+
console.print("[green]All dependencies are now up-to-date![/green]")
381348

382349

383350
def do_install_dependencies(
@@ -408,11 +375,10 @@ def do_install_dependencies(
408375
for category in categories:
409376
lockfile = project.get_or_create_lockfile(categories=categories)
410377
if not bare:
411-
click.secho(
412-
"Installing dependencies from Pipfile.lock ({})...".format(
413-
lockfile["_meta"].get("hash", {}).get("sha256")[-6:]
414-
),
415-
bold=True,
378+
console.print(
379+
f"Installing dependencies from Pipfile.lock "
380+
f"({lockfile['_meta'].get('hash', {}).get('sha256')[-6:]})...",
381+
style="bold",
416382
)
417383
dev = dev or dev_only
418384
deps_list = list(
@@ -552,10 +518,9 @@ def batch_install(
552518
extra_pip_args=extra_pip_args,
553519
)
554520
except StopIteration:
555-
click.secho(
521+
console.print(
556522
f"Unable to find {index_name} in sources, please check dependencies: {dependencies}",
557-
fg="red",
558-
bold=True,
523+
style="bold red",
559524
)
560525
sys.exit(1)
561526

@@ -569,7 +534,7 @@ def _cleanup_procs(project, procs):
569534
out, err = c.stdout, c.stderr
570535
failed = c.returncode != 0
571536
if project.s.is_verbose():
572-
click.secho(out.strip() or err.strip(), fg="yellow")
537+
console.print(out.strip() or err.strip(), style="yellow")
573538
# The Installation failed...
574539
if failed:
575540
# The Installation failed...
@@ -625,31 +590,26 @@ def do_init(
625590
new_hash = project.calculate_pipfile_hash()
626591
if new_hash != old_hash:
627592
if deploy:
628-
click.secho(
629-
"Your Pipfile.lock (old_hash[-6:]) is out of date."
630-
" Expected: ({new_hash[-6:]}).",
631-
fg="red",
593+
console.print(
594+
f"Your Pipfile.lock ({old_hash[-6:]}) is out of date. Expected: ({new_hash[-6:]}).",
595+
style="red",
632596
)
633597
raise exceptions.DeployException
634598
if (system or allow_global) and not (project.s.PIPENV_VIRTUALENV):
635-
click.secho(
636-
"Pipfile.lock ({}) out of date, but installation "
637-
"uses {} re-building lockfile must happen in "
638-
"isolation. Please rebuild lockfile in a virtualenv. "
639-
"Continuing anyway...".format(old_hash[-6:], "--system"),
640-
fg="yellow",
641-
err=True,
599+
err.print(
600+
f"Pipfile.lock ({old_hash[-6:]}) out of date, but installation uses --system so"
601+
f"re-building lockfile must happen in isolation."
602+
f" Please rebuild lockfile in a virtualenv. Continuing anyway...",
603+
style="yellow",
642604
)
643605
else:
644606
if old_hash:
645607
msg = "Pipfile.lock ({0}) out of date, updating to ({1})..."
646608
else:
647609
msg = "Pipfile.lock is corrupt, replaced with ({1})..."
648-
click.secho(
610+
err.print(
649611
msg.format(old_hash[-6:], new_hash[-6:]),
650-
fg="yellow",
651-
bold=True,
652-
err=True,
612+
style="bold yellow",
653613
)
654614
do_lock(
655615
project,
@@ -671,10 +631,9 @@ def do_init(
671631
"See also: --deploy flag.",
672632
)
673633
else:
674-
click.secho(
634+
err.print(
675635
"Pipfile.lock not found, creating...",
676-
bold=True,
677-
err=True,
636+
style="bold",
678637
)
679638
do_lock(
680639
project,
@@ -697,11 +656,7 @@ def do_init(
697656

698657
# Hint the user what to do to activate the virtualenv.
699658
if not allow_global and not deploy and "PIPENV_ACTIVE" not in os.environ:
700-
click.echo(
701-
"To activate this project's virtualenv, run {}.\n"
702-
"Alternatively, run a command "
703-
"inside the virtualenv with {}.".format(
704-
click.style("pipenv shell", fg="yellow"),
705-
click.style("pipenv run", fg="yellow"),
706-
)
659+
console.print(
660+
"To activate this project's virtualenv, run [yellow]pipenv shell[/yellow].\n"
661+
"Alternatively, run a command inside the virtualenv with [yellow]pipenv run[/yellow]."
707662
)

tests/integration/test_install_twists.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ class TestDirectDependencies:
7070
def helper_dependency_links_install_make_setup(pipenv_instance, deplink):
7171
setup_py = os.path.join(pipenv_instance.path, "setup.py")
7272
with open(setup_py, "w") as fh:
73-
contents = """
73+
contents = f"""
7474
from setuptools import setup
7575
7676
setup(
7777
name='testdeplinks',
7878
version='0.1',
7979
packages=[],
8080
install_requires=[
81-
'{0}'
81+
'{deplink}'
8282
],
8383
)
84-
""".strip().format(deplink)
84+
""".strip()
8585
fh.write(contents)
8686

8787
@staticmethod
@@ -251,7 +251,7 @@ def test_multiple_editable_packages_should_not_race(pipenv_instance_private_pypi
251251
source_path = p._pipfile.get_fixture_path(f"git/{pkg_name}/").as_posix()
252252
shutil.copytree(source_path, pkg_name)
253253

254-
pipfile_string += '"{0}" = {{path = "./{0}", editable = true}}\n'.format(pkg_name)
254+
pipfile_string += f'"{pkg_name}" = {{path = "./{pkg_name}", editable = true}}\n'
255255

256256
with open(p.pipfile_path, 'w') as f:
257257
f.write(pipfile_string.strip())

0 commit comments

Comments
 (0)