aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Richard Brown <rbrown@exherbo.org> 2007-11-07 09:27:51 +0000
committerAvatar Richard Brown <rbrown@exherbo.org> 2007-11-07 09:27:51 +0000
commit92b12ddcea4a14a7b54af0d14c8f0cc57ce787ea (patch)
tree7581e4381578b75352d6a37d845b2284b0b74575
parent587e7d6ca580c9a5fd174cfb9efccd741c8fe33a (diff)
downloadpaludis-92b12ddcea4a14a7b54af0d14c8f0cc57ce787ea.tar.gz
paludis-92b12ddcea4a14a7b54af0d14c8f0cc57ce787ea.tar.xz
(ruby) Add FetchActionFailure
-rw-r--r--doc/api/ruby/example_action.rb64
-rw-r--r--ruby/action.cc175
-rw-r--r--ruby/action_TEST.rb33
-rw-r--r--ruby/paludis_ruby.cc55
-rw-r--r--ruby/paludis_ruby.hh1
5 files changed, 296 insertions, 32 deletions
diff --git a/doc/api/ruby/example_action.rb b/doc/api/ruby/example_action.rb
index 962db8b..742e34f 100644
--- a/doc/api/ruby/example_action.rb
+++ b/doc/api/ruby/example_action.rb
@@ -34,40 +34,40 @@ ids.each do | id |
:safe_resume => true
}))
-### begin
+ 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
+ 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
diff --git a/ruby/action.cc b/ruby/action.cc
index 1a0b74b..d2d0c6c 100644
--- a/ruby/action.cc
+++ b/ruby/action.cc
@@ -2,6 +2,7 @@
/*
* Copyright (c) 2007 Ciaran McCreesh
+ * Copyright (c) 2007 Richard Brown
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -34,6 +35,7 @@ namespace
static VALUE c_action;
static VALUE c_fetch_action;
static VALUE c_fetch_action_options;
+ static VALUE c_fetch_action_failure;
const FetchActionOptions
value_to_fetch_action_options(VALUE v)
@@ -65,6 +67,21 @@ namespace
}
}
+ const FetchActionFailure
+ value_to_fetch_action_failure(VALUE v)
+ {
+ if (rb_obj_is_kind_of(v, c_fetch_action_failure))
+ {
+ FetchActionFailure * v_ptr;
+ Data_Get_Struct(v, FetchActionFailure, v_ptr);
+ return *v_ptr;
+ }
+ else
+ {
+ rb_raise(rb_eTypeError, "Can't convert %s into FetchActionFailure", rb_obj_classname(v));
+ }
+ }
+
VALUE
supports_fetch_action_init(int, VALUE *, VALUE self)
{
@@ -208,6 +225,132 @@ namespace
return fetch_action_options_to_value(tr1::static_pointer_cast<FetchAction>(*p)->options);
}
+ VALUE
+ fetch_action_failure_init(int, VALUE *, VALUE self)
+ {
+ return self;
+ }
+
+ /*
+ * call-seq:
+ * FetchActionFailure.new(target_file, requires_manual_fetching, failed_automatic_fetching, failed_integrity_checks) -> FetchActionFailure
+ * FetchActionFailure.new(Hash) -> FetchActionFailure
+ *
+ * FetchActionFailure.new can either be called with all parameters in order, or with one hash
+ * parameter, where the hash keys are symbols with the names above.
+ */
+ VALUE
+ fetch_action_failure_new(int argc, VALUE *argv, VALUE self)
+ {
+ FetchActionFailure * ptr(0);
+ try
+ {
+ std::string v_target_file;
+ bool v_requires_manual_fetching;
+ bool v_failed_automatic_fetching;
+ std::string v_failed_integrity_checks;
+
+ if (1 == argc && rb_obj_is_kind_of(argv[0], rb_cHash))
+ {
+ if (Qnil == rb_hash_aref(argv[0], ID2SYM(rb_intern("target_file"))))
+ rb_raise(rb_eArgError, "Missing Parameter: target_file");
+ if (Qnil == rb_hash_aref(argv[0], ID2SYM(rb_intern("requires_manual_fetching"))))
+ rb_raise(rb_eArgError, "Missing Parameter: requires_manual_fetching");
+ if (Qnil == rb_hash_aref(argv[0], ID2SYM(rb_intern("failed_automatic_fetching"))))
+ rb_raise(rb_eArgError, "Missing Parameter: failed_automatic_fetching");
+ if (Qnil == rb_hash_aref(argv[0], ID2SYM(rb_intern("failed_integrity_checks"))))
+ rb_raise(rb_eArgError, "Missing Parameter: failed_integrity_checks");
+ VALUE v0 = (rb_hash_aref(argv[0], ID2SYM(rb_intern("target_file"))));
+ v_target_file = StringValuePtr(v0);
+ v_requires_manual_fetching =
+ rb_hash_aref(argv[0], ID2SYM(rb_intern("requires_manual_fetching"))) != Qfalse;
+ v_failed_automatic_fetching =
+ rb_hash_aref(argv[0], ID2SYM(rb_intern("failed_automatic_fetching"))) != Qfalse;
+ VALUE v1 = rb_hash_aref(argv[0], ID2SYM(rb_intern("failed_integrity_checks")));
+ v_failed_integrity_checks = StringValuePtr(v1);
+ }
+ else if (4 == argc)
+ {
+ v_target_file = StringValuePtr(argv[0]);
+ v_requires_manual_fetching = argv[1] != Qfalse;
+ v_failed_automatic_fetching = argv[2] != Qfalse;
+ v_failed_integrity_checks = StringValuePtr(argv[3]);
+ }
+ else
+ {
+ rb_raise(rb_eArgError, "FetchActionOptions expects one or four arguments, but got %d",argc);
+ }
+
+ ptr = new FetchActionFailure(FetchActionFailure::create()
+ .target_file(v_target_file)
+ .requires_manual_fetching(v_requires_manual_fetching)
+ .failed_automatic_fetching(v_failed_automatic_fetching)
+ .failed_integrity_checks(v_failed_integrity_checks)
+ );
+
+ VALUE tdata(Data_Wrap_Struct(self, 0, &Common<FetchActionFailure>::free, ptr));
+ rb_obj_call_init(tdata, argc, argv);
+ return tdata;
+ }
+ catch (const std::exception & e)
+ {
+ delete ptr;
+ exception_to_ruby_exception(e);
+ }
+ }
+
+ /*
+ * Document-method: target_file
+ *
+ * call-seq: target_file -> String
+ *
+ * Our target file.
+ */
+ /*
+ * Document-method: failed_integrity_checks
+ *
+ * call-seq: failed_integrity_checks -> String
+ *
+ * Our failed integrity checks.
+ */
+ template <std::string FetchActionFailure::* m_>
+ struct FailureStringFetch
+ {
+ static VALUE
+ fetch(VALUE self)
+ {
+ FetchActionFailure * ptr;
+ Data_Get_Struct(self, FetchActionFailure, ptr);
+ return rb_str_new2(((*ptr).*m_).c_str());
+ }
+ };
+
+ /*
+ * Document-method: requires_manual_fetching?
+ *
+ * call-seq: requires_manual_fetching? -> true or false
+ *
+ * Do we require manual fetching?
+ */
+ /*
+ * Document-method: failed_automatic_fetching?
+ *
+ * call-seq: failed_automatic_fetching? -> true or false
+ *
+ * Did we fail automatic fetching?
+ */
+ template <bool FetchActionFailure::* m_>
+ struct FailureBoolFetch
+ {
+ static VALUE
+ fetch(VALUE self)
+ {
+ FetchActionFailure * ptr;
+ Data_Get_Struct(self, FetchActionFailure, ptr);
+ return (((*ptr).*m_)) != Qfalse;
+ }
+ };
+
void do_register_action()
{
/*
@@ -255,6 +398,23 @@ namespace
rb_define_method(c_fetch_action_options, "initialize", RUBY_FUNC_CAST(&fetch_action_options_init), -1);
rb_define_method(c_fetch_action_options, "fetch_unneeded", RUBY_FUNC_CAST(&fetch_action_options_fetch_unneeded), 0);
rb_define_method(c_fetch_action_options, "safe_resume", RUBY_FUNC_CAST(&fetch_action_options_safe_resume), 0);
+
+ /*
+ * Document-class: Paludis::FetchActionFailure
+ *
+ * A failed fetch action part.
+ */
+ c_fetch_action_failure = rb_define_class_under(paludis_module(), "FetchActionFailure", rb_cObject);
+ rb_define_singleton_method(c_fetch_action_failure, "new", RUBY_FUNC_CAST(&fetch_action_failure_new), -1);
+ rb_define_method(c_fetch_action_failure, "initialize", RUBY_FUNC_CAST(&fetch_action_failure_init), -1);
+ rb_define_method(c_fetch_action_failure, "target_file",
+ RUBY_FUNC_CAST((&FailureStringFetch<&FetchActionFailure::target_file>::fetch)), 0);
+ rb_define_method(c_fetch_action_failure, "requires_manual_fetching?",
+ RUBY_FUNC_CAST((&FailureBoolFetch<&FetchActionFailure::requires_manual_fetching>::fetch)), 0);
+ rb_define_method(c_fetch_action_failure, "failed_automatic_fetching?",
+ RUBY_FUNC_CAST((&FailureBoolFetch<&FetchActionFailure::failed_automatic_fetching>::fetch)), 0);
+ rb_define_method(c_fetch_action_failure, "failed_integrity_checks",
+ RUBY_FUNC_CAST((&FailureStringFetch<&FetchActionFailure::failed_integrity_checks>::fetch)), 0);
}
}
@@ -289,6 +449,21 @@ paludis::ruby::value_to_action(VALUE v)
}
}
+VALUE
+paludis::ruby::fetch_action_failure_to_value(const FetchActionFailure & m)
+{
+ FetchActionFailure * m_ptr(new FetchActionFailure(m));
+ try
+ {
+ return Data_Wrap_Struct(c_fetch_action_failure, 0, &Common<FetchActionFailure>::free, m_ptr);
+ }
+ catch (const std::exception & e)
+ {
+ delete m_ptr;
+ exception_to_ruby_exception(e);
+ }
+}
+
RegisterRubyClass::Register paludis_ruby_register_action PALUDIS_ATTRIBUTE((used))
(&do_register_action);
diff --git a/ruby/action_TEST.rb b/ruby/action_TEST.rb
index 8c9d692..87672d1 100644
--- a/ruby/action_TEST.rb
+++ b/ruby/action_TEST.rb
@@ -52,6 +52,39 @@ module Paludis
end
end
+ class TestCase_FetchActionFailure < Test::Unit::TestCase
+ def test_create
+ assert_kind_of FetchActionFailure, FetchActionFailure.new('target_file', false, false, 'fic')
+ assert_kind_of FetchActionFailure, FetchActionFailure.new(
+ {
+ :requires_manual_fetching => false, :failed_automatic_fetching => false,
+ :target_file => 'target_file', :failed_integrity_checks => 'fic'
+ }
+ )
+ end
+
+ def test_methods_hash_args
+ failure = FetchActionFailure.new(
+ {
+ :requires_manual_fetching => false, :failed_automatic_fetching => false,
+ :target_file => 'target_file', :failed_integrity_checks => 'fic'
+ }
+ )
+ assert_equal 'target_file', failure.target_file;
+ assert ! failure.requires_manual_fetching?
+ assert ! failure.failed_automatic_fetching?
+ assert_equal 'fic', failure.failed_integrity_checks;
+ end
+
+ def test_methods_4_args
+ failure = FetchActionFailure.new('target_file', false, false, 'fic')
+ assert_equal 'target_file', failure.target_file;
+ assert ! failure.requires_manual_fetching?
+ assert ! failure.failed_automatic_fetching?
+ assert_equal 'fic', failure.failed_integrity_checks;
+ end
+ end
+
class TestCase_FetchAction < Test::Unit::TestCase
def test_create
assert_kind_of FetchAction, FetchAction.new(FetchActionOptions.new(false, false))
diff --git a/ruby/paludis_ruby.cc b/ruby/paludis_ruby.cc
index a0e86ce..6f1c3cd 100644
--- a/ruby/paludis_ruby.cc
+++ b/ruby/paludis_ruby.cc
@@ -67,6 +67,8 @@ namespace
static VALUE c_use_requirements_not_met_error;
static VALUE c_downgrade_not_allowed_error;
static VALUE c_no_destination_error;
+ static VALUE c_fetch_action_error;
+ static VALUE c_action_error;
static VALUE c_environment;
static VALUE c_no_config_environment;
@@ -169,6 +171,19 @@ void paludis::ruby::exception_to_ruby_exception(const std::exception & ee)
rb_raise(c_config_file_error, dynamic_cast<const paludis::ConfigFileError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::ConfigurationError *>(&ee))
rb_raise(c_configuration_error, dynamic_cast<const paludis::ConfigurationError *>(&ee)->message().c_str());
+ else if (0 != dynamic_cast<const paludis::FetchActionError *>(&ee))
+ {
+ VALUE ex_args[2];
+ ex_args[1] = rb_ary_new();
+ const FetchActionError * e = dynamic_cast<const paludis::FetchActionError *>(&ee);
+ for (Sequence<FetchActionFailure>::ConstIterator f(e->failures()->begin()), f_end(e->failures()->end()) ;
+ f != f_end ; ++f)
+ rb_ary_unshift(ex_args[1], fetch_action_failure_to_value(*f));
+ rb_exc_raise(rb_class_new_instance(2, ex_args, c_fetch_action_error));
+
+ }
+ else if (0 != dynamic_cast<const paludis::ActionError *>(&ee))
+ rb_raise(c_action_error, dynamic_cast<const paludis::ActionError *>(&ee)->message().c_str());
else if (0 != dynamic_cast<const paludis::Exception *>(&ee))
rb_raise(rb_eRuntimeError, "Caught paludis::Exception: %s (%s)",
@@ -242,6 +257,29 @@ has_query_property_error_query(VALUE self)
return rb_attr_get(self, rb_intern("query"));
}
+static VALUE
+fetch_action_error_init(int argc, VALUE* argv, VALUE self)
+{
+ VALUE failures;
+
+ failures = (argc > 1) ? argv[--argc] : Qnil;
+ rb_call_super(argc, argv);
+ rb_iv_set(self, "failures", failures);
+ return self;
+}
+
+/*
+ * call-seq:
+ * failures -> Array
+ *
+ * Our failures
+ */
+VALUE
+fetch_action_error_failures(VALUE self)
+{
+ return rb_attr_get(self, rb_intern("failures"));
+}
+
void PALUDIS_VISIBLE paludis::ruby::init()
{
/*
@@ -303,6 +341,23 @@ void PALUDIS_VISIBLE paludis::ruby::init()
c_downgrade_not_allowed_error = rb_define_class_under(c_paludis_module, "DowngradeNotAllowedError", c_dep_list_error);
c_no_destination_error = rb_define_class_under(c_paludis_module, "NoDestinationError", c_dep_list_error);
+ /*
+ * Document-class: Paludis::ActionError
+ *
+ * Base class for Action related errors
+ */
+ c_action_error = rb_define_class_under(c_paludis_module, "ActionError", rb_eRuntimeError);
+
+ /*
+ * Document-class: Paludis::FetchActionError
+ *
+ * Thrown if a PackageID fails to perform a FetchAction.
+ */
+ c_fetch_action_error = rb_define_class_under(c_paludis_module, "FetchActionError", c_action_error);
+ rb_define_module_function(c_fetch_action_error, "failures", RUBY_FUNC_CAST(&fetch_action_error_failures), 0);
+ rb_define_method(c_fetch_action_error, "initialize", RUBY_FUNC_CAST(&fetch_action_error_init), -1);
+ rb_define_method(c_fetch_action_error, "failures", RUBY_FUNC_CAST(&fetch_action_error_failures), 0);
+
rb_define_module_function(c_paludis_module, "match_package", RUBY_FUNC_CAST(&paludis_match_package), 3);
rb_define_const(c_paludis_module, "Version", rb_str_new2((stringify(PALUDIS_VERSION_MAJOR) + "."
diff --git a/ruby/paludis_ruby.hh b/ruby/paludis_ruby.hh
index eaa1222..c8220c5 100644
--- a/ruby/paludis_ruby.hh
+++ b/ruby/paludis_ruby.hh
@@ -88,6 +88,7 @@ namespace paludis
VALUE contents_to_value(tr1::shared_ptr<const Contents>);
VALUE repository_mask_info_to_value(tr1::shared_ptr<const RepositoryMaskInfo>);
VALUE metadata_key_to_value(tr1::shared_ptr<const MetadataKey> m);
+ VALUE fetch_action_failure_to_value(const FetchActionFailure &);
#ifdef ENABLE_RUBY_QA
VALUE qa_message_to_value(const QAMessage &);
#endif