lib/App/Followme/Initialize.pm

filter: Statementstmt 94.4% 120 / 127 Branchbran 72.5% 29 / 40 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 10 totaltotal 82.7% 178 / 215 CC 26 SCAR 14.1
CC
26
Cov
88%
initialize
10.1
create_dirs
5.0
next_command
5.0
CRAP 4.1
« lib/App/Followme/FormatPage.pm ↑ Summary lib/App/Followme/PodData.pm »
1 package App::Followme::Initialize;
2 2 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Spec::Functions qw(splitdir catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use App::Followme::NestedText;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = "2.03";
15  
16 2 require Exporter;
17 2 our @ISA = qw(Exporter);
18 2 our @EXPORT_OK = qw(initialize);
19  
20 2 our $var = {};
21 2 ▶use constant CMD_PREFIX => '#>>>';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #----------------------------------------------------------------------
24 # Initialize a new web site
25  
26 1 ▶sub initialize {
Subroutine initialize: called CC=8 SCAR=23.2
Pod undocumented
27 1 my ($directory) = @_;
28  
29 1 ▶ chdir($directory) if defined $directory;
Branch: 1 branch
BranchTrueFalseTotal
if defined $directory 1 0 1
30 1 my ($read, $unread) = data_readers();
31  
32 1 ▶ while (my ($command, $lines) = next_command($read, $unread)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = next_command($read, $unread)) 14 1 15
33 14 my @args = split(' ', $command);
34 14 my $cmd = shift @args;
35  
36 14 ▶ write_error("Missing lines after command", $command)
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "copy" and @$lines == 0 0 14 14
Condition33%1/3
$cmd eq "copy" and @$lines == 0
!ll&&!rl&&r
0140
MC/DC0%0/2
$cmd eq "copy" and @$lines == 0
$cmd eq "copy" @$lines == 0
Truth table33%1/3
A and B
A= $cmd eq "copy"B= @$lines == 0
ABresult
0X0
100
111
37 if $cmd eq 'copy' && @$lines == 0;
38  
39 14 ▶ write_error("Unexpected lines after command", $command)
Branch: 1 branch
BranchTrueFalseTotal
if $cmd ne "copy" and @$lines > 0 0 14 14
Condition33%1/3
$cmd ne "copy" and @$lines > 0
!ll&&!rl&&r
1400
MC/DC0%0/2
$cmd ne "copy" and @$lines > 0
$cmd ne "copy" @$lines > 0
Truth table33%1/3
A and B
A= $cmd ne "copy"B= @$lines > 0
ABresult
0X0
100
111
40 if $cmd ne 'copy' && @$lines > 0;
41  
42 14 ▶ if ($cmd eq 'copy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq 'copy') { } 14 0 14
43 14 write_file($lines, @args);
44  
45 } else {
46 0 write_error("Error in command name", $command);
47 }
48 }
49  
50 1 return;
51 }
52  
53 #----------------------------------------------------------------------
54 # Copy a binary file
55  
56 2 ▶sub copy_binary {
Subroutine copy_binary: called CC=4 SCAR=14.0
Pod undocumented
57 2 my($file, $lines, @args) = @_;
58 2 ▶ return if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 2 2
59  
60 2 ▶ my $out = IO::File->new($file, 'w') or die "Couldn't write $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $out = "IO::File"->new($file, "w") or die "Couldn't write ${file}: $!\n" 2 0 2
61 2 binmode($out);
62  
63 2 foreach my $line (@$lines) {
64 150 print $out decode_base64($line);
65 }
66  
67 2 close($out);
68 2 return;
69 }
70  
71 #----------------------------------------------------------------------
72 # Copy a configuration file
73  
74 2 ▶sub copy_configuration {
Subroutine copy_configuration: called CC=3 SCAR=11.5
Pod undocumented
75 2 my ($file, $lines, @args) = @_;
76
77 2 my $config;
78 2 my %old_config = nt_parse_almost_yaml_string(join('', @$lines));
79  
80 2 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 1 1 2
81 1 my $page = fio_read_page($file);
82  
83 1 ▶ if ($page =~ /:[ \n]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ /:[ \n]/) { } 1 0 1
84 1 my %new_config = nt_parse_almost_yaml_string($page);
85 1 $config = nt_merge_items(\%old_config, \%new_config);
86  
87 } else {
88 0 my $new_file = $file;
89 0 $new_file =~ s/\.*$/ocfg/;
90 0 rename($file, $new_file);
91 0 $config = \%old_config;
92 }
93  
94 } else {
95 1 $config = \%old_config;
96 }
97  
98 2 nt_write_almost_yaml_file($file, %$config);
99 2 return;
100 }
101  
102 #----------------------------------------------------------------------
103 # Copy a text file
104  
105 14 ▶sub copy_text {
Subroutine copy_text: called CC=4 SCAR=14.1
Pod undocumented
106 14 my ($file, $lines, @args) = @_;
107 14 ▶ return if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 14 14
108  
109 14 ▶ my $out = IO::File->new($file, 'w') or die "Couldn't write $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
my $out = "IO::File"->new($file, "w") or die "Couldn't write ${file}: $!\n" 14 0 14
110 14 foreach my $line (@$lines) {
111 477 print $out $line;
112 }
113  
114 14 close($out);
115 14 return;
116 }
117  
118 #----------------------------------------------------------------------
119 # Check path and create directories as necessary
120  
121 18 ▶sub create_dirs {
Subroutine create_dirs: called CC=5 SCAR=16.1
Pod undocumented
122 18 my ($file) = @_;
123  
124 18 my @dirs = splitdir($file);
125 18 pop @dirs;
126  
127 18 my @path;
128 18 ▶ while (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 40 18 58
129 40 push(@path, shift(@dirs));
130 40 my $path = catfile(@path);
131  
132 40 ▶ if ($path && ! -d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path and not -d $path) 4 36 40
Condition100%3/3
$path and not -d $path
!ll&&!rl&&r
4324
MC/DC100%2/2
$path and not -d $path
$path not -d $path
Truth table100%3/3
A and not(B)
A= $pathB= not -d $path
ABresult
0X0
100
111
133 4 ▶ mkdir($path, 0755) or die "Couldn't create $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $path, 493 or die "Couldn't create ${path}: $!\n" 4 0 4
134 }
135 }
136  
137 18 return;
138 }
139  
140 #----------------------------------------------------------------------
141 # Return closures to read the data section of this file
142  
143 1 ▶sub data_readers {
Subroutine data_readers: called CC=1 SCAR=0.0
Pod undocumented
144 1 my @pushback;
145  
146 653 ▶ my $read = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
147 653 ▶ if (@pushback) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pushback) { } 13 640 653
148 13 return pop(@pushback);
149 } else {
150 640 return <DATA>;
151 }
152 1 };
153  
154 13 ▶ my $unread = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
155 13 my ($line) = @_;
156 13 push(@pushback, $line);
157 1 };
158  
159 1 return ($read, $unread);
160 }
161  
162 #----------------------------------------------------------------------
163 # Is the line a command?
164  
165 653 ▶sub is_command {
Subroutine is_command: called CC=2 SCAR=6.9
Pod undocumented
166 653 my ($line) = @_;
167  
168 653 my $command;
169 653 my $prefix = CMD_PREFIX;
170  
171 653 ▶ if ($line =~ s/^$prefix//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^$prefix//) 28 625 653
172 28 $command = $line;
173 28 chomp $command;
174 }
175  
176 653 return $command;
177 }
178  
179 #----------------------------------------------------------------------
180 # Get the name and contents of the next file
181  
182 15 ▶sub next_command {
Subroutine next_command: called CC=5 SCAR=16.1
Pod undocumented
183 15 my ($read, $unread) = @_;
184  
185 15 my $line = $read->();
186 15 ▶ return unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 14 15
187  
188 14 my $command = is_command($line);
189 14 ▶ die "Command not supported: $line" unless $command;
Branch: 1 branch
BranchTrueFalseTotal
unless $command 0 14 14
190  
191 14 my @lines;
192 14 ▶ while ($line = $read->()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line = &$read()) 637 1 638
193 637 ▶ if (is_command($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_command($line)) { } 13 624 637
194 13 $unread->($line);
195 13 last;
196  
197 } else {
198 624 push(@lines, $line);
199 }
200 }
201  
202 14 return ($command, \@lines);
203 }
204  
205 #----------------------------------------------------------------------
206 # Die with error
207  
208 0 ▶sub write_error {
Subroutine write_error: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my ($msg, $line) = @_;
210 0 die "$msg: " . substr($line, 0, 30) . "\n";
211 }
212  
213 #----------------------------------------------------------------------
214 # Write a copy of the input file
215  
216 18 ▶sub write_file {
Subroutine write_file: called CC=1 SCAR=0.0
Pod undocumented
217 18 my ($lines, @args) = @_;
218  
219 2 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
220 18 my $type = shift(@args);
221 18 my $file = shift(@args);
222  
223 18 create_dirs($file);
224  
225 18 my $sub = "copy_$type";
226 18 &$sub($file, $lines, @args);
227  
228 18 return;
229 }
230  
231 2 1;
232 __DATA__
« lib/App/Followme/FormatPage.pm ↑ Summary lib/App/Followme/PodData.pm »