Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/portupgrade
Path: blob/master/bin/portsdb
102 views
#!/usr/bin/env ruby
# -*- ruby -*-
# vim: set sts=2 sw=2 ts=8 et:
#
# Copyright (c) 2000-2004 Akinori MUSHA
# Copyright (c) 2006-2008 Sergey Matveychuk <[email protected]>
# Copyright (c) 2009-2012 Stanislav Sedov <[email protected]>
# Copyright (c) 2012 Bryan Drewery <[email protected]>
#
# 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.
#

MYNAME = File.basename($0)

require "optparse"
require "pkgtools"

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

def init_global
  $exclude_ports = []
  $fetch_index = false
  $force = false
  $master_recursive = false
  $noconfig = false
  $recursive = false
  #$sanity_check = true
  #$slave_recursive = false
  $tempdir = ""
  $update_db = false
  $update_index = false
  $upward_recursive = false
end

def main(argv)
  usage = <<-"EOF"
usage: #{MYNAME} [-hfFMqrRuU] [-x port_glob] [port_glob ...]
    EOF

  banner = <<-"EOF"
#{MYNAME} #{Version} (#{PkgTools::DATE})

#{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("-f", "--force", "Force to update database regardless of timestamps") { |v|
      $force = v
    }

    opts.def_option("-F", "--fetchindex", "Fetch INDEX") { |b|
      $update_index = $fetch_index = b
    }

    opts.def_option("-M", "--master-recursive", "List all the master ports of the given packages#{NEXTLINE}as well") { |v|
      $master_recursive = v
    }

#    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("-r", "--recursive", "List all those depending on the given packages#{NEXTLINE}as well") { |v|
      $recursive = v
    }

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

#    opts.def_option("-S", "--slave-recursive", "List all the slave ports of the given packages#{NEXTLINE}as well") { |v|
#      $slave_recursive = v
#    }

    opts.def_option("-u", "--update", "Update INDEX.db") { |v|
      $update_db = v
    }

    opts.def_option("-U", "--updateindex", "Update INDEX") { |v|
      $update_index = v
    }

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

      $exclude_ports |= $portsdb.glob(pattern).collect { |portinfo| portinfo.origin }
    }

    opts.def_tail_option '
If a port_glob is specified, the ports matching the pattern are listed.
A pattern can be either in the pkgname from or in the origin form including
a slash (\'/\'), and in which you can use wildcards *, ?, and [..],
or an extended regular expression preceded by a colon (:).

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

    if argv.empty?
      print opts
      return 0
    end

    begin
      init_global
      init_pkgtools_global

      rest = opts.order(*argv)

      unless $noconfig
	load_config
      else
	argv = rest
      end

      dry_parse = false

      opts.order!(argv)

      if $update_index
	$portsdb.update($fetch_index)
      end

      if $update_db
	$portsdb.update_db($force)
      end

      list = []
      
      opts.order(*argv) do |arg|
	pattern = $portsdb.strip(arg) || arg	# allow pkgname_glob

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

	$portsdb.glob(pattern) do |portinfo|
	  list |= $portsdb.recurse(portinfo, $recursive, $upward_recursive)
	end
      end

      ports = []

      list.each do |portinfo|
	if $master_recursive
	  if masters = portinfo.masters
	    masters.each do |master|
	      ports << $portsdb.origin(master)
	    end
	  end
	end

        ports << portinfo.origin
      end

      ports -= $exclude_ports

      $portsdb.sort!(ports)

      puts(*ports) unless ports.empty?
    rescue OptionParser::ParseError => e
      STDERR.puts "#{MYNAME}: #{e}", usage
      exit 64
    rescue => e
      STDERR.puts "#{MYNAME}: #{e}"
      exit 1
    end
  end

  0
end

if $0 == __FILE__
  set_signal_handlers

  exit(main(ARGV) || 1)
end