From ecec1ac0f9352af002e5790ae4fc4fc82a9a2a59 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Tue, 6 Nov 2018 14:14:55 +0900 Subject: [PATCH] Just format --- pipenv/cli/command.py | 18 ++++++---- pipenv/core.py | 69 +++++++++++++---------------------- pipenv/utils.py | 84 +++++++++++++++++++++++++++++-------------- 3 files changed, 94 insertions(+), 77 deletions(-) diff --git a/pipenv/cli/command.py b/pipenv/cli/command.py index 74f66d7e..74c57720 100644 --- a/pipenv/cli/command.py +++ b/pipenv/cli/command.py @@ -98,12 +98,12 @@ def cli( warn_in_virtualenv, do_where, project, - spinner, cleanup_virtualenv, ensure_project, format_help, do_clear, ) + from ..utils import create_spinner if man: if system_which("man"): @@ -179,7 +179,7 @@ def cli( ) ) ) - with spinner(): + with create_spinner(text="Running..."): # Remove the virtualenv. cleanup_virtualenv(bare=True) return 0 @@ -312,8 +312,12 @@ def lock( # Ensure that virtualenv is available. ensure_project(three=state.three, python=state.python, pypi_mirror=state.pypi_mirror) if state.installstate.requirementstxt: - do_init(dev=state.installstate.dev, requirements=state.installstate.requirementstxt, - pypi_mirror=state.pypi_mirror, pre=state.installstate.pre) + do_init( + dev=state.installstate.dev, + requirements=state.installstate.requirementstxt, + pypi_mirror=state.pypi_mirror, + pre=state.installstate.pre, + ) do_lock( ctx=ctx, clear=state.clear, @@ -549,8 +553,10 @@ def run_open(state, module, *args, **kwargs): from ..core import which, ensure_project # Ensure that virtualenv is available. - ensure_project(three=state.three, python=state.python, validate=False, - pypi_mirror=state.pypi_mirror) + ensure_project( + three=state.three, python=state.python, + validate=False, pypi_mirror=state.pypi_mirror, + ) c = delegator.run( '{0} -c "import {1}; print({1}.__file__);"'.format(which("python"), module) ) diff --git a/pipenv/core.py b/pipenv/core.py index 2091bd08..d0136671 100644 --- a/pipenv/core.py +++ b/pipenv/core.py @@ -1,6 +1,5 @@ # -*- coding=utf-8 -*- -import contextlib import logging import os import sys @@ -18,7 +17,6 @@ import warnings import six import urllib3.util as urllib3_util -from functools import partial from .cmdparse import Script from .project import Project, SourceNotFound @@ -42,7 +40,8 @@ from .utils import ( clean_resolved_dep, parse_indexes, escape_cmd, - fix_venv_site + fix_venv_site, + create_spinner, ) from . import environments, pep508checker, progress from .environments import ( @@ -101,26 +100,6 @@ if PIPENV_COLORBLIND: crayons.disable() -@contextlib.contextmanager -def _spinner(text=None, nospin=None, spinner_name=None): - if not text: - text = "Running..." - if not spinner_name: - spinner_name = environments.PIPENV_SPINNER - if nospin is None: - nospin = environments.PIPENV_NOSPIN - with vistir.spin.create_spinner( - spinner_name=spinner_name, - start_text=text, - nospin=nospin - ) as sp: - yield sp - - -spinner = partial(_spinner, text="Running...", nospin=environments.PIPENV_NOSPIN, - spinner_name=environments.PIPENV_SPINNER) - - def which(command, location=None, allow_global=False): if not allow_global and location is None: if project.virtualenv_exists: @@ -300,7 +279,8 @@ def ensure_pipfile(validate=True, skip_requirements=False, system=False): if project.pipfile_is_empty: # Show an error message and exit if system is passed and no pipfile exists if system and not PIPENV_VIRTUALENV: - raise exceptions.PipenvOptionsError("--system", + raise exceptions.PipenvOptionsError( + "--system", "--system is intended to be used for pre-existing Pipfile " "installation, not installation of specific packages. Aborting." ) @@ -314,9 +294,7 @@ def ensure_pipfile(validate=True, skip_requirements=False, system=False): ) # Create a Pipfile… project.create_pipfile(python=python) - with spinner(text=vistir.compat.fs_str("Importing requirements..."), - spinner_name=environments.PIPENV_SPINNER, - nospin=environments.PIPENV_NOSPIN) as sp: + with create_spinner("Importing requirements...") as sp: # Import requirements.txt. try: import_requirements() @@ -466,9 +444,7 @@ def ensure_python(three=None, python=None): crayons.normal(fix_utf8("…"), bold=True), ) ) - with spinner(text=vistir.compat.fs_str("Installing python..."), - spinner_name=environments.PIPENV_SPINNER, - nospin=environments.PIPENV_NOSPIN) as sp: + with create_spinner("Installing python...") as sp: try: c = pyenv.install(version) except PyenvError as e: @@ -762,8 +738,10 @@ def do_install_dependencies( procs = queue.Queue(maxsize=PIPENV_MAX_SUBPROCESS) trusted_hosts = [] - deps_list_bar = progress.bar(deps_list, width=32, - label=INSTALL_LABEL if os.name != "nt" else "") + deps_list_bar = progress.bar( + deps_list, width=32, + label=INSTALL_LABEL if os.name != "nt" else "", + ) indexes = [] for dep in deps_list_bar: index = None @@ -921,9 +899,11 @@ def do_create_virtualenv(python=None, site_packages=False, pypi_mirror=None): # Actually create the virtualenv. nospin = environments.PIPENV_NOSPIN - c = vistir.misc.run(cmd, verbose=False, return_object=True, - spinner_name=environments.PIPENV_SPINNER, combine_stderr=False, - block=False, nospin=nospin, env=pip_config) + c = vistir.misc.run( + cmd, verbose=False, return_object=True, + spinner_name=environments.PIPENV_SPINNER, combine_stderr=False, + block=False, nospin=nospin, env=pip_config, + ) click.echo(crayons.blue("{0}".format(c.out)), err=True) if c.returncode != 0: raise exceptions.VirtualenvCreationException( @@ -1253,7 +1233,8 @@ def do_init( # Unless we're in a virtualenv not managed by pipenv, abort if we're # using the system's python. if (system or allow_global) and not (PIPENV_VIRTUALENV): - raise exceptions.PipenvOptionsError("--system", + raise exceptions.PipenvOptionsError( + "--system", "--system is intended to be used for Pipfile installation, " "not installation of specific packages. Aborting.\n" "See also: --deploy flag." @@ -1940,9 +1921,8 @@ def do_install( ) ) # pip install: - with vistir.contextmanagers.temp_environ(), spinner(text="Installing...", - spinner_name=environments.PIPENV_SPINNER, - nospin=environments.PIPENV_NOSPIN) as sp: + with vistir.contextmanagers.temp_environ(), \ + create_spinner("Installing...") as sp: os.environ["PIP_USER"] = vistir.compat.fs_str("0") try: pkg_requirement = Requirement.from_line(pkg_line) @@ -2040,7 +2020,6 @@ def do_uninstall( ): from .environments import PIPENV_USE_SYSTEM from .vendor.requirementslib.models.requirements import Requirement - from .vendor.requirementslib.models.lockfile import Lockfile from .vendor.packaging.utils import canonicalize_name # Automatically use an activated virtualenv. @@ -2052,8 +2031,10 @@ def do_uninstall( ensure_project(three=three, python=python, pypi_mirror=pypi_mirror) # Un-install all dependencies, if --all was provided. if not any([packages, editable_packages, all_dev, all]): - raise exceptions.MissingParameter(crayons.red("No package provided!"), ctx=ctx, - param_type="parameter") + raise exceptions.MissingParameter( + crayons.red("No package provided!"), + ctx=ctx, param_type="parameter", + ) editable_pkgs = [ Requirement.from_line("-e {0}".format(p)).name for p in editable_packages if p ] @@ -2151,8 +2132,8 @@ def do_uninstall( # Uninstall the package. if package_name in packages_to_remove: cmd = "{0} uninstall {1} -y".format( - escape_grouped_arguments(which_pip(allow_global=system)), package_name - ) + escape_grouped_arguments(which_pip(allow_global=system)), package_name, + ) if environments.is_verbose(): click.echo("$ {0}".format(cmd)) c = delegator.run(cmd) diff --git a/pipenv/utils.py b/pipenv/utils.py index c0d5a356..c9feeafd 100644 --- a/pipenv/utils.py +++ b/pipenv/utils.py @@ -1,16 +1,17 @@ # -*- coding: utf-8 -*- +import contextlib import errno import logging import os import re import shutil +import stat import sys +import warnings import crayons import parse import six -import stat -import warnings from click import echo as click_echo from first import first @@ -363,17 +364,31 @@ class Resolver(object): def populate_file_hashes(self): from pipenv.vendor.vistir.compat import Path, to_native_string from pipenv.vendor.vistir.path import url_to_path + + def _should_include(ireq): + # We can only hash artifacts. + try: + if not ireq.link.is_artifact: + return False + except AttributeError: + return False + + # But we don't want normal pypi artifcats since the normal resolver + # handles those + if is_pypi_url(ireq.link.url): + return False + + # We also don't want to try to hash directories as this will fail + # as these are editable deps and are not hashable. + if (ireq.link.scheme == "file" and + Path(to_native_string(url_to_path(ireq.link.url))).is_dir()): + return False + return True + self.hashes.update({ ireq: self.resolver._hash_cache.get_hash(ireq.link) - for ireq in self.parsed_constraints if (getattr(ireq, "link", None) - # We can only hash artifacts, but we don't want normal pypi artifcats since the - # normal resolver handles those - and ireq.link.is_artifact and not (is_pypi_url(ireq.link.url) or ( - # We also don't want to try to hash directories as this will fail - # as these are editable deps and are not hashable - ireq.link.scheme == "file" and - Path(to_native_string(url_to_path(ireq.link.url))).is_dir() - ))) + for ireq in self.parsed_constraints + if _should_include(ireq) }) @property @@ -428,8 +443,9 @@ def actually_resolve_deps( if not req_dir: req_dir = create_tracked_tempdir(suffix="-requirements", prefix="pipenv-") - constraints = get_resolver_metadata(deps, index_lookup, markers_lookup, project, - sources) + constraints = get_resolver_metadata( + deps, index_lookup, markers_lookup, project, sources, + ) resolver = Resolver(constraints, req_dir, project, sources, clear=clear, pre=pre) resolved_tree = resolver.resolve() hashes = resolver.resolve_hashes() @@ -437,6 +453,21 @@ def actually_resolve_deps( return (resolved_tree, hashes, markers_lookup, resolver) +@contextlib.contextmanager +def create_spinner(text, nospin=None, spinner_name=None): + import vistir.spin + if not spinner_name: + spinner_name = environments.PIPENV_SPINNER + if nospin is None: + nospin = environments.PIPENV_NOSPIN + with vistir.spin.create_spinner( + spinner_name=spinner_name, + start_text=vistir.compat.fs_str(text), + nospin=nospin + ) as sp: + yield sp + + def venv_resolve_deps( deps, which, @@ -450,7 +481,6 @@ def venv_resolve_deps( from .vendor.vistir.compat import Path, to_native_string, JSONDecodeError from .vendor.vistir.path import create_tracked_tempdir from .cmdparse import Script - from .core import spinner from .vendor.pexpect.exceptions import EOF, TIMEOUT from .vendor import delegator from . import resolver @@ -481,8 +511,7 @@ def venv_resolve_deps( os.environ["PIP_NO_INPUT"] = fs_str("1") out = to_native_string("") EOF.__module__ = "pexpect.exceptions" - with spinner(text=fs_str("Locking..."), spinner_name=environments.PIPENV_SPINNER, - nospin=environments.PIPENV_NOSPIN) as sp: + with create_spinner(text=fs_str("Locking...")) as sp: c = delegator.run(Script.parse(cmd).cmdify(), block=False, env=os.environ.copy()) _out = decode_output("") result = None @@ -1221,8 +1250,6 @@ def get_vcs_deps( dev=False, pypi_mirror=None, ): - from .vendor.vistir.compat import Path - from .vendor.vistir.path import create_tracked_tempdir from .vendor.requirementslib.models.requirements import Requirement section = "vcs_dev_packages" if dev else "vcs_packages" @@ -1280,8 +1307,10 @@ def translate_markers(pipfile_entry): marker_set.add(str(Marker("{0}{1}".format(m, entry)))) new_pipfile.pop(m) if marker_set: - new_pipfile["markers"] = str(Marker(" or ".join(["{0}".format(s) if " and " in s else s - for s in sorted(dedup(marker_set))]))).replace('"', "'") + new_pipfile["markers"] = str(Marker(" or ".join( + "{0}".format(s) if " and " in s else s + for s in sorted(dedup(marker_set)) + ))).replace('"', "'") return new_pipfile @@ -1289,9 +1318,6 @@ def clean_resolved_dep(dep, is_top_level=False, pipfile_entry=None): name = pep423_name(dep["name"]) # We use this to determine if there are any markers on top level packages # So we can make sure those win out during resolution if the packages reoccur - dep_keys = ( - [k for k in getattr(pipfile_entry, "keys", list)()] if is_top_level else [] - ) lockfile = {"version": "=={0}".format(dep["version"])} for key in ["hashes", "index", "extras"]: if key in dep: @@ -1406,10 +1432,14 @@ def looks_like_dir(path): def parse_indexes(line): from argparse import ArgumentParser parser = ArgumentParser("indexes") - parser.add_argument("--index", "-i", "--index-url", metavar="index_url", - action="store", nargs="?",) - parser.add_argument("--extra-index-url", "--extra-index", metavar="extra_indexes", - action="append") + parser.add_argument( + "--index", "-i", "--index-url", + metavar="index_url", action="store", nargs="?", + ) + parser.add_argument( + "--extra-index-url", "--extra-index", + metavar="extra_indexes",action="append", + ) parser.add_argument("--trusted-host", metavar="trusted_hosts", action="append") args, remainder = parser.parse_known_args(line.split()) index = [] if not args.index else [args.index,]