aboutsummaryrefslogtreecommitdiff
path: root/doc/api/ruby/example_action.rb
blob: 79223444cfffa2f8d60ada123cbd4833cf464f9c (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby
# vim: set sw=4 sts=4 et tw=100 :

=begin description
This example demonstarates how to use actions. It uses FetchAction to fetch source
files for all versions of sys-apps/paludis that support fetching
=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 'sys-apps/paludis'
ids = env[Selection::AllVersionsSorted.new(Generator::Matches.new(
    Paludis::parse_user_package_dep_spec("sys-apps/paludis", env, [])))]

# For each ID:
ids.each do | id |
    # Do we support a FetchAction? We find out by creating a SupportsActionTest object, and
    # querying via the PackageID#supports_action method.
    supports_fetch_action = SupportsActionTest.new(FetchAction)
    if not id.supports_action(supports_fetch_action)
        puts "ID #{id} does not support the fetch action."
    else
        puts "ID #{id} supports the fetch action, trying to fetch:"

        # Carry out a FetchAction. We need to specify various options when creating a FetchAction,
        # controlling whether safe resume is used and whether unneeded (e.g. due to disabled USE
        # flags) source files should still be fetched.
        fetch_action = FetchAction.new(FetchActionOptions.new({
            :fetch_unneeded => false,
            :safe_resume => true
        }))

        begin
            id.perform_action(fetch_action)

        rescue FetchActionError => e
            exit_status |= 1
            puts "Caught FetchActionError, with the following details:"

            e.failures.each do | f |
                print "  * File '#{f.target_file}': "
                need_comma = false

                if f.requires_manual_fetching?
                    print "requires manual fetching"
                    need_comma = true
                end

                if f.failed_automatic_fetching?
                    if need_comma
                        print ", "
                    end
                    print "failed automatic fetching"
                    need_comma = true
                end

                if not f.failed_integrity_checks.empty?
                    if need_comma
                        print ", "
                    end
                    print "failed integrity checks: #{f.failed_integrity_checks}"
                    need_comma = true
                end
                puts
            end
        end

    end

    puts
end

exit exit_status