From 8762b81a17aebbe30c8bb7d0cdd8448eb1848753 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 20 Aug 2009 17:36:17 +0000 Subject: [PATCH] extracted first_time into once package --- lib/PXElator/once.pm | 14 ++++++++++++++ lib/PXElator/t/once.t | 14 ++++++++++++++ lib/PXElator/upstream.pm | 15 +++------------ 3 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 lib/PXElator/once.pm create mode 100755 lib/PXElator/t/once.t diff --git a/lib/PXElator/once.pm b/lib/PXElator/once.pm new file mode 100644 index 0000000..cfd3a68 --- /dev/null +++ b/lib/PXElator/once.pm @@ -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 index 0000000..3fb435f --- /dev/null +++ b/lib/PXElator/t/once.t @@ -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' ); + diff --git a/lib/PXElator/upstream.pm b/lib/PXElator/upstream.pm index 2e4ef62..23d5310 100644 --- a/lib/PXElator/upstream.pm +++ b/lib/PXElator/upstream.pm @@ -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" ); } } -- 2.20.1