From af42367dce6ab1ad18a1a8d10e4d54b5accae449 Mon Sep 17 00:00:00 2001 From: Richard Crowley Date: Tue, 1 Mar 2011 20:44:11 +0000 Subject: (#6527) Added pip package provider. Python's pip package manager is analogous to RubyGems and should be included in Puppet. Reviewed-by: Matt Robinson --- lib/puppet/provider/package/pip.rb | 115 +++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 lib/puppet/provider/package/pip.rb (limited to 'lib') diff --git a/lib/puppet/provider/package/pip.rb b/lib/puppet/provider/package/pip.rb new file mode 100644 index 000000000..00797563a --- /dev/null +++ b/lib/puppet/provider/package/pip.rb @@ -0,0 +1,115 @@ +# Puppet package provider for Python's `pip` package management frontend. +# + +require 'puppet/provider/package' +require 'xmlrpc/client' + +Puppet::Type.type(:package).provide :pip, + :parent => ::Puppet::Provider::Package do + + desc "Python packages via `pip`." + + has_feature :installable, :uninstallable, :upgradeable, :versionable + + # Parse lines of output from `pip freeze`, which are structured as + # _package_==_version_. + def self.parse(line) + if line.chomp =~ /^([^=]+)==([^=]+)$/ + {:ensure => $2, :name => $1, :provider => name} + else + nil + end + end + + # Return an array of structured information about every installed package + # that's managed by `pip` or an empty array if `pip` is not available. + def self.instances + packages = [] + execpipe "#{command :pip} freeze" do |process| + process.collect do |line| + next unless options = parse(line) + packages << new(options) + end + end + packages + rescue Puppet::DevError + [] + end + + # Return structured information about a particular package or `nil` if + # it is not installed or `pip` itself is not available. + def query + execpipe "#{command :pip} freeze" do |process| + process.each do |line| + options = self.class.parse(line) + return options if options[:name] == @resource[:name] + end + end + nil + rescue Puppet::DevError + nil + end + + # Ask the PyPI API for the latest version number. There is no local + # cache of PyPI's package list so this operation will always have to + # ask the web service. + def latest + client = XMLRPC::Client.new2("http://pypi.python.org/pypi") + client.http_header_extra = {"Content-Type" => "text/xml"} + result = client.call("package_releases", @resource[:name]) + result.first + end + + # Install a package. The ensure parameter may specify installed, + # latest, a version number, or, in conjunction with the source + # parameter, an SCM revision. In that case, the source parameter + # gives the fully-qualified URL to the repository. + def install + args = %w{install -q} + if @resource[:source] + args << "-e" + if String === @resource[:ensure] + args << "#{@resource[:source]}@#{@resource[:ensure]}#egg=#{ + @resource[:name]}" + else + args << "#{@resource[:source]}#egg=#{@resource[:name]}" + end + else + case @resource[:ensure] + when String + args << "#{@resource[:name]}==#{@resource[:ensure]}" + when :latest + args << "--upgrade" << @resource[:name] + else + args << @resource[:name] + end + end + lazy_pip *args + end + + # Uninstall a package. Uninstall won't work reliably on Debian/Ubuntu + # unless this issue gets fixed. + # + def uninstall + lazy_pip "uninstall", "-y", "-q", @resource[:name] + end + + def update + install + end + + # Execute a `pip` command. If Puppet doesn't yet know how to do so, + # try to teach it and if even that fails, raise the error. + private + def lazy_pip(*args) + pip *args + rescue NoMethodError => e + if pathname = `which pip`.chomp + self.class.commands :pip => pathname + pip *args + else + raise e + end + end + +end -- cgit From 0170cebba039378597fdf9f0086339c2766df408 Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Tue, 22 Mar 2011 15:28:52 -0700 Subject: (#6527) Fix uninstall problem and refactor Uninstall wasn't working properly because the instances method relied on the pip command having been defined by calling lazy_pip. This lazy_pip pattern is a nice way of getting around Puppet's problem of disqualifying providers at the beginning of a run because the command doesn't yet exist even though part way through the run the command might exist. Really, we need to fix puppet to lazily evaluate a provider command for validity only at the time the provider is needed. The refactoring also pointed out that query could just reuse the logic from instances. The tests were also refactored to use real resources instead of stubbed ones, and they reflect the implementation changes to instances. Paired-with: Richard Crowley --- lib/puppet/provider/package/pip.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/puppet/provider/package/pip.rb b/lib/puppet/provider/package/pip.rb index 00797563a..5abbc135a 100644 --- a/lib/puppet/provider/package/pip.rb +++ b/lib/puppet/provider/package/pip.rb @@ -25,29 +25,23 @@ Puppet::Type.type(:package).provide :pip, # that's managed by `pip` or an empty array if `pip` is not available. def self.instances packages = [] - execpipe "#{command :pip} freeze" do |process| + pip_cmd = which('pip') or return [] + execpipe "#{pip_cmd} freeze" do |process| process.collect do |line| next unless options = parse(line) packages << new(options) end end packages - rescue Puppet::DevError - [] end # Return structured information about a particular package or `nil` if # it is not installed or `pip` itself is not available. def query - execpipe "#{command :pip} freeze" do |process| - process.each do |line| - options = self.class.parse(line) - return options if options[:name] == @resource[:name] - end + self.class.instances.each do |provider_pip| + return provider_pip.properties if @resource[:name] == provider_pip.name end - nil - rescue Puppet::DevError - nil + return nil end # Ask the PyPI API for the latest version number. There is no local @@ -104,7 +98,7 @@ Puppet::Type.type(:package).provide :pip, def lazy_pip(*args) pip *args rescue NoMethodError => e - if pathname = `which pip`.chomp + if pathname = which('pip') self.class.commands :pip => pathname pip *args else -- cgit