Class: AptCache

Inherits:
Object
  • Object
show all
Defined in:
lib/xs/apt_cache.rb

Class Method Summary collapse

Class Method Details

.installed?(pkg) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/xs/apt_cache.rb', line 72

def self.installed?(pkg)
  Open3.popen3("dpkg -l #{pkg}") { |stdin, stdout, stderr|
    stdout.each { |line|
      if line =~ /^ii\s+#{Regexp.escape(pkg)}\s+/
        return true
      end
    }
  }

  false
end

.parse(pkgdesc, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/xs/apt_cache.rb', line 2

def self.parse(pkgdesc, options = {})
  if pkgdesc =~ /([\w\-\_\.\+]+)\s-\s(.+)$/
    pkgname = $1
    description = $2
    pattern = options[:pattern]

    if options[:complete] or options[:section]
      return AptCache.show(pkgname, options)
    elsif options[:description]
      return AptCache.show(pkgname, options) if description =~ /#{pattern}/
    else
      return AptCache.show(pkgname, options) if pkgname =~ /#{pattern}/
    end
  end

  false
end

.show(pkg, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
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
# File 'lib/xs/apt_cache.rb', line 20

def self.show(pkg, options)
  compact = options[:compact]

  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

    if options[:section]
        return if not hash[:section] =~ /#{options[:pattern]}/
    end

    installed = AptCache.installed?(pkg)

    if not compact
      print " \033[92m*\033[00m #{hash[:section]}/\033[01m#{hash[:package]}\033[00m  "
      if installed
        puts "[\033[01;32mINSTALLED\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"
    else
      print " \033[92m*\033[00m #{hash[:section]}/\033[01m#{hash[:package]}\033[00m (\033[32m#{hash[:version]}\033[00m): #{hash[:description]} "
      if installed
        print "[\033[01;32mINSTALLED\033[00m]"
      else
        print ""
      end
    end
    puts ""
    return true
  }

  false
end