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 = EnvironmentMaker.instance.make_from_spec(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", [])))] 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 " Slot: ".ljust(40) + id.slot 00033 puts " Repository: ".ljust(40) + id.repository_name 00034 00035 # The PackageID.canonical_form method should be used when 00036 # outputting a package 00037 puts " PackageIDCanonicalForm::Full: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::Full) 00038 puts " PackageIDCanonicalForm::Version: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::Version) 00039 puts " PackageIDCanonicalForm::NoVersion: ".ljust(40) + id.canonical_form(PackageIDCanonicalForm::NoVersion) 00040 00041 # Let's see what keys we have. Other examples cover keys in depth, 00042 # so we'll just use the basic methods here. 00043 puts " Keys: ".ljust(40) 00044 id.each_metadata do |key| 00045 puts " #{key.raw_name}: ".ljust(40) + key.human_name 00046 end 00047 00048 # And what about masks? Again, these are covered in depth 00049 # elsewhere. 00050 if id.masked? 00051 puts " Masks: ".ljust(40) 00052 id.masks.each do |mask| 00053 puts " #{mask.key}: ".ljust(40) + mask.description 00054 end 00055 end 00056 00057 # Let's see which actions we support. There's no particularly nice 00058 # way of doing this, since it's not something we'd expect to be 00059 # doing. 00060 actions = [] 00061 actions << "install" if id.supports_action(SupportsActionTest.new(InstallAction)) 00062 actions << "installed" if id.supports_action(SupportsActionTest.new(InstalledAction)) 00063 actions << "uninstall" if id.supports_action(SupportsActionTest.new(UninstallAction)) 00064 actions << "pretend" if id.supports_action(SupportsActionTest.new(PretendAction)) 00065 actions << "config" if id.supports_action(SupportsActionTest.new(ConfigAction)) 00066 actions << "fetch" if id.supports_action(SupportsActionTest.new(FetchAction)) 00067 actions << "info" if id.supports_action(SupportsActionTest.new(InfoAction)) 00068 00069 puts " Actions: ".ljust(40) + actions.join(' ') 00070 00071 puts 00072 end 00073 00074 exit exit_status 00075
