Skip to content

[WIP] Added --save option to install command #2988

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

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
26 changes: 26 additions & 0 deletions pip/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ def __init__(self, *args, **kw):
help="Include pre-release and development versions. By default, "
"pip only finds stable versions.")

cmd_opts.add_option(
'--save',
action='store_true',
dest='save',
default=False,
help='Add package(s) to requirements.txt'
)

cmd_opts.add_option(
'--save-dest',
dest='save',
help='Specify file to add package in requirements format'
)

cmd_opts.add_option(cmdoptions.no_clean())

index_opts = cmdoptions.make_option_group(
Expand Down Expand Up @@ -377,4 +391,16 @@ def run(self, options, args):
target_item_dir
)
shutil.rmtree(temp_target_dir)
if options.save:
if isinstance(options.save, bool):
req_file = 'requirements.txt'
else:
req_file = options.save
with open(req_file, 'a') as requirements_file:
for requirement in requirement_set.requirements.values():
if not requirement.comes_from:
requirements_file.write(
'{pkg}=={pkg_version}\n'
.format(pkg=requirement.name,
pkg_version=requirement.installed_version))
return requirement_set