Module: PuppetX::Counsyl::Pip

Defined in:
lib/puppet_x/counsyl/pip.rb

Instance Method Summary collapse

Instance Method Details

#installObject

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/puppet_x/counsyl/pip.rb', line 30

def install
  # Getting the python package name, regardless of whether we
  # are in a virtualenv.
  pypkg = @resource[:name].split('@')[0]
  args = %w{install -q}

  # Adding any install options
  opts = install_options
  if opts
    args << opts
  end
  if @resource[:source]
    if String === @resource[:ensure]
      # If there's a SCM revision specified, ensure a `--upgrade`
      # is specified to ensure package is actually installed.
      self.class.instances.each do |pip_package|
        if pip_package.name == pypkg
          # Specify default upgrade-strategy
          if args.include? "--upgrade-strategy" or args.any? { |arg| arg.is_a?(Hash) and arg.key?("--upgrade-strategy")}
            args << '--upgrade'
          else
            args << '--upgrade' << '--upgrade-strategy' << 'only-if-needed'
          end
          break
        end
      end
      args << "#{@resource[:source]}@#{@resource[:ensure]}#egg=#{pypkg}"
    else
      args << "#{@resource[:source]}#egg=#{pypkg}"
    end
  else
    case @resource[:ensure]
    when String
      args << "#{pypkg}==#{@resource[:ensure]}"
    when :latest
      # Specify default upgrade-strategy
      if args.include? "--upgrade-strategy" or args.any? { |arg| arg.is_a?(Hash) and arg.key?("--upgrade-strategy")}
        args << '--upgrade' << pypkg
      else
        args << '--upgrade' << '--upgrade-strategy' << 'only-if-needed' << pypkg
      end
    else
      args << pypkg
    end
  end
  lazy_pip *args
end

#install_optionsObject



89
90
91
# File 'lib/puppet_x/counsyl/pip.rb', line 89

def install_options
  join_options(resource[:install_options])
end

#join_options(options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/puppet_x/counsyl/pip.rb', line 93

def join_options(options)
  return unless options

  options.collect do |val|
    case val
    when Hash
      val.keys.sort.collect do |k|
        "#{k}=#{val[k]}"
      end.join(' ')
    else
      val
    end
  end
end

#latestObject

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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/puppet_x/counsyl/pip.rb', line 7

def latest
  pypkg = @resource[:name].split('@')[0]
  if @resource.class.validattr?(:pypi)
    # Setting PyPI URL parameter (supported only on `venv_package`).
    pypi_url = @resource[:pypi]
  else
    # TODO: Support trying to use `--index-url` from install_options.
    pypi_url = 'https://pypi.python.org/pypi'
  end
  client = XMLRPC::Client.new2(pypi_url)
  client.http_header_extra = {'Content-Type' => 'text/xml'}
  client.timeout = 10
  self.debug "Querying latest for '#{pypkg}' from '#{pypi_url}'"
  result = client.call('package_releases', pypkg)
  result.first
rescue Timeout::Error => detail
  raise Puppet::Error, "Timeout while contacting PyPI: #{detail}";
end

#uninstallObject

Uninstall a package. Uninstall won’t work reliably on Debian/Ubuntu unless this issue gets fixed. <bugs.debian.org/cgi-bin/bugreport.cgi?bug=562544>



81
82
83
# File 'lib/puppet_x/counsyl/pip.rb', line 81

def uninstall
  lazy_pip 'uninstall', '-y', '-q', @resource[:name]
end

#updateObject



85
86
87
# File 'lib/puppet_x/counsyl/pip.rb', line 85

def update
  install
end