|
10 | 10 | with_cleanup,
|
11 | 11 | )
|
12 | 12 | from pip._internal.cli.status_codes import SUCCESS
|
13 |
| -from pip._internal.models.pylock import Pylock |
| 13 | +from pip._internal.models.pylock import Pylock, is_valid_pylock_file_name |
14 | 14 | from pip._internal.operations.build.build_tracker import get_build_tracker
|
15 | 15 | from pip._internal.req.req_install import (
|
16 | 16 | check_legacy_setup_py_options,
|
@@ -45,6 +45,18 @@ class LockCommand(RequirementCommand):
|
45 | 45 | %prog [options] <archive url/path> ..."""
|
46 | 46 |
|
47 | 47 | def add_options(self) -> None:
|
| 48 | + self.cmd_opts.add_option( |
| 49 | + cmdoptions.PipOption( |
| 50 | + "--output-file", |
| 51 | + "--output", |
| 52 | + "-o", |
| 53 | + dest="output_file", |
| 54 | + metavar="path", |
| 55 | + type="path", |
| 56 | + default="pylock.toml", |
| 57 | + help="Lock file name (default=pylock.toml). Use - for stdout.", |
| 58 | + ) |
| 59 | + ) |
48 | 60 | self.cmd_opts.add_option(cmdoptions.requirements())
|
49 | 61 | self.cmd_opts.add_option(cmdoptions.constraints())
|
50 | 62 | self.cmd_opts.add_option(cmdoptions.no_deps())
|
@@ -131,9 +143,23 @@ def run(self, options: Values, args: List[str]) -> int:
|
131 | 143 |
|
132 | 144 | requirement_set = resolver.resolve(reqs, check_supported_wheels=True)
|
133 | 145 |
|
134 |
| - pyproject_lock = Pylock.from_install_requirements( |
135 |
| - requirement_set.requirements.values(), base_dir=Path.cwd() |
136 |
| - ) |
137 |
| - sys.stdout.write(pyproject_lock.as_toml()) |
| 146 | + if options.output_file == "-": |
| 147 | + base_dir = Path.cwd() |
| 148 | + else: |
| 149 | + output_file_path = Path(options.output_file) |
| 150 | + if not is_valid_pylock_file_name(output_file_path): |
| 151 | + logger.warning( |
| 152 | + "%s is not a valid lock file name.", |
| 153 | + output_file_path, |
| 154 | + ) |
| 155 | + base_dir = output_file_path.parent.absolute() |
| 156 | + pyproject_lock_toml = Pylock.from_install_requirements( |
| 157 | + requirement_set.requirements.values(), base_dir=base_dir |
| 158 | + ).as_toml() |
| 159 | + if options.output_file == "-": |
| 160 | + sys.stdout.write(pyproject_lock_toml) |
| 161 | + else: |
| 162 | + with output_file_path.open("w", encoding="utf-8") as f: |
| 163 | + f.write(pyproject_lock_toml) |
138 | 164 |
|
139 | 165 | return SUCCESS
|
0 commit comments