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

example_environment.rb

This example demonstrates how to use EnvironmentFactory and the resultant Environment.

00001 #!/usr/bin/env ruby
00002 # vim: set sw=4 sts=4 et tw=80 :
00003 
00004 =begin description
00005 This example demonstrates how to use EnvironmentFactory and the resultant
00006 Environment.
00007 =end
00008 
00009 require 'Paludis'
00010 require 'example_command_line'
00011 
00012 include Paludis
00013 
00014 exit_status = 0
00015 
00016 # We use EnvironmentFactory to construct an environment from the user's
00017 # --environment commandline choice. With an empty string, this uses the
00018 # distribution-defined default environment. With a non-empty string, it
00019 # is split into two parts upon the first colon (if there is no colon,
00020 # the second part is considered empty). The first part is the name of
00021 # the environment class to use (e.g. 'paludis', 'portage') and the
00022 # second part is passed as parameters to be handled by that
00023 # environment's constructor.
00024 env = EnvironmentFactory.instance.create(ExampleCommandLine.instance.environment)
00025 
00026 # A lot of the Environment members aren't very useful to clients. The mask
00027 # related methods are used by PackageID, and shouldn't usually be called
00028 # directly from clients. The system information and mirror functions are mostly
00029 # for use by Repository subclasses. The [] operator, for selections, is covered
00030 # in other examples. That leaves the package database, sets and (currently,
00031 # although this may well change in the future) use flag queries. The package
00032 # database has its own examples, so we'll start with sets:
00033 
00034 world = env.set('world')
00035 if (world)
00036     # see examples_dep_tree.rb for how to make use of this set
00037     puts "World set exists"
00038 else
00039     puts "No world set defined"
00040 end
00041 
00042 # And use flags, for which we need package IDs:
00043 ids = env[Selection::AllVersionsSorted.new(
00044     Generator::Matches.new(Paludis::parse_user_package_dep_spec('sys-apps/paludis', env, [])) |
00045     Filter::SupportsAction.new(InstalledAction))]
00046 
00047 if (ids.length > 0)
00048     id = ids.last
00049     print "Use flag 'ruby' for ID '#{id.to_s}' is "
00050     puts env.query_use('ruby', id) ? 'enabled' : 'disabled'
00051 end
00052