All pastes #834096 Raw Edit

cosa

public text v1 · immutable
#834096 ·published 2007-12-27 22:48 UTC
rendered paste body
#!/usr/bin/env ruby
# Guardar todo en un fichero en /usr/local/bin/cosa, darle permisos con
# chmod a+x /usr/local/bin/cosa y ejecutar en una consola con: cosa paquete

require 'open3'

if ARGV.empty?
	puts "Usage: #{File.basename $0} <pattern>"
	exit -1
end

TARGET = ARGV.first
APT_SEARCH = "apt-cache search #{TARGET}"

class AptCache
	def self.parse(pkgdesc)
		if pkgdesc =~ /([\w\-\_\.\+]+)\s-\s(.+)$/
			AptCache.show $1
		end
	end
	
	def self.show(pkg)
		Open3.popen3("apt-cache show #{pkg}") { |stdin, stdout, stderr|
			hash = {}
			stdout.each { |line|
				if line =~ /([\w\-]+):\s(.+)$/
					hash[$1.downcase.to_sym] = $2
				end
			}
			
			if hash.empty?
				puts "FAILED: #{pkg}"
			end
			
			installed = AptCache.installed?(pkg)
			
			print " \033[92m*\033[00m #{hash[:section]}/\033[01m#{hash[:package]}\033[00m  "
			if installed
				puts "[\033[42mINSTALLED\033[00m]"
			else
				puts ""
			end
			puts "   \033[32mVersion:\033[00m \033[96m#{hash[:version]}\033[00m"
			
			if hash.has_key? :homepage
				puts "   \033[32mHomepage:\033[00m #{hash[:homepage]}"
			end
			
			puts "   \033[32mDescription:\033[00m #{hash[:description]}"
			puts "   \033[32mDownload size:\033[00m #{((hash[:size].to_i + 1023) / 1024)} KiB"
			puts ""
		}
	end
	
	
	def self.installed?(pkg)
		Open3.popen3("dpkg -l #{pkg}") { |stdin, stdout, stderr|
			stdout.each { |line|
				if line =~ /^ii\s+#{pkg}\s+/
					return true
				end
			}
		}
		
		false
	end
end


begin
Open3.popen3(APT_SEARCH) { |stdin, stdout, stderr|
	stdout.each { |line|
		AptCache.parse(line)
	}
}
rescue Interrupt
end