Skip to content

Commit

Permalink
feat: add --update-reuse to lock command (#2419)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Dec 1, 2023
1 parent 99cfa33 commit e106524
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 15 deletions.
30 changes: 19 additions & 11 deletions src/pdm/cli/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
set_env_in_reg,
)
from pdm.environments import BareEnvironment
from pdm.exceptions import PdmUsageError, ProjectError
from pdm.exceptions import PdmException, PdmUsageError, ProjectError
from pdm.models.candidates import Candidate
from pdm.models.requirements import Requirement, parse_requirement
from pdm.project import Project
from pdm.project.lockfile import FLAG_CROSS_PLATFORM, FLAG_DIRECT_MINIMAL_VERSIONS
from pdm.resolver import resolve
from pdm.termui import logger
from pdm.utils import deprecation_warning


Expand Down Expand Up @@ -143,16 +144,23 @@ def resolve_candidates_from_lockfile(
if cross_platform:
provider.repository.ignore_compatibility = True
resolver: Resolver = project.core.resolver_class(provider, reporter)
mapping, *_ = resolve(
resolver,
reqs,
project.environment.python_requires,
resolve_max_rounds,
record_markers=cross_platform,
)
spinner.update("Fetching hashes for resolved packages...")
fetch_hashes(provider.repository, mapping)
return mapping
try:
mapping, *_ = resolve(
resolver,
reqs,
project.environment.python_requires,
resolve_max_rounds,
record_markers=cross_platform,
)
except ResolutionImpossible as e:
logger.exception("Broken lockfile")
raise PdmException(
"Resolving from lockfile failed. You may fix the lockfile by `pdm lock --update-reuse` and retry."
) from e
else:
spinner.update("Fetching hashes for resolved packages...")
fetch_hashes(provider.repository, mapping)
return mapping


def check_lockfile(project: Project, raise_not_exist: bool = True) -> str | None:
Expand Down
9 changes: 9 additions & 0 deletions src/pdm/cli/commands/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
action="store_true",
help="Check if the lock file is up to date and quit",
)
parser.add_argument(
"--update-reuse",
action="store_const",
dest="update_strategy",
default="all",
const="reuse",
help="Reuse pinned versions already present in lock file if possible",
)

def handle(self, project: Project, options: argparse.Namespace) -> None:
if options.check:
Expand All @@ -56,6 +64,7 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
actions.do_lock(
project,
refresh=options.refresh,
strategy=options.update_strategy,
groups=selection.all(),
strategy_change=options.strategy_change,
hooks=HookManager(project, options.skip),
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/completions/pdm.bash
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ _pdm_a919b69078acdf0a_complete()
;;

(lock)
opts="--check --dev --global --group --help --lockfile --no-cross-platform --no-default --no-isolation --no-static-urls --production --project --quiet --refresh --skip --static-urls --strategy --verbose"
opts="--check --dev --global --group --help --lockfile --no-cross-platform --no-default --no-isolation --no-static-urls --production --project --quiet --refresh --skip --static-urls --strategy --update-reuse --verbose"
;;

(plugin)
Expand Down
1 change: 1 addition & 0 deletions src/pdm/cli/completions/pdm.fish
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l refresh -d 'Don\'t u
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l skip -d 'Skip some tasks and/or hooks by their comma-separated names. Can be supplied multiple times. Use ":all" to skip all hooks. Use ":pre" and ":post" to skip all pre or post hooks.'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l static-urls -d '[DEPRECATED] Store static file URLs in the lockfile'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l strategy -d 'Specify lock strategy (cross_platform, static_urls, direct_minimal_versions). Add \'no_\' prefix to disable. Can be supplied multiple times or split by comma.'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l update-reuse -d 'Reuse pinned versions already present in lock file if possible'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l verbose -d 'Use `-v` for detailed output and `-vv` for more detailed'

# plugin
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/cli/completions/pdm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function TabExpansion($line, $lastWord) {
[Option]::new(@(
"--global", "-g", "--no-isolation", "--refresh", "-L", "--lockfile", "--check", "--dev", "--prod",
"--production", "-d", "--no-default", "--no-cross-platform", "--static-urls", "--no-static-urls",
"--strategy", "-S"
"--strategy", "-S", "--update-reuse"
)),
$skipOption,
$sectionOption,
Expand Down
1 change: 1 addition & 0 deletions src/pdm/cli/completions/pdm.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ _pdm() {
{-G+,--group+}'[Select group of optional-dependencies or dev-dependencies(with -d). Can be supplied multiple times, use ":all" to include all groups under the same species]:group:_pdm_groups'
{-d,--dev}"[Select dev dependencies]"
{--prod,--production}"[Unselect dev dependencies]"
'--update-reuse[Reuse pinned versions already present in lock file if possible]'
"--static-urls[(DEPRECATED) Store static file URLs in the lockfile]"
"--no-static-urls[(DEPRECATED) Do not store static file URLs in the lockfile]"
"--no-default[Don\'t include dependencies from the default group]"
Expand Down
2 changes: 1 addition & 1 deletion src/pdm/models/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def _identify_candidate(self, candidate: Candidate) -> CandidateKey:
def _get_dependencies_from_lockfile(self, candidate: Candidate) -> CandidateInfo:
err = (
f"Missing package {candidate.identify()} from the lockfile, "
"the lockfile may be broken. Run `pdm update` to fix it."
"the lockfile may be broken. Run `pdm lock --update-reuse` to fix it."
)
try:
return self.candidate_info[self._identify_candidate(candidate)]
Expand Down
2 changes: 1 addition & 1 deletion tests/cli/test_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def test_lock_command(project, pdm, mocker):
m = mocker.patch.object(actions, "do_lock")
pdm(["lock"], obj=project)
m.assert_called_with(project, refresh=False, groups=["default"], hooks=ANY, strategy_change=None)
m.assert_called_with(project, refresh=False, groups=["default"], hooks=ANY, strategy_change=None, strategy="all")


@pytest.mark.usefixtures("repository")
Expand Down

0 comments on commit e106524

Please sign in to comment.