def check_package_state(package)
Chef::Log.debug("#{@new_resource} checking package status for #{package}")
installed = false
shell_out!("apt-cache#{expand_options(default_release_options)} policy #{package}").stdout.each_line do |line|
case line
when /^\s{2}Installed: (.+)$/
installed_version = $1
if installed_version == '(none)'
Chef::Log.debug("#{@new_resource} current version is nil")
@current_resource.version(nil)
else
Chef::Log.debug("#{@new_resource} current version is #{installed_version}")
@current_resource.version(installed_version)
installed = true
end
when /^\s{2}Candidate: (.+)$/
candidate_version = $1
if candidate_version == '(none)'
@is_virtual_package = true
showpkg = shell_out!("apt-cache showpkg #{package}").stdout
providers = Hash.new
showpkg.rpartition(/Reverse Provides:? #{$/}/)[2].each_line do |line|
provider, version = line.split
providers[provider] = version
end
num_providers = providers.length
raise Chef::Exceptions::Package, "#{@new_resource.package_name} has no candidate in the apt-cache" if num_providers == 0
raise Chef::Exceptions::Package, "#{@new_resource.package_name} is a virtual package provided by #{num_providers} packages, you must explicitly select one to install" if num_providers > 1
Chef::Log.info("#{@new_resource} is a virtual package, actually acting on package[#{providers.keys.first}]")
installed = check_package_state(providers.keys.first)
else
Chef::Log.debug("#{@new_resource} candidate version is #{$1}")
@candidate_version = $1
end
end
end
return installed
end