example_contents.rb
This example demonstrates how to use contents. It displays details about the files installed by 'sys-apps/paludis'.
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 contents. It displays details about 00006 the files installed by 'sys-apps/paludis'. 00007 =end 00008 00009 require 'Paludis' 00010 require 'example_command_line' 00011 00012 include Paludis 00013 00014 exit_status = 0 00015 00016 # We start with an Environment, respecting the user's '--environment' choice. 00017 env = EnvironmentFactory.instance.create(ExampleCommandLine.instance.environment) 00018 00019 # Fetch package IDs for installed 'sys-apps/paludis' 00020 ids = env[Selection::AllVersionsSorted.new( 00021 Generator::Matches.new(Paludis::parse_user_package_dep_spec("sys-apps/paludis", env, []), []) | 00022 Filter::InstalledAtRoot.new("/"))] 00023 00024 # For each ID: 00025 ids.each do | id | 00026 # Do we have a contents key? PackageID _key methods can return Nil. 00027 if not id.contents_key 00028 puts "ID '#{id}' does not provide a contents key." 00029 else 00030 puts "ID '#{id}' provides contents key:" 00031 00032 # Contents is made up of a collection of ContentsEntry instances. 00033 id.contents_key.value.each do | c | 00034 00035 # Some ContentsEntry subclasses contain more information than others 00036 if c.kind_of? ContentsOtherEntry 00037 puts "other #{c.location_key.value}" 00038 00039 elsif c.kind_of? ContentsFileEntry 00040 puts "file #{c.location_key.value}" 00041 00042 elsif c.kind_of? ContentsDirEntry 00043 puts "dir #{c.location_key.value}" 00044 00045 elsif c.kind_of? ContentsSymEntry 00046 puts "sym #{c.location_key.value} -> #{c.target_key.value}" 00047 00048 else 00049 puts "unknown #{c}" 00050 end 00051 end 00052 00053 puts 00054 end 00055 end 00056 00057 exit exit_status 00058
