listupgrade 20051121

portversion の出力じゃバージョン番号が分からなくてイヤんなので、かたくなにそれっぽいツールを自作してるという自己満足に浸るオレ様がきましたよ。

1年前の自分と比べると、

  1. 短いのにクラスとか使っちゃって OO かぶれのつもりかこの野郎
  2. でも短いメソッドに分解できて読みやすくて嬉しいわ
  3. ブロックを使って while を書かなくなった
  4. portversion と組み合わせた方が portupgrade -an 一本槍より少しは速かろう

の辺りに成長が見られるでしょうか。FreeBSD + portversion + portupgrade な環境にべったり依存したスクリプトなので使ってない人は華麗にスルーしてください。

#! /usr/bin/env ruby
# -*- ruby -*-

if ( (`which portversion`.chomp.size < 1) or
       (`which portupgrade`.chomp.size < 1) )
  abort "You need to install portupgrade first !"
end

puts "Have you executed PORTSNAP or CVSUP recently ?"
puts "Please wait for a while."

#
# Manipulating Upgradable Ports
#
class UpgradablePorts

  def initialize
    @needupgrade   = Hash.new()
    @pkgdb_correct = false

    make_list()
    store_versions()
  end # of new()

  #
  # List up upgradable ports with 'portversion' command
  #
  # without version detail
  #
  def make_list()
    open( '| portversion' ).each { |line|
      if ( /^([^\s]+)\s+\</ =~ line )
        @needupgrade.store( $1, false )
      end
    }
  end # of make_list()

  #
  # Storing two version infos, current installed one and latest one
  # with 'portupgrade -n' command
  #
  def store_versions()
    @needupgrade.each_key { |port|
      open( '| portupgrade -n ' + port + ' 2>&1' ).each { |line|
        if ( /Upgrading '(.*)' to '(.*)'/ =~ line )
          @needupgrade.store( $1, $2 )
        elsif ( /Listing the results/ =~ line )
          # This line will appear if portupgrade terminate normally.
          @pkgdb_correct = true
        end
      }
    }
  end # of store_versions()

  #
  # Output result
  #
  def output()
    if ( @needupgrade.size > 0 )
      maxlength = 0
      @needupgrade.each_key { |portname|
        if ( portname.length > maxlength )
          maxlength = portname.length
        end
      }

      @needupgrade.keys.sort.each { |port|
        if ( @needupgrade[port] != false )
          printf( "%-*s --> %s\n", maxlength, port, @needupgrade[port] )
        end
      }
    elsif ( @pkgdb_correct == true )
      puts "All ports are up-to-date ! Congratulation !"
    else
      puts "Something wrong with ``pkgdb'', maybe ? "
    end
  end # of output()

end # of class UpgradablePorts

#
# main()
#
list = UpgradablePorts.new()
list.output()

More