aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-09-25 21:16:27 +0000
committerAvatar Ciaran McCreesh <ciaran.mccreesh@googlemail.com> 2006-09-25 21:16:27 +0000
commit1dd82ca7634cc9e8921693e52faa104c4085da96 (patch)
treedbc09f6cf6cda67353459a3a0cfa870c0cc3bf19
parentd47c6da31c673ba098e484c9ef0e3b094b9a62c8 (diff)
downloadpaludis-1dd82ca7634cc9e8921693e52faa104c4085da96.tar.gz
paludis-1dd82ca7634cc9e8921693e52faa104c4085da96.tar.xz
Add some demo Ruby code
-rw-r--r--configure.ac1
-rw-r--r--ruby/Makefile.am2
-rw-r--r--ruby/default_environment.cc36
-rw-r--r--ruby/demos/Makefile.am38
-rw-r--r--ruby/demos/version_spec_distributions.rb127
-rw-r--r--ruby/paludis_ruby.cc3
6 files changed, 206 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac
index f88f348..c39842a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -610,6 +610,7 @@ AC_OUTPUT(
paludis/tasks/Makefile
paludis/util/Makefile
ruby/Makefile
+ ruby/demos/Makefile
src/Makefile
src/paludis/Makefile
src/gtkpaludis/Makefile
diff --git a/ruby/Makefile.am b/ruby/Makefile.am
index 439d31b..5dd1edf 100644
--- a/ruby/Makefile.am
+++ b/ruby/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = .
+SUBDIRS = . demos
AM_CXXFLAGS = -I$(top_srcdir) -I$(srcdir)/ \
@PALUDIS_CXXFLAGS_WITHOUT_PEDANTIC@ \
diff --git a/ruby/default_environment.cc b/ruby/default_environment.cc
index fbbe129..234e8b7 100644
--- a/ruby/default_environment.cc
+++ b/ruby/default_environment.cc
@@ -19,6 +19,7 @@
#include <paludis_ruby.hh>
#include <paludis/default_environment.hh>
+#include <paludis/default_config.hh>
#include <ruby.h>
using namespace paludis;
@@ -29,6 +30,7 @@ using namespace paludis::ruby;
namespace
{
static VALUE c_default_environment;
+ static VALUE c_default_config;
VALUE
default_environment_query_use(int argc, VALUE * argv, VALUE)
@@ -128,6 +130,33 @@ namespace
}
}
+ 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);
+ }
+ }
+
void do_register_default_environment()
{
rb_require("singleton");
@@ -139,6 +168,13 @@ namespace
rb_define_method(c_default_environment, "accept_license", RUBY_FUNC_CAST(&default_environment_accept_license), -1);
rb_define_method(c_default_environment, "mask_reasons", RUBY_FUNC_CAST(&default_environment_mask_reasons), 1);
rb_define_method(c_default_environment, "package_database", RUBY_FUNC_CAST(&default_environment_package_database), 0);
+
+ 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);
}
}
diff --git a/ruby/demos/Makefile.am b/ruby/demos/Makefile.am
new file mode 100644
index 0000000..9756583
--- /dev/null
+++ b/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/ruby/demos/version_spec_distributions.rb b/ruby/demos/version_spec_distributions.rb
new file mode 100644
index 0000000..c4362f1
--- /dev/null
+++ b/ruby/demos/version_spec_distributions.rb
@@ -0,0 +1,127 @@
+#!/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 ],
+ [ '--config-suffix', GetoptLong::REQUIRED_ARGUMENT ],
+ [ '--image', GetoptLong::REQUIRED_ARGUMENT ])
+
+output_image = nil
+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 " --config-suffix dir Set configuration directory suffix"
+ 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 '--config-suffix'
+ Paludis::DefaultConfig.config_suffix = arg
+
+ when '--image'
+ output_image = arg
+
+ end
+end
+
+distribution = Distribution.new
+Paludis::DefaultEnvironment.instance.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/ruby/paludis_ruby.cc b/ruby/paludis_ruby.cc
index a0c5254..77b24c1 100644
--- a/ruby/paludis_ruby.cc
+++ b/ruby/paludis_ruby.cc
@@ -118,6 +118,9 @@ extern "C"
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();
}
}