Paludis Logo Introduction Examples
Bugs, Requests, Support C++ (core)
Overview Ruby (bindings)
FAQ Python (bindings)
Clients  
Configuration  
API  

example_selection.rb

This example demonstrates how to use the standard Selection, Generator and Filter classes.

00001 #!/usr/bin/env ruby
00002 # vim: set sw=4 sts=4 et tw=100 :
00003 
00004 =begin description
00005 This example demonstrates how to use the standard Selection, Generator and
00006 Filter classes.
00007 =end
00008 
00009 require 'Paludis'
00010 require 'example_command_line'
00011 
00012 include Paludis
00013 
00014 # Run a particular selection, and show its results.
00015 def show_selection env, selection
00016     # Selections support a crude form of stringification.
00017     puts "#{selection}:"
00018 
00019     # Usually the only thing clients will do with a Selection object is pass it
00020     # to Environment#[].
00021     ids = env[selection]
00022 
00023     # Show the results
00024     ids.each {|id| puts id}
00025     puts
00026 end
00027 
00028 exit_status = 0
00029 
00030 # We start with an Environment, respecting the user's '--environment' choice.
00031 env = EnvironmentFactory.instance.create(ExampleCommandLine.instance.environment)
00032 
00033 # Make some selections, and display what they give. The selection
00034 # object used determines the number and ordering of results. In the
00035 # simplest form, it takes a Generator as a parameter.
00036 show_selection(env, Selection::AllVersionsSorted.new(
00037         Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", env, []), nil, [])))
00038 
00039 # Generators can be passed through a Filter. The Selection optimises
00040 # the code internally to avoid doing excess work.
00041 show_selection(env, Selection::AllVersionsSorted.new(
00042         Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", env, []), nil, []) |
00043         Filter::InstalledAtRoot.new("/")))
00044 
00045 # Filters can be combined. Usually Filter::NotMasked should be combined
00046 # with Filter::SupportsAction.new(InstallAction), since installed packages
00047 # aren't masked.
00048 show_selection(env, Selection::AllVersionsSorted.new(
00049         Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", env, []), nil, []) |
00050         Filter::SupportsAction.new(InstallAction) |
00051         Filter::NotMasked.new))
00052 
00053 # Selection::AllVersionsSorted can be expensive, particularly if there
00054 # is no metadata cache. Consider using other Selection objects if
00055 # you only need the best matching or some arbitrary matching ID.
00056 show_selection(env, Selection::BestVersionOnly.new(
00057         Generator::Matches.new(parse_user_package_dep_spec("sys-apps/paludis", env, []), nil, []) |
00058         Filter::SupportsAction.new(InstallAction) |
00059         Filter::NotMasked.new))
00060 
00061 exit exit_status
00062