lib/App/Followme/UploadSite.pm

filter: Statementstmt 73.6% 137 / 186 Branchbran 40.9% 27 / 66 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 84.6% 22 / 26 Podpod 12.5% 2 / 16 totaltotal 62.2% 191 / 307 CC 41 SCAR 26.3
CC
41
Cov
64%
clean_files
42.0
update_folder
29.5
run
12.0
CRAP 13.9
« lib/App/Followme/UploadFtp.pm ↑ Summary lib/App/Followme/NestedText.pm »
1 package App::Followme::UploadSite;
2  
3 1 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use base qw(App::Followme::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use File::Spec::Functions qw(abs2rel rel2abs splitdir catfile catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use App::Followme::Web;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = "2.03";
17  
18 1 ▶use constant SEED => 96;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 #----------------------------------------------------------------------
21 # Read the default parameter values
22  
23 8 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
24 8 my ($pkg) = @_;
25  
26 return (
27 8 verbose => 0,
28 max_errors => 5,
29 remote_url => '',
30 hash_file => 'upload.hash',
31 credentials => 'upload.cred',
32 state_directory => '_state',
33 data_pkg => 'App::Followme::UploadData',
34 upload_pkg => 'App::Followme::UploadFtp',
35 );
36 }
37  
38 #----------------------------------------------------------------------
39 # Upload changed files in a directory tree
40  
41 0 ▶sub run {
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
42 0 my ($self, $folder) = @_;
43  
44 0 my ($hash, $local) = $self->get_state();
45  
46 0 my ($user, $pass) = $self->get_word();
47 0 $self->{upload}->open($user, $pass);
48  
49 0 eval {
50 chdir($self->{top_directory})
51 0 ▶ or die "Can't cd to $self->{top_directory}";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"top_directory"} or die qq[Can't cd to $self->{"top_directory"}] 0 0 0
52  
53 0 $self->update_folder($self->{top_directory}, $hash, $local);
54 0 $self->clean_files($hash, $local);
55 0 $self->{upload}->close();
56  
57 0 chdir($folder);
58 };
59  
60 0 my $error = $@;
61 0 $self->write_hash_file($hash);
62  
63 0 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
64 0 return;
65 }
66  
67 #----------------------------------------------------------------------
68 # ASK_WORD -- Ask for user name and password if file not found
69  
70 0 ▶sub ask_word {
Subroutine ask_word: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my ($self) = @_;
72  
73 0 print "\nUser name: ";
74 0 my $user = <STDIN>;
75 0 chomp ($user);
76  
77 0 print "Password: ";
78 0 my $pass = <STDIN>;
79 0 chomp ($pass);
80  
81 0 return ($user, $pass);
82 }
83  
84 #----------------------------------------------------------------------
85 # Delete files on remote site when they are no longer on local site
86  
87 0 ▶sub clean_files {
Subroutine clean_files: not called CC=6 SCAR=37.4
Pod undocumented
88 0 my ($self, $hash, $local) = @_;
89  
90 # Sort files so that files in directories are deleted before
91 # their directories are
92 0 my @filenames = sort {length($b) <=> length($a)} keys(%$local);
93  
94 0 foreach my $filename (@filenames) {
95 0 my $flag;
96 0 ▶ if ($hash->{$filename} eq 'dir') {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash->{$filename} eq 'dir') { } 0 0 0
97 0 $flag = $self->{upload}->delete_directory($filename);
98 } else {
99 0 $flag = $self->{upload}->delete_file($filename);
100 }
101  
102 0 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) { } 0 0 0
103 0 delete $hash->{$filename};
104 0 ▶ print "delete $filename\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
105  
106 } else {
107 0 ▶ die "Too many upload errors\n" if $self->{max_errors} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_errors"} == 0 0 0 0
108 0 $self->{max_errors} --;
109 }
110 }
111  
112 0 return;
113 }
114  
115 #----------------------------------------------------------------------
116 # Get the state of the site, contained in the hash file
117  
118 1 ▶sub get_state {
Subroutine get_state: called CC=2 SCAR=7.0
Pod undocumented
119 1 my ($self) = @_;
120  
121  
122 my $hash_file = catfile($self->{top_directory},
123 $self->{state_directory},
124 1 $self->{hash_file});
125  
126 1 ▶ if (-e $hash_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $hash_file) 1 0 1
127 1 $self->{target_date} = fio_get_date($hash_file);
128 }
129  
130 1 my $hash = $self->read_hash_file($hash_file);
131 1 my %local = map {$_ => 1} keys %$hash;
132  
133 1 return ($hash, \%local);
134 }
135  
136 #----------------------------------------------------------------------
137 # GET_WORD -- Say the secret word, the duck comes down and you win $100
138  
139 0 ▶sub get_word {
Subroutine get_word: not called CC=2 SCAR=17.9
Pod undocumented
140 0 my ($self) = @_;
141  
142 my $filename = catfile(
143 $self->{top_directory},
144 $self->{state_directory},
145 $self->{credentials}
146 0 );
147  
148 0 my ($user, $pass);
149 0 ▶ if (-e $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) { } 0 0 0
150 0 ($user, $pass) = $self->read_word($filename);
151 } else {
152 0 ($user, $pass) = $self->ask_word();
153 0 $self->write_word($filename, $user, $pass);
154 }
155  
156 0 return ($user, $pass);
157 }
158  
159 #----------------------------------------------------------------------
160 # Add obfuscation to string
161  
162 1 ▶sub obfuscate {
Subroutine obfuscate: called CC=2 SCAR=6.9
Pod undocumented
163 1 my ($self, $user, $pass) = @_;
164  
165 1 my $obstr = '';
166 1 my $seed = SEED;
167 1 my $str = "$user:$pass";
168  
169 1 ▶ for (my $i = 0; $i < length($str); $i += 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 15 1 16
170 15 my $val = ord(substr($str, $i, 1));
171 15 $seed = $val ^ $seed;
172 15 $obstr .= sprintf("%02x", $seed);
173 }
174  
175 1 return $obstr;
176 }
177  
178 #----------------------------------------------------------------------
179 # Read the hash for each file on the site from a file
180  
181 2 ▶sub read_hash_file {
Subroutine read_hash_file: called CC=4 SCAR=14.0
Pod undocumented
182 2 my ($self, $filename) = @_;
183  
184 2 my %hash;
185 2 my $page = fio_read_page($filename);
186  
187 2 ▶ if ($page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page) 2 0 2
188 2 my @lines = split(/\n/, $page);
189 2 foreach my $line (@lines) {
190 8 my ($name, $value) = split (/\t/, $line, 2);
191 8 ▶ die "Bad line in hash file: ($name)" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 8 8
192  
193 8 $hash{$name} = $value;
194 }
195 }
196  
197 2 return \%hash;
198 }
199  
200 #----------------------------------------------------------------------
201 # Read the user name and password from a file
202  
203 1 ▶sub read_word {
Subroutine read_word: called CC=2 SCAR=7.0
Pod undocumented
204 1 my ($self, $filename) = @_;
205  
206 1 ▶ my $obstr = fio_read_page($filename) || die "Cannot read $filename\n";
Condition50%1/2
fio_read_page($filename) || die("Cannot read $filename\n")
l!l
10
MC/DC0%0/1
fio_read_page($filename) || die("Cannot read $filename\n")
fio_read_page($filename)
Truth table50%1/2
A
A= fio_read_page($filename)
Aresult
11
00
207 1 chomp($obstr);
208  
209 1 my ($user, $pass) = $self->unobfuscate($obstr);
210 1 return ($user, $pass);
211 }
212  
213 #----------------------------------------------------------------------
214 # Rewrite the base tag of an html page
215  
216 12 ▶sub rewrite_base_tag {
Subroutine rewrite_base_tag: called CC=1 SCAR=0.0
Pod undocumented
217 12 my ($self, $page) = @_;
218  
219 12 ▶ my $base_parser = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
220 12 my ($metadata, @tokens) = @_;
221 12 return "<base href=\"$self->{remote_url}\">";
222 12 };
223  
224 12 my $global = 0;
225 12 my $metadata = [];
226 12 my $new_page = web_substitute_tags('<base href="*">',
227 $page,
228 $base_parser,
229 $metadata,
230 $global
231 );
232  
233 12 return $new_page;
234 }
235  
236 #----------------------------------------------------------------------
237 # Initialize the configuration parameters
238  
239 2 ▶sub setup {
Subroutine setup: called CC=3 SCAR=11.2
Pod documented
240 2 my ($self) = @_;
241  
242 # Turn off messages when in quick mode
243 2 ▶ $self->{verbose} = 0 if $self->{quick_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quick_mode"} 0 2 2
244  
245 # The target date is the date of the hash file, used in quick mode
246 # to select which files to test
247 2 $self->{target_date} = 0;
248  
249 # Remove any trailing slash from url
250 2 ▶ if ($self->{remote_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remote_url"}) 2 0 2
251 2 $self->{remote_url} =~ s/\/$//;
252 }
253  
254 2 return;
255 }
256  
257 #----------------------------------------------------------------------
258 # Remove obfuscation from string
259  
260 1 ▶sub unobfuscate {
Subroutine unobfuscate: called CC=2 SCAR=6.9
Pod undocumented
261 1 my ($self, $obstr) = @_;
262  
263 1 my $str = '';
264 1 my $seed = SEED;
265  
266 1 ▶ for (my $i = 0; $i < length($obstr); $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $obstr) 15 1 16
267 15 my $val = hex(substr($obstr, $i, 2));
268 15 $str .= chr($val ^ $seed);
269 15 $seed = $val;
270 }
271  
272 1 return split(/:/, $str, 2);
273 }
274  
275 #----------------------------------------------------------------------
276 # Update an individual file
277  
278 6 ▶sub update_file {
Subroutine update_file: called CC=7 SCAR=21.0
Pod undocumented
279 6 my ($self, $file, $hash) = @_;
280  
281 6 my $local_file = $file;
282  
283 # If there is a remote url, rewrite it into a new file
284 6 ▶ if ($self->{remote_url}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"remote_url"}) 6 0 6
285  
286 # Check extension, skip if not a web file
287 6 my ($dir, $basename) = fio_split_filename($file);
288 6 my ($ext) = $basename =~ /\.([^\.]*)$/;
289 6 ▶ if ($ext eq $self->{web_extension}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq $self->{"web_extension"}) 6 0 6
290 6 my $page = fio_read_page($file);
291  
292 6 ▶ if ($page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page) 6 0 6
293 6 $page = $self->rewrite_base_tag($page);
294 6 $local_file = rel2abs(catfile($self->{state_directory}, $basename));
295 6 fio_write_page($local_file, $page);
296 }
297 }
298 }
299  
300 # Upload the file and return the status of the upload
301  
302 6 my $status = 0;
303 6 my $remote_file = abs2rel($file, $self->{top_directory});
304 6 ▶ if ($self->{upload}->add_file($local_file, $remote_file)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'upload'}->add_file($local_file, $remote_file)) { } 6 0 6
305 6 $status = 1;
306  
307 } else {
308 0 ▶ die "Too many upload errors\n" if $self->{max_errors} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_errors"} == 0 0 0 0
309 0 $self->{max_errors} --;
310 }
311  
312 # Remove any temporary file
313 6 ▶ unlink($local_file) if $file ne $local_file;
Branch: 1 branch
BranchTrueFalseTotal
if $file ne $local_file 6 0 6
314 6 return $status;
315 }
316  
317 #----------------------------------------------------------------------
318 # Update files in one folder
319  
320 2 ▶sub update_folder {
Subroutine update_folder: called CC=17 SCAR=33.8
Pod undocumented
321 2 my ($self, $folder, $hash, $local) = @_;
322  
323 2 my $index_file = $self->to_file($folder);
324  
325 # Check if folder is new
326  
327 2 ▶ if ($folder ne $self->{top_directory}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folder ne $self->{"top_directory"}) 1 1 2
328 1 $folder = abs2rel($folder, $self->{top_directory});
329 1 ▶ delete $local->{$folder} if exists $local->{$folder};
Branch: 1 branch
BranchTrueFalseTotal
if exists $local->{$folder} 1 0 1
330  
331 1 ▶ if (! exists $hash->{$folder} ||
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $hash->{$folder} or $hash->{$folder} ne "dir") 1 0 1
Condition33%1/3
not exists $hash->{$folder} or $hash->{$folder} ne "dir"
l!l&&r!l&&!r
100
MC/DC0%0/2
not exists $hash->{$folder} or $hash->{$folder} ne "dir"
not exists $hash->{$folder} $hash->{$folder} ne "dir"
Truth table33%1/3
not(A) or B
A= not exists $hash->{$folder}B= $hash->{$folder} ne "dir"
ABresult
1X1
011
000
332 $hash->{$folder} ne 'dir') {
333  
334 1 ▶ if ($self->{upload}->add_directory($folder)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'upload'}->add_directory($folder)) { } 1 0 1
335 1 $hash->{$folder} = 'dir';
336 1 ▶ print "add $folder\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
337  
338 } else {
339 0 ▶ die "Too many upload errors\n" if $self->{max_errors} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"max_errors"} == 0 0 0 0
340 0 $self->{max_errors} --;
341 }
342 }
343 }
344  
345 # Check each of the files in the directory
346  
347 2 my $files = $self->{data}->build('files', $index_file);
348  
349 2 foreach my $filename (@$files) {
350 # Skip check if in quick mode and modification date is old
351  
352 6 ▶ if ($self->{quick_update}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"quick_update"}) 0 6 6
353 0 ▶ next if $self->{target_date} > fio_get_date($filename);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"target_date"} > fio_get_date($filename) 0 0 0
354 }
355  
356 6 my $file = abs2rel($filename, $self->{top_directory});
357 6 ▶ delete $local->{$file} if exists $local->{$file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $local->{$file} 6 0 6
358  
359 6 my $value = ${$self->{data}->build('checksum', $filename)};
360  
361 # Add file if new or changed
362  
363 6 ▶ if (! exists $hash->{$file} || $hash->{$file} ne $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $hash->{$file} or $hash->{$file} ne $value) 6 0 6
Condition33%1/3
not exists $hash->{$file} or $hash->{$file} ne $value
l!l&&r!l&&!r
600
MC/DC0%0/2
not exists $hash->{$file} or $hash->{$file} ne $value
not exists $hash->{$file} $hash->{$file} ne $value
Truth table33%1/3
not(A) or B
A= not exists $hash->{$file}B= $hash->{$file} ne $value
ABresult
1X1
011
000
364 6 ▶ if ($self->update_file($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->update_file($filename)) 6 0 6
365 6 $hash->{$file} = $value;
366 6 ▶ print "add $file\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 6 6
367 }
368 }
369 }
370  
371 # Recursively check each of the subdirectories
372  
373 2 my $folders = $self->{data}->build('folders', $folder);
374 2 foreach my $subfolder (@$folders) {
375 1 $self->update_folder($subfolder, $hash, $local);
376 }
377  
378 2 return;
379 }
380  
381 #----------------------------------------------------------------------
382 # Write the hash back to a file
383  
384 1 ▶sub write_hash_file {
Subroutine write_hash_file: called CC=2 SCAR=6.9
Pod undocumented
385 1 my ($self, $hash) = @_;
386  
387 1 my @hash_list;
388 1 ▶ while (my ($name, $value) = each(%$hash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hash) 4 1 5
389 4 push(@hash_list, "$name\t$value\n");
390 }
391  
392 my $filename = catfile($self->{top_directory},
393 $self->{state_directory},
394 1 $self->{hash_file});
395  
396 1 fio_write_page($filename, join('', @hash_list));
397  
398 1 return;
399 }
400  
401 #----------------------------------------------------------------------
402 # WRITE_WORD -- Write the secret word to a file
403  
404 1 ▶sub write_word {
Subroutine write_word: called CC=1 SCAR=0.0
Pod undocumented
405 1 my ($self, $filename, $user, $pass) = @_;
406  
407 1 my $obstr = $self->obfuscate ($user, $pass);
408 1 fio_write_page($filename, "$obstr\n");
409 1 chmod (0600, $filename);
410  
411 1 return;
412 }
413  
414 1 1;
415 __END__
« lib/App/Followme/UploadFtp.pm ↑ Summary lib/App/Followme/NestedText.pm »