Skip to content

Commit c2697c6

Browse files
committed
(PUP-4198) Add install_options feature to pip provider
Add support for passing in additional options to pip install. The current provider has no way to send additional options to pip. For example, there are cases where you may want to connect to a different package index, or add an extra index url to look for packages. In my use case, we maintained our own pre-built wheels and wanted to connect only to our pypi index and not to the general index at all, so --find-links and --no-index achieve this.
1 parent 4dae4b3 commit c2697c6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/puppet/provider/package/pip.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
Puppet::Type.type(:package).provide :pip,
88
:parent => ::Puppet::Provider::Package do
99

10-
desc "Python packages via `pip`."
10+
desc "Python packages via `pip`.
1111
12-
has_feature :installable, :uninstallable, :upgradeable, :versionable
12+
This provider supports the `install_options` attribute, which allows command-line flags to be passed to pip.
13+
These options should be specified as a string (e.g. '--flag'), a hash (e.g. {'--flag' => 'value'}),
14+
or an array where each element is either a string or a hash."
15+
16+
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
1317

1418
# Parse lines of output from `pip freeze`, which are structured as
1519
# _package_==_version_.
@@ -71,6 +75,7 @@ def latest
7175
# gives the fully-qualified URL to the repository.
7276
def install
7377
args = %w{install -q}
78+
args += install_options if @resource[:install_options]
7479
if @resource[:source]
7580
if String === @resource[:ensure]
7681
args << "#{@resource[:source]}@#{@resource[:ensure]}#egg=#{
@@ -115,4 +120,8 @@ def lazy_pip(*args)
115120
raise e, 'Could not locate the pip command.', e.backtrace
116121
end
117122
end
123+
124+
def install_options
125+
join_options(@resource[:install_options])
126+
end
118127
end

spec/unit/provider/package/pip_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@
193193
@provider.install
194194
end
195195

196+
it "should handle install options" do
197+
@resource[:ensure] = :installed
198+
@resource[:source] = nil
199+
@resource[:install_options] = [{"--timeout" => "10"}, "--no-index"]
200+
@provider.expects(:lazy_pip).
201+
with("install", "-q", "--timeout=10", "--no-index", "fake_package")
202+
@provider.install
203+
end
204+
196205
end
197206

198207
describe "uninstall" do

0 commit comments

Comments
 (0)