From 78fec9cac44d62eb3389b1cc9c4c67ba7147f874 Mon Sep 17 00:00:00 2001 From: Tobias Macey Date: Sat, 25 Jul 2015 17:05:06 -0400 Subject: [PATCH] Added --save option to install command I added a pair of new command options to the install command. --save will append the specified packages to the end of the 'requirements.txt' file. --save-dest will take a filename or path argument and the specified packages will be appended to the given file. --- pip/commands/install.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pip/commands/install.py b/pip/commands/install.py index dbcf100e1e4..6c745c06f72 100644 --- a/pip/commands/install.py +++ b/pip/commands/install.py @@ -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( @@ -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