PidFile.pm

filter: Statementstmt 53.0% 43 / 81 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 52.3% 11 / 21 Podpod 100% 6 / 6 totaltotal 46.5% 60 / 129 CC 11 SCAR 22.2
CC
11
Cov
43%
init_pid
30.0
store
20.0
does_file_exist
6.0
CRAP 9.2
« BuildCommand/Submitter/PkgSubmit.pm ↑ Summary BuildCommand/Builder.pm »
1 package PkgForge::PidFile; # -*- perl -*-
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # $Id: PidFile.pm.in 15149 2010-12-17 09:00:50Z squinney@INF.ED.AC.UK $
6 # $Source:$
7 # $Revision: 15149 $
8 # $HeadURL: https://svn.lcfg.org/svn/source/tags/PkgForge-Server/PkgForge_Server_1_1_10/lib/PkgForge/PidFile.pm.in $
9 # $Date: 2010-12-17 09:00:50 +0000 (Fri, 17 Dec 2010) $
10  
11 2 our $VERSION = '1.1.10';
12  
13 2 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Fcntl qw(:flock O_WRONLY O_EXCL O_CREAT);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use MooseX::Getopt::OptionTypeMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use MooseX::Types::Moose qw(Int Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 coerce 'PkgForge::PidFile',
24 from Str,
25 0 ▶ via { PkgForge::PidFile->new( file => $_ ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26  
27 2 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
28 'PkgForge::PidFile' => '=s',
29 );
30  
31 2 has 'file' => (
32 is => 'rw',
33 isa => Str,
34 lazy => 1,
35 predicate => 'has_pidfile',
36 builder => 'init_pidfile',
37 );
38  
39 2 has 'pid' => (
40 is => 'rw',
41 isa => Int,
42 lazy => 1,
43 predicate => 'has_pid',
44 clearer => 'clear_pid',
45 builder => 'init_pid',
46 );
47  
48 has 'progname' => (
49 is => 'rw',
50 isa => Str,
51 0 ▶ default => sub { return (File::Spec->splitpath( $PROGRAM_NAME ) )[-1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 2 );
53  
54 has 'basedir' => (
55 is => 'rw',
56 isa => Str,
57 0 ▶ default => sub { File::Spec->tmpdir },
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 2 );
59  
60 has 'mode' => (
61 is => 'rw',
62 isa => Int,
63 0 ▶ default => sub { oct '0644' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 2 );
65  
66 0 ▶sub init_pid {
Subroutine init_pid: not called CC=5 SCAR=34.0
Pod documented
67 0 my ($self) = @_;
68  
69 0 my $pid;
70 0 ▶ if ( $self->does_file_exist ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->does_file_exist) 0 0 0
71 0 my $file = $self->file;
72  
73 0 ▶ my $fh = IO::File->new( $file, 'r' )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file, "r") or die "Could not open PID file ${file}: $PkgForge::PidFile::OS_ERROR\n" 0 0 0
74 or die "Could not open PID file $file: $OS_ERROR\n";
75 0 chomp( my $contents = $fh->getline );
76 0 $fh->close;
77  
78 0 ▶ if ( $contents =~ m/^(\d+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contents =~ /^(\d+)/) { } 0 0 0
79 0 $pid = $1;
80 }
81 else {
82 0 die "Failed to parse contents of PID file $file\n";
83 }
84 }
85  
86 0 ▶ $pid = $PROCESS_ID if !defined $pid; # Default
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
87  
88 0 return $pid;
89 }
90  
91 0 ▶sub init_pidfile {
Subroutine init_pidfile: not called CC=1 SCAR=6.9
Pod documented
92 0 my ($self) = @_;
93  
94 0 my $file = $self->progname . '.pid';
95 0 return File::Spec->catfile( $self->basedir, $file );
96 }
97  
98 0 ▶sub store {
Subroutine store: not called CC=4 SCAR=30.0
Pod documented
99 0 my ($self) = @_;
100  
101 0 my $file = $self->file;
102  
103 0 my $pid = $self->pid;
104  
105 0 ▶ my $fh = IO::File->new( $file, O_WRONLY|O_EXCL|O_CREAT, $self->mode )
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "IO::File"->new($file, 193, $self->mode) or die "Could not open PID file ${file}: $PkgForge::PidFile::OS_ERROR\n" 0 0 0
106 or die "Could not open PID file $file: $OS_ERROR\n";
107 0 ▶ flock( $fh, LOCK_EX|LOCK_NB ) or die "Could not lock: $OS_ERROR\n";
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 6 or die "Could not lock: $PkgForge::PidFile::OS_ERROR\n" 0 0 0
108 0 $fh->print($pid . "\n");
109 0 ▶ $fh->close or die "Could not close PID file: $OS_ERROR\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or die "Could not close PID file: $PkgForge::PidFile::OS_ERROR\n" 0 0 0
110  
111 0 return 1;
112 }
113  
114 0 ▶sub does_file_exist {
Subroutine does_file_exist: not called CC=2 SCAR=17.9
Pod documented
115 0 my ($self) = @_;
116  
117 # Done this way to avoid it returning an undef
118 0 ▶ return ( -f $self->file || 0 );
Condition0%0/2
-f $self->file || 0
l!l
00
MC/DC0%0/1
-f $self->file || 0
-f $self->file
Truth table0%0/2
A
A= -f $self->file
Aresult
11
00
119 }
120  
121 0 ▶sub is_running {
Subroutine is_running: not called CC=2 SCAR=17.9
Pod documented
122 0 my ($self) = @_;
123  
124 0 my $pid = $self->pid;
125  
126 0 ▶ if ( -d "/proc/$pid" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "/proc/$pid") { } 0 0 0
127 0 return 1;
128 } else {
129 0 return kill 0, $pid;
130 }
131  
132 }
133  
134 0 ▶sub remove {
Subroutine remove: not called CC=2 SCAR=17.9
Pod documented
135 0 my ($self) = @_;
136  
137 0 ▶ if ( $self->does_file_exist ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->does_file_exist) { } 0 0 0
138 0 return unlink $self->file;
139 }
140 else {
141 0 return 1;
142 }
143 }
144  
145 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 2 __PACKAGE__->meta->make_immutable;
147  
148 2 1;
149 __END__
« BuildCommand/Submitter/PkgSubmit.pm ↑ Summary BuildCommand/Builder.pm »