diff options
Diffstat (limited to '0.8.0/ruby')
28 files changed, 2644 insertions, 0 deletions
diff --git a/0.8.0/ruby/Makefile.am b/0.8.0/ruby/Makefile.am new file mode 100644 index 000000000..744be4911 --- /dev/null +++ b/0.8.0/ruby/Makefile.am @@ -0,0 +1,135 @@ +SUBDIRS = . demos + +AM_CXXFLAGS = -I$(top_srcdir) -I$(srcdir)/ \ + @PALUDIS_CXXFLAGS_WITHOUT_PEDANTIC@ \ + @PALUDIS_CXXFLAGS_NO_WREDUNDANT_DECLS@ \ + @PALUDIS_CXXFLAGS_NO_WOLD_STYLE_CAST@ + +DEFS= \ + -DSYSCONFDIR=\"$(sysconfdir)\" \ + -DDATADIR=\"$(datadir)\" \ + -DLIBEXECDIR=\"$(libexecdir)\" \ + -DLIBDIR=\"$(libdir)\" + +CLEANFILES = *~ gmon.out *.gcov *.gcno *.gcda *.o *.so .libs/*.so +MAINTAINERCLEANFILES = Makefile.in + +IF_RUBY_TESTS = \ + paludis_ruby_TEST.rb \ + log_TEST.rb \ + environment_TEST.rb \ + package_database_entry_TEST.rb \ + version_spec_TEST.rb \ + mask_reasons_TEST.rb \ + dep_atom_TEST.rb \ + package_database_TEST.rb \ + repository_TEST.rb + +IF_RUBY_SOURCES = \ + paludis_ruby.cc paludis_ruby.hh \ + environment.cc \ + package_database_entry.cc \ + version_spec.cc \ + mask_reasons.cc \ + dep_atom.cc \ + log.cc \ + package_database.cc \ + repository.cc + +EXTRA_DIST = $(IF_RUBY_TESTS) $(IF_RUBY_SOURCES) \ + environment_TEST_setup.sh environment_TEST_cleanup.sh \ + package_database_TEST_setup.sh package_database_TEST_cleanup.sh \ + repository_TEST_setup.sh repository_TEST_cleanup.sh + +TESTS_ENVIRONMENT = env \ + PALUDIS_NO_GLOBAL_HOOKS="yes" \ + PALUDIS_NO_XTERM_TITLES="yes" \ + PALUDIS_EBUILD_DIR="`$(top_srcdir)/ebuild/utils/canonicalise $(top_srcdir)/ebuild/`" \ + PALUDIS_EBUILD_DIR_FALLBACK="`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/ebuild/`" \ + PALUDIS_REPOSITORY_SO_DIR="`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/paludis/repositories`" \ + SYSCONFDIR="$(sysconfdir)" \ + LD_LIBRARY_PATH=`echo "\`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/paludis/.libs\`: \ + \`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/paludis/util/.libs\`: \ + \`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/paludis/environment/default/.libs\`: \ + \`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/paludis/environment/no_config/.libs\`" \ + | tr -d ' '` \ + bash $(top_srcdir)/test/run_test.sh "ruby -I ./.libs/" + +if ENABLE_RUBY + +OUR_OBJECTS = \ + environment.o \ + dep_atom.o \ + log.o \ + mask_reasons.o \ + package_database.o \ + package_database_entry.o \ + version_spec.o \ + repository.o \ + paludis_ruby.o + +TESTS = $(IF_RUBY_TESTS) +noinst_DATA = $(OUR_OBJECTS) +check_DATA = .libs/Paludis.so +rubylibdir = $(DESTDIR)/@RUBY_INSTALL_DIR@ +rubylib_DATA = Paludis.so + +OUR_CXXCOMPILE = $(CXX) -fPIC $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) \ + -I. -I@RUBY_SITEARCHDIR@ -I@RUBY_ARCHDIR@ -c + +paludis_ruby.o : paludis_ruby.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/paludis_ruby.cc + +environment.o : environment.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/environment.cc + +dep_atom.o : dep_atom.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/dep_atom.cc + +package_database_entry.o : package_database_entry.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/package_database_entry.cc + +package_database.o : package_database.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/package_database.cc + +version_spec.o : version_spec.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/version_spec.cc + +mask_reasons.o : mask_reasons.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/mask_reasons.cc + +log.o : log.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/log.cc + +repository.o : repository.cc paludis_ruby.hh + $(OUR_CXXCOMPILE) -o $@ $(srcdir)/repository.cc + +.libs/Paludis.so : $(OUR_OBJECTS) + mkdir -p .libs + $(CXX) -fPIC -shared $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) \ + -I@RUBY_SITEARCHDIR@ -l@RUBY_SONAME@ \ + -o $@ \ + $^ \ + $(top_builddir)/paludis/util/.libs/libpaludisutil.so \ + $(top_builddir)/paludis/.libs/libpaludis.so \ + $(top_builddir)/paludis/environment/default/.libs/libpaludisdefaultenvironment.so \ + $(top_builddir)/paludis/environment/no_config/.libs/libpaludisnoconfigenvironment.so + +Paludis.so : $(OUR_OBJECTS) + mkdir -p .libs + $(CXX) -fPIC -shared $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) \ + $(AM_LDFLAGS) $(LDFLAGS) \ + -I@RUBY_SITEARCHDIR@ -l@RUBY_SONAME@ \ + -o $@ \ + $^ \ + -L$(top_builddir)/paludis/util/.libs/ -lpaludisutil \ + -L$(top_builddir)/paludis/.libs/ -lpaludis \ + -L$(top_builddir)/paludis/environment/default/.libs/ -lpaludisdefaultenvironment \ + -L$(top_builddir)/paludis/environment/no_config/.libs/ -lpaludisnoconfigenvironment + +endif + diff --git a/0.8.0/ruby/demos/Makefile.am b/0.8.0/ruby/demos/Makefile.am new file mode 100644 index 000000000..975658312 --- /dev/null +++ b/0.8.0/ruby/demos/Makefile.am @@ -0,0 +1,38 @@ +SUBDIRS = . + +AM_CXXFLAGS = -I$(top_srcdir) -I$(srcdir)/ -I$(top_srcdir)/ruby/ \ + @PALUDIS_CXXFLAGS_WITHOUT_PEDANTIC@ \ + @PALUDIS_CXXFLAGS_NO_WREDUNDANT_DECLS@ \ + @PALUDIS_CXXFLAGS_NO_WOLD_STYLE_CAST@ + +DEFS= \ + -DSYSCONFDIR=\"$(sysconfdir)\" \ + -DDATADIR=\"$(datadir)\" \ + -DLIBEXECDIR=\"$(libexecdir)\" \ + -DLIBDIR=\"$(libdir)\" + +CLEANFILES = *~ gmon.out *.gcov *.gcno *.gcda *.o *.so +MAINTAINERCLEANFILES = Makefile.in + +IF_RUBY_DEMOS = \ + version_spec_distributions.rb + +EXTRA_DIST = $(IF_RUBY_DEMOS) + +TESTS_ENVIRONMENT = env \ + PALUDIS_NO_GLOBAL_HOOKS="yes" \ + PALUDIS_NO_XTERM_TITLES="yes" \ + PALUDIS_EBUILD_DIR="`$(top_srcdir)/ebuild/utils/canonicalise $(top_srcdir)/ebuild/`" \ + PALUDIS_EBUILD_DIR_FALLBACK="`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/ebuild/`" \ + PALUDIS_REPOSITORY_SO_DIR="`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/paludis/repositories`" \ + SYSCONFDIR="$(sysconfdir)" \ + LD_LIBRARY_PATH="`$(top_srcdir)/ebuild/utils/canonicalise $(top_builddir)/paludis/.libs`" \ + bash $(top_srcdir)/test/run_test.sh "ruby -I ../.libs/" + +if ENABLE_RUBY + +noinst_DATA = $(IF_RUBY_DEMOS) + +endif + + diff --git a/0.8.0/ruby/demos/version_spec_distributions.rb b/0.8.0/ruby/demos/version_spec_distributions.rb new file mode 100644 index 000000000..6de4568b7 --- /dev/null +++ b/0.8.0/ruby/demos/version_spec_distributions.rb @@ -0,0 +1,129 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +require 'Paludis' +require 'getoptlong' + +Paludis::Log.instance.log_level = Paludis::LogLevel::Warning + +class Distribution + def initialize + @groups = [ "0.1", "0.2", "0.5", "1", "2", "5", "10", "100" ].map do | x | + Paludis::VersionSpec.new x + end + @counts = Hash.new(0) + @rest_count = 0 + end + + def add_version version + group = @groups.find { | x | version < x } + if group + @counts[group] += 1 + else + @rest_count += 1 + end + end + + def add_repository repo + repo.category_names.each do | cat | + repo.package_names(cat).each do | pkg | + versions = repo.version_specs(pkg) + unless versions.empty? + add_version versions.last + end + end + end + end + + def text_graph + biggest_group = [ @rest_count, @groups.inject(1) { | x, y | [ x, @counts[y] ].max } ].max + unit_scale = 40.0 / biggest_group + @groups.each do | group | + printf "<%-10.10s %10d %s\n", group, @counts[group], "#" * (@counts[group] * unit_scale) + end + printf "%-11.11s %10d %s\n", "rest", @rest_count, "#" * (@rest_count * unit_scale) + end + + def gruff_graph file_name + require 'rubygems' + require 'gruff' + + g = Gruff::Bar.new + g.title = "Package versions distribution" + + g.data("Number of packages", (@groups.map { | group | @counts[group] }) << @rest_count) + labels = Hash.new + @groups.each_with_index do | group, idx | + labels[idx] = "<" + group.to_s + end + labels[@groups.length] = "rest" + g.labels = labels + + g.minimum_value = 0 + g.write file_name + end +end + +opts = GetoptLong.new( + [ '--help', '-h', GetoptLong::NO_ARGUMENT ], + [ '--version', '-V', GetoptLong::NO_ARGUMENT ], + [ '--log-level', GetoptLong::REQUIRED_ARGUMENT ], + [ '--repository-dir', GetoptLong::REQUIRED_ARGUMENT ], + [ '--image', GetoptLong::REQUIRED_ARGUMENT ]) + +output_image = nil +repository_dir = Dir.getwd +opts.each do | opt, arg | + case opt + when '--help' + puts "Usage: " + $0 + " [options]" + puts + puts "Options:" + puts " --help Display a help message" + puts " --version Display program version" + puts + puts " --log-level level Set log level (debug, qa, warning, silent)" + puts " --repository-dir dir Set repository directory (default: cwd)" + puts + puts " --image foo.png Output as the specified image rather than as text" + exit 0 + + when '--version' + puts $0.to_s.split(/\//).last + " " + Paludis::Version + exit 0 + + when '--log-level' + case arg + when 'debug' + Paludis::Log.instance.log_level = Paludis::LogLevel::Debug + when 'qa' + Paludis::Log.instance.log_level = Paludis::LogLevel::Qa + when 'warning' + Paludis::Log.instance.log_level = Paludis::LogLevel::Warning + when 'silent' + Paludis::Log.instance.log_level = Paludis::LogLevel::Silent + else + puts "Bad --log-level value " + arg + exit 1 + end + + when '--repository-dir' + repository_dir = arg + + when '--image' + output_image = arg + + end +end + +distribution = Distribution.new +env = Paludis::NoConfigEnvironment.new repository_dir +env.package_database.repositories.each do | repo | + distribution.add_repository repo +end +if output_image + distribution.gruff_graph output_image +else + distribution.text_graph +end + diff --git a/0.8.0/ruby/dep_atom.cc b/0.8.0/ruby/dep_atom.cc new file mode 100644 index 000000000..3759c4249 --- /dev/null +++ b/0.8.0/ruby/dep_atom.cc @@ -0,0 +1,91 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/dep_atom.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_dep_atom; + static VALUE c_package_dep_atom; + + VALUE + dep_atom_init(VALUE self) + { + return self; + } + + VALUE + package_dep_atom_init(VALUE self, VALUE) + { + return self; + } + + VALUE + package_dep_atom_new(VALUE self, VALUE s) + { + PackageDepAtom::Pointer * ptr(0); + try + { + ptr = new PackageDepAtom::Pointer(new PackageDepAtom(STR2CSTR(s))); + VALUE tdata(Data_Wrap_Struct(self, 0, &Common<PackageDepAtom::Pointer>::free, ptr)); + rb_obj_call_init(tdata, 1, &s); + return tdata; + } + catch (const std::exception & e) + { + delete ptr; + exception_to_ruby_exception(e); + } + } + + void do_register_dep_atom() + { + c_dep_atom = rb_define_class_under(master_class(), "DepAtom", rb_cObject); + rb_funcall(c_dep_atom, rb_intern("private_class_method"), 1, rb_str_new2("new")); + + c_package_dep_atom = rb_define_class_under(master_class(), "PackageDepAtom", c_dep_atom); + rb_define_singleton_method(c_package_dep_atom, "new", RUBY_FUNC_CAST(&package_dep_atom_new), 1); + rb_define_method(c_package_dep_atom, "initialize", RUBY_FUNC_CAST(&package_dep_atom_init), 1); + rb_define_method(c_package_dep_atom, "to_s", RUBY_FUNC_CAST(&Common<PackageDepAtom::Pointer>::to_s_via_ptr), 0); + } +} + +PackageDepAtom::Pointer +paludis::ruby::value_to_package_dep_atom(VALUE v) +{ + if (T_STRING == TYPE(v)) + return PackageDepAtom::Pointer(new PackageDepAtom(STR2CSTR(v))); + else + { + PackageDepAtom::Pointer * v_ptr; + Data_Get_Struct(v, PackageDepAtom::Pointer, v_ptr); + return *v_ptr; + } +} + +RegisterRubyClass::Register paludis_ruby_register_dep_atom PALUDIS_ATTRIBUTE((used)) + (&do_register_dep_atom); + diff --git a/0.8.0/ruby/dep_atom_TEST.rb b/0.8.0/ruby/dep_atom_TEST.rb new file mode 100644 index 000000000..fc1da2643 --- /dev/null +++ b/0.8.0/ruby/dep_atom_TEST.rb @@ -0,0 +1,48 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +require 'test/unit' +require 'Paludis' + +class Paludis + class TestCase_VersionSpec < Test::Unit::TestCase + def test_create + v = PackageDepAtom.new(">=foo/bar-1") + end + + def test_create_error + assert_raise TypeError do + v = PackageDepAtom.new(0) + end + assert_raise PackageDepAtomError do + v = PackageDepAtom.new("=sys-apps/foo") + end + assert_raise NoMethodError do + v = DepAtom.new + end + end + + def test_to_s + assert_equal ">=foo/bar-1", PackageDepAtom.new(">=foo/bar-1").to_s + end + end +end + + diff --git a/0.8.0/ruby/environment.cc b/0.8.0/ruby/environment.cc new file mode 100644 index 000000000..69af47e35 --- /dev/null +++ b/0.8.0/ruby/environment.cc @@ -0,0 +1,275 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/environment/default/default_environment.hh> +#include <paludis/environment/default/default_config.hh> +#include <paludis/environment/no_config/no_config_environment.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_environment; + static VALUE c_default_environment; + static VALUE c_default_config; + static VALUE c_no_config_environment; + + class EnvironmentData + { + private: + Environment * _e; + + public: + Environment * const env_ptr; + + EnvironmentData(Environment * const ee, Environment * const free_e = 0) : + _e(free_e), + env_ptr(ee) + { + } + + ~EnvironmentData() + { + delete _e; + } + }; + + VALUE + environment_query_use(int argc, VALUE * argv, VALUE self) + { + EnvironmentData * env_data; + Data_Get_Struct(self, EnvironmentData, env_data); + + try + { + if (1 == argc || 2 == argc) + { + PackageDatabaseEntry * pde_ptr(0); + if (2 == argc) + Data_Get_Struct(argv[1], PackageDatabaseEntry, pde_ptr); + + return env_data->env_ptr->query_use(UseFlagName(STR2CSTR(argv[0])), pde_ptr) ? Qtrue : Qfalse; + } + else + rb_raise(rb_eArgError, "Environment.query_use expects one or two arguments, but got %d", argc); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + environment_accept_keyword(int argc, VALUE * argv, VALUE self) + { + EnvironmentData * env_data; + Data_Get_Struct(self, EnvironmentData, env_data); + + try + { + if (1 == argc || 2 == argc) + { + PackageDatabaseEntry * pde_ptr(0); + if (2 == argc) + Data_Get_Struct(argv[1], PackageDatabaseEntry, pde_ptr); + + return env_data->env_ptr->accept_keyword(KeywordName( + STR2CSTR(argv[0])), pde_ptr) ? Qtrue : Qfalse; + } + else + rb_raise(rb_eArgError, "Environment.accept_keyword expects one or two arguments, but got %d", argc); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + environment_accept_license(int argc, VALUE * argv, VALUE self) + { + EnvironmentData * env_data; + Data_Get_Struct(self, EnvironmentData, env_data); + + try + { + if (1 == argc || 2 == argc) + { + PackageDatabaseEntry * pde_ptr(0); + if (2 == argc) + Data_Get_Struct(argv[1], PackageDatabaseEntry, pde_ptr); + + return env_data->env_ptr->accept_license( + std::string(STR2CSTR(argv[0])), pde_ptr) ? Qtrue : Qfalse; + } + else + rb_raise(rb_eArgError, "Environment.accept_license expects one or two arguments, but got %d", argc); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + environment_mask_reasons(VALUE self, VALUE pde) + { + EnvironmentData * env_data; + Data_Get_Struct(self, EnvironmentData, env_data); + + PackageDatabaseEntry * pde_ptr(0); + Data_Get_Struct(pde, PackageDatabaseEntry, pde_ptr); + try + { + MaskReasons r(env_data->env_ptr->mask_reasons(*pde_ptr)); + return create_mask_reasons(r); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + environment_package_database(VALUE self) + { + EnvironmentData * env_data; + Data_Get_Struct(self, EnvironmentData, env_data); + + try + { + return create_package_database(env_data->env_ptr->package_database()); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + default_config_config_suffix(VALUE) + { + try + { + return rb_str_new2(DefaultConfig::config_suffix().c_str()); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + default_config_config_suffix_set(VALUE klass, VALUE str) + { + try + { + DefaultConfig::set_config_suffix(stringify(STR2CSTR(str))); + return klass; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + no_config_environment_init(VALUE self, VALUE) + { + return self; + } + + VALUE + no_config_environment_new(VALUE self, VALUE s) + { + std::string path; + try + { + if (T_STRING == TYPE(s)) + path = STR2CSTR(s); + else if (rb_obj_is_kind_of(s, rb_cDir)) + path = STR2CSTR(rb_funcall(s, rb_intern("path"), 0)); + else + rb_raise(rb_eTypeError, "NoConfigEnvironment.new expects a string or Dir"); + + NoConfigEnvironment * e(new NoConfigEnvironment(NoConfigEnvironmentParams::create() + .repository_dir(FSEntry(path)) + .accept_unstable(false))); + EnvironmentData * ptr(new EnvironmentData(e, e)); + VALUE tdata(Data_Wrap_Struct(self, 0, &Common<EnvironmentData>::free, ptr)); + rb_obj_call_init(tdata, 1, &s); + return tdata; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + default_environment_new(VALUE self) + { + try + { + EnvironmentData * ptr(new EnvironmentData(DefaultEnvironment::get_instance())); + return Data_Wrap_Struct(self, 0, &Common<EnvironmentData>::free, ptr); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + void do_register_environment() + { + rb_require("singleton"); + + c_environment = rb_define_class_under(master_class(), "Environment", rb_cObject); + rb_funcall(c_environment, rb_intern("private_class_method"), 1, rb_str_new2("new")); + rb_define_method(c_environment, "query_use", RUBY_FUNC_CAST(&environment_query_use), -1); + rb_define_method(c_environment, "accept_keyword", RUBY_FUNC_CAST(&environment_accept_keyword), -1); + rb_define_method(c_environment, "accept_license", RUBY_FUNC_CAST(&environment_accept_license), -1); + rb_define_method(c_environment, "mask_reasons", RUBY_FUNC_CAST(&environment_mask_reasons), 1); + rb_define_method(c_environment, "package_database", RUBY_FUNC_CAST(&environment_package_database), 0); + + c_default_environment = rb_define_class_under(master_class(), "DefaultEnvironment", c_environment); + rb_define_singleton_method(c_default_environment, "new", RUBY_FUNC_CAST(&default_environment_new), 0); + rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")), rb_intern("included"), 1, c_default_environment); + + c_default_config = rb_define_class_under(master_class(), "DefaultConfig", rb_cObject); + rb_funcall(c_default_config, rb_intern("private_class_method"), 1, rb_str_new2("new")); + rb_define_singleton_method(c_default_config, "config_suffix", + RUBY_FUNC_CAST(&default_config_config_suffix), 0); + rb_define_singleton_method(c_default_config, "config_suffix=", + RUBY_FUNC_CAST(&default_config_config_suffix_set), 1); + + c_no_config_environment = rb_define_class_under(master_class(), "NoConfigEnvironment", c_environment); + rb_define_singleton_method(c_no_config_environment, "new", RUBY_FUNC_CAST(&no_config_environment_new), 1); + rb_define_method(c_no_config_environment, "initialize", RUBY_FUNC_CAST(&no_config_environment_init), 1); + } +} + +RegisterRubyClass::Register paludis_ruby_register_environment PALUDIS_ATTRIBUTE((used)) + (&do_register_environment); + diff --git a/0.8.0/ruby/environment_TEST.rb b/0.8.0/ruby/environment_TEST.rb new file mode 100644 index 000000000..5eba5f79b --- /dev/null +++ b/0.8.0/ruby/environment_TEST.rb @@ -0,0 +1,259 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +ENV["PALUDIS_HOME"] = Dir.getwd().to_s + "/environment_TEST_dir/home"; + +require 'test/unit' +require 'Paludis' + +Paludis::Log.instance.log_level = Paludis::LogLevel::Warning + +class Paludis + + class TestCase_DefaultEnvironment < Test::Unit::TestCase + def test_instance + assert_equal DefaultEnvironment.instance.__id__, DefaultEnvironment.instance.__id__ + assert_kind_of Environment, DefaultEnvironment.instance + assert_kind_of DefaultEnvironment, DefaultEnvironment.instance + end + + def test_no_create + assert_raise NoMethodError do + x = DefaultEnvironment.new() + end + end + end + + class TestCase_NoConfigEnvironment < Test::Unit::TestCase + def test_create + e = NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo") + assert_kind_of Environment, e + assert_kind_of NoConfigEnvironment, e + + e = NoConfigEnvironment.new(Dir.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo")) + assert_kind_of Environment, e + assert_kind_of NoConfigEnvironment, e + + assert_raise TypeError do + e = NoConfigEnvironment.new(123) + end + end + end + + class TestCase_DefaultEnvironmentUse < Test::Unit::TestCase + def test_query_use + assert DefaultEnvironment.instance.query_use("enabled") + assert ! DefaultEnvironment.instance.query_use("not_enabled") + assert ! DefaultEnvironment.instance.query_use("sometimes_enabled") + + pde = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo") + + assert DefaultEnvironment.instance.query_use("enabled", pde) + assert ! DefaultEnvironment.instance.query_use("not_enabled", pde) + assert DefaultEnvironment.instance.query_use("sometimes_enabled", pde) + end + + def test_query_use_bad + assert_raise ArgumentError do + DefaultEnvironment.instance.query_use(1, 2, 3) + end + assert_raise TypeError do + DefaultEnvironment.instance.query_use(123) + end + end + end + + class TestCase_NoConfigEnvironmentUse < Test::Unit::TestCase + def env + NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo") + end + + def test_query_use + assert ! env.query_use("foo") + pde = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo") + assert ! env.query_use("foo", pde) + end + + def test_query_use_bad + assert_raise ArgumentError do + env.query_use(1, 2, 3) + end + assert_raise TypeError do + env.query_use(123) + end + end + end + + class TestCase_DefaultEnvironmentAcceptKeyword < Test::Unit::TestCase + def test_accept_keyword + assert DefaultEnvironment.instance.accept_keyword("test") + assert ! DefaultEnvironment.instance.accept_keyword("bad") + assert ! DefaultEnvironment.instance.accept_keyword("~test") + + pde = PackageDatabaseEntry.new("foo/bar", "1.0", "testrepo") + + assert DefaultEnvironment.instance.accept_keyword("test", pde) + assert ! DefaultEnvironment.instance.accept_keyword("bad", pde) + assert DefaultEnvironment.instance.accept_keyword("~test", pde) + end + + def test_accept_keyword_bad + assert_raise ArgumentError do + DefaultEnvironment.instance.accept_keyword(1, 2, 3) + end + assert_raise TypeError do + DefaultEnvironment.instance.accept_keyword(123) + end + end + end + + class TestCase_NoConfigEnvironmentAcceptKeyword < Test::Unit::TestCase + def env + NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo") + end + + def test_accept_keyword + assert env.accept_keyword("test") + assert ! env.accept_keyword("bad") + assert ! env.accept_keyword("~test") + + pde = PackageDatabaseEntry.new("foo/bar", "1.0", "testrepo") + + assert env.accept_keyword("test", pde) + assert ! env.accept_keyword("bad", pde) + assert ! env.accept_keyword("~test", pde) + end + + def test_accept_keyword_bad + assert_raise ArgumentError do + env.accept_keyword(1, 2, 3) + end + assert_raise TypeError do + env.accept_keyword(123) + end + end + end + + class TestCase_DefaultEnvironmentAcceptLicense < Test::Unit::TestCase + def test_accept_license + assert DefaultEnvironment.instance.accept_license("test") + + pde = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo") + + assert DefaultEnvironment.instance.accept_license("test", pde) + end + + def test_accept_license_bad + assert_raise ArgumentError do + DefaultEnvironment.instance.accept_license(1, 2, 3) + end + assert_raise TypeError do + DefaultEnvironment.instance.accept_license(123) + end + end + end + + class TestCase_NoConfigEnvironmentAcceptLicense < Test::Unit::TestCase + def env + NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo") + end + + def test_accept_license + assert env.accept_license("test") + pde = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo") + assert env.accept_license("test", pde) + end + + def test_accept_license_bad + assert_raise ArgumentError do + env.accept_license(1, 2, 3) + end + assert_raise TypeError do + env.accept_license(123) + end + end + end + + class TestCase_NoConfigEnvironmentMaskReasons < Test::Unit::TestCase + def env + NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo") + end + + def test_mask_reasons + p = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "testrepo") + + m = env.mask_reasons(p) + assert m.empty? + end + + def test_mask_reasons_not_empty + p = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("2.0"), "testrepo") + + m = env.mask_reasons(p) + assert ! m.empty? + assert m.include?("keyword") + assert_equal ["keyword"], m.to_a + end + + def test_mask_reasons_no_such_repo + p = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("1.0"), "nosuchrepo") + + assert_raise RuntimeError do + env.mask_reasons p + end + end + + def test_mask_reasons_bad + assert_raise ArgumentError do + env.mask_reasons(1, 2) + end + assert_raise TypeError do + env.mask_reasons(123) + end + end + end + + class TestCase_DefaultEnvironmentPackageDatabase < Test::Unit::TestCase + def db + DefaultEnvironment.instance.package_database + end + + def test_package_database + assert_kind_of PackageDatabase, db + assert_equal "testrepo", db.fetch_repository("testrepo").name + end + end + + class TestCase_NoConfigEnvironmentPackageDatabase < Test::Unit::TestCase + def env + NoConfigEnvironment.new(Dir.getwd().to_s + "/environment_TEST_dir/testrepo") + end + + def db + env.package_database + end + + def test_package_database + assert_kind_of PackageDatabase, db + assert_equal "testrepo", db.fetch_repository("testrepo").name + end + end +end + diff --git a/0.8.0/ruby/environment_TEST_cleanup.sh b/0.8.0/ruby/environment_TEST_cleanup.sh new file mode 100755 index 000000000..2efc8a29a --- /dev/null +++ b/0.8.0/ruby/environment_TEST_cleanup.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# vim: set ft=sh sw=4 sts=4 et : + +if [ -d environment_TEST_dir ] ; then + rm -fr environment_TEST_dir +else + true +fi + + + + diff --git a/0.8.0/ruby/environment_TEST_setup.sh b/0.8.0/ruby/environment_TEST_setup.sh new file mode 100755 index 000000000..0be171cce --- /dev/null +++ b/0.8.0/ruby/environment_TEST_setup.sh @@ -0,0 +1,66 @@ +#!/bin/bash +# vim: set ft=sh sw=4 sts=4 et : + +mkdir environment_TEST_dir || exit 1 +cd environment_TEST_dir || exit 1 + +mkdir -p home/.paludis/repositories + +cat <<END > home/.paludis/repositories/testrepo.conf +location = `pwd`/testrepo +format = portage +cache = /var/empty +profiles = \${location}/profiles/testprofile +END + +cat <<END > home/.paludis/keywords.conf +* test +~foo/bar-1 ~test +END + +cat <<END > home/.paludis/use.conf +* enabled +~foo/bar-1 sometimes_enabled +END + +cat <<END > home/.paludis/licenses.conf +* * +END + +mkdir -p testrepo/{eclass,distfiles,profiles/testprofile,foo/bar/files} || exit 1 +cd testrepo || exit 1 +echo "testrepo" > profiles/repo_name || exit 1 +cat <<END > profiles/categories || exit 1 +foo +END +cat <<END > profiles/testprofile/make.defaults +ARCH=test +USERLAND=test +KERNEL=test +END +cat <<END > profiles/profiles.desc +test testprofile stable +END + +cat <<"END" > foo/bar/bar-1.0.ebuild || exit 1 +DESCRIPTION="Test package" +HOMEPAGE="http://paludis.berlios.de/" +SRC_URI="" +SLOT="0" +IUSE="" +LICENSE="GPL-2" +KEYWORDS="test" +END + +cat <<"END" > foo/bar/bar-2.0.ebuild || exit 1 +DESCRIPTION="Test package" +HOMEPAGE="http://paludis.berlios.de/" +SRC_URI="" +SLOT="0" +IUSE="" +LICENSE="GPL-2" +KEYWORDS="~test" +END +cd .. + + diff --git a/0.8.0/ruby/log.cc b/0.8.0/ruby/log.cc new file mode 100644 index 000000000..1dd9abb36 --- /dev/null +++ b/0.8.0/ruby/log.cc @@ -0,0 +1,74 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/util/log.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_log; + static VALUE c_log_level; + + VALUE + log_log_level(VALUE) + { + return INT2FIX(Log::get_instance()->log_level()); + } + + VALUE + log_log_level_set(VALUE self, VALUE value) + { + Log::get_instance()->set_log_level(static_cast<LogLevel>(NUM2INT(value))); + return self; + } + + VALUE + log_message(VALUE self, VALUE log_level, VALUE message) + { + Log::get_instance()->message(static_cast<LogLevel>(NUM2INT(log_level)), lc_no_context, + stringify(STR2CSTR(message))); + return self; + } + + void do_register_log() + { + rb_require("singleton"); + + c_log = rb_define_class_under(master_class(), "Log", rb_cObject); + rb_funcall(rb_const_get(rb_cObject, rb_intern("Singleton")), rb_intern("included"), 1, c_log); + rb_define_method(c_log, "log_level", RUBY_FUNC_CAST(&log_log_level), 0); + rb_define_method(c_log, "log_level=", RUBY_FUNC_CAST(&log_log_level_set), 1); + rb_define_method(c_log, "message", RUBY_FUNC_CAST(&log_message), 2); + + c_log_level = rb_define_class_under(master_class(), "LogLevel", rb_cObject); + for (LogLevel l(static_cast<LogLevel>(0)), l_end(last_ll) ; l != l_end ; + l = static_cast<LogLevel>(static_cast<int>(l) + 1)) + rb_define_const(c_log_level, value_case_to_RubyCase(stringify(l)).c_str(), INT2FIX(l)); + } +} + +RegisterRubyClass::Register paludis_ruby_register_log PALUDIS_ATTRIBUTE((used)) (&do_register_log); + + diff --git a/0.8.0/ruby/log_TEST.rb b/0.8.0/ruby/log_TEST.rb new file mode 100644 index 000000000..f745594c4 --- /dev/null +++ b/0.8.0/ruby/log_TEST.rb @@ -0,0 +1,71 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +ENV["PALUDIS_HOME"] = Dir.getwd().to_s + "/default_environment_TEST_dir/home"; + +require 'test/unit' +require 'Paludis' + +class Paludis + class TestCase_Log < Test::Unit::TestCase + def test_instance + assert_equal Log.instance.__id__, Log.instance.__id__ + end + + def test_no_create + assert_raise NoMethodError do + x = Log.new() + end + end + end + + class TestCase_LogLogLevel < Test::Unit::TestCase + def test_log_level + assert_equal Log.instance.log_level, Log.instance.log_level + assert Log.instance.log_level >= LogLevel::Debug + assert Log.instance.log_level <= LogLevel::Silent + end + + def test_log_level_set + Log.instance.log_level = LogLevel::Debug; + assert_equal LogLevel::Debug, Log.instance.log_level + + Log.instance.log_level = LogLevel::Warning; + assert_equal LogLevel::Warning, Log.instance.log_level + end + end + + class TestCase_LogMessage < Test::Unit::TestCase + def test_log_message + Log.instance.message LogLevel::Warning, "This is a test warning message" + end + + def test_log_message_bad + assert_raise ArgumentError do + Log.instance.message "This should fail" + end + + assert_raise TypeError do + Log.instance.message "Warning", "This should fail" + end + end + end +end + diff --git a/0.8.0/ruby/mask_reasons.cc b/0.8.0/ruby/mask_reasons.cc new file mode 100644 index 000000000..85f77e042 --- /dev/null +++ b/0.8.0/ruby/mask_reasons.cc @@ -0,0 +1,99 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/mask_reasons.hh> +#include <paludis/util/stringify.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_mask_reasons; + + VALUE + mask_reasons_init(VALUE self) + { + return self; + } + + VALUE + mask_reasons_new(VALUE self) + { + MaskReasons * ptr(0); + try + { + ptr = new MaskReasons; + VALUE tdata(Data_Wrap_Struct(self, 0, &Common<MaskReasons>::free, ptr)); + rb_obj_call_init(tdata, 0, &self); + return tdata; + } + catch (const std::exception & e) + { + delete ptr; + exception_to_ruby_exception(e); + } + } + + VALUE + mask_reasons_each(VALUE self) + { + MaskReasons * m_ptr; + Data_Get_Struct(self, MaskReasons, m_ptr); + for (MaskReason i(static_cast<MaskReason>(0)), i_end(last_mr) ; i != i_end ; + i = static_cast<MaskReason>(static_cast<int>(i) + 1)) + if ((*m_ptr)[i]) + rb_yield(rb_str_new2(stringify(i).c_str())); + return self; + } + + VALUE + mask_reasons_empty(VALUE self) + { + MaskReasons * m_ptr; + Data_Get_Struct(self, MaskReasons, m_ptr); + return m_ptr->any() ? Qfalse : Qtrue; + return self; + } + + void do_register_mask_reasons() + { + c_mask_reasons = rb_define_class_under(master_class(), "MaskReasons", rb_cObject); + rb_define_singleton_method(c_mask_reasons, "new", RUBY_FUNC_CAST(&mask_reasons_new), 0); + rb_define_method(c_mask_reasons, "initialize", RUBY_FUNC_CAST(&mask_reasons_init), 0); + rb_define_method(c_mask_reasons, "each", RUBY_FUNC_CAST(&mask_reasons_each), 0); + rb_include_module(c_mask_reasons, rb_mEnumerable); + rb_define_method(c_mask_reasons, "empty?", RUBY_FUNC_CAST(&mask_reasons_empty), 0); + } +} + +VALUE +paludis::ruby::create_mask_reasons(const MaskReasons & m) +{ + return Data_Wrap_Struct(c_mask_reasons, 0, &Common<MaskReasons>::free, new MaskReasons(m)); +} + +RegisterRubyClass::Register paludis_ruby_register_mask_reasons PALUDIS_ATTRIBUTE((used)) + (&do_register_mask_reasons); + + diff --git a/0.8.0/ruby/mask_reasons_TEST.rb b/0.8.0/ruby/mask_reasons_TEST.rb new file mode 100644 index 000000000..93fc095dc --- /dev/null +++ b/0.8.0/ruby/mask_reasons_TEST.rb @@ -0,0 +1,43 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +require 'test/unit' +require 'Paludis' + +class Paludis + class TestCase_MaskReasons < Test::Unit::TestCase + + def test_create + m = MaskReasons.new + end + + def test_each + m = MaskReasons.new + assert_equal [], m.to_a + end + + def test_empty + m = MaskReasons.new + assert m.empty? + end + + end +end + diff --git a/0.8.0/ruby/package_database.cc b/0.8.0/ruby/package_database.cc new file mode 100644 index 000000000..4d7931656 --- /dev/null +++ b/0.8.0/ruby/package_database.cc @@ -0,0 +1,167 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/package_database.hh> +#include <paludis/util/stringify.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_package_database; + static VALUE c_package_database_install_state; + + VALUE + package_database_favourite_repository(VALUE self) + { + try + { + PackageDatabase::Pointer * self_ptr; + Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr); + return rb_str_new2(stringify((*self_ptr)->favourite_repository()).c_str()); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + package_database_fetch_unique_qualified_package_name(VALUE self, VALUE pkg) + { + try + { + PackageDatabase::Pointer * self_ptr; + Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr); + return rb_str_new2(stringify((*self_ptr)->fetch_unique_qualified_package_name( + PackageNamePart(STR2CSTR(pkg)))).c_str()); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + package_database_query(VALUE self, VALUE atom, VALUE state) + { + try + { + PackageDatabase::Pointer * self_ptr; + Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr); + + PackageDatabaseEntryCollection::ConstPointer items((*self_ptr)->query( + value_to_package_dep_atom(atom), + static_cast<InstallState>(NUM2INT(state)))); + + VALUE result(rb_ary_new()); + for (PackageDatabaseEntryCollection::Iterator i(items->begin()), + i_end(items->end()) ; i != i_end ; ++i) + rb_ary_push(result, create_package_database_entry(*i)); + return result; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + package_database_repositories(VALUE self) + { + try + { + PackageDatabase::Pointer * self_ptr; + Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr); + + VALUE result(rb_ary_new()); + for (PackageDatabase::RepositoryIterator r((*self_ptr)->begin_repositories()), + r_end((*self_ptr)->end_repositories()) ; r != r_end ; ++r) + rb_ary_push(result, create_repository(*r)); + + return result; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + package_database_fetch_repository(VALUE self, VALUE name) + { + try + { + PackageDatabase::Pointer * self_ptr; + Data_Get_Struct(self, PackageDatabase::Pointer, self_ptr); + + return create_repository((*self_ptr)->fetch_repository(RepositoryName(STR2CSTR(name)))); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + void do_register_package_database() + { + c_package_database = rb_define_class_under(master_class(), "PackageDatabase", rb_cObject); + rb_funcall(c_package_database, rb_intern("private_class_method"), 1, rb_str_new2("new")); + rb_define_method(c_package_database, "favourite_repository", + RUBY_FUNC_CAST(&package_database_favourite_repository), 0); + rb_define_method(c_package_database, "fetch_unique_qualified_package_name", + RUBY_FUNC_CAST(&package_database_fetch_unique_qualified_package_name), 1); + rb_define_method(c_package_database, "query", + RUBY_FUNC_CAST(&package_database_query), 2); + rb_define_method(c_package_database, "repositories", + RUBY_FUNC_CAST(&package_database_repositories), 0); + rb_define_method(c_package_database, "fetch_repository", + RUBY_FUNC_CAST(&package_database_fetch_repository), 1); + + c_package_database_install_state = rb_define_class_under(master_class(), "InstallState", rb_cObject); + for (InstallState l(static_cast<InstallState>(0)), l_end(last_install_state) ; l != l_end ; + l = static_cast<InstallState>(static_cast<int>(l) + 1)) + rb_define_const(c_package_database_install_state, value_case_to_RubyCase(stringify(l)).c_str(), INT2FIX(l)); + } +} + +VALUE +paludis::ruby::create_package_database(PackageDatabase::Pointer m) +{ + PackageDatabase::Pointer * m_ptr(0); + try + { + m_ptr = new PackageDatabase::Pointer(m); + return Data_Wrap_Struct(c_package_database, 0, &Common<PackageDatabase::Pointer>::free, m_ptr); + } + catch (const std::exception & e) + { + delete m_ptr; + exception_to_ruby_exception(e); + } +} + +RegisterRubyClass::Register paludis_ruby_register_package_database PALUDIS_ATTRIBUTE((used)) + (&do_register_package_database); + diff --git a/0.8.0/ruby/package_database_TEST.rb b/0.8.0/ruby/package_database_TEST.rb new file mode 100644 index 000000000..dca941db2 --- /dev/null +++ b/0.8.0/ruby/package_database_TEST.rb @@ -0,0 +1,115 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +ENV["PALUDIS_HOME"] = Dir.getwd().to_s + "/package_database_TEST_dir/home"; + +require 'test/unit' +require 'Paludis' + +Paludis::Log.instance.log_level = Paludis::LogLevel::Warning + +class Paludis + class TestCase_PackageDatabase < Test::Unit::TestCase + def test_no_create + assert_raise NoMethodError do + p = PackageDatabase.new + end + end + end + + class TestCase_PackageDatabaseFavouriteRepository < Test::Unit::TestCase + def test_package_database_favourite_repository + assert_equal "testrepo", DefaultEnvironment.instance.package_database.favourite_repository + end + end + + class TestCase_PackageDatabaseFetchUniqueQualifiedPackageName < Test::Unit::TestCase + def db + return DefaultEnvironment.instance.package_database + end + + def test_package_database_fetch_unique_qualified_package_name + assert_equal "foo/bar", db.fetch_unique_qualified_package_name("bar") + end + end + + class TestCase_PackageDatabaseQuery < Test::Unit::TestCase + def db + return DefaultEnvironment.instance.package_database + end + + def test_package_database_query + a = db.query("=foo/bar-1.0", InstallState::UninstalledOnly) + assert_equal a, [ PackageDatabaseEntry.new("foo/bar", "1.0", "testrepo") ] + + a = db.query(PackageDepAtom.new("=foo/bar-1.0"), InstallState::Either) + assert_equal a, [ PackageDatabaseEntry.new("foo/bar", "1.0", "testrepo") ] + + a = db.query("foo/bar", InstallState::UninstalledOnly) + assert_equal a, [ + PackageDatabaseEntry.new("foo/bar", "1.0", "testrepo"), + PackageDatabaseEntry.new("foo/bar", "2.0", "testrepo") ] + + a = db.query(">=foo/bar-27", InstallState::UninstalledOnly) + assert a.empty? + + a = db.query("foo/bar", InstallState::InstalledOnly) + assert a.empty? + end + + def test_package_database_query_bad + assert_raise TypeError do + db.query(123, InstallState::Either) + end + assert_raise TypeError do + db.query(PackageDepAtom.new("foo/bar"), "Either") + end + end + end + + class TestCase_PackageDatabaseRepositories < Test::Unit::TestCase + def db + return DefaultEnvironment.instance.package_database + end + + def test_repositories + assert_equal 3, db.repositories.length + + a = db.repositories.find_all do | repo | + repo.name == "testrepo" + end + assert_equal 1, a.length + + a = db.repositories.find_all do | repo | + repo.name == "foorepo" + end + assert a.empty? + end + + def test_fetch_repository + assert_equal "testrepo", db.fetch_repository("testrepo").name + + assert_raise RuntimeError do + db.fetch_repository("barrepo") + end + end + end +end + diff --git a/0.8.0/ruby/package_database_TEST_cleanup.sh b/0.8.0/ruby/package_database_TEST_cleanup.sh new file mode 100755 index 000000000..959cd7ee4 --- /dev/null +++ b/0.8.0/ruby/package_database_TEST_cleanup.sh @@ -0,0 +1,13 @@ +#!/bin/bash +# vim: set ft=sh sw=4 sts=4 et : + +if [ -d package_database_TEST_dir ] ; then + rm -fr package_database_TEST_dir +else + true +fi + + + + + diff --git a/0.8.0/ruby/package_database_TEST_setup.sh b/0.8.0/ruby/package_database_TEST_setup.sh new file mode 100755 index 000000000..7ca169339 --- /dev/null +++ b/0.8.0/ruby/package_database_TEST_setup.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# vim: set ft=sh sw=4 sts=4 et : + +mkdir package_database_TEST_dir || exit 1 +cd package_database_TEST_dir || exit 1 + +mkdir -p home/.paludis/repositories + +cat <<END > home/.paludis/repositories/testrepo.conf +location = `pwd`/testrepo +format = portage +cache = /var/empty +profiles = \${location}/profiles/testprofile +END + +cat <<END > home/.paludis/keywords.conf +* test +~foo/bar-1 ~test +END + +cat <<END > home/.paludis/use.conf +* enabled +~foo/bar-1 sometimes_enabled +END + +cat <<END > home/.paludis/licenses.conf +* * +END + +mkdir -p testrepo/{eclass,distfiles,profiles/testprofile,foo/bar/files} || exit 1 +cd testrepo || exit 1 +echo "testrepo" > profiles/repo_name || exit 1 +cat <<END > profiles/categories || exit 1 +foo +END +cat <<END > profiles/testprofile/make.defaults +ARCH=test +USERLAND=test +KERNEL=test +END + +cat <<"END" > foo/bar/bar-1.0.ebuild || exit 1 +DESCRIPTION="Test package" +HOMEPAGE="http://paludis.berlios.de/" +SRC_URI="" +SLOT="0" +IUSE="" +LICENSE="GPL-2" +KEYWORDS="test" +END + +cat <<"END" > foo/bar/bar-2.0.ebuild || exit 1 +DESCRIPTION="Test package" +HOMEPAGE="http://paludis.berlios.de/" +SRC_URI="" +SLOT="0" +IUSE="" +LICENSE="GPL-2" +KEYWORDS="~test" +END +cd .. + + diff --git a/0.8.0/ruby/package_database_entry.cc b/0.8.0/ruby/package_database_entry.cc new file mode 100644 index 000000000..9a1eaf113 --- /dev/null +++ b/0.8.0/ruby/package_database_entry.cc @@ -0,0 +1,92 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/package_database_entry.hh> +#include <paludis/util/compare.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_package_database_entry; + + VALUE + package_database_entry_init(VALUE self, VALUE, VALUE, VALUE) + { + return self; + } + + VALUE + package_database_entry_new(VALUE self, VALUE q, VALUE v, VALUE n) + { + VALUE argv[3] = { q, v, n }; + PackageDatabaseEntry * ptr(0); + try + { + VersionSpec vv(value_to_version_spec(v)); + + ptr = new PackageDatabaseEntry(QualifiedPackageName(STR2CSTR(q)), vv, + RepositoryName(STR2CSTR(n))); + VALUE tdata(Data_Wrap_Struct(self, 0, &Common<PackageDatabaseEntry>::free, ptr)); + rb_obj_call_init(tdata, 3, argv); + return tdata; + } + catch (const std::exception & e) + { + delete ptr; + exception_to_ruby_exception(e); + } + } + + void do_register_package_database_entry() + { + c_package_database_entry = rb_define_class_under(master_class(), "PackageDatabaseEntry", rb_cObject); + rb_define_singleton_method(c_package_database_entry, "new", RUBY_FUNC_CAST(&package_database_entry_new), 3); + rb_define_method(c_package_database_entry, "initialize", RUBY_FUNC_CAST(&package_database_entry_init), 3); + rb_define_method(c_package_database_entry, "<=>", RUBY_FUNC_CAST(&Common<PackageDatabaseEntry>::compare), 1); + rb_include_module(c_package_database_entry, rb_mComparable); + rb_define_method(c_package_database_entry, "to_s", RUBY_FUNC_CAST(&Common<PackageDatabaseEntry>::to_s), 0); + } +} + +VALUE +paludis::ruby::create_package_database_entry(const PackageDatabaseEntry & d) +{ + PackageDatabaseEntry * m_ptr(0); + try + { + m_ptr = new PackageDatabaseEntry(d); + return Data_Wrap_Struct(c_package_database_entry, 0, &Common<PackageDatabaseEntry>::free, m_ptr); + } + catch (const std::exception & e) + { + delete m_ptr; + exception_to_ruby_exception(e); + } + +} + +RegisterRubyClass::Register paludis_ruby_register_package_database_entry PALUDIS_ATTRIBUTE((used)) + (&do_register_package_database_entry); + diff --git a/0.8.0/ruby/package_database_entry_TEST.rb b/0.8.0/ruby/package_database_entry_TEST.rb new file mode 100644 index 000000000..dae3418da --- /dev/null +++ b/0.8.0/ruby/package_database_entry_TEST.rb @@ -0,0 +1,47 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +require 'test/unit' +require 'Paludis' + +class Paludis + class TestCase_PackageDatabaseEntry < Test::Unit::TestCase + def test_create + v = PackageDatabaseEntry.new("foo/bar", VersionSpec.new("0"), "moo") + w = PackageDatabaseEntry.new("foo/bar", "0", "moo") + end + + def test_create_error + assert_raise TypeError do + v = PackageDatabaseEntry.new("foo/bar", "0", 123) + end + assert_raise TypeError do + v = PackageDatabaseEntry.new("foo/bar", 123, "moo") + end + assert_raise NameError do + v = PackageDatabaseEntry.new("asdf", "0", "moo") + end + end + + def test_to_s + assert_equal "foo/bar-10::moo", PackageDatabaseEntry.new("foo/bar", "10", "moo").to_s + end + end +end + diff --git a/0.8.0/ruby/paludis_ruby.cc b/0.8.0/ruby/paludis_ruby.cc new file mode 100644 index 000000000..77b24c19f --- /dev/null +++ b/0.8.0/ruby/paludis_ruby.cc @@ -0,0 +1,127 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis/paludis.hh> +#include <paludis_ruby.hh> +#include <ruby.h> +#include <list> +#include <ctype.h> + +using namespace paludis; +using namespace paludis::ruby; + +namespace paludis +{ + template<> + struct Implementation<RegisterRubyClass> : + InternalCounted<Implementation<RegisterRubyClass> > + { + std::list<void (*)()> funcs; + }; +} + +namespace +{ + static VALUE c_master_class; + static VALUE c_name_error; + static VALUE c_package_dep_atom_error; +} + +RegisterRubyClass::RegisterRubyClass() : + PrivateImplementationPattern<RegisterRubyClass>(new Implementation<RegisterRubyClass>) +{ +} + +RegisterRubyClass::~RegisterRubyClass() +{ +} + +void +RegisterRubyClass::execute() const +{ + for (std::list<void (*)()>::const_iterator f(_imp->funcs.begin()), f_end(_imp->funcs.end()) ; + f != f_end ; ++f) + (*f)(); +} + +RegisterRubyClass::Register::Register(void (* f)()) +{ + RegisterRubyClass::get_instance()->_imp->funcs.push_back(f); +} + +void paludis::ruby::exception_to_ruby_exception(const std::exception & ee) +{ + if (0 != dynamic_cast<const paludis::InternalError *>(&ee)) + rb_raise(rb_eRuntimeError, "Unexpected paludis::InternalError: %s (%s)", + dynamic_cast<const paludis::InternalError *>(&ee)->message().c_str(), ee.what()); + else if (0 != dynamic_cast<const paludis::NameError *>(&ee)) + rb_raise(c_name_error, dynamic_cast<const paludis::NameError *>(&ee)->message().c_str()); + else if (0 != dynamic_cast<const paludis::PackageDepAtomError *>(&ee)) + rb_raise(c_package_dep_atom_error, dynamic_cast<const paludis::PackageDepAtomError *>(&ee)->message().c_str()); + else if (0 != dynamic_cast<const paludis::Exception *>(&ee)) + rb_raise(rb_eRuntimeError, "Caught paludis::Exception: %s (%s)", + dynamic_cast<const paludis::Exception *>(&ee)->message().c_str(), ee.what()); + else + rb_raise(rb_eRuntimeError, "Unexpected std::exception: (%s)", ee.what()); +} + +std::string +paludis::ruby::value_case_to_RubyCase(const std::string & s) +{ + if (s.empty()) + return s; + + bool upper_next(true); + std::string result; + for (std::string::size_type p(0), p_end(s.length()) ; p != p_end ; ++p) + { + if ('_' == s[p]) + upper_next = true; + else if (upper_next) + { + result.append(std::string(1, toupper(s[p]))); + upper_next = false; + } + else + result.append(std::string(1, s[p])); + } + + return result; +} + +VALUE +paludis::ruby::master_class() +{ + return c_master_class; +} + +extern "C" +{ + void Init_Paludis() + { + c_master_class = rb_define_class("Paludis", rb_cObject); + c_name_error = rb_define_class_under(c_master_class, "NameError", rb_eRuntimeError); + c_package_dep_atom_error = rb_define_class_under(c_master_class, "PackageDepAtomError", rb_eRuntimeError); + + rb_define_const(c_master_class, "Version", rb_str_new2((stringify(PALUDIS_VERSION_MAJOR) + "." + + stringify(PALUDIS_VERSION_MINOR) + "." + stringify(PALUDIS_VERSION_MICRO)).c_str())); + RegisterRubyClass::get_instance()->execute(); + } +} + diff --git a/0.8.0/ruby/paludis_ruby.hh b/0.8.0/ruby/paludis_ruby.hh new file mode 100644 index 000000000..7e48a8cd4 --- /dev/null +++ b/0.8.0/ruby/paludis_ruby.hh @@ -0,0 +1,115 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PALUDIS_GUARD_RUBY_RUBY_PALUDIS_RUBY_HH +#define PALUDIS_GUARD_RUBY_RUBY_PALUDIS_RUBY_HH 1 + +#include <paludis/util/instantiation_policy.hh> +#include <paludis/util/private_implementation_pattern.hh> +#include <paludis/util/attributes.hh> +#include <paludis/util/exception.hh> +#include <paludis/util/compare.hh> +#include <paludis/package_database.hh> +#include <paludis/mask_reasons.hh> +#include <ruby.h> + +namespace paludis +{ + namespace ruby + { + /* general utilities */ + + void exception_to_ruby_exception(const std::exception &) PALUDIS_ATTRIBUTE((noreturn)); + + std::string value_case_to_RubyCase(const std::string & s); + + /* constructors */ + + VALUE master_class(); + VALUE create_mask_reasons(const MaskReasons &); + VALUE create_package_database(PackageDatabase::Pointer); + VALUE create_package_database_entry(const PackageDatabaseEntry &); + VALUE create_repository(Repository::ConstPointer); + VALUE create_version_spec(const VersionSpec &); + + VersionSpec value_to_version_spec(VALUE v); + PackageDepAtom::Pointer value_to_package_dep_atom(VALUE v); + + /* registration */ + + class RegisterRubyClass : + public InstantiationPolicy<RegisterRubyClass, instantiation_method::SingletonAsNeededTag>, + private PrivateImplementationPattern<RegisterRubyClass> + { + friend class InstantiationPolicy<RegisterRubyClass, instantiation_method::SingletonAsNeededTag>; + + private: + RegisterRubyClass(); + ~RegisterRubyClass(); + + public: + class Register; + friend class Register; + + void execute() const; + }; + + class RegisterRubyClass::Register : + public InstantiationPolicy<RegisterRubyClass, instantiation_method::NonCopyableTag> + { + public: + Register(void (* func)()); + }; + + template <typename T_> + struct Common + { + static void free(void * p) + { + delete static_cast<T_ *>(p); + } + + static VALUE compare(VALUE left, VALUE right) + { + T_ * left_ptr, * right_ptr; + Data_Get_Struct(left, T_, left_ptr); + Data_Get_Struct(right, T_, right_ptr); + return INT2FIX(paludis::compare(*left_ptr, *right_ptr)); + } + + static VALUE to_s(VALUE self) + { + T_ * self_ptr; + Data_Get_Struct(self, T_, self_ptr); + + return rb_str_new2(stringify(*self_ptr).c_str()); + } + + static VALUE to_s_via_ptr(VALUE self) + { + T_ * self_ptr; + Data_Get_Struct(self, T_, self_ptr); + + return rb_str_new2(stringify(**self_ptr).c_str()); + } + }; + } +} + +#endif diff --git a/0.8.0/ruby/paludis_ruby_TEST.rb b/0.8.0/ruby/paludis_ruby_TEST.rb new file mode 100644 index 000000000..83c62d7d9 --- /dev/null +++ b/0.8.0/ruby/paludis_ruby_TEST.rb @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +require 'test/unit' + +class TC_Basic < Test::Unit::TestCase + def test_require + require 'Paludis' + end +end + diff --git a/0.8.0/ruby/repository.cc b/0.8.0/ruby/repository.cc new file mode 100644 index 000000000..6d46e7aae --- /dev/null +++ b/0.8.0/ruby/repository.cc @@ -0,0 +1,191 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/repository.hh> +#include <paludis/util/stringify.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_repository; + + VALUE + repository_name(VALUE self) + { + try + { + Repository::ConstPointer * self_ptr; + Data_Get_Struct(self, Repository::ConstPointer, self_ptr); + return rb_str_new2(stringify((*self_ptr)->name()).c_str()); + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + repository_has_category_named(VALUE self, VALUE cat) + { + try + { + Repository::ConstPointer * self_ptr; + Data_Get_Struct(self, Repository::ConstPointer, self_ptr); + return (*self_ptr)->has_category_named(CategoryNamePart(STR2CSTR(cat))) ? Qtrue : Qfalse; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + repository_has_package_named(VALUE self, VALUE name) + { + try + { + Repository::ConstPointer * self_ptr; + Data_Get_Struct(self, Repository::ConstPointer, self_ptr); + return (*self_ptr)->has_package_named(QualifiedPackageName(STR2CSTR(name))) ? Qtrue : Qfalse; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + repository_has_version(VALUE self, VALUE name, VALUE version) + { + try + { + Repository::ConstPointer * self_ptr; + Data_Get_Struct(self, Repository::ConstPointer, self_ptr); + return (*self_ptr)->has_version(QualifiedPackageName(STR2CSTR(name)), + value_to_version_spec(version)) ? Qtrue : Qfalse; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + repository_category_names(VALUE self) + { + try + { + Repository::ConstPointer * self_ptr; + Data_Get_Struct(self, Repository::ConstPointer, self_ptr); + VALUE result(rb_ary_new()); + CategoryNamePartCollection::ConstPointer c((*self_ptr)->category_names()); + for (CategoryNamePartCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i) + rb_ary_push(result, rb_str_new2(stringify(*i).c_str())); + return result; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + repository_package_names(VALUE self, VALUE cat) + { + try + { + Repository::ConstPointer * self_ptr; + Data_Get_Struct(self, Repository::ConstPointer, self_ptr); + CategoryNamePart category(STR2CSTR(cat)); + + VALUE result(rb_ary_new()); + QualifiedPackageNameCollection::ConstPointer c((*self_ptr)->package_names(category)); + for (QualifiedPackageNameCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i) + rb_ary_push(result, rb_str_new2(stringify(*i).c_str())); + return result; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + VALUE + repository_version_specs(VALUE self, VALUE qpn) + { + try + { + Repository::ConstPointer * self_ptr; + Data_Get_Struct(self, Repository::ConstPointer, self_ptr); + QualifiedPackageName q(STR2CSTR(qpn)); + + VALUE result(rb_ary_new()); + VersionSpecCollection::ConstPointer c((*self_ptr)->version_specs(q)); + for (VersionSpecCollection::Iterator i(c->begin()), i_end(c->end()) ; i != i_end ; ++i) + rb_ary_push(result, create_version_spec(*i)); + return result; + } + catch (const std::exception & e) + { + exception_to_ruby_exception(e); + } + } + + void do_register_repository() + { + c_repository = rb_define_class_under(master_class(), "Repository", rb_cObject); + rb_funcall(c_repository, rb_intern("private_class_method"), 1, rb_str_new2("new")); + rb_define_method(c_repository, "name", RUBY_FUNC_CAST(&repository_name), 0); + + rb_define_method(c_repository, "has_category_named?", RUBY_FUNC_CAST(&repository_has_category_named), 1); + rb_define_method(c_repository, "has_package_named?", RUBY_FUNC_CAST(&repository_has_package_named), 1); + rb_define_method(c_repository, "has_version?", RUBY_FUNC_CAST(&repository_has_version), 2); + + rb_define_method(c_repository, "category_names", RUBY_FUNC_CAST(&repository_category_names), 0); + rb_define_method(c_repository, "package_names", RUBY_FUNC_CAST(&repository_package_names), 1); + rb_define_method(c_repository, "version_specs", RUBY_FUNC_CAST(&repository_version_specs), 1); + } +} + +VALUE +paludis::ruby::create_repository(Repository::ConstPointer m) +{ + Repository::ConstPointer * m_ptr(0); + try + { + m_ptr = new Repository::ConstPointer(m); + return Data_Wrap_Struct(c_repository, 0, &Common<Repository::ConstPointer>::free, m_ptr); + } + catch (const std::exception & e) + { + delete m_ptr; + exception_to_ruby_exception(e); + } +} + +RegisterRubyClass::Register paludis_ruby_register_repository PALUDIS_ATTRIBUTE((used)) + (&do_register_repository); + + diff --git a/0.8.0/ruby/repository_TEST.rb b/0.8.0/ruby/repository_TEST.rb new file mode 100644 index 000000000..8e4126379 --- /dev/null +++ b/0.8.0/ruby/repository_TEST.rb @@ -0,0 +1,120 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +ENV["PALUDIS_HOME"] = Dir.getwd().to_s + "/repository_TEST_dir/home"; + +require 'test/unit' +require 'Paludis' + +Paludis::Log.instance.log_level = Paludis::LogLevel::Warning + +class Paludis + class TestCase_Repository < Test::Unit::TestCase + def test_no_create + assert_raise NoMethodError do + p = Repository.new + end + end + end + + module RepositoryTestCase + def repo + db.fetch_repository "testrepo" + end + + def db + DefaultEnvironment.instance.package_database + end + end + + class TestCase_RepositoryHasVersion < Test::Unit::TestCase + include RepositoryTestCase + + def test_has_version + assert repo.has_version?("foo/bar", "1.0") + assert repo.has_version?("foo/bar", "2.0") + + assert repo.has_version?("foo/bar", VersionSpec.new("1.0")) + assert repo.has_version?("foo/bar", VersionSpec.new("2.0")) + + assert ! repo.has_version?("foo/barbar", "1.0") + assert ! repo.has_version?("foo/bar", "3.0") + + assert ! repo.has_version?("foo/barbar", VersionSpec.new("1.0")) + assert ! repo.has_version?("foo/bar", VersionSpec.new("3.0")) + end + end + + class TestCase_RepositoryHasCategoryNamed < Test::Unit::TestCase + include RepositoryTestCase + + def test_has_category_named + assert repo.has_category_named?("foo") + assert ! repo.has_category_named?("bar") + end + end + + class TestCase_RepositoryHasPackageNamed < Test::Unit::TestCase + include RepositoryTestCase + + def test_has_package_named + assert repo.has_package_named?("foo/bar") + assert ! repo.has_package_named?("bar/bar") + assert ! repo.has_package_named?("foo/foo") + end + end + + class TestCase_RepositoryVersionSpecs < Test::Unit::TestCase + include RepositoryTestCase + + def test_version_specs + a = repo.version_specs "foo/bar" + assert_equal 2, a.length + assert_equal VersionSpec.new("1.0"), a[0] + assert_equal VersionSpec.new("2.0"), a[1] + + b = repo.version_specs "bar/baz" + assert b.empty? + end + end + + class TestCase_RepositoryCategoryNames < Test::Unit::TestCase + include RepositoryTestCase + + def test_category_names + a = repo.category_names + assert_equal 1, a.length + assert_equal "foo", a[0] + end + end + + class TestCase_RepositoryPackageNames < Test::Unit::TestCase + include RepositoryTestCase + + def test_package_names + a = repo.package_names "foo" + assert_equal 1, a.length + assert_equal "foo/bar", a[0] + + assert repo.package_names("bar").empty? + end + end +end + diff --git a/0.8.0/ruby/repository_TEST_cleanup.sh b/0.8.0/ruby/repository_TEST_cleanup.sh new file mode 100755 index 000000000..4ba9e4746 --- /dev/null +++ b/0.8.0/ruby/repository_TEST_cleanup.sh @@ -0,0 +1,14 @@ +#!/bin/bash +# vim: set ft=sh sw=4 sts=4 et : + +if [ -d repository_TEST_dir ] ; then + rm -fr repository_TEST_dir +else + true +fi + + + + + + diff --git a/0.8.0/ruby/repository_TEST_setup.sh b/0.8.0/ruby/repository_TEST_setup.sh new file mode 100755 index 000000000..53c0b466e --- /dev/null +++ b/0.8.0/ruby/repository_TEST_setup.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# vim: set ft=sh sw=4 sts=4 et : + +mkdir repository_TEST_dir || exit 1 +cd repository_TEST_dir || exit 1 + +mkdir -p home/.paludis/repositories + +cat <<END > home/.paludis/repositories/testrepo.conf +location = `pwd`/testrepo +format = portage +cache = /var/empty +profiles = \${location}/profiles/testprofile +END + +cat <<END > home/.paludis/keywords.conf +* test +~foo/bar-1 ~test +END + +cat <<END > home/.paludis/use.conf +* enabled +~foo/bar-1 sometimes_enabled +END + +cat <<END > home/.paludis/licenses.conf +* * +END + +mkdir -p testrepo/{eclass,distfiles,profiles/testprofile,foo/bar/files} || exit 1 +cd testrepo || exit 1 +echo "testrepo" > profiles/repo_name || exit 1 +cat <<END > profiles/categories || exit 1 +foo +END +cat <<END > profiles/testprofile/make.defaults +ARCH=test +USERLAND=test +KERNEL=test +END + +cat <<"END" > foo/bar/bar-1.0.ebuild || exit 1 +DESCRIPTION="Test package" +HOMEPAGE="http://paludis.berlios.de/" +SRC_URI="" +SLOT="0" +IUSE="" +LICENSE="GPL-2" +KEYWORDS="test" +END + +cat <<"END" > foo/bar/bar-2.0.ebuild || exit 1 +DESCRIPTION="Test package" +HOMEPAGE="http://paludis.berlios.de/" +SRC_URI="" +SLOT="0" +IUSE="" +LICENSE="GPL-2" +KEYWORDS="~test" +END +cd .. + + + diff --git a/0.8.0/ruby/version_spec.cc b/0.8.0/ruby/version_spec.cc new file mode 100644 index 000000000..96f400dec --- /dev/null +++ b/0.8.0/ruby/version_spec.cc @@ -0,0 +1,92 @@ +/* vim: set sw=4 sts=4 et foldmethod=syntax : */ + +/* + * Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> + * + * 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 + * Public License version 2, as published by the Free Software Foundation. + * + * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include <paludis_ruby.hh> +#include <paludis/version_spec.hh> +#include <ruby.h> + +using namespace paludis; +using namespace paludis::ruby; + +#define RUBY_FUNC_CAST(x) reinterpret_cast<VALUE (*)(...)>(x) + +namespace +{ + static VALUE c_version_spec; + + VALUE + version_spec_init(VALUE self, VALUE) + { + return self; + } + + VALUE + version_spec_new(VALUE self, VALUE s) + { + VersionSpec * ptr(0); + try + { + ptr = new VersionSpec(std::string(STR2CSTR(s))); + VALUE tdata(Data_Wrap_Struct(self, 0, &Common<VersionSpec>::free, ptr)); + rb_obj_call_init(tdata, 1, &s); + return tdata; + } + catch (const std::exception & e) + { + delete ptr; + exception_to_ruby_exception(e); + } + } + + void do_register_version_spec() + { + c_version_spec = rb_define_class_under(master_class(), "VersionSpec", rb_cObject); + rb_define_singleton_method(c_version_spec, "new", RUBY_FUNC_CAST(&version_spec_new), 1); + rb_define_method(c_version_spec, "initialize", RUBY_FUNC_CAST(&version_spec_init), 1); + rb_define_method(c_version_spec, "<=>", RUBY_FUNC_CAST(&Common<VersionSpec>::compare), 1); + rb_include_module(c_version_spec, rb_mComparable); + rb_define_method(c_version_spec, "to_s", RUBY_FUNC_CAST(&Common<VersionSpec>::to_s), 0); + } +} + +VersionSpec +paludis::ruby::value_to_version_spec(VALUE v) +{ + if (T_STRING == TYPE(v)) + return VersionSpec(STR2CSTR(v)); + else + { + VersionSpec * v_ptr; + Data_Get_Struct(v, VersionSpec, v_ptr); + return *v_ptr; + } +} + +VALUE +paludis::ruby::create_version_spec(const VersionSpec & v) +{ + VersionSpec * vv(new VersionSpec(v)); + return Data_Wrap_Struct(c_version_spec, 0, &Common<VersionSpec>::free, vv); +} + +RegisterRubyClass::Register paludis_ruby_register_version_spec PALUDIS_ATTRIBUTE((used)) + (&do_register_version_spec); + + + diff --git a/0.8.0/ruby/version_spec_TEST.rb b/0.8.0/ruby/version_spec_TEST.rb new file mode 100644 index 000000000..6ca2bd8f0 --- /dev/null +++ b/0.8.0/ruby/version_spec_TEST.rb @@ -0,0 +1,56 @@ +#!/usr/bin/ruby +# vim: set sw=4 sts=4 et tw=80 : + +# +# Copyright (c) 2006 Ciaran McCreesh <ciaranm@ciaranm.org> +# +# 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 +# Public License version 2, as published by the Free Software Foundation. +# +# Paludis is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# + +require 'test/unit' +require 'Paludis' + +class Paludis + class TestCase_VersionSpec < Test::Unit::TestCase + def test_create + v = VersionSpec.new("0") + end + + def test_create_error + assert_raise TypeError do + v = VersionSpec.new(0) + end + end + + def test_compare + v0 = VersionSpec.new("0") + v1 = VersionSpec.new("0.1") + v2 = VersionSpec.new("1.0") + + assert_operator v0, :<, v1 + assert_operator v0, :<, v2 + assert_operator v1, :<, v2 + + assert_operator v0, :<=, v0 + assert_operator v0, :<=, v1 + assert_operator v0, :<=, v2 + assert_operator v1, :<=, v2 + end + + def test_to_s + assert_equal "0.1_alpha2", VersionSpec.new("0.1_alpha2").to_s + end + end +end + |