Postcodes/Update.pm

filter: Statementstmt 9.3% 10 / 107 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 42.8% 3 / 7 Podpod 100% 1 / 1 totaltotal 8.0% 14 / 174 CC 32 SCAR 53.1
CC
32
Cov
6%
_update_module
306.0
update
182.0
_download_it
20.0
CRAP 201.4
« Postcodes.pm ↑ Summary
1 package Geo::Postcodes::Update;
2  
3 #################################################################################
4 # #
5 # This file is written by Arne Sommer - perl@bbop.org #
6 # #
7 #################################################################################
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use LWP::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.311';
15  
16 #################################################################################
17  
18 sub update
19 0 ▶{
Subroutine update: not called CC=13 SCAR=52.0
Pod documented
20 0 my $module_name = shift;
21 0 my $file_name = shift;
22 0 my $full_url = shift; # Can be 'undef'
23 0 my $procedure = shift;
24 0 my $update = shift; # Do not update the module, but show the resulting perl code.
25  
26 0 ▶ $module_name = "$module_name.pm" unless $module_name =~ /\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $module_name =~ /\.pm$/ 0 0 0
27
28 0 ▶ unless (-e $module_name)
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $module_name) 0 0 0
29 {
30 0 ▶ if (-e "lib/Geo/Postcodes/")
Branch: 2 branches
BranchTrueFalseTotal
if (-e 'lib/Geo/Postcodes/') { } 0 0 0
elsif (-e '../lib/Geo/Postcodes/') { } 0 0 0
31 {
32 0 chdir("lib/Geo/Postcodes/");
33 }
34 elsif (-e "../lib/Geo/Postcodes/")
35 {
36 0 chdir("../lib/Geo/Postcodes/");
37 }
38 }
39  
40 0 ▶ die("Unable to find '$module_name'") unless -e $module_name;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $module_name 0 0 0
41  
42 0 my $in = "../../../misc/$file_name";
43  
44 0 my $downloaded = 0;
45  
46 0 ▶ unless (-e $in)
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $in) 0 0 0
47 {
48 0 print "The file 'misc/$file_name' does not exist.\n";
49 0 ▶ $downloaded = _download_it($full_url, $in) if $full_url;
Branch: 1 branch
BranchTrueFalseTotal
if $full_url 0 0 0
50 }
51  
52 0 ▶ die "Unable to open the file 'misc/$file_name'.\n - Unable to update the module.\n" unless -e $in;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $in 0 0 0
53  
54 0 ▶ unless (_update_module($module_name, $in, $procedure, $update))
Branch: 1 branch
BranchTrueFalseTotal
unless (_update_module($module_name, $in, $procedure, $update)) 0 0 0
55 {
56 0 ▶ unless ($downloaded)
Branch: 1 branch
BranchTrueFalseTotal
unless ($downloaded) 0 0 0
57 {
58 0 ▶ $downloaded = _download_it($full_url, $in) if $full_url;
Branch: 1 branch
BranchTrueFalseTotal
if $full_url 0 0 0
59  
60 0 ▶ _update_module($module_name, $in, $procedure, $update) if $downloaded;
Branch: 1 branch
BranchTrueFalseTotal
if $downloaded 0 0 0
61 }
62 }
63 }
64  
65 ################################################################################
66  
67 sub _download_it
68 0 ▶{
Subroutine _download_it: not called CC=4 SCAR=30.0
69 0 my $full_url = shift;
70 0 my $in = shift;
71  
72 0 ▶ if (_ask("Do you want to download '$full_url'"))
Branch: 1 branch
BranchTrueFalseTotal
if (_ask("Do you want to download '${full_url}'")) 0 0 0
73 {
74 0 ▶ if (-e $in)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $in) 0 0 0
75 {
76 0 rename($in, $in . "." . time());
77 }
78  
79 0 ▶ if (LWP::Simple::getstore($full_url, $in))
Branch: 1 branch
BranchTrueFalseTotal
if (&LWP::Simple::getstore($full_url, $in)) 0 0 0
80 {
81 0 print "Downloaded file '$full_url'.\n";
82 0 return 1;
83  
84 0 print "Unable to download file '$full_url'.\n";
85 0 return 0;
86 }
87 }
88 }
89  
90 ################################################################################
91  
92 sub _update_module
93 0 ▶{
Subroutine _update_module: not called CC=17 SCAR=57.2
94 0 my $module_name = shift;
95 0 my $in = shift;
96 0 my $procedure = shift;
97 0 my $update = shift;
98  
99 0 my $file = $in =~ /(misc.*)/; # Get rid of the leading "../../.." to avoid confusion.
100  
101 0 ▶ unless ($update) # No update of the module.
Branch: 1 branch
BranchTrueFalseTotal
unless ($update) 0 0 0
102 {
103 0 print "Verbose mode; no file update.\n";
104 0 print "----------------------------------------------------\n";
105 0 print "## misc/update begin\n";
106 0 print "## This data structure was auto generated on " . localtime() . ". Do NOT edit it!\n\n";
107  
108 0 ▶ open(IN, $in) or die "Unable to open the file $file.\n";
Branch: 1 branch
BranchTrueFalseTotal
open IN, $in or die "Unable to open the file $file.\n" 0 0 0
109 0 my @new = &$procedure(<IN>);
110 0 print @new;
111 0 close IN;
112 0 print "----------------------------------------------------\n";
113 0 print "(Verbose mode, no file update!)\n";
114 0 print "Run the program with the '-update' argument to update the module.\n";
115 0 return 1;
116 }
117  
118 0 ▶ open(OLD, $module_name) or die "Unable to open the file $module_name.\n";
Branch: 1 branch
BranchTrueFalseTotal
open OLD, $module_name or die "Unable to open the file $module_name.\n" 0 0 0
119 0 ▶ open(NEW, ">$module_name.new") or die "Unable to open the file '$module_name.new'.\n";
Branch: 1 branch
BranchTrueFalseTotal
open NEW, ">$module_name.new" or die "Unable to open the file '$module_name.new'.\n" 0 0 0
120  
121 ## Copy the old file - part 1
122  
123 0 my $part = 1; # 1 (first) 2 (skip) 3 (last)
124 0 my @part1;
125 my @part2; # For comparison.
126 0 my @part3;
127  
128 0 foreach (<OLD>)
129 {
130 0 ▶ if (m{^\#\# misc/update begin})
Branch: 5 branches
BranchTrueFalseTotal
if (m[^\#\# misc/update begin]) { } 0 0 0
elsif (m[^\#\# misc/update end]) { } 0 0 0
elsif ($part == 1) { } 0 0 0
elsif ($part == 2) { } 0 0 0
elsif ($part == 3) { } 0 0 0
131 {
132 0 $part = 2; # This part we skip.
133 }
134 elsif (m{^\#\# misc/update end})
135 {
136 0 $part = 3; push(@part3, $_);
137 }
138 0 elsif ($part == 1) { push(@part1, $_); }
139 0 elsif ($part == 2) { push(@part2, $_); }
140 0 elsif ($part == 3) { push(@part3, $_); }
141 }
142  
143 0 close OLD;
144 0 print NEW @part1;
145  
146 ## Update the postal info - part 2
147  
148 0 print NEW "## misc/update begin\n";
149 0 print NEW "## This data structure was auto generated on " . localtime() . ". Do NOT edit it!\n\n";
150  
151 0 ▶ open(IN, $in) or die "Unable to open the file $file.\n";
Branch: 1 branch
BranchTrueFalseTotal
open IN, $in or die "Unable to open the file $file.\n" 0 0 0
152  
153 0 my @new = &$procedure(<IN>);
154  
155 0 print NEW @new;
156 0 close IN;
157  
158 ## Copy the old file - part 3
159  
160 0 print NEW @part3;
161 0 close NEW;
162  
163 ## Check for equality ###
164  
165 0 my $old_line; my $new_line;
166  
167 0 my $difference = 0;
168  
169 0 shift @part2; shift @part2; # Get rid of some initial lines.
170  
171 FRCH:
172 0 foreach $new_line (@new)
173 {
174 0 ▶ $old_line = shift(@part2) || "";
Condition0%0/2
shift @part2 || ''
l!l
00
MC/DC0%0/1
shift @part2 || ''
shift @part2
Truth table0%0/2
A
A= shift @part2
Aresult
11
00
175 0 ▶ if ($old_line ne $new_line)
Branch: 1 branch
BranchTrueFalseTotal
if ($old_line ne $new_line) 0 0 0
176 {
177 0 $difference++;
178 0 last FRCH;
179 }
180 }
181  
182 0 ▶ if ($difference)
Branch: 1 branch
BranchTrueFalseTotal
if ($difference) { } 0 0 0
183 {
184 0 my $source = "$module_name." . time;
185 0 ▶ rename($module_name, $source) or die "Unable to rename file $module_name.\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $module_name, $source or die "Unable to rename file $module_name.\n" 0 0 0
186 0 rename("$module_name.new", $module_name);
187 0 print "The postcodes are updated in the module.\n";
188 0 print " - The old file is available as '$source'\n";
189 0 return 1;
190 }
191 else
192 {
193 0 $in =~ m{.*/(.*)}; # Drop all the leading line nose; e.g. 'tilbud5' instead of
194 # '../../../misc/tilbud5'.
195  
196 0 print "The postcodes in the module are the same as in the file '$1'.\n";
197 0 print " - The module did not need an update.\n";
198 0 unlink "$module_name.new";
199 0 return 0;
200 }
201 }
202  
203 ################################################################################
204  
205 sub _ask
206 0 ▶{
Subroutine _ask: not called CC=1 SCAR=6.9
207 0 my $question = shift;
208 0 print "$question (y/n): ";
209 0 my $answer = <STDIN>;
210 0 return $answer =~ /y/i; # Accept Y as well.
211 }
212  
213 ################################################################################
214  
215 __END__
« Postcodes.pm ↑ Summary