Skip to content

Commit ed39792

Browse files
add and implement --print-download-urls option to download
1 parent 5cf9840 commit ed39792

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/pip/_internal/commands/download.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import json
12
import logging
23
import os
34
from optparse import Values
4-
from typing import List
5+
from typing import Dict, List
56

67
from pip._internal.cli import cmdoptions
78
from pip._internal.cli.cmdoptions import make_target_python
@@ -62,6 +63,14 @@ def add_options(self) -> None:
6263
help="Download packages into <dir>.",
6364
)
6465

66+
self.cmd_opts.add_option(
67+
'--print-download-urls',
68+
dest='print_download_urls',
69+
metavar='output-file',
70+
default=None,
71+
help=("Print URLs of any downloaded distributions to this file."),
72+
)
73+
6574
cmdoptions.add_target_python_options(self.cmd_opts)
6675

6776
index_opts = cmdoptions.make_option_group(
@@ -128,12 +137,22 @@ def run(self, options: Values, args: List[str]) -> int:
128137
requirement_set = resolver.resolve(reqs, check_supported_wheels=True)
129138

130139
downloaded: List[str] = []
140+
download_infos: List[Dict[str, str]] = []
131141
for req in requirement_set.requirements.values():
132142
if req.satisfied_by is None:
133143
assert req.name is not None
144+
assert req.link is not None
145+
download_infos.append({
146+
'name': req.name,
147+
'url': req.link.url,
148+
})
134149
preparer.save_linked_requirement(req)
135150
downloaded.append(req.name)
151+
136152
if downloaded:
137153
write_output("Successfully downloaded %s", " ".join(downloaded))
154+
if options.print_download_urls:
155+
with open(options.print_download_urls, 'w') as f:
156+
json.dump(download_infos, f, indent=4)
138157

139158
return SUCCESS

0 commit comments

Comments
 (0)