4
class Macports < Chef::Provider::Package
5
def load_current_resource
6
@current_resource = Chef::Resource::Package.new(@new_resource.name)
7
@current_resource.package_name(@new_resource.package_name)
9
@current_resource.version(current_installed_version)
10
Chef::Log.debug("Current version is #{@current_resource.version}") if @current_resource.version
12
@candidate_version = macports_candidate_version
14
if !@new_resource.version and !@candidate_version
15
raise Chef::Exceptions::Package, "Could not get a candidate version for this package -- #{@new_resource.name} does not seem to be a valid package!"
18
Chef::Log.debug("MacPorts candidate version is #{@candidate_version}") if @candidate_version
23
def current_installed_version
24
command = "port installed #{@new_resource.package_name}"
25
output = get_response_from_command(command)
28
output.each_line do |line|
29
match = line.match(/^.+ @([^\s]+) \(active\)$/)
30
response = match[1] if match
35
def macports_candidate_version
36
command = "port info --version #{@new_resource.package_name}"
37
output = get_response_from_command(command)
39
match = output.match(/^version: (.+)$/)
41
match ? match[1] : nil
44
def install_package(name, version)
45
unless @current_resource.version == version
46
command = "port install #{name}"
47
command << " @#{version}" if version and !version.empty?
54
def purge_package(name, version)
55
command = "port uninstall #{name}"
56
command << " @#{version}" if version and !version.empty?
62
def remove_package(name, version)
63
command = "port deactivate #{name}"
64
command << " @#{version}" if version and !version.empty?
71
def upgrade_package(name, version)
72
# Saving this to a variable -- weird rSpec behavior
73
# happens otherwise...
74
current_version = @current_resource.version
76
if current_version.nil? or current_version.empty?
77
# Macports doesn't like when you upgrade a package
78
# that hasn't been installed.
79
install_package(name, version)
80
elsif current_version != version
82
:command => "port upgrade #{name} @#{version}"
88
def get_response_from_command(command)
90
status = popen4(command) do |pid, stdin, stdout, stderr|
94
raise Chef::Exceptions::Package, "Could not read from STDOUT on command: #{command}"
97
unless status.exitstatus == 0 || status.exitstatus == 1
98
raise Chef::Exceptions::Package, "#{command} failed - #{status.insect}!"