example_action.rb
This example demonstarates how to use actions. It uses FetchAction to fetch source files for all versions of sys-apps/paludis that support fetching
00001 #!/usr/bin/env ruby 00002 # vim: set sw=4 sts=4 et tw=100 : 00003 00004 =begin description 00005 This example demonstarates how to use actions. It uses FetchAction to fetch source 00006 files for all versions of sys-apps/paludis that support fetching 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 'sys-apps/paludis' 00020 ids = env[Selection::AllVersionsSorted.new(Generator::Matches.new( 00021 Paludis::parse_user_package_dep_spec("sys-apps/paludis", env, []), nil, []))] 00022 00023 # For each ID: 00024 ids.each do | id | 00025 # Do we support a FetchAction? We find out by creating a SupportsActionTest object, and 00026 # querying via the PackageID#supports_action method. 00027 supports_fetch_action = SupportsActionTest.new(FetchAction) 00028 if not id.supports_action(supports_fetch_action) 00029 puts "ID #{id} does not support the fetch action." 00030 else 00031 puts "ID #{id} supports the fetch action, trying to fetch:" 00032 00033 # Carry out a FetchAction. We need to specify various options when creating a FetchAction, 00034 # controlling whether safe resume is used and whether unneeded (e.g. due to disabled USE 00035 # flags) and unmirrorable source files should still be fetched. 00036 fetch_action = FetchAction.new(FetchActionOptions.new({ 00037 :exclude_unmirrorable => false, 00038 :fetch_unneeded => false, 00039 :safe_resume => true 00040 })) 00041 00042 begin 00043 id.perform_action(fetch_action) 00044 00045 rescue FetchActionError => e 00046 exit_status |= 1 00047 puts "Caught FetchActionError, with the following details:" 00048 00049 e.failures.each do | f | 00050 print " * File '#{f.target_file}': " 00051 need_comma = false 00052 00053 if f.requires_manual_fetching? 00054 print "requires manual fetching" 00055 need_comma = true 00056 end 00057 00058 if f.failed_automatic_fetching? 00059 if need_comma 00060 print ", " 00061 end 00062 print "failed automatic fetching" 00063 need_comma = true 00064 end 00065 00066 if not f.failed_integrity_checks.empty? 00067 if need_comma 00068 print ", " 00069 end 00070 print "failed integrity checks: #{f.failed_integrity_checks}" 00071 need_comma = true 00072 end 00073 puts 00074 end 00075 end 00076 00077 end 00078 00079 puts 00080 end 00081 00082 exit exit_status 00083
