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, []), nil, []) | 00022 Filter::InstalledAtRoot.new("/"))] 00023 00024 # For each ID: 00025 ids.each do | id | 00026 # Do we have contents? This can return nil 00027 contents = id.contents 00028 if contents 00029 puts "ID '#{id}' does not provide a contents key." 00030 else 00031 puts "ID '#{id}' provides contents key:" 00032 00033 # Contents is made up of a collection of ContentsEntry instances. 00034 contents.each do | c | 00035 00036 # Some ContentsEntry subclasses contain more information than others 00037 if c.kind_of? ContentsOtherEntry 00038 puts "other #{c.location_key.parse_value}" 00039 00040 elsif c.kind_of? ContentsFileEntry 00041 puts "file #{c.location_key.parse_value}" 00042 00043 elsif c.kind_of? ContentsDirEntry 00044 puts "dir #{c.location_key.parse_value}" 00045 00046 elsif c.kind_of? ContentsSymEntry 00047 puts "sym #{c.location_key.parse_value} -> #{c.target_key.parse_value}" 00048 00049 else 00050 puts "unknown #{c}" 00051 end 00052 end 00053 00054 puts 00055 end 00056 end 00057 00058 exit exit_status 00059