tshm
1/31/2014 - 8:25 AM

cleandeb.rb

#!/usr/bin/ruby
class Clean
  def initialize()
    @list = "| ls /var/cache/apt/archives/*.deb"
    @th = 2
    @version = []
    @cur_name = "" 
  end

  def main()
    IO.foreach(@list) {|line|
      name,ver = line.scan(/([^_]*)_(.*)\.deb/)[0]
      if (name == @cur_name)
        @version.push(ver)
      else
        spit(name, ver)
      end
    }
    spit()
  end

  def spit(name="", ver="")
    @version.sort! {|a,b|
      system("dpkg --compare-versions #{a} '>' #{b}") == 0 ? 1:-1
    }
    @th.times { @version.pop }
    @version.each {|v| print "#{@cur_name}_#{v}.deb\n" }
    @cur_name = name
    @version.clear()
    @version.push(ver)
  end
end

a  = Clean.new
a.main()