Moose/DataFolder.pm

filter: Statementstmt 99.1% 115 / 116 Branchbran 75.0% 15 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 25 / 25 Podpod 0.0% 0 / 1 totaltotal 93.4% 156 / 167 CC 12 SCAR 8.3
CC
12
Cov
94%
BUILD
6.0
_copy_user_script
3.0
_get_folder_number
3.0
CRAP 2.3
« Moose/Instrument/AttoCube_AMC.pm ↑ Summary Moose/Instrument/DummySource.pm »
1 package Lab::Moose::DataFolder;
2 27 $Lab::Moose::DataFolder::VERSION = '3.931';
3 #ABSTRACT: Create a data directory with meta data
4  
5 27 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use MooseX::Params::Validate;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 27 ▶use Lab::Moose::Catfile 'our_catfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use File::Basename qw/basename dirname/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 ▶use File::Copy 'copy';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 27 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 27 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 27 ▶use Time::HiRes qw/gettimeofday tv_interval/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 27 ▶use POSIX qw/strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 27 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 27 ▶use Encode 'decode';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 27 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 27 ▶use Lab::Moose::DataFile::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Get a copy of @ARGV, before it get's mangled by the user script.
31  
32 27 our @ARGV_COPY;
33  
34 27 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 27 @ARGV_COPY = ( $0, @ARGV );
36 }
37  
38  
39 27 has path => (
40 is => 'ro',
41 isa => 'Str',
42 writer => '_path',
43 predicate => 'has_path',
44 );
45  
46 27 has date_prefix => (
47 is => 'ro',
48 isa => 'Bool',
49 default => 1
50 );
51  
52 27 has time_prefix => (
53 is => 'ro',
54 isa => 'Bool',
55 default => 1
56 );
57  
58 27 has meta_file => (
59 is => 'ro',
60 isa => 'Lab::Moose::DataFile::Meta',
61 init_arg => undef,
62 writer => '_meta_file'
63 );
64  
65 27 has copy_script => (
66 is => 'ro',
67 isa => 'Bool',
68 default => 1
69 );
70  
71 27 has script_name => (
72 is => 'ro',
73 isa => 'Str',
74 );
75  
76 73 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
77 73 my $self = shift;
78  
79 73 ▶ if ( not $self->has_path() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_path) 1 72 73
80 1 $self->_path('MEAS');
81 }
82  
83 73 my $folder = $self->path();
84 73 my $dirname = dirname($folder);
85 73 my $basename = basename($folder);
86  
87 73 ▶ if ( $self->time_prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->time_prefix) 34 39 73
88 34 $basename = strftime( '%H-%M-%S', localtime() ) . "_$basename";
89 34 $folder = our_catfile( $dirname, $basename );
90 }
91  
92 73 ▶ if ( $self->date_prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->date_prefix) 34 39 73
93 34 $basename = strftime( '%Y-%m-%d', localtime() ) . "_$basename";
94 34 $folder = our_catfile( $dirname, $basename );
95 }
96  
97 73 my $folder_number = _get_folder_number(
98 basename => $basename,
99 dirname => $dirname
100 );
101  
102 73 $folder .= sprintf( '_%03d', $folder_number );
103  
104 73 ▶ mkdir $folder
Branch: 1 branch
BranchTrueFalseTotal
mkdir $folder or croak("cannot make directory '${folder}': $!") 73 0 73
105 or croak "cannot make directory '$folder': $!";
106  
107 73 $self->_path($folder);
108  
109 73 $self->_create_meta_file();
110  
111 73 ▶ if ( $self->copy_script ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->copy_script) 73 0 73
112 73 $self->_copy_user_script();
113 }
114  
115 }
116  
117 73 ▶sub _copy_user_script {
Subroutine _copy_user_script: called CC=3 SCAR=11.0
118 73 my $self = shift;
119 73 my $script = $0;
120  
121 73 my $basename;
122 73 my $script_name = $self->script_name();
123  
124 73 ▶ if ($script_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_name) { } 1 72 73
125 1 $basename = $script_name;
126 }
127 else {
128 72 $basename = basename($script);
129 }
130  
131 73 my $copy = our_catfile( $self->path, $basename );
132  
133 73 ▶ copy( $script, $copy )
Branch: 1 branch
BranchTrueFalseTotal
copy($script, $copy) or croak("copy of $script to $copy failed: $!") 73 0 73
134 or croak "copy of $script to $copy failed: $!";
135 }
136  
137 73 ▶sub _create_meta_file {
Subroutine _create_meta_file: called CC=2 SCAR=7.1
138 73 my $self = shift;
139 73 my $time = [ gettimeofday() ];
140  
141 my %meta_data = (
142 argv => [@ARGV_COPY],
143  
144 # See http://stackoverflow.com/questions/3526420/how-do-i-get-the-current-user-in-perl-in-a-portable-way
145 user => getlogin() || getpwuid($<),
146 host => hostname(),
147 date => strftime( "%c", localtime() ),
148 73 ▶ timestamp => join( '.', @{$time} ),
Condition33%1/3
getlogin || getpwuid $<
l!l&&r!l&&!r
0730
MC/DC0%0/2
getlogin || getpwuid $<
getlogin getpwuid $<
Truth table33%1/3
A || B
A= getloginB= getpwuid $<
ABresult
1X1
011
000
149 version => _get_version(),
150 );
151  
152 73 my $meta_file = Lab::Moose::DataFile::Meta->new(
153 folder => $self,
154 filename => 'META.yml',
155 );
156  
157 73 $self->_meta_file($meta_file);
158  
159 73 $meta_file->log( meta => \%meta_data );
160 }
161  
162 73 ▶sub _get_version {
Subroutine _get_version: called CC=2 SCAR=7.2
163 27 ▶ no strict 'vars'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
164 73 ▶ if ( defined $VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $VERSION) { } 73 0 73
165 73 return $VERSION;
166 }
167 else {
168 0 return 'git';
169 }
170 }
171  
172 73 ▶sub _get_folder_number {
Subroutine _get_folder_number: called CC=3 SCAR=11.0
173 73 my ( $basename, $dirname ) = validated_list(
174 \@_,
175 basename => { isa => 'Str' },
176 dirname => { isa => 'Str' },
177 );
178  
179 73 ▶ opendir my $dh, $dirname
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dirname or croak("cannot open directory '${dirname}'") 73 0 73
180 or croak "cannot open directory '$dirname'";
181  
182 73 my @entries = readdir $dh;
183  
184 73 @entries = map { decode( 'UTF-8', $_ ) } @entries;
185  
186 my $max = max map {
187 73 my $entry = $_;
188 764 ▶ if ( $entry =~ /^\Q${basename}\E_([0-9]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /^\Q$basename\E_([0-9]+)$/u) { } 541 223 764
189 541 my $num = $1;
190 }
191 else {
192 223 my $num = 0;
193 }
194 } @entries;
195  
196 73 return $max + 1;
197 }
198  
199 27 __PACKAGE__->meta->make_immutable();
200 27 1;
201  
202 __END__
« Moose/Instrument/AttoCube_AMC.pm ↑ Summary Moose/Instrument/DummySource.pm »