Handler/Initialise.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Queue/Entry.pm ↑ Summary BuildInfo.pm »
1 package PkgForge::Handler::Initialise; # -*-perl-*-
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # $Id: Initialise.pm.in 15409 2011-01-12 17:25:17Z squinney@INF.ED.AC.UK $
6 # $Source:$
7 # $Revision: 15409 $
8 # $HeadURL: https://svn.lcfg.org/svn/source/tags/PkgForge-Server/PkgForge_Server_1_1_10/lib/PkgForge/Handler/Initialise.pm.in $
9 # $Date: 2011-01-12 17:25:17 +0000 (Wed, 12 Jan 2011) $
10  
11 our $VERSION = '1.1.10';
12  
13 1 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶use PkgForge::Utils ();
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 use Moose;
19 use MooseX::Types::Moose qw(Bool);
20  
21 extends 'PkgForge::Handler';
22  
23 has 'zap' => (
24 is => 'rw',
25 isa => Bool,
26 default => 0,
27 documentation => 'Delete existing directory contents',
28 );
29  
30 no Moose;
31 __PACKAGE__->meta->make_immutable;
32  
33 sub execute {
34 my ($self) = @_;
35  
36 # Before anything else we MUST ensure that the log directory exists.
37  
38 my $logdir = $self->logdir;
39 if ( !-d $logdir ) {
40 my $created = eval { File::Path::mkpath($logdir) };
41 if ( $EVAL_ERROR || $created == 0 ) {
42 $self->logger->log_and_die( level => 'critical',
43 message => "Failed to create $logdir: $EVAL_ERROR" );
44 }
45 }
46  
47 $self->logger->info('Initialising server environment');
48  
49 for my $attr ( $self->meta->get_all_attributes ) {
50 if ( !$attr->does('PkgForge::Meta::Attribute::Trait::Directory') ) {
51 next;
52 }
53  
54 my $dir = $attr->get_value($self);
55  
56 if ( -d $dir && $self->zap ) {
57 $self->logger->info("Wiping directory $dir");
58 my ( @errors, @results );
59 my $options = { error => \@errors, keep_root => 1 };
60 if ( $self->debug ) {
61 $options->{result} = \@results;
62 }
63  
64 PkgForge::Utils::remove_tree($dir, $options);
65  
66 if ( $self->debug ) {
67 $self->logger->debug("Removed @results");
68 }
69  
70 if ( scalar @errors > 1 ) {
71 $self->logger->log_and_die( level => 'critical',
72 message => "Failed to wipe $dir: @errors" );
73 }
74 }
75  
76 if ( !-d $dir ) {
77 $self->logger->info("Creating directory $dir");
78 my $created = eval { File::Path::mkpath($dir) };
79 if ( $EVAL_ERROR || $created == 0 ) {
80 $self->logger->log_and_die( level => 'critical',
81 message => "Failed to create $dir: $EVAL_ERROR" );
82 }
83 }
84 }
85  
86 return 1;
87 }
88  
89 1;
90 __END__
« Queue/Entry.pm ↑ Summary BuildInfo.pm »