Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --cache-dir option to pip-compile #1022

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions piptools/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pip._vendor.packaging.requirements import Requirement

from .exceptions import PipToolsError
from .locations import CACHE_DIR
from .utils import as_tuple, key_from_req, lookup_table


Expand Down Expand Up @@ -49,9 +48,7 @@ class DependencyCache(object):
Where X.Y indicates the Python version.
"""

def __init__(self, cache_dir=None):
if cache_dir is None:
cache_dir = CACHE_DIR
def __init__(self, cache_dir):
if not os.path.isdir(cache_dir):
os.makedirs(cache_dir)
py_version = ".".join(str(digit) for digit in sys.version_info[:2])
Expand Down
2 changes: 1 addition & 1 deletion piptools/repositories/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
path_to_url,
url_to_path,
)
from ..cache import CACHE_DIR
from ..click import progressbar
from ..exceptions import NoCandidateFound
from ..locations import CACHE_DIR
from ..logging import log
from ..utils import (
create_install_command,
Expand Down
5 changes: 1 addition & 4 deletions piptools/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from . import click
from ._compat import install_req_from_line
from .cache import DependencyCache
from .logging import log
from .utils import (
UNSAFE_PACKAGES,
Expand Down Expand Up @@ -95,7 +94,7 @@ def __init__(
self,
constraints,
repository,
cache=None,
cache,
prereleases=False,
clear_caches=False,
allow_unsafe=False,
Expand All @@ -108,8 +107,6 @@ def __init__(
self.our_constraints = set(constraints)
self.their_constraints = set()
self.repository = repository
if cache is None:
cache = DependencyCache() # pragma: no cover
self.dependency_cache = cache
self.prereleases = prereleases
self.clear_caches = clear_caches
Expand Down
16 changes: 16 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

from .. import click
from .._compat import install_req_from_line, parse_requirements
from ..cache import DependencyCache
from ..exceptions import PipToolsError
from ..locations import CACHE_DIR
from ..logging import log
from ..repositories import LocalRequirementsRepository, PyPIRepository
from ..resolver import Resolver
Expand Down Expand Up @@ -172,6 +174,15 @@
default=True,
help="Add the find-links option to generated file",
)
@click.option(
"--cache-dir",
help="Specify a directory to cache dependency information (defaults to {})".format(
atugushev marked this conversation as resolved.
Show resolved Hide resolved
CACHE_DIR
),
envvar="PIP_TOOLS_CACHE_DIR",
show_envvar=True,
type=click.Path(file_okay=False, writable=True),
)
atugushev marked this conversation as resolved.
Show resolved Hide resolved
def cli(
ctx,
verbose,
Expand All @@ -198,6 +209,7 @@ def cli(
max_rounds,
build_isolation,
emit_find_links,
cache_dir,
atugushev marked this conversation as resolved.
Show resolved Hide resolved
):
"""Compiles requirements.txt from requirements.in specs."""
log.verbosity = verbose - quiet
Expand Down Expand Up @@ -348,11 +360,15 @@ def cli(
for find_link in dedup(repository.finder.find_links):
log.debug(" -f {}".format(find_link))

if cache_dir is None:
cache_dir = CACHE_DIR
atugushev marked this conversation as resolved.
Show resolved Hide resolved

try:
resolver = Resolver(
constraints,
repository,
prereleases=repository.finder.allow_all_prereleases or pre,
cache=DependencyCache(cache_dir),
clear_caches=rebuild,
allow_unsafe=allow_unsafe,
)
Expand Down
1 change: 1 addition & 0 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"--upgrade",
"--upgrade-package",
"--verbose",
"--cache-dir",
}


Expand Down
25 changes: 14 additions & 11 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import mock
import pytest
from click.testing import CliRunner
from pytest import mark

from .constants import MINIMAL_WHEELS_PATH, PACKAGES_PATH
Expand All @@ -15,6 +14,11 @@
from piptools.scripts.compile import cli


@pytest.fixture(autouse=True)
def temp_dep_cache(tmpdir, monkeypatch):
monkeypatch.setenv("PIP_TOOLS_CACHE_DIR", str(tmpdir / "cache"))


def test_default_pip_conf_read(pip_with_index_conf, runner):
# preconditions
with open("requirements.in", "w"):
Expand Down Expand Up @@ -69,13 +73,13 @@ def test_command_line_setuptools_read(pip_conf, runner):
(["setup.py", "--output-file", "output.txt"], "output.txt"),
],
)
def test_command_line_setuptools_output_file(pip_conf, options, expected_output_file):
def test_command_line_setuptools_output_file(
pip_conf, runner, options, expected_output_file
):
"""
Test the output files for setup.py as a requirement file.
"""
runner = CliRunner(mix_stderr=False)
with runner.isolated_filesystem():
package = open("setup.py", "w")
with open("setup.py", "w") as package:
package.write(
dedent(
"""\
Expand All @@ -84,11 +88,10 @@ def test_command_line_setuptools_output_file(pip_conf, options, expected_output_
"""
)
)
package.close()

out = runner.invoke(cli, options)
assert out.exit_code == 0
assert os.path.exists(expected_output_file)
out = runner.invoke(cli, options)
assert out.exit_code == 0
assert os.path.exists(expected_output_file)


def test_find_links_option(runner):
Expand Down Expand Up @@ -519,7 +522,7 @@ def test_generate_hashes_verbose(pip_conf, runner):


@pytest.mark.skipif(PIP_VERSION < (9,), reason="needs pip 9 or greater")
def test_filter_pip_markers(runner):
def test_filter_pip_markers(pip_conf, runner):
"""
Check that pip-compile works with pip environment markers (PEP496)
"""
Expand Down Expand Up @@ -592,7 +595,7 @@ def test_not_specified_input_file(runner):
assert out.exit_code == 2


def test_stdin(runner):
def test_stdin(pip_conf, runner):
"""
Test compile requirements from STDIN.
"""
Expand Down