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

example_package_id.rb

This example demonstrates how to use PackageID. See "example_action.rb" for more on actions. See "example_metadata_key.rb" for more on metadata keys. See "example_mask.rb" for more on masks.

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 PackageID.
00006 
00007 See "example_action.rb" for more on actions. See "example_metadata_key.rb" for more on 
00008 metadata keys. See "example_mask.rb" for more on masks.
00009 =end
00010 
00011 require 'Paludis'
00012 require 'example_command_line'
00013 
00014 include Paludis
00015 
00016 exit_status = 0
00017 
00018 # We start with an Environment, respecting the user's '--environment' choice.
00019 env = EnvironmentFactory.instance.create(ExampleCommandLine.instance.environment)
00020 
00021 # Fetch package IDs for installed 'sys-apps/paludis'
00022 ids = env[Selection::AllVersionsSorted.new(
00023     Generator::Matches.new(Paludis::parse_user_package_dep_spec("sys-apps/paludis", env, []), nil, []))]
00024 
00025 # For each ID:
00026 ids.each do | id |
00027     puts "#{id}:"
00028 
00029     # Start by outputting some basic properties:
00030     puts "    Name: ".ljust(40) + id.name
00031     puts "    Version: ".ljust(40) + id.version.to_s
00032     puts "    Repository: ".ljust(40) + id.repository_name
00033 
00034     # The PackageID.canonical_form method should be used when
00035     # outputting a package
00036     puts "    PackageIDCanonicalForm::Full: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::Full)
00037     puts "    PackageIDCanonicalForm::Version: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::Version)
00038     puts "    PackageIDCanonicalForm::NoVersion: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::NoVersion)
00039 
00040     # Let's see what keys we have. Other examples cover keys in depth,
00041     # so we'll just use the basic methods here.
00042     puts "    Keys: ".ljust(40)
00043     id.each_metadata do |key|
00044         puts "        #{key.raw_name}: ".ljust(40) + key.human_name
00045     end
00046 
00047     # And what about masks? Again, these are covered in depth
00048     # elsewhere.
00049     if id.masked?
00050         puts "    Masks: ".ljust(40)
00051         id.masks.each do |mask|
00052             puts "        #{mask.key}: ".ljust(40) + mask.description
00053         end
00054     end
00055 
00056     # Let's see which actions we support. There's no particularly nice
00057     # way of doing this, since it's not something we'd expect to be
00058     # doing.
00059     actions = []
00060     actions << "install" if id.supports_action(SupportsActionTest.new(InstallAction))
00061     actions << "uninstall" if id.supports_action(SupportsActionTest.new(UninstallAction))
00062     actions << "pretend" if id.supports_action(SupportsActionTest.new(PretendAction))
00063     actions << "config" if id.supports_action(SupportsActionTest.new(ConfigAction))
00064     actions << "fetch" if id.supports_action(SupportsActionTest.new(FetchAction))
00065     actions << "info" if id.supports_action(SupportsActionTest.new(InfoAction))
00066 
00067     puts "    Actions: ".ljust(40) + actions.join(' ')
00068 
00069     puts
00070 end
00071 
00072 exit exit_status
00073