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 = EnvironmentMaker.instance.make_from_spec(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", [])))] 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) source files should still be fetched. 00036 fetch_action = FetchAction.new(FetchActionOptions.new({ 00037 :fetch_unneeded => false, 00038 :safe_resume => true 00039 })) 00040 00041 begin 00042 id.perform_action(fetch_action) 00043 00044 rescue FetchActionError => e 00045 exit_status |= 1 00046 puts "Caught FetchActionError, with the following details:" 00047 00048 e.failures.each do | f | 00049 print " * File '#{f.target_file}': " 00050 need_comma = false 00051 00052 if f.requires_manual_fetching? 00053 print "requires manual fetching" 00054 need_comma = true 00055 end 00056 00057 if f.failed_automatic_fetching? 00058 if need_comma 00059 print ", " 00060 end 00061 print "failed automatic fetching" 00062 need_comma = true 00063 end 00064 00065 if not f.failed_integrity_checks.empty? 00066 if need_comma 00067 print ", " 00068 end 00069 print "failed integrity checks: #{f.failed_integrity_checks}" 00070 need_comma = true 00071 end 00072 puts 00073 end 00074 end 00075 00076 end 00077 00078 puts 00079 end 00080 00081 exit exit_status 00082
