Permalink
Browse files

internal version

  • Loading branch information...
1 parent 87eeccb commit 06a540ccaa5d00297d0c2d0d9da4e1fde09824ff Charlie Root committed Jan 6, 2012
Showing with 111 additions and 0 deletions.
  1. +111 −0 lib/puppet/provider/package/easypip.rb
@@ -0,0 +1,111 @@
+require 'puppet/provider/package'
+
+include Puppet::Util::Package
+
+Puppet::Type.type(:package).provide(:easypip,
+ :parent => ::Puppet::Provider::Package) do
+
+ has_feature :ensurable,:versionable
+
+ commands:easy_install=>"/usr/local/bin/easy_install"
+ desc "Python packages Installation via `easy_install` and Uninstallation via 'pip'."
+
+ # 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
+
+ def self.instances
+ debug("Gathering Installed Instances")
+ pip_cmd = which('pip') or return []
+ packages = []
+ execpipe "#{pip_cmd} freeze" do |process|
+ process.collect do |line|
+ next unless options = parse(line)
+ packages << new(options)
+ end
+ end
+ return packages
+ end
+
+ def extractname(name)
+ if name =~ /^([^-]+)-([0-9.]+)/
+ name = $1;
+ return name
+ end
+ return nil
+ end
+
+ def query
+ if !(@resource[:source] =~ /http:\/\/csn-mgmt.csnzoo.com\/tb\/packages\/egg*/)
+ raise Puppet::Error, "Trying to fetch from invalid domain"
+ end
+
+ packages=self.class.instances
+ return nil unless packages != nil
+ packages.each do |provider_pip|
+ name = extractname @resource[:name]
+ if name == provider_pip.name
+ debug("Package Found - If the version doesnt match, you can expect an install")
+ #This is the parameter based on which puppet makes a decision whether or not to install the package based on the version number/present/absent values in the ensure parameter
+ return provider_pip.properties
+ return nil
+ end
+ end
+ return nil
+ end
+
+ def install
+ debug("Installing!!")
+ args = %w{-v}
+ name = @resource[:name]
+
+ source = @resource[:source] + @resource[:name] + "-py2.7.egg";
+ case @resource[:ensure]
+ when String
+ args << source
+ when :present
+ args << source
+ #for advanced versions, we might have to add features like latest or holdable. This case branch would be useful then.
+ end
+ ezy_install *args
+ end
+
+ def uninstall
+ name = extractname @resource[:name]
+ PiP "uninstall", "-y", "-q", name
+ end
+
+ private
+ # executes easy_install command with the passed arguments
+ def ezy_install(*args)
+ easy_install *args
+ rescue NoMethodError => e
+
+ if pathname = which('easy_install')
+ self.class.commands :easy_install => pathname
+ easy_install *args
+ else
+ raise e
+ end
+ end
+
+ # executes pip command with the passed arguments
+ def PiP(*args)
+ pip *args
+ rescue NoMethodError => e
+
+ if pathname = which('pip')
+ self.class.commands :pip => pathname
+ pip *args
+ else
+ raise e
+ end
+ end
+ end
+

0 comments on commit 06a540c

Please sign in to comment.