head	1.11;
access;
symbols
	RELEASE_2_4_6:1.8
	RELEASE_2_4_5:1.8
	RELEASE_2_4_3:1.6
	RELEASE_2_4_2:1.6
	RELEASE_2_4_1:1.6
	RELEASE_2_4_0:1.6
	RELEASE_2_3_2:1.6
	RELEASE_2_3_1:1.5
	STABLE_2_3:1.5.0.2
	RELEASE_2_3_0:1.5
	RELEASE_2_2_6:1.4.2.1
	RELEASE_2_2_5:1.4.2.1
	RELEASE_2_2_4:1.5
	RELEASE_2_2_3:1.4.2.1
	STABLE_2_2:1.4.0.2
	RELEASE_2_2_2:1.4
	RELEASE_2_2_1:1.4
	RELEASE_2_2_0:1.4
	RELEASE_2_1_3_3:1.1.1.1
	RELEASE_2_1_7:1.4
	RELEASE_2_1_6:1.3
	RELEASE_2_1_3_2:1.1.1.1
	RELEASE_2_1_5:1.3
	RELEASE_2_1_3_1:1.1.1.1.0.4
	STABLE:1.1.1.1.0.2
	PKGTOOLS_2_1_4:1.2
	PKGTOOLS_2_1_3:1.1.1.1
	sem:1.1.1;
locks; strict;
comment	@# @;


1.11
date	2011.08.19.00.57.22;	author stas;	state dead;
branches;
next	1.10;

1.10
date	2010.11.08.04.40.27;	author stas;	state Exp;
branches;
next	1.9;

1.9
date	2008.07.17.00.26.38;	author sem;	state Exp;
branches;
next	1.8;

1.8
date	2008.07.09.15.31.18;	author sem;	state Exp;
branches;
next	1.7;

1.7
date	2008.06.23.13.10.30;	author sem;	state Exp;
branches;
next	1.6;

1.6
date	2008.01.08.11.32.27;	author sem;	state Exp;
branches;
next	1.5;

1.5
date	2007.02.22.13.36.34;	author sem;	state Exp;
branches;
next	1.4;

1.4
date	2006.08.13.11.15.21;	author sem;	state Exp;
branches
	1.4.2.1;
next	1.3;

1.3
date	2006.06.30.20.09.28;	author sem;	state Exp;
branches;
next	1.2;

1.2
date	2006.06.14.11.52.38;	author sem;	state Exp;
branches;
next	1.1;

1.1
date	2006.06.13.12.59.00;	author sem;	state Exp;
branches
	1.1.1.1;
next	;

1.4.2.1
date	2007.02.22.14.00.37;	author sem;	state Exp;
branches;
next	;

1.1.1.1
date	2006.06.13.12.59.00;	author sem;	state Exp;
branches;
next	;


desc
@@


1.11
log
@- The project data has moved to github (https://github.com/stass/pkgtools).
@
text
@#!/usr/bin/env ruby
# -*- ruby -*-
#
# Copyright (c) 2000-2004 Akinori MUSHA
# Copyright (c) 2006-2008 Sergey Matveychuk <sem@@FreeBSD.org>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD: projects/pkgtools/bin/portversion,v 1.10 2010-11-08 04:40:27 stas Exp $
#

MYREVISION = %w$Rev: 71 $[1]
MYDATE = %w$Date: 2008/07/09 15:31:18 $[1]
MYNAME = File.basename($0)

require "optparse"
require "pkgtools"

COLUMNSIZE = 24
NEXTLINE = "\n%*s" % [5 + COLUMNSIZE, '']

SUGGESTIONS = {
  ?< => 'needs updating (port has %s)',
  ?= => 'up-to-date with port',
  ?> => 'succeeds port (port has %s)',
  ?? => 'error - origin not found',
  ?! => 'error - port broken',
  ?# => 'error - origin not recorded; run pkgdb -F',
}

def init_global
  $command = 'portupgrade'
  $command_output = false
  $display = :name
  $exclude_packages = []
  $ignore_moved = false
  $inv_limit_chars = nil
  $limit_chars = nil
  $noconfig = false
  $recursive = false
  $sanity_check = true
  $tempdir = ""
  $test_version = false
  $upward_recursive = false
  $verbose_output = :default
end

def main(argv)
  usage = <<-"EOF"
usage: #{MYNAME} [-hOqv] [-l limit_chars] [-L inv_limit_chars]
        [pkgname_glob ...]
       #{MYNAME} [-v] -t ver0 ver1 [ver2 ...]
  EOF

  banner = <<-"EOF"
#{MYNAME} #{Version} (#{MYDATE})

#{usage}
    EOF

  dry_parse = true

  OptionParser.new(banner, COLUMNSIZE) do |opts|
    opts.def_option("-h", "--help", "Show this message") {
      print opts
      exit 0
    }

    opts.def_option("-c", "--command-output", "Enable command output") { |v|
      $command_output = v
    }

    opts.def_option("-C", "--command-args=ARGS", "Specify the arguments for portupgrade in command#{NEXTLINE}output (default: none)") { |command_args|
      $command = 'portupgrade ' + command_args
    }

    opts.def_option("-F", "--fullname", "Display a package full name") {
      $display = :fullname
    }

    opts.def_option("--ignore-moved",
		    "Ignore MOVED file") { |v|
      $ignore_moved = v
    }

    opts.def_option("-l", "--limit=CHARS", "Only include the packages with the specified#{NEXTLINE}status flags") { |v|
      $limit_chars = v
      # XXX: workaround: optparse treats `-l=' as '-l ""'
      if $limit_chars.empty?
	$limit_chars = '='
      end
    }

    opts.def_option("-L", "--inv-limit=CHARS", "Exclude the packages with the specified#{NEXTLINE}status flags") { |v|
      $inv_limit_chars = v
      # XXX: workaround: optparse treats `-L=' as '-L ""'
      if $inv_limit_chars.empty?
	$inv_limit_chars = '='
      end
    }

    opts.def_option("-o", "--origin", "Display package origin instead of package name") {
      $display = :origin
    }

    opts.def_option("-O", "--omit-check", "Omit sanity checks for dependencies") {
      $sanity_check = false
    }

    opts.def_option("-q", "--noconfig", "Do not read pkgtools.conf") { |v|
      $noconfig = v
    }

    opts.def_option("-Q", "--quiet", "Do not display status chars") {
      $verbose_output = :quiet
    }

    opts.def_option("-r", "--recursive", "Check for all those depending on the given#{NEXTLINE}packages as well") { |v|
      $recursive
    }

    opts.def_option("-R", "--upward-recursive", "Check for all those required by the given#{NEXTLINE}packages as well") { |v|
      $upward_recursive = v
    }

    opts.def_option("-t", "--test=VERSION", "Compare the version with the following one(s),#{NEXTLINE}and print the result(s)") { |v|
      $test_version = v
      $test_version = PkgVersion.new($test_version)
    }

    opts.def_option("-v", "--verbose", "Be verbose") { |v|
      $verbose = v
      $verbose_output = :verbose
      $display = :fullname
    }

    opts.def_option("-x", "--exclude=GLOB", "Exclude packages matching the specified glob#{NEXTLINE}pattern") { |arg|
      begin
	pattern = parse_pattern(arg)
      rescue RegexpError => e
	warning_message e.message.capitalize
	break
      end

      $exclude_packages |= $pkgdb.glob(pattern, false) unless dry_parse
    }

    opts.def_tail_option '
pkgname_glob is one of these: a full pkgname, a pkgname w/o version,
a shell glob pattern in which you can use wildcards *, ?, and [..],
an extended regular expression preceded by a colon (:), or a date range
specification preceded by either < or >.  See pkg_glob(1) for details.
If none is given, all the installed packages are checked.

Environment Variables [default]:
    PKGTOOLS_CONF            configuration file [$PREFIX/etc/pkgtools.conf]
    PKG_DBDIR                packages DB directory [/var/db/pkg]
    PORTSDIR                 ports directory [/usr/ports]
    PORTS_DBDIR              ports db directory [$PORTSDIR]
    PORTS_INDEX              ports index file [$PORTSDIR/INDEX]'

    all = '*'

    argv << all

    tasks = []

    begin
      init_global
      init_pkgtools_global

      rest = opts.order(*argv)

      unless $noconfig
	init_global
	load_config
      else
	argv = rest
      end

      dry_parse = false

      opts.order!(argv)

      opts.order(*argv) do |arg|
	next if arg.empty?

	if arg.equal? all
	  pattern = arg

	  $recursive = false
	  $upward_recursive = false
	else
	  all.replace ''

	  pattern = $pkgdb.strip(arg, true)

	  if pattern.nil?
	    pattern = arg

	    if pattern.include?('/')
	      # `|| arg' just in case
	      pattern = $pkgdb.strip(pattern) || arg
	    end
	  end

	  begin
	    pattern = parse_pattern(pattern)
	  rescue RegexpError => e
	    warning_message e.message.capitalize
	    next
	  end

	  if $test_version
	    version = PkgVersion.new(arg)
	    x = $test_version <=> version
	    sign = if x < 0 then ?< elsif x == 0 then ?= else ?> end

	    if $verbose
	      printf "%s %c %s\n", $test_version, sign, version
	    else
	      printf "%c\n", sign
	    end
	    
	    next
	  end
	end

	list = []

	begin
	  $pkgdb.glob(pattern, false).each do |pkgname|
	    list |= $pkgdb.recurse(pkgname, $recursive, $upward_recursive, $sanity_check)
	  end
	rescue => e
	  STDERR.puts e.message
	  exit 1
	end

	if list.empty?
	  warning_message "No matching package found: #{arg}"
	  next
	end

	list -= $exclude_packages

	if list.empty?
          warning_message "No matching packages left after exclusion: #{arg}"
	  next
	end

	tasks |= list
      end
    rescue OptionParser::ParseError => e
      STDERR.puts "#{MYNAME}: #{e}", usage
      exit 64
    rescue ArgumentError => e
      STDERR.puts "#{MYNAME}: #{e}", usage
      exit 64
    end

    tasks -= $exclude_packages

    check_pkgs tasks.sort.collect {|pkgname| PkgInfo.new(pkgname) }

    if $command_output
      printf %Q`\nif [ X"$pkgs" != X"" ]; then\n  %s "$@@" $pkgs\nfi\n`, $command
    end
  end

  0
end

def check_pkg(pkg, origin = nil)
  pkgname = pkg.fullname

  if origin.nil?
    return nil, ?#
  end

  if portinfo = $portsdb[origin]
    newpkg = portinfo.pkgname
  elsif $portsdb.exist?(origin, true)
    pkgname = $portsdb.exist?(origin) or return nil, ?!
    newpkg = PkgInfo.new(pkgname)	# raises ArgumentError
  else
    return nil, ??
  end

  cmp = newpkg.version <=> pkg.version

  if cmp > 0 then
    return newpkg, ?<
  elsif cmp < 0 then
    return newpkg, ?>
  end

  return newpkg, ?=
rescue
  return nil, ?!
end

def check_pkgs(pkgs)
  pkgs.each do |pkg|
    if !$ignore_moved and \
       !config_ignore_moved?(pkg) and \
       (moved = $portsdb.moved.trace(pkg.origin)) and \
       (o = moved.last.to)
      origin = o
    else
      origin = pkg.origin
    end

    newpkg, sign = check_pkg(pkg, origin)

    next if $command_output && sign != ?<

    next if ($limit_chars && !$limit_chars.include?(sign)) ||
      ($inv_limit_chars && $inv_limit_chars.include?(sign))

    held = config_held?(pkg)

    suggestion = SUGGESTIONS[sign] % [ newpkg ? newpkg.version : nil ]

    if held
      suggestion = '[held] ' + suggestion
    end

    if $command_output
      printf %Q`#\n#  %s%s\n#  %s\n#\n%spkgs="$pkgs %s"\n\n`,
	pkg.name,
	held ? ' [held]' : '',
	suggestion,
	held ? '# ' : '',
	pkg.fullname
    else
	  case $verbose_output
	  when :verbose then
	printf "%-26s  %c  %s %s\n",
	  pkg.send($display),
	  sign,
	  suggestion,
	  (origin != pkg.origin) ? "(=> '#{origin}')" : ''
	  when :default then
	printf "%-26s  %c\n",
	  pkg.send($display),
	  sign
	  when :quiet then
	printf "%s\n", pkg.send($display)
      end
    end
  end
rescue PortsDB::IndexFileError
  warning_message "Error reading the ports INDEX."
rescue PortsDB::DBError
  warning_message "Error reading the ports database."
end

if $0 == __FILE__
  set_signal_handlers

  exit(main(ARGV) || 1)
end
@


1.10
log
@- After portversion get -F option, -v stops showing full packages names.
  Fix it.

Submitted by:	sem
@
text
@d30 1
a30 1
# $FreeBSD$
@


1.9
log
@Adapt to ruby 1.9
@
text
@d30 2
d155 1
@


1.8
log
@* Document new options
* Refine descriptions for the options
@
text
@d32 1
a32 1
MYDATE = %w$Date: 2008/06/23 13:10:30 $[1]
d88 2
a89 2
    opts.def_option("-c", "--command-output", "Enable command output") {
      |$command_output|
d92 1
a92 2
    opts.def_option("-C", "--command-args=ARGS", "Specify the arguments for portupgrade in command#{NEXTLINE}output (default: none)") {
      |command_args|
d101 2
a102 2
		    "Ignore MOVED file") {
      |$ignore_moved|
d105 2
a106 2
    opts.def_option("-l", "--limit=CHARS", "Only include the packages with the specified#{NEXTLINE}status flags") {
      |$limit_chars|
d113 2
a114 2
    opts.def_option("-L", "--inv-limit=CHARS", "Exclude the packages with the specified#{NEXTLINE}status flags") {
      |$inv_limit_chars|
d129 2
a130 2
    opts.def_option("-q", "--noconfig", "Do not read pkgtools.conf") {
      |$noconfig|
d137 2
a138 2
    opts.def_option("-r", "--recursive", "Check for all those depending on the given#{NEXTLINE}packages as well") {
      |$recursive|
d141 2
a142 2
    opts.def_option("-R", "--upward-recursive", "Check for all those required by the given#{NEXTLINE}packages as well") {
      |$upward_recursive|
d145 2
a146 2
    opts.def_option("-t", "--test=VERSION", "Compare the version with the following one(s),#{NEXTLINE}and print the result(s)") {
      |$test_version|
d150 2
a151 2
    opts.def_option("-v", "--verbose", "Be verbose") {
      |$verbose|
d155 1
a155 2
    opts.def_option("-x", "--exclude=GLOB", "Exclude packages matching the specified glob#{NEXTLINE}pattern") {
      |arg|
@


1.7
log
@Add a patch from Romain Tartiere= <romain@@blogreen.org>.
It add -F, -o, -Q options for full package name, origin and quiet output.
@
text
@d32 1
a32 1
MYDATE = %w$Date: 2008/01/08 11:32:27 $[1]
d97 1
a97 1
    opts.def_option("-F", "--fullname", "Display package full-name.") {
d122 1
a122 1
    opts.def_option("-o", "--origin", "Display package origin instead of package name.") {
d126 1
a126 1
    opts.def_option("-O", "--omit-check", "Omit sanity checks for dependencies.") {
d134 1
a134 1
    opts.def_option("-Q", "--quiet", "Only display package name") {
@


1.6
log
@New year in COPYRIGHTS
@
text
@d32 1
a32 1
MYDATE = %w$Date: 2007/02/22 13:36:34 $[1]
d53 1
d64 1
d97 4
d122 4
d134 4
d153 1
d357 2
a358 1
      if $verbose then
d360 1
a360 1
	  pkg.fullname,
d364 1
a364 1
      else
d366 1
a366 1
	  pkg.name,
d368 2
@


1.5
log
@Update COPYRIGHTS
@
text
@d5 1
a5 1
# Copyright (c) 2006,2007 Sergey Matveychuk <sem@@FreeBSD.org>
d32 1
a32 1
MYDATE = %w$Date: 2006/08/13 11:15:21 $[1]
@


1.4
log
@Adjust Copyright notices
@
text
@d5 1
a5 1
# Copyright (c) 2006 Sergey Matveychuk <sem@@FreeBSD.org>
d32 1
a32 1
MYDATE = %w$Date: 2006/06/30 20:09:28 $[1]
@


1.4.2.1
log
@Merge patches from HEAD
@
text
@d5 1
a5 1
# Copyright (c) 2006,2007 Sergey Matveychuk <sem@@FreeBSD.org>
d32 1
a32 1
MYDATE = %w$Date: 2007/02/22 13:36:34 $[1]
@


1.3
log
@Get back second init_global. It's necessary for doubled options like -DD.
Really it's needed only for portupgrade and portsclean, but be consistent.
@
text
@d5 1
d32 1
a32 1
MYDATE = %w$Date: 2006/06/14 11:52:38 $[1]
@


1.2
log
@- Refactoring:
  * Get rid of warnings with ruby --debug
  * Rework ugly init_pkgtools_global call. Now it's more consistent.
@
text
@d31 1
a31 1
MYDATE = %w$Date: 2006/06/13 12:59:00 $[1]
d179 1
@


1.1
log
@Initial revision
@
text
@d31 1
a31 1
MYDATE = %w$Date: 2006-01-04 01:25:27 +0900 (Wed, 04 Jan 2006) $[1]
d59 1
d174 1
a178 1
	init_global
@


1.1.1.1
log
@Initial import
@
text
@@
