Skip to content

Commit

Permalink
Allow to specify another lockfile by CLI option or env var (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Apr 25, 2022
1 parent 9dce735 commit 66e2108
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 70 deletions.
4 changes: 4 additions & 0 deletions docs/docs/usage/dependency.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ There are two similar commands to do this job with a slight difference:
- `pdm sync` installs dependencies in the lock file and will error out if it doesn't exist.
Besides, `pdm sync` can also remove unneeded packages if `--clean` option is given.

## Specify the lockfile to use

You can specify another lockfile than the default `pdm.lock` by using the `-L/--lockfilie <filepath>` option or the `PDM_LOCKFILE` environment variable.

### Select a subset of dependencies with CLI options

Say we have a project with following dependencies:
Expand Down
1 change: 1 addition & 0 deletions news/1038.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow specifying lockfile other than `pdm.lock` by `--lockfile` option or `PDM_LOCKFILE` env var.
2 changes: 2 additions & 0 deletions pdm/cli/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pdm.cli.options import (
dry_run_option,
install_group,
lockfile_option,
packages_group,
prerelease_option,
save_strategy_group,
Expand All @@ -19,6 +20,7 @@ class Command(BaseCommand):
"""Add package(s) to pyproject.toml and install them"""

arguments = BaseCommand.arguments + [
lockfile_option,
save_strategy_group,
update_strategy_group,
prerelease_option,
Expand Down
3 changes: 2 additions & 1 deletion pdm/cli/commands/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pdm.cli.actions import resolve_candidates_from_lockfile
from pdm.cli.commands.base import BaseCommand
from pdm.cli.options import groups_group
from pdm.cli.options import groups_group, lockfile_option
from pdm.cli.utils import translate_groups
from pdm.formats import FORMATS
from pdm.models.candidates import Candidate
Expand All @@ -18,6 +18,7 @@ class Command(BaseCommand):
"""Export the locked packages set to other formats"""

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
lockfile_option.add_to_parser(parser)
parser.add_argument(
"-f",
"--format",
Expand Down
3 changes: 2 additions & 1 deletion pdm/cli/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pdm.cli import actions
from pdm.cli.commands.base import BaseCommand
from pdm.cli.commands.run import run_script_if_present
from pdm.cli.options import dry_run_option, groups_group, install_group
from pdm.cli.options import dry_run_option, groups_group, install_group, lockfile_option
from pdm.project import Project


Expand All @@ -18,6 +18,7 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
groups_group.add_to_parser(parser)
install_group.add_to_parser(parser)
dry_run_option.add_to_parser(parser)
lockfile_option.add_to_parser(parser)
parser.add_argument(
"--no-lock",
dest="lock",
Expand Down
4 changes: 2 additions & 2 deletions pdm/cli/commands/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from pdm.cli import actions
from pdm.cli.commands.base import BaseCommand
from pdm.cli.commands.run import run_script_if_present
from pdm.cli.options import no_isolation_option
from pdm.cli.options import lockfile_option, no_isolation_option
from pdm.project import Project


class Command(BaseCommand):
"""Resolve and lock dependencies"""

arguments = BaseCommand.arguments + [no_isolation_option]
arguments = BaseCommand.arguments + [lockfile_option, no_isolation_option]

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
Expand Down
3 changes: 2 additions & 1 deletion pdm/cli/commands/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from pdm.cli import actions
from pdm.cli.commands.base import BaseCommand
from pdm.cli.options import dry_run_option, install_group
from pdm.cli.options import dry_run_option, install_group, lockfile_option
from pdm.project import Project


Expand All @@ -12,6 +12,7 @@ class Command(BaseCommand):
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
install_group.add_to_parser(parser)
dry_run_option.add_to_parser(parser)
lockfile_option.add_to_parser(parser)
parser.add_argument(
"-d",
"--dev",
Expand Down
9 changes: 8 additions & 1 deletion pdm/cli/commands/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

from pdm.cli import actions
from pdm.cli.commands.base import BaseCommand
from pdm.cli.options import clean_group, dry_run_option, groups_group, install_group
from pdm.cli.options import (
clean_group,
dry_run_option,
groups_group,
install_group,
lockfile_option,
)
from pdm.project import Project


Expand All @@ -12,6 +18,7 @@ class Command(BaseCommand):
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
groups_group.add_to_parser(parser)
dry_run_option.add_to_parser(parser)
lockfile_option.add_to_parser(parser)
parser.add_argument(
"-r",
"--reinstall",
Expand Down
2 changes: 2 additions & 0 deletions pdm/cli/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pdm.cli.options import (
groups_group,
install_group,
lockfile_option,
prerelease_option,
save_strategy_group,
unconstrained_option,
Expand All @@ -19,6 +20,7 @@ class Command(BaseCommand):
arguments = BaseCommand.arguments + [
groups_group,
install_group,
lockfile_option,
save_strategy_group,
update_strategy_group,
prerelease_option,
Expand Down
14 changes: 7 additions & 7 deletions pdm/cli/completions/pdm.bash
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ _pdm_a919b69078acdf0a_complete()
case "$com" in

(add)
opts="--dev --dry-run --editable --global --group --help --no-editable --no-isolation --no-self --no-sync --prerelease --project --save-compatible --save-exact --save-minimum --save-wildcard --unconstrained --update-eager --update-reuse --verbose"
opts="--dev --dry-run --editable --global --group --help --lockfile --no-editable --no-isolation --no-self --no-sync --prerelease --project --save-compatible --save-exact --save-minimum --save-wildcard --unconstrained --update-eager --update-reuse --verbose"
;;

(build)
Expand All @@ -49,7 +49,7 @@ _pdm_a919b69078acdf0a_complete()
;;

(export)
opts="--dev --format --global --group --help --no-default --output --production --project --pyproject --verbose --without-hashes"
opts="--dev --format --global --group --help --lockfile --no-default --output --production --project --pyproject --verbose --without-hashes"
;;

(import)
Expand All @@ -65,23 +65,23 @@ _pdm_a919b69078acdf0a_complete()
;;

(install)
opts="--check --dev --dry-run --global --group --help --no-default --no-editable --no-isolation --no-lock --no-self --production --project --verbose"
opts="--check --dev --dry-run --global --group --help --lockfile --no-default --no-editable --no-isolation --no-lock --no-self --production --project --verbose"
;;

(list)
opts="--freeze --global --graph --help --json --project --reverse --verbose"
;;

(lock)
opts="--global --help --no-isolation --project --refresh --verbose"
opts="--global --help --lockfile --no-isolation --project --refresh --verbose"
;;

(plugin)
opts="--help --verbose"
;;

(remove)
opts="--dev --dry-run --global --group --help --no-editable --no-isolation --no-self --no-sync --project --verbose"
opts="--dev --dry-run --global --group --help --lockfile --no-editable --no-isolation --no-self --no-sync --project --verbose"
;;

(run)
Expand All @@ -97,11 +97,11 @@ _pdm_a919b69078acdf0a_complete()
;;

(sync)
opts="--clean --dev --dry-run --global --group --help --no-clean --no-default --no-editable --no-isolation --no-self --production --project --reinstall --verbose"
opts="--clean --dev --dry-run --global --group --help --lockfile --no-clean --no-default --no-editable --no-isolation --no-self --production --project --reinstall --verbose"
;;

(update)
opts="--dev --global --group --help --no-default --no-editable --no-isolation --no-self --no-sync --outdated --prerelease --production --project --save-compatible --save-exact --save-minimum --save-wildcard --top --unconstrained --update-eager --update-reuse --verbose"
opts="--dev --global --group --help --lockfile --no-default --no-editable --no-isolation --no-self --no-sync --outdated --prerelease --production --project --save-compatible --save-exact --save-minimum --save-wildcard --top --unconstrained --update-eager --update-reuse --verbose"
;;

(use)
Expand Down
7 changes: 7 additions & 0 deletions pdm/cli/completions/pdm.fish
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from add' -l editable -d 'Specify
complete -c pdm -A -n '__fish_seen_subcommand_from add' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from add' -l group -d 'Specify the target dependency group to add into'
complete -c pdm -A -n '__fish_seen_subcommand_from add' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from add' -l lockfile -d 'Specify another lockfile path, or use `PDM_LOCKFILE` env variable. Default: pdm.lock'
complete -c pdm -A -n '__fish_seen_subcommand_from add' -l no-editable -d 'Install non-editable versions for all packages'
complete -c pdm -A -n '__fish_seen_subcommand_from add' -l no-isolation -d 'Do not isolate the build in a clean environment'
complete -c pdm -A -n '__fish_seen_subcommand_from add' -l no-self -d 'Don\'t install the project itself'
Expand Down Expand Up @@ -96,6 +97,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from export' -l format -d 'Specify
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l group -d '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.'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l lockfile -d 'Specify another lockfile path, or use `PDM_LOCKFILE` env variable. Default: pdm.lock'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l no-default -d 'Don\'t include dependencies from the default group'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l output -d 'Write output to the given file, or print to stdout if not given'
complete -c pdm -A -n '__fish_seen_subcommand_from export' -l production -d 'Unselect dev dependencies'
Expand Down Expand Up @@ -137,6 +139,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from install' -l dry-run -d 'Show
complete -c pdm -A -n '__fish_seen_subcommand_from install' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from install' -l group -d '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.'
complete -c pdm -A -n '__fish_seen_subcommand_from install' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from install' -l lockfile -d 'Specify another lockfile path, or use `PDM_LOCKFILE` env variable. Default: pdm.lock'
complete -c pdm -A -n '__fish_seen_subcommand_from install' -l no-default -d 'Don\'t include dependencies from the default group'
complete -c pdm -A -n '__fish_seen_subcommand_from install' -l no-editable -d 'Install non-editable versions for all packages'
complete -c pdm -A -n '__fish_seen_subcommand_from install' -l no-isolation -d 'Do not isolate the build in a clean environment'
Expand All @@ -159,6 +162,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from list' -l verbose -d '-v for d
# lock
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l lockfile -d 'Specify another lockfile path, or use `PDM_LOCKFILE` env variable. Default: pdm.lock'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l no-isolation -d 'Do not isolate the build in a clean environment'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l project -d 'Specify another path as the project root, which changes the base of pyproject.toml and __pypackages__'
complete -c pdm -A -n '__fish_seen_subcommand_from lock' -l refresh -d 'Don\'t update pinned versions, only refresh the lock file'
Expand All @@ -174,6 +178,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l dry-run -d 'Show t
complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l group -d 'Specify the target dependency group to remove from'
complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l lockfile -d 'Specify another lockfile path, or use `PDM_LOCKFILE` env variable. Default: pdm.lock'
complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l no-editable -d 'Install non-editable versions for all packages'
complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l no-isolation -d 'Do not isolate the build in a clean environment'
complete -c pdm -A -n '__fish_seen_subcommand_from remove' -l no-self -d 'Don\'t install the project itself'
Expand Down Expand Up @@ -212,6 +217,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l dry-run -d 'Show the
complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l group -d '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.'
complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l lockfile -d 'Specify another lockfile path, or use `PDM_LOCKFILE` env variable. Default: pdm.lock'
complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l no-clean -d 'don\'t clean unused packages'
complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l no-default -d 'Don\'t include dependencies from the default group'
complete -c pdm -A -n '__fish_seen_subcommand_from sync' -l no-editable -d 'Install non-editable versions for all packages'
Expand All @@ -227,6 +233,7 @@ complete -c pdm -A -n '__fish_seen_subcommand_from update' -l dev -d 'Select dev
complete -c pdm -A -n '__fish_seen_subcommand_from update' -l global -d 'Use the global project, supply the project root with `-p` option'
complete -c pdm -A -n '__fish_seen_subcommand_from update' -l group -d '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.'
complete -c pdm -A -n '__fish_seen_subcommand_from update' -l help -d 'show this help message and exit'
complete -c pdm -A -n '__fish_seen_subcommand_from update' -l lockfile -d 'Specify another lockfile path, or use `PDM_LOCKFILE` env variable. Default: pdm.lock'
complete -c pdm -A -n '__fish_seen_subcommand_from update' -l no-default -d 'Don\'t include dependencies from the default group'
complete -c pdm -A -n '__fish_seen_subcommand_from update' -l no-editable -d 'Install non-editable versions for all packages'
complete -c pdm -A -n '__fish_seen_subcommand_from update' -l no-isolation -d 'Do not isolate the build in a clean environment'
Expand Down
14 changes: 7 additions & 7 deletions pdm/cli/completions/pdm.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function TabExpansion($line, $lastWord) {

"add" {
$completer.AddOpts(@(
[Option]::new(("-d", "--dev", "--save-compatible", "--save-wildcard", "--dry-run", "--save-exact", "--save-minimum", "--update-eager", "--update-reuse", "-g", "--global", "--no-sync", "--no-editable", "--no-self", "-u", "--unconstrained", "--no-isolation", "--pre", "--prerelease")),
[Option]::new(("-d", "--dev", "--save-compatible", "--save-wildcard", "--dry-run", "--save-exact", "--save-minimum", "--update-eager", "--update-reuse", "-g", "--global", "--no-sync", "--no-editable", "--no-self", "-u", "--unconstrained", "--no-isolation", "--pre", "--prerelease", "-L", "--lockfile")),
$sectionOption,
$projectOption,
[Option]::new(@("-e", "--editable")).WithValues(@(getPyPIPackages))
Expand Down Expand Up @@ -230,7 +230,7 @@ function TabExpansion($line, $lastWord) {
}
"export" {
$completer.AddOpts(@(
[Option]::new(@("--dev", "--output", "--global", "--no-default", "--prod", "--production", "-g", "-d", "-o", "--without-hashes")),
[Option]::new(@("--dev", "--output", "--global", "--no-default", "--prod", "--production", "-g", "-d", "-o", "--without-hashes", "-L", "--lockfile")),
$formatOption,
$sectionOption,
$projectOption
Expand Down Expand Up @@ -264,7 +264,7 @@ function TabExpansion($line, $lastWord) {
}
"install" {
$completer.AddOpts(@(
[Option]::new(("-d", "--dev", "-g", "--global", "--dry-run", "--no-default", "--no-lock", "--prod", "--production", "--no-editable", "--no-self", "--no-isolation", "--check")),
[Option]::new(("-d", "--dev", "-g", "--global", "--dry-run", "--no-default", "--no-lock", "--prod", "--production", "--no-editable", "--no-self", "--no-isolation", "--check", "-L", "--lockfile")),
$sectionOption,
$projectOption
))
Expand All @@ -281,7 +281,7 @@ function TabExpansion($line, $lastWord) {
"lock" {
$completer.AddOpts(
@(
[Option]::new(@("--global", "-g", "--no-isolation", "--refresh")),
[Option]::new(@("--global", "-g", "--no-isolation", "--refresh", "-L", "--lockfile")),
$projectOption
))
break
Expand Down Expand Up @@ -310,7 +310,7 @@ function TabExpansion($line, $lastWord) {
"remove" {
$completer.AddOpts(
@(
[Option]::new(@("--global", "-g", "--dev", "-d", "--dry-run", "--no-sync", "--no-editable", "--no-self", "--no-isolation")),
[Option]::new(@("--global", "-g", "--dev", "-d", "--dry-run", "--no-sync", "--no-editable", "--no-self", "--no-isolation", "-L", "--lockfile")),
$projectOption,
$sectionOption
))
Expand All @@ -337,15 +337,15 @@ function TabExpansion($line, $lastWord) {
}
"sync" {
$completer.AddOpts(@(
[Option]::new(("-d", "--dev", "-g", "--global", "--no-default", "--clean", "--no-clean", "--dry-run", "-r", "--reinstall", "--prod", "--production", "--no-editable", "--no-self", "--no-isolation")),
[Option]::new(("-d", "--dev", "-g", "--global", "--no-default", "--clean", "--no-clean", "--dry-run", "-r", "--reinstall", "--prod", "--production", "--no-editable", "--no-self", "--no-isolation", "-L", "--lockfile")),
$sectionOption,
$projectOption
))
break
}
"update" {
$completer.AddOpts(@(
[Option]::new(("-d", "--dev", "--save-compatible", "--prod", "--production", "--save-wildcard", "--save-exact", "--save-minimum", "--update-eager", "--update-reuse", "-g", "--global", "--dry-run", "--outdated", "--top", "-u", "--unconstrained", "--no-editable", "--no-self", "--no-isolation", "--no-sync", "--pre", "--prerelease")),
[Option]::new(("-d", "--dev", "--save-compatible", "--prod", "--production", "--save-wildcard", "--save-exact", "--save-minimum", "--update-eager", "--update-reuse", "-g", "--global", "--dry-run", "--outdated", "--top", "-u", "--unconstrained", "--no-editable", "--no-self", "--no-isolation", "--no-sync", "--pre", "--prerelease", "-L", "--lockfile")),
$sectionOption,
$projectOption
))
Expand Down
Loading

0 comments on commit 66e2108

Please sign in to comment.