aboutsummaryrefslogtreecommitdiff
path: root/doc/api/ruby/example_contents.rb
blob: d653fe57a4cd49ebf6d05edb037edd478033d68f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env ruby
# vim: set sw=4 sts=4 et tw=100 :

=begin description
This example demonstrates how to use contents. It displays details about
the files installed by 'sys-apps/paludis'.
=end

require 'Paludis'
require 'example_command_line'

include Paludis

exit_status = 0

# We start with an Environment, respecting the user's '--environment' choice.
env = EnvironmentFactory.instance.create(ExampleCommandLine.instance.environment)

# Fetch package IDs for installed 'sys-apps/paludis'
ids = env[Selection::AllVersionsSorted.new(
    Generator::Matches.new(Paludis::parse_user_package_dep_spec("sys-apps/paludis", env, []), nil, []) |
    Filter::InstalledAtRoot.new("/"))]

# For each ID:
ids.each do | id |
    # Do we have a contents key? PackageID _key methods can return Nil.
    if not id.contents_key
        puts "ID '#{id}' does not provide a contents key."
    else
        puts "ID '#{id}' provides contents key:"

        # Contents is made up of a collection of ContentsEntry instances.
        id.contents_key.value.each do | c |

            # Some ContentsEntry subclasses contain more information than others
            if c.kind_of? ContentsOtherEntry
                puts "other     #{c.location_key.value}"

            elsif c.kind_of? ContentsFileEntry
                puts "file      #{c.location_key.value}"

            elsif c.kind_of? ContentsDirEntry
                puts "dir       #{c.location_key.value}"

            elsif c.kind_of? ContentsSymEntry
                puts "sym       #{c.location_key.value} -> #{c.target_key.value}"

            else
                puts "unknown   #{c}"
            end
        end

        puts
    end
end

exit exit_status