Skip to content

Commit 78fec9c

Browse files
committed
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.
1 parent bd8a33f commit 78fec9c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pip/commands/install.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ def __init__(self, *args, **kw):
165165
help="Include pre-release and development versions. By default, "
166166
"pip only finds stable versions.")
167167

168+
cmd_opts.add_option(
169+
'--save',
170+
action='store_true',
171+
dest='save',
172+
default=False,
173+
help='Add package(s) to requirements.txt'
174+
)
175+
176+
cmd_opts.add_option(
177+
'--save-dest',
178+
dest='save',
179+
help='Specify file to add package in requirements format'
180+
)
181+
168182
cmd_opts.add_option(cmdoptions.no_clean())
169183

170184
index_opts = cmdoptions.make_option_group(
@@ -377,4 +391,16 @@ def run(self, options, args):
377391
target_item_dir
378392
)
379393
shutil.rmtree(temp_target_dir)
394+
if options.save:
395+
if isinstance(options.save, bool):
396+
req_file = 'requirements.txt'
397+
else:
398+
req_file = options.save
399+
with open(req_file, 'a') as requirements_file:
400+
for requirement in requirement_set.requirements.values():
401+
if not requirement.comes_from:
402+
requirements_file.write(
403+
'{pkg}=={pkg_version}\n'
404+
.format(pkg=requirement.name,
405+
pkg_version=requirement.installed_version))
380406
return requirement_set

0 commit comments

Comments
 (0)