Module: Puppet::Util::PTomulik::Package::Ports::PkgSearch

Includes:
Execution, Functions
Included in:
Puppet::Util::PTomulik::Package::Ports
Defined in:
lib/puppet/util/ptomulik/package/ports/pkg_search.rb

Overview

One method is useful for mortals, the #search_packages method.

Constant Summary collapse

PORTVERSION_MAX_NAMES =

Maximum number of package names provided to ‘portversion` when searching installed ports. Used by #portversion_search. If there is more names requested by caller, the search will be divided into mutliple stages (max 60 names per stage) to keep commandline of reasonable length at each stage.

60

Constants included from Functions

Functions::PKGNAME_RE, Functions::PORTNAME_RE, Functions::PORTORIGIN_RE, Functions::PORTVERSION_RE

Instance Method Summary collapse

Methods included from Execution

#execpipe, #execute_command

Methods included from Functions

#escape_pattern, #fullname_to_pattern, #mk_search_pattern, #options_files, #options_files_default_syntax, #options_files_portorigin, #pkgname?, #pkgname_to_pattern, #pkgng_active?, #port_dbdir, #portname?, #portname_to_pattern, #portorigin?, #portorigin_to_pattern, #portsdir, #split_pkgname, #strings_to_pattern

Instance Method Details

#execute_portversion(args, options = {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/puppet/util/ptomulik/package/ports/pkg_search.rb', line 179

def execute_portversion(args, options = {})
  key_check = determine_portversion_key_check(args)
  cmd = portversion_command(args, options)
  execute_command(cmd, options) do |process|
    process.each_line do |line|
      fields = line.strip.split(/\s+/,3)
      # portversion sometimes puts garbage to its output; we skip such lines
      if key_check.call(fields.first)
        yield fields
      end
    end
  end
end

#portversion_command(args, options) ⇒ Object

Return ‘portversion …’ command (as array) to be used with execpipe().



194
195
196
197
198
# File 'lib/puppet/util/ptomulik/package/ports/pkg_search.rb', line 194

def portversion_command(args, options)
  portversion = options[:portversion] ||
    (self.respond_to?(:command) ? command(:portversion) : 'portversion')
  [portversion, *(args.flatten)]
end

#portversion_search(names = nil, args = [], options = {}) { ... } ⇒ Object

Search for installed ports.

This method calls ‘portversion` to search through installed ports.

The yielded ‘fields` (see below) are formed as follows:

  • fields` - contains the portname, pkgname or portorigin depending on what was printed by `portversion` (depending on flags in `args`),

  • fields` (optional) - contains the port status, it’s a single character, one of ‘<`, `=`, `>`, `?`, `!`, `#`

  • fields` (optional) - contains additional information about available update for the package

What is particularly yielded by #portversion_search dependends on ‘args`. See [portversion(1)](www.freebsd.org/cgi/man.cgi?query=portversion&manpath=ports&sektion=1).

Supported ‘options` are:

  • :execpipe - custom execpipe method (used to call ‘portversion`),

  • options supported by the #portversion_command method.

Parameters:

  • names (Array|nil) (defaults to: nil)

    list of packages to search for; if ‘nil` - show all,

  • args (Array) (defaults to: [])

    an array of command line flags to ‘portversion`

  • options (Hash) (defaults to: {})

    additional options,

Yields:

  • Array for each found package. If ‘name` is `nil`: an array of `fields` (up to 3) returned by `portversion` for each package; if `name` is not `nil`: a 2-element array in form `[name, fields]` for each package found by `portversion`, where name is one of the `names` and `fields` are values printed by `portversion` in consecutive columns (up to 3).



123
124
125
126
127
128
129
130
131
132
# File 'lib/puppet/util/ptomulik/package/ports/pkg_search.rb', line 123

def portversion_search(names=nil, args=[], options={})
  if names
    names = sort_names_for_portversion(names)
    names.each_slice(PORTVERSION_MAX_NAMES) do |slice|
      portversion_search_1(slice, args, options) { |xfields| yield xfields }
    end
  else
    execute_portversion(args, options) { |fields| yield fields }
  end
end

#search_packages(names = nil, fields = PkgRecord.default_fields, options = {}) {|[String,PkgRecord]|PkgRecord| ... } ⇒ Object

Search installed packages

**Usage example 1**:

search_packages do |record|
  print "#{record.inspect}\n\n"
end

**Usage example 2**:

search_packages(['apache22', 'lang/ruby']) do |name,record|
  print "#{name}:\n"
  print "#{record.inspect}\n\n"
end

Parameters:

  • names (Array|nil) (defaults to: nil)

    list of package names (may mix portorigins, pkgnames and portnames); if ‘nil` - yield all installed packages,

  • fields (Array) (defaults to: PkgRecord.default_fields)

    list of fields to be included in resultant records,

  • options (defaults to: {})

    additional options

Yields:

  • ([String,PkgRecord]|PkgRecord)

    for each found package; the second form appears at output if ‘names` were not provided (or were `nil`).



41
42
43
44
45
46
47
48
49
# File 'lib/puppet/util/ptomulik/package/ports/pkg_search.rb', line 41

def search_packages(names=nil, fields=PkgRecord.default_fields, options={})
  amend = names ? lambda {|r| r[1].amend!(fields)} :
                  lambda {|r| r.amend!(fields)}
  search_fields = PkgRecord.determine_search_fields(fields)
  search_packages_1(names,search_fields,options) do |record|
    amend.call(record)
    yield record
  end
end

#sort_names_for_portversion(names) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/puppet/util/ptomulik/package/ports/pkg_search.rb', line 167

def sort_names_for_portversion(names)
  # XXX: portversion (at least 2.4.11) sorts its output by pkgname/portname,
  # so we must do the same with input list to match ones to the others; this
  # is horrible and there are no docs saying that this sorting method is
  # guaranted; for now we just have to live with this uncertainity.
  names.sort{ |a,b|
    a = a.split('/').last if portorigin?(a)
    b = b.split('/').last if portorigin?(b)
    a <=> b
  }
end