extracted first_time into once package
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 20 Aug 2009 17:36:17 +0000 (17:36 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 20 Aug 2009 17:36:17 +0000 (17:36 +0000)
lib/PXElator/once.pm [new file with mode: 0644]
lib/PXElator/t/once.t [new file with mode: 0755]
lib/PXElator/upstream.pm

diff --git a/lib/PXElator/once.pm b/lib/PXElator/once.pm
new file mode 100644 (file)
index 0000000..cfd3a68
--- /dev/null
@@ -0,0 +1,14 @@
+package once;
+
+use Storable;
+
+my $once_path = '/tmp/pxelator.once';
+our $just_once = retrieve $once_path if -e $once_path;
+sub first_time {
+       my $what = shift;
+       return if $just_once->{$what}++;
+       store $just_once, $once_path;
+       return 1;
+}
+
+1;
diff --git a/lib/PXElator/t/once.t b/lib/PXElator/t/once.t
new file mode 100755 (executable)
index 0000000..3fb435f
--- /dev/null
@@ -0,0 +1,14 @@
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use autodie;
+
+use Test::More tests => 4;
+
+use_ok 'once';
+
+ok( my $rand = rand(), 'rand' );
+ok(   once::first_time("test-$rand"), 'first_time' );
+ok( ! once::first_time("test-$rand"), 'first_time again' );
+
index 2e4ef62..23d5310 100644 (file)
@@ -6,8 +6,8 @@ use autodie;
 
 use Data::Dump qw/dump/;
 use LWP::Simple qw/mirror RC_NOT_MODIFIED/;
-use Storable;
 use server;
+use once;
 
 sub mirror_file {
        my ( $url, $file ) = @_;
@@ -19,15 +19,6 @@ sub mirror_file {
                ;
 }
 
-my $once_path = '/tmp/pxelator.once';
-our $just_once = retrieve $once_path if -e $once_path;
-sub first_time {
-       my $what = shift;
-       return if $just_once->{$what}++;
-       store $just_once, $once_path;
-       return 1;
-}
-
 sub iso {
        my $url = shift;
 
@@ -44,7 +35,7 @@ sub iso {
 
        my $iso = "$dir/$file";
 
-       mirror_file( $url, $iso ) if first_time $url;
+       mirror_file( $url, $iso ) if once::first_time $url;
 
        $file =~ s{\.iso$}{}i;
        my $mnt = "$server::base_dir/tftp/$name";
@@ -73,7 +64,7 @@ sub files {
 
        foreach my $file ( @files ) {
                mirror_file( "$url/$file", "$path/$file" )
-                       if ! -e "$path/$file" || first_time( "$url/$file" );
+                       if ! -e "$path/$file" || once::first_time( "$url/$file" );
        }
 }