Skip to content

Commit

Permalink
Apply version constraint specified during package upgrade with -P
Browse files Browse the repository at this point in the history
  • Loading branch information
richafrank committed Nov 18, 2018
1 parent a1c0ed5 commit bc1d8b8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ To update a specific package to the latest or a specific version use the
$ pip-compile --upgrade-package flask # only update the flask package
$ pip-compile --upgrade-package flask --upgrade-package requests # update both the flask and requests packages
$ pip-compile -P flask -P requests # same as above, but shorter
$ pip-compile -P flask -P requests==2.0.0 # update the flask package to the latest, and requests to v2.0.0
If you use multiple Python versions, you can run ``pip-compile`` as
``py -X.Y -m piptools compile ...`` on Windows and
Expand Down
9 changes: 7 additions & 2 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,18 @@ def cli(verbose, dry_run, pre, rebuild, find_links, index_url, extra_index_url,
session = pip_command._build_session(pip_options)
repository = PyPIRepository(pip_options, session)

upgrade_install_reqs = {}
# Proxy with a LocalRequirementsRepository if --upgrade is not specified
# (= default invocation)
if not upgrade and os.path.exists(dst_file):
ireqs = parse_requirements(dst_file, finder=repository.finder, session=repository.session, options=pip_options)
# Exclude packages from --upgrade-package/-P from the existing pins: We want to upgrade.
upgrade_pkgs_key = {key_from_req(install_req_from_line(pkg).req) for pkg in upgrade_packages}
upgrade_reqs_gen = (install_req_from_line(pkg) for pkg in upgrade_packages)
upgrade_install_reqs = {key_from_req(install_req.req): install_req for install_req in upgrade_reqs_gen}

existing_pins = {key_from_req(ireq.req): ireq
for ireq in ireqs
if is_pinned_requirement(ireq) and key_from_req(ireq.req) not in upgrade_pkgs_key}
if is_pinned_requirement(ireq) and key_from_req(ireq.req) not in upgrade_install_reqs}
repository = LocalRequirementsRepository(existing_pins, repository)

log.debug('Using indexes:')
Expand Down Expand Up @@ -178,6 +181,8 @@ def cli(verbose, dry_run, pre, rebuild, find_links, index_url, extra_index_url,
constraints.extend(parse_requirements(
src_file, finder=repository.finder, session=repository.session, options=pip_options))

constraints.extend(upgrade_install_reqs.values())

# Filter out pip environment markers which do not match (PEP496)
constraints = [req for req in constraints
if req.markers is None or req.markers.evaluate()]
Expand Down
24 changes: 23 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,29 @@ def test_upgrade_packages_option(tmpdir):
req_in.write('small-fake-a==0.1\nsmall-fake-b==0.1')

out = runner.invoke(cli, [
'-P', 'small_fake_b',
'-P', 'small-fake-b',
'-f', fake_package_dir,
])

assert out.exit_code == 0
assert 'small-fake-a==0.1' in out.output
assert 'small-fake-b==0.3' in out.output


def test_upgrade_packages_version_option(tmpdir):
"""
piptools respects --upgrade-package/-P inline list with specified versions.
"""
fake_package_dir = os.path.join(os.path.split(__file__)[0], 'test_data', 'minimal_wheels')
runner = CliRunner()
with runner.isolated_filesystem():
with open('requirements.in', 'w') as req_in:
req_in.write('small-fake-a\nsmall-fake-b')
with open('requirements.txt', 'w') as req_in:
req_in.write('small-fake-a==0.1\nsmall-fake-b==0.1')

out = runner.invoke(cli, [
'-P', 'small-fake-b==0.2',
'-f', fake_package_dir,
])

Expand Down
Binary file not shown.

0 comments on commit bc1d8b8

Please sign in to comment.