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
-
#execute_make_search(key, pattern, fields = PortRecord.default_fields, options = {}) {|PortRecord| ... } ⇒ Object
Search ports using ‘“make search”` command.
-
#make_search_command(key, pattern, fields, options) ⇒ Array
Construct ‘make search` command to be executed with execpipe.
-
#search_ports(names, fields = PortRecord.default_fields, options = {}) {|String, PortRecord| ... } ⇒ Object
Search ports by name.
-
#search_ports_by(key, values, fields = PortRecord.default_fields, options = {}) {|String, PortRecord| ... } ⇒ Object
Search ports by either ‘:name`, `:pkgname`, `:portname` or `:portorigin`.
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`.
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, ={}) # 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,) 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.
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, ) make = [: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
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, ={}) origins = names.select{|name| portorigin?(name)} do_search_ports(:portorigin,origins,fields,,names) do |name,record| yield [name,record] end do_search_ports(:pkgname,names.dup,fields,,names) do |name,record| yield [name,record] end do_search_ports(:portname,names,fields,) 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
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, ={}) 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, ) do |record| val = record[key].dup record.delete(key) if delete_key yield [val, record] end end end |