Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lots of small fixes. Now fact classes are loaded. Yay!

  • Loading branch information...
commit 5e20701d9a19234db8221c980db2bd2aa71c8193 1 parent d6c4ed3
@cosimo authored
View
5 lib/Facter.pm
@@ -48,8 +48,8 @@ method collection {
$!collection //= Facter::Util::Collection.new
}
-method version () {
- return $VERSION
+method version {
+ $VERSION
}
method BUILD {
@@ -146,6 +146,7 @@ method to_hash (*@args) {
method add ($name, Sub $block) {
# TODO add %options support
#multi method add ($name, %options = (), $block) {
+ Facter.debug("Facter: adding fact $name as " ~ $block.perl);
my $instance = self // Facter.get_instance;
$instance.collection.add($name, $block);
}
View
16 lib/Facter/Util/Collection.pm
@@ -23,11 +23,15 @@ method add($fact_name, Sub $block) {
# TODO add %options support
my %options = ();
+ Facter.debug("collection.add $fact_name: " ~ $block.perl);
+
my $name = self.canonize($fact_name);
my $fact = %!facts{$name};
unless $fact {
- $fact = Facter::Util::Fact.new($name);
+ Facter.debug("new Fact '$name'");
+ $fact = Facter::Util::Fact.new(name => $name);
+ Facter.debug("new Fact '$name' created: " ~ $fact.perl);
%!facts{$name} = $fact;
}
@@ -41,6 +45,8 @@ method add($fact_name, Sub $block) {
}
if $block {
+
+ Facter.debug("Fact " ~ $fact ~ " adding block " ~ $block.perl);
my $resolve = $fact.add($block);
# Set any resolve-appropriate options
@@ -74,7 +80,13 @@ method each () {
# Return a fact by name.
method fact($name) {
my $fact_name = self.canonize($name);
- self.loader.load($fact_name) unless %!facts{$fact_name};
+ Facter.debug("self.canonize($name) = $fact_name");
+
+ unless %!facts{$fact_name} {
+ Facter.debug("Loading fact $fact_name through loader");
+ self.loader.load($fact_name);
+ }
+
return %!facts{$fact_name};
}
View
18 lib/Facter/Util/Confine.pm
@@ -1,15 +1,9 @@
-=begin pod
-
-=head1 NAME
-
-Facter::Util::Confine
-
-=head1 DESCRIPTION
-
-A restricting tag for fact resolution mechanisms. The tag must be true
-for the resolution mechanism to be suitable.
-
-=end pod
+#
+# Facter::Util::Confine
+#
+# A restricting tag for fact resolution mechanisms. The tag must be true
+# for the resolution mechanism to be suitable.
+#
class Facter::Util::Confine;
View
19 lib/Facter/Util/Fact.pm
@@ -1,11 +1,6 @@
-use v6;
-
-# FIXME Doesn't work because of search_path already defined
-#use Facter;
-
class Facter::Util::Fact;
-#se Facter::Util::Resolution;
+use Facter::Util::Resolution;
our $TIMEOUT = 5;
@@ -34,22 +29,26 @@ method initialize($name, %options = ()) {
# Add a new resolution mechanism. This requires a block, which will then
# be evaluated in the context of the new mechanism.
-method add(Sub $block) {
+method add($block) {
#raise ArgumentError, "You must pass a block to Fact<instance>.add" unless block_given?
if ! $block {
die "You must pass a block to Fact<instance>.add";
}
- my $resolve = Facter::Util::Resolution.new($.name);
- $resolve.instance_eval($block);
+ Facter.debug("Fact.add($.name, $block)");
+ my $resolve = Facter::Util::Resolution.new(name => $.name);
+
+ # ruby: resolve.instance_eval(block);
+ $block($resolve);
+
@.resolves.push($resolve);
# Immediately sort the resolutions, so that we always have
# a sorted list for looking up values.
# We always want to look them up in the order of number of
# confines, so the most restricted resolution always wins.
- #@resolves.sort! { |a, b| b.length <=> a.length }
+ @.resolves = sort { $^b.elems <=> $^a.elems }, @.resolves;
return $resolve;
}
View
8 lib/Facter/Util/Loader.pm
@@ -18,10 +18,10 @@ method load($fact) {
my $module = "Facter::$shortname";
try {
require $module;
- CATCH {
- warn "Unable to load fact $shortname: $!";
- return False;
- }
+ }
+ CATCH {
+ warn "Unable to load fact $shortname: $!";
+ return False;
}
return True;
View
97 lib/Facter/Util/Resolution.pm
@@ -1,41 +1,33 @@
-=begin pod
-
-=head1 NAME
-
-Facter::Util::Resolution
-
-=head1 DESCRIPTION
-
-An actual fact resolution mechanism. These are largely just chunks of
-code, with optional confinements restricting the mechanisms to only working on
-specific systems. Note that the confinements are always ANDed, so any
-confinements specified must all be true for the resolution to be
-suitable.
-
-=end pod
+#
+# Facter::Util::Resolution
+#
+# An actual fact resolution mechanism. These are largely just chunks of
+# code, with optional confinements restricting the mechanisms to only working on
+# specific systems. Note that the confinements are always ANDed, so any
+# confinements specified must all be true for the resolution to be
+# suitable.
+#
class Facter::Util::Resolution;
-use Facter::Util::Confine;
-
-#require 'timeout'
-#require 'rbconfig'
+#equire 'timeout'
+#equire 'rbconfig'
has $.code is rw;
has $.interpreter is rw;
has $.name is rw;
-has $.value is rw;
+has $!value is rw;
has $.timeout is rw;
has @.confines is rw;
our $WINDOWS = $*OS ~~ m:i/mswin|win32|dos|mingw|cygwin/;
-our $INTERPRETER = $WINDOWS ?? 'cmd.exe' :: '/bin/sh'
+our $INTERPRETER = $WINDOWS ?? 'cmd.exe' !! '/bin/sh';
our $HAVE_WHICH;
method have_which {
if ! $HAVE_WHICH.defined {
- if Facter.value('kernel') == 'windows' {
- $HAVE_WHICH = False
+ if Facter.value('kernel') eq 'windows' {
+ $HAVE_WHICH = False;
} else {
$HAVE_WHICH = run('which which >/dev/null 2>&1') == 0;
}
@@ -50,7 +42,7 @@ method have_which {
#
method exec($code, $interpreter = $INTERPRETER) {
- unless $interpreter == $INTERPRETER {
+ unless $interpreter eq $INTERPRETER {
die "invalid interpreter";
}
@@ -74,30 +66,29 @@ method exec($code, $interpreter = $INTERPRETER) {
# we don't have the binary necessary
return if $path eq "" or $path.match(/Command not found\./);
}
-
return unless $path.IO ~~ :e;
}
my $out;
+ Facter.debug("Running command $code");
try {
$out = qx{$code}.chomp;
- } CATCH {
- warn "Command failed: $!";
- return;
}
-
- if $out == "" {
- return
+ CATCH {
+ Facter.debug("Command failed: $!");
+ return;
}
+ return if $out eq "";
return $out;
}
# Add a new confine to the resolution mechanism.
method confine(%confines) {
+ require Facter::Util::Confine;
for %confines.kv -> $fact, $values {
- @.confines.push(Facter::Util::Confine.new($fact, $values);
+ @.confines.push(Facter::Util::Confine.new($fact, $values));
}
}
@@ -105,7 +96,7 @@ method confine(%confines) {
method initialize($name) {
$.name = $name;
@.confines = ();
- $.value = Mu;
+ $!value = Mu;
$.timeout = 0;
return;
}
@@ -119,70 +110,52 @@ method length {
# of ruby seem to already have a 'timeout' method and we can't
# seem to override the instance methods, somehow.
method limit {
- $.timeout
+ $.timeout;
}
# Set our code for returning a value.
-method setcode($string = Mu, $interp = Mu, Sub $block)
+method setcode($string = "", $interp = "", $block) {
if $string {
$.code = $string;
$.interpreter = $interp || $INTERPRETER;
} elsif $block {
- $.code = $block
+ $.code = $block;
} else {
- die "You must pass either code or a block"
+ die "You must pass either code or a block";
}
}
# Is this resolution mechanism suitable on the system in question?
-method suitable
+method suitable {
unless defined $.suitable {
- $.suitable = ! any(@confines, False);
+ $.suitable = ! (any(@.confines) == False);
}
return $.suitable;
}
method Str {
- return self.value()
+ return self.value;
}
# How we get a value for our resolution mechanism.
method value {
if ! $.code and ! $.interpreter {
+ Facter.debug("No code and no interpreter. Can't get value of fact $.name");
return;
}
my $result;
my $starttime = time;
-=begin ruby
- begin
- Timeout.timeout(limit) do
- if @code.is_a?(Proc)
- result = @code.call()
- else
- result = Facter::Util::Resolution.exec(@code,@interpreter)
- }
- }
- rescue Timeout::Error => detail
- warn "Timed out seeking value for %s" % self.name
-
- # This call avoids zombies -- basically, create a thread that will
- # dezombify all of the child processes that we're ignoring because
- # of the timeout.
- Thread.new { Process.waitall }
- return nil
- rescue => details
- warn "Could not retrieve %s: %s" % [self.name, details]
- return nil
- }
-=end ruby
+ Facter.debug("Getting value of fact $.name...");
try {
if "Sub()" eq $.code.WHAT {
+ Facter.debug(" Running block $.code");
$result = $.code();
} else {
+ Facter.debug(" Running command $.code through $.interpreter");
$result = Facter::Util::Resolution.exec($.code, $.interpreter);
}
}
View
10 lib/Facter/perl6os.pm
@@ -1,3 +1,7 @@
-Facter.add(<perl6os>, sub {
- $*OS.Str;
-});
+Facter.add("perl6os", sub ($f) {
+ Facter.debug("perl6os fact block running");
+ $f.setcode(block => sub {
+ $*OS.Str
+ });
+})
+
View
3  t/000-load-classes.t
@@ -5,6 +5,9 @@ use Facter;
use Facter::Util::Fact;
use Facter::Util::Collection;
use Facter::Util::Loader;
+use Facter::Util::Resolution;
+use Facter::Util::Confine;
+use Facter::Util::Values;
ok(1, 'Facter main classes loaded');
View
15 t/basic.t
@@ -13,11 +13,20 @@ ok($coll, "Collection object is there");
my $test-fact = 'perl6os';
-my $lsbdistname = $facter.fact($test-fact);
+my $fact = $facter.fact($test-fact);
+ok($fact, "test fact is loaded");
+ok($fact.^can("value"), "fact object has a value() method");
+
+my $lsbdistname = $facter.fact($test-fact).Str;
ok($lsbdistname, "fact '$test-fact' is loaded");
+is($lsbdistname, $*OS, "test fact has correct value");
+
+my $same-value = $facter.fact($test-fact).value;
+is($lsbdistname, $same-value, "fact.value and fact.Str should yield the same result");
-@search_dirs = $facter.search_path;
-ok(@search_dirs.elems > 0, "Search path should be filled now");
+# Unused for now...
+#@search_dirs = $facter.search_path;
+#ok(@search_dirs.elems > 0, "Search path should be filled now");
diag("Collection object:" ~ $coll.perl);
diag("Search dirs:" ~ @search_dirs.perl);
Please sign in to comment.
Something went wrong with that request. Please try again.