Skip to content

Commit

Permalink
Additional progress on #2075
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobHenner committed Jun 5, 2018
1 parent 299baa7 commit 787a466
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 21 deletions.
6 changes: 6 additions & 0 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ If you'd like to override the default PyPI index urls with the url for a PyPI mi

$ pipenv update --pypi-mirror <mirror_url>

$ pipenv sync --pypi-mirror <mirror_url>

$ pipenv lock --pypi-mirror <mirror_url>

$ pipenv uninstall --pypi-mirror <mirror_url>

Alternatively, you can set the ``PIPENV_PYPI_MIRROR`` environment variable.

☤ Injecting credentials into Pipfiles via environment variables
Expand Down
33 changes: 26 additions & 7 deletions pipenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ def install(
dev=False,
three=False,
python=False,
pypi_mirror=False,
pypi_mirror=None,
system=False,
lock=True,
ignore_pipfile=False,
Expand Down Expand Up @@ -465,6 +465,13 @@ def install(
default=False,
help=u"Keep out–dated dependencies from being updated in Pipfile.lock.",
)
@option(
'--pypi-mirror',
default=PIPENV_PYPI_MIRROR,
nargs=1,
callback=validate_pypi_mirror,
help="Specify a PyPI mirror.",
)
def uninstall(
package_name=False,
more_packages=False,
Expand All @@ -476,6 +483,7 @@ def uninstall(
all=False,
verbose=False,
keep_outdated=False,
pypi_mirror=None,
):
from .core import do_uninstall

Expand All @@ -490,6 +498,7 @@ def uninstall(
all=all,
verbose=verbose,
keep_outdated=keep_outdated,
pypi_mirror=pypi_mirror,
)


Expand Down Expand Up @@ -551,7 +560,7 @@ def uninstall(
def lock(
three=None,
python=False,
pypi_mirror=False,
pypi_mirror=None,
verbose=False,
requirements=False,
dev=False,
Expand All @@ -566,7 +575,7 @@ def lock(
if requirements:
do_init(dev=dev, requirements=requirements, pypi_mirror=pypi_mirror)
do_lock(
verbose=verbose, clear=clear, pre=pre, keep_outdated=keep_outdated
verbose=verbose, clear=clear, pre=pre, keep_outdated=keep_outdated, pypi_mirror=pypi_mirror
)


Expand Down Expand Up @@ -775,7 +784,7 @@ def update(
ctx,
three=None,
python=False,
pypi_mirror=False,
pypi_mirror=None,
system=False,
verbose=False,
clear=False,
Expand Down Expand Up @@ -803,7 +812,7 @@ def update(
if not outdated:
outdated = bool(dry_run)
if outdated:
do_outdated()
do_outdated(pypi_mirror=pypi_mirror)
if not package:
echo(
'{0} {1} {2} {3}{4}'.format(
Expand All @@ -815,7 +824,7 @@ def update(
)
)
do_lock(
verbose=verbose, clear=clear, pre=pre, keep_outdated=keep_outdated
verbose=verbose, clear=clear, pre=pre, keep_outdated=keep_outdated, pypi_mirror=pypi_mirror
)
do_sync(
ctx=ctx,
Expand All @@ -830,6 +839,7 @@ def update(
clear=clear,
unused=False,
sequential=sequential,
pypi_mirror=pypi_mirror,
)
else:
for package in ([package] + list(more_packages) or []):
Expand All @@ -843,7 +853,7 @@ def update(
err=True,
)
sys.exit(1)
ensure_lockfile(keep_outdated=project.lockfile_exists)
ensure_lockfile(keep_outdated=project.lockfile_exists, pypi_mirror=pypi_mirror)
# Install the dependencies.
do_install(
package_name=package,
Expand Down Expand Up @@ -952,6 +962,13 @@ def run_open(module, three=None, python=None):
callback=validate_python_path,
help="Specify which version of Python virtualenv should use.",
)
@option(
'--pypi-mirror',
default=PIPENV_PYPI_MIRROR,
nargs=1,
callback=validate_pypi_mirror,
help="Specify a PyPI mirror.",
)
@option('--bare', is_flag=True, default=False, help="Minimal output.")
@option(
'--clear', is_flag=True, default=False, help="Clear the dependency cache."
Expand All @@ -976,6 +993,7 @@ def sync(
unused=False,
package_name=None,
sequential=False,
pypi_mirror=None,
):
from .core import do_sync

Expand All @@ -992,6 +1010,7 @@ def sync(
clear=clear,
unused=unused,
sequential=sequential,
pypi_mirror=pypi_mirror,
)


Expand Down
33 changes: 21 additions & 12 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,7 @@ def do_lock(
pre=False,
keep_outdated=False,
write=True,
pypi_mirror = None,
):
"""Executes the freeze functionality."""
from .utils import get_vcs_deps
Expand Down Expand Up @@ -1059,6 +1060,7 @@ def do_lock(
clear=clear,
pre=pre,
allow_global=system,
pypi_mirror=pypi_mirror
)
# Add develop dependencies to lockfile.
for dep in results:
Expand Down Expand Up @@ -1109,6 +1111,7 @@ def do_lock(
clear=False,
pre=pre,
allow_global=system,
pypi_mirror=pypi_mirror,
)
# Add default dependencies to lockfile.
for dep in results:
Expand Down Expand Up @@ -1284,7 +1287,7 @@ def do_init(
pre=False,
keep_outdated=False,
requirements_dir=None,
pypi_mirror=False,
pypi_mirror=None,
):
"""Executes the init functionality."""
if not system:
Expand Down Expand Up @@ -1342,7 +1345,7 @@ def do_init(
),
err=True,
)
do_lock(system=system, pre=pre, keep_outdated=keep_outdated)
do_lock(system=system, pre=pre, keep_outdated=keep_outdated, pypi_mirror=pypi_mirror)
# Write out the lockfile if it doesn't exist.
if not project.lockfile_exists and not skip_lock:
# Unless we're in a virtualenv not managed by pipenv, abort if we're
Expand All @@ -1368,6 +1371,7 @@ def do_init(
pre=pre,
keep_outdated=keep_outdated,
verbose=verbose,
pypi_mirror=pypi_mirror,
)
do_install_dependencies(
dev=dev,
Expand Down Expand Up @@ -1693,7 +1697,7 @@ def warn_in_virtualenv():
)


def ensure_lockfile(keep_outdated=False):
def ensure_lockfile(keep_outdated=False, pypi_mirror=None):
"""Ensures that the lockfile is up–to–date."""
if not keep_outdated:
keep_outdated = project.settings.get('keep_outdated')
Expand All @@ -1711,9 +1715,9 @@ def ensure_lockfile(keep_outdated=False):
),
err=True,
)
do_lock(keep_outdated=keep_outdated)
do_lock(keep_outdated=keep_outdated, pypi_mirror=pypi_mirror)
else:
do_lock(keep_outdated=keep_outdated)
do_lock(keep_outdated=keep_outdated, pypi_mirror=pypi_mirror)


def do_py(system=False):
Expand All @@ -1723,7 +1727,7 @@ def do_py(system=False):
click.echo(crayons.red('No project found!'))


def do_outdated():
def do_outdated(pypi_mirror=None):
packages = {}
results = delegator.run('{0} freeze'.format(which('pip'))).out.strip(
).split(
Expand All @@ -1734,7 +1738,7 @@ def do_outdated():
dep = Requirement.from_line(result)
packages.update(dep.as_pipfile())
updated_packages = {}
lockfile = do_lock(write=False)
lockfile = do_lock(write=False, pypi_mirror=pypi_mirror)
for section in ('develop', 'default'):
for package in lockfile[section]:
try:
Expand Down Expand Up @@ -1766,7 +1770,7 @@ def do_install(
dev=False,
three=False,
python=False,
pypi_mirror=False,
pypi_mirror=None,
system=False,
lock=True,
ignore_pipfile=False,
Expand Down Expand Up @@ -1918,7 +1922,7 @@ def do_install(
# Capture . argument and assign it to nothing
if package_name == '.':
package_name = False
# Install editable local packages before locking - this givves us acceess to dist-info
# Install editable local packages before locking - this gives us access to dist-info
if project.pipfile_exists and (
not project.lockfile_exists or not project.virtualenv_exists
):
Expand Down Expand Up @@ -1950,6 +1954,7 @@ def do_install(
deploy=deploy,
pre=pre,
requirements_dir=requirements_directory,
pypi_mirror=pypi_mirror,
)
requirements_directory.cleanup()
sys.exit(0)
Expand Down Expand Up @@ -2070,6 +2075,7 @@ def do_install(
keep_outdated=keep_outdated,
requirements_dir=requirements_directory,
deploy=deploy,
pypi_mirror=pypi_mirror,
)
requirements_directory.cleanup()

Expand All @@ -2085,6 +2091,7 @@ def do_uninstall(
all=False,
verbose=False,
keep_outdated=False,
pypi_mirror=None,
):
# Automatically use an activated virtualenv.
if PIPENV_USE_SYSTEM:
Expand Down Expand Up @@ -2157,7 +2164,7 @@ def do_uninstall(
project.remove_package_from_pipfile(package_name, dev=True)
project.remove_package_from_pipfile(package_name, dev=False)
if lock:
do_lock(system=system, keep_outdated=keep_outdated)
do_lock(system=system, keep_outdated=keep_outdated, pypi_mirror=pypi_mirror)


def do_shell(three=None, python=False, fancy=False, shell_args=None):
Expand Down Expand Up @@ -2556,6 +2563,7 @@ def do_sync(
clear=False,
unused=False,
sequential=False,
pypi_mirror=None,
):
# The lock file needs to exist because sync won't write to it.
if not project.lockfile_exists:
Expand All @@ -2581,17 +2589,18 @@ def do_sync(
concurrent=(not sequential),
requirements_dir=requirements_dir,
ignore_pipfile=True, # Don't check if Pipfile and lock match.
pypi_mirror=pypi_mirror,
)
requirements_dir.cleanup()
click.echo(crayons.green('All dependencies are now up-to-date!'))


def do_clean(
ctx, three=None, python=None, dry_run=False, bare=False, verbose=False
ctx, three=None, python=None, dry_run=False, bare=False, verbose=False, pypi_mirror=None
):
# Ensure that virtualenv is available.
ensure_project(three=three, python=python, validate=False)
ensure_lockfile()
ensure_lockfile(pypi_mirror=pypi_mirror)

installed_package_names = []
pip_freeze_command = delegator.run('{0} freeze'.format(which_pip()))
Expand Down
3 changes: 2 additions & 1 deletion pipenv/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def main():
for i, package in enumerate(packages):
if package.startswith('--'):
del packages[i]
pypi_mirror_source = pipenv.utils.create_mirror_source(os.environ['PIPENV_PYPI_MIRROR']) if 'PIPENV_PYPI_MIRROR' in os.environ else None
project = pipenv.core.project

def resolve(packages, pre, sources, verbose, clear, system):
Expand All @@ -66,7 +67,7 @@ def resolve(packages, pre, sources, verbose, clear, system):
results = resolve(
packages,
pre=do_pre,
sources=project.pipfile_sources,
sources = pipenv.utils.replace_pypi_sources(project.pipfile_sources, pypi_mirror_source) if pypi_mirror_source else project.pipfile_sources,
verbose=is_verbose,
clear=do_clear,
system=system,
Expand Down
7 changes: 6 additions & 1 deletion pipenv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ class PipCommand(basecommand.Command):
pypi = PyPIRepository(
pip_options=pip_options, use_json=True, session=session
)
print(pip_options)
if verbose:
logging.log.verbose = True
piptools_logging.log.verbose = True
Expand Down Expand Up @@ -325,7 +326,7 @@ class PipCommand(basecommand.Command):


def venv_resolve_deps(
deps, which, project, pre=False, verbose=False, clear=False, allow_global=False
deps, which, project, pre=False, verbose=False, clear=False, allow_global=False, pypi_mirror=None
):
from .vendor import delegator
from . import resolver
Expand All @@ -343,6 +344,8 @@ def venv_resolve_deps(
)
with temp_environ():
os.environ['PIPENV_PACKAGES'] = '\n'.join(deps)
if pypi_mirror:
os.environ['PIPENV_PYPI_MIRROR'] = str(pypi_mirror)
c = delegator.run(cmd, block=True)
try:
assert c.return_code == 0
Expand Down Expand Up @@ -926,6 +929,8 @@ def is_valid_url(url):
def is_pypi_url(url):
return bool(re.match(r'^http[s]?:\/\/pypi(?:\.python)?\.org\/simple[\/]?$', url))

def replace_pypi_sources(sources, pypi_replacement_source):
return [pypi_replacement_source] + [source for source in sources if not is_pypi_url(source['url'])]

def create_mirror_source(url):
return {'url': url, 'verify_ssl': url.startswith('https://'), 'name': urlparse(url).hostname}
Expand Down

0 comments on commit 787a466

Please sign in to comment.