Module: Puppet::Util::PTomulik::Packagex::Portsx::PortSearch

Includes:
Functions
Included in:
Puppet::Util::PTomulik::Packagex::Portsx
Defined in:
lib/puppet/util/ptomulik/packagex/portsx/port_search.rb

Overview

Utilities for searching through FreeBSD ports INDEX (based on ‘make search` command).

Two methods are useful for mortals: #search_ports and #search_ports_by.

Constant Summary collapse

MAKE_SEARCH_MAX_NAMES =

Maximum number of package names provided to ‘make search` when searching ports. Used by #search_ports_by. 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 Functions

#escape_pattern, #fullname_to_pattern, #mk_search_pattern, #options_files, #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_make_search(key, pattern, fields = PortRecord.default_fields, options = {}) {|PortRecord| ... } ⇒ Object

Search ports using ‘“make search”` command.

By default, the search returns only existing ports. Ports marked as ‘’Moved:‘` are filtered out from output (see `options` parameter). To include also `’Moved:‘’ fields in output, set ‘:moved` option to `true`.

Parameters:

  • key (Symbol)

    search key, see #make_search_command,

  • pattern (String)

    search pattern, see #make_search_command,

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

    fields to be requested, see #make_search_command,

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

    additional options to alter method behavior,

Options Hash (options):

Yields:

  • (PortRecord)

    records extracted from the output of ‘make search` command.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/puppet/util/ptomulik/packagex/portsx/port_search.rb', line 158

def execute_make_search(key, pattern, fields=PortRecord.default_fields, options={})

  # We must validate `key` here; `make search` prints error message when key
  # is wrong but exits with 0 (EXIT_SUCCESS), so we have no error indication
  # from make (we use execpipe which mixes stderr and stdout).
  unless PortRecord.search_keys.include?(key)
    raise ArgumentError, "Invalid search key #{key}"
  end

  search_fields = PortRecord.determine_search_fields(fields,key)
  do_execute_make_search(key,pattern,search_fields,options) do |record|
    # add extra fields requested by user
    record.amend!(fields)
    yield record
  end
end

#make_search_command(key, pattern, fields, options) ⇒ Array

Construct ‘make search` command to be executed with execpipe.

Parameters:

  • key (Symbol)

    search key to be used in ‘#key=#pattern` expression of `make search` command; if __key__ is `’name’‘ for example, then the resultan search command will be `make … search name=…`

  • pattern (Sting)

    search pattern to be used in ‘#key=#pattern` expression of `make search` command; if __key__ is `’name’‘ and __pattern__ is `’foo’‘ for example, then the resultant `make search command` will be `make … search name=foo …`,

  • fields (Array)

    fields to be requested; if __fields__ are ‘[’f1’,‘f2’,…]‘ for example, then the resultant search command will be `make search … display=f1,f2,…`,

  • options (Hash)

    additional options to alter methods behavior,

Options Hash (options):

  • :make (String)

    absolute path to ‘make` program,

Returns:

  • (Array)

    the command to be executed.



218
219
220
221
222
223
224
225
# File 'lib/puppet/util/ptomulik/packagex/portsx/port_search.rb', line 218

def make_search_command(key, pattern, fields, options)
  make = options[:make] ||
    (self.respond_to?(:command) ? command(:make) : 'make')
  args = ['-C', portsdir, 'search', "#{key}='#{pattern}'"]
  fields = fields.join(',') unless fields.is_a?(String)
  args << "display='#{fields}'"
  [make,*args]
end

#search_ports(names, fields = PortRecord.default_fields, options = {}) {|String, PortRecord| ... } ⇒ Object

Search ports by name.

This method performs ‘search_ports_by(:portorigin, …)` for `names` representing port origins, then `search_ports_by(:pkgname, …)` and finally `search_ports_by(:portname,…)` for the remaining `names`.

**Usage example**:

names = ['apache22-2.2.26', 'ruby19']
search_ports(names) do |name,record|
  print "name: #{name}\n" # from the names list
  print "portorigin: #{record[:portorigin]}\n"
  print "\n"
end

Parameters:

  • names (Array)

    a list of port names, may mix portorigins, pkgnames and portnames,

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

    a list of fields to be included in the resultant records,

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

Yields:

  • (String, PortRecord)

    for each port found in the ports INDEX



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/util/ptomulik/packagex/portsx/port_search.rb', line 37

def search_ports(names, fields=PortRecord.default_fields, options={})
  origins = names.select{|name| portorigin?(name)}
  do_search_ports(:portorigin,origins,fields,options,names) do |name,record|
    yield [name,record]
  end
  do_search_ports(:pkgname,names.dup,fields,options,names) do |name,record|
    yield [name,record]
  end
  do_search_ports(:portname,names,fields,options) do |name,record|
    yield [name,record]
  end
end

#search_ports_by(key, values, fields = PortRecord.default_fields, options = {}) {|String, PortRecord| ... } ⇒ Object

Search ports by either ‘:name`, `:pkgname`, `:portname` or `:portorigin`.

This method uses ‘make search` command to search through ports INDEX.

Example:

search_ports_by(:portname, ['apache22', 'apache24']) do |k,r|
  print "#{k}:\n#{r.inspect}\n\n"
end

Parameters:

  • key (Symbol)

    search key, one of ‘:name`, `:pkgname`, `:portname` or `:portorigin`.

  • values (Array)

    determines what to find, it is either sting or list of strings determining the name or names of packages to lookup for,

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

    additional options to alter method behavior, see #execute_make_search,

Yields:

  • (String, PortRecord)

    for each port found by ‘make search`.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/puppet/util/ptomulik/packagex/portsx/port_search.rb', line 98

def search_ports_by(key, values, fields=PortRecord.default_fields, options={})
  key = key.downcase.intern unless key.instance_of?(Symbol)
  search_key = determine_search_key(key)

  delete_key = if fields.include?(key)
    false
  else
    fields << key
    true
  end

  # query in chunks to keep command-line of reasonable length
  values.each_slice(MAKE_SEARCH_MAX_NAMES) do |slice|
    pattern = mk_search_pattern(key,slice)
    execute_make_search(search_key, pattern, fields, options) do |record|
      val = record[key].dup
      record.delete(key) if delete_key
      yield [val, record]
    end
  end
end