script/ConvertConfig.pl untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 192 CC 6 SCAR 17.5
CC
6
Cov
0%
GetNewsClipperVersion
12.0
WriteConfig
12.0
AddUpdateNewsClipperVersion
6.0
CRAP 5.8
1 #!/usr/local/bin/perl
2 # use perl -*- mode: Perl; -*-
3  
4 use strict;
5  
6 # The version of the config
7 my $NC_CONFIG_VERSION = '1.30';
8  
9 my $VERSION = 0.40;
10  
11 #-------------------------------------------------------------------------------
12  
13 die <<EOF
14 $0 /path/NewsClipper.cfg
15  
16 Run this program to modernize a NewsClipper.cfg file from a version of News
17 Clipper prior to $NC_CONFIG_VERSION
18 EOF
19 if $#ARGV == -1 || $ARGV[0] =~ /^-/;
20  
21 my $configFile = $ARGV[0];
22 open CFG,$configFile or die "Can't open \"$configFile\": $!\n";
23 my $code = join '',<CFG>;
24 close CFG;
25  
26 my $old_code = $code;
27  
28 my $old_version = GetNewsClipperVersion($code);
29  
30 if (defined $old_version && $old_version >= $NC_CONFIG_VERSION)
31 {
32 print <<" EOF";
33 Your existing NewsClipper.cfg file is for News Clipper version
34 $old_version, and this ConverConfig will update up to version
35 $NC_CONFIG_VERSION. No need to update.
36 EOF
37 exit;
38 }
39  
40 if ($old_version < 1.18)
41 {
42 $code = ConvertHandlerConfig($code);
43 $code = AddAutoDownload($code);
44 $code = AddFTP($code);
45 }
46  
47 if ($old_version < 1.21)
48 {
49 $code = AddUpdateNewsClipperVersion($code,$NC_CONFIG_VERSION);
50 }
51  
52 if ($old_version < 1.29)
53 {
54 $code = AddTagText($code);
55 $code = AddChmod($code);
56 $code = AddLogging($code);
57 $code = UpdateOptionNames($code);
58 $code = AddUpdateNewsClipperVersion($code,$NC_CONFIG_VERSION);
59 }
60  
61 if ($old_version < 1.30)
62 {
63 $code = AddEmail($code);
64 $code = AddUpdateNewsClipperVersion($code,$NC_CONFIG_VERSION);
65 }
66  
67 if ($old_code eq $code)
68 {
69 print "No change to NewsClipper.cfg -- nothing written\n";
70 }
71 else
72 {
73 WriteConfig($configFile,$code);
74 }
75  
76 #-------------------------------------------------------------------------------
77  
78 sub ConvertHandlerConfig
79 {
80 my $code = shift;
81  
82 my $imgCache;
83 ($code,$imgCache) = ExtractImgCache($code);
84  
85 $code =~ s/\n*1;\n*/\n\n/s;
86  
87 $code .=<<" EOF";
88 # -----------------------------------------------------------------------------
89  
90 \%NewsClipper::Handler::Filter::cacheimages::handlerconfig = (
91  
92 $imgCache
93 );
94  
95 # -----------------------------------------------------------------------------
96  
97 1;
98 EOF
99  
100 return $code;
101 }
102  
103 #-------------------------------------------------------------------------------
104  
105 sub AddAutoDownload
106 {
107 my $code = shift;
108  
109 my $auto_download_bugfix_updates=<<' EOF';
110 # Automatically download any bugfix handlers. (Checks every few hours.)
111 'auto_download_bugfix_updates' => 'yes',
112  
113 EOF
114  
115 $code =~ s/\n+(\);\s*# --------------)/\n\n$auto_download_bugfix_updates$1/s;
116  
117 return $code;
118 }
119  
120 # -----------------------------------------------------------------------------
121  
122 sub AddFTP
123 {
124 my $code = shift;
125  
126 my ($outputFilesCode) = $code =~ /outputFiles.*?\[(.*?)\]/s;
127  
128 $outputFilesCode =~ s/(["'])\s*$/$1,/s;
129  
130 my $numFiles = ($outputFilesCode =~ tr/,//);
131  
132  
133 my $ftp=<<' EOF';
134 # ftpFiles allows you to ftp your output files to your web server. Make sure
135 # there is one set of data for each output file. The first set applies to the
136 # first output file, the second set to the second output file, etc. If you
137 # don't want to FTP a file, just use {} for the information.
138 'ftpFiles' => [
139 EOF
140  
141 $ftp .= " {},\n" x $numFiles;
142  
143 $ftp .=<<' EOF';
144 # {'server' => 'SERVER',
145 # 'username' => 'USER NAME',
146 # 'password' => 'PASSWORD',
147 # 'dir' => 'DEST DIR'},
148 ],
149 EOF
150  
151 $code =~ s/('outputFiles.*?\]\s*,)\s*/$1\n\n$ftp\n/s;
152  
153 return $code;
154 }
155  
156 # -----------------------------------------------------------------------------
157  
158 sub AddEmail
159 {
160 my $code = shift;
161  
162 my ($ftp_file_code) = $code =~ /(ftp_files.*?\[.*?\].*?\n)/s;
163  
164 my $email=<<' EOF';
165 # email_files allows you to email your output files to one or more email
166 # addresses. Make sure there is one set of emails for each output file. The
167 # first set applies to the first output file, the second set to the second
168 # output file, etc. If you don't want to email a file, just use [] for the
169 # information. The example shows how to send one output file.
170 'email_files' => [
171 # {'From' => 'News Clipper <newsclipper@your.server>',
172 # 'To' => 'First Person <person1@their.server>',
173 # 'Cc' => '',
174 # 'Bcc' => '',
175 # 'Subject' => 'Newsletter'},
176 ],
177 EOF
178  
179 $code =~ s/(ftp_files.*?\[.*?\].*?\n)/$1\n$email/s;
180  
181 return $code;
182 }
183  
184 # -----------------------------------------------------------------------------
185  
186 sub GetNewsClipperVersion
187 {
188 my $code = shift;
189  
190 my ($for_news_clipper_version) =
191 $code =~ /'(?:for_news_clipper_version|forNewsClipperVersion)' *=> *(.*?) *,/s;
192  
193 # Ug. Pre "forNewsClipperVersion" days...
194 if (!defined $for_news_clipper_version)
195 {
196 # A 1.18-style config file.
197 if ($code =~ /ftpFiles/)
198 {
199 return 1.18;
200 }
201 else
202 {
203 return 1.00;
204 }
205 }
206 else
207 {
208 return $for_news_clipper_version;
209 }
210 }
211  
212 # -----------------------------------------------------------------------------
213  
214 sub AddUpdateNewsClipperVersion
215 {
216 my $code = shift;
217 my $NC_CONFIG_VERSION = shift;
218  
219 my ($nc_version_code,$for_news_clipper_version) =
220 $code =~ /('(?:for_news_clipper_version|forNewsClipperVersion)' *=> *(.*?) *,)/s;
221  
222 if (defined $nc_version_code)
223 {
224 my $new_version_code = $nc_version_code;
225 $new_version_code =~ s/\Q$for_news_clipper_version\E/$NC_CONFIG_VERSION/;
226 $code =~ s/\Q$nc_version_code\E/$new_version_code/s;
227 }
228 else
229 {
230 my $new_version_code =<<" EOF";
231 # This value lets News Clipper know if the configuration file is incompatible.
232 'forNewsClipperVersion' => $NC_CONFIG_VERSION,
233 EOF
234 $code =~ s/(\%config *= *\( *\n)/$1\n$new_version_code/s;
235 }
236  
237 return $code;
238 }
239  
240 # -----------------------------------------------------------------------------
241  
242 sub AddTagText
243 {
244 my $code = shift;
245  
246 my $add_code =<<EOF;
247 # The keyword to indicate News Clipper commands. The default is "newsclipper",
248 # which results in <!-- newsclipper ... --> as the default command comment.
249 'tag_text' => 'newsclipper',
250  
251 EOF
252  
253 $code =~ s/(auto_download_bugfix.*?\n)(\);)/$1$add_code$2/s;
254  
255 return $code;
256 }
257  
258 # -----------------------------------------------------------------------------
259  
260 sub AddChmod
261 {
262 my $code = shift;
263  
264 my $add_code =<<EOF;
265 # Determines whether output files should be made executable as well as
266 # readable.
267 'make_output_files_executable' => 'yes',
268  
269 EOF
270  
271 $code =~ s/(tag_text.*?\n)(\);)/$1$add_code$2/s;
272  
273 return $code;
274 }
275  
276 # -----------------------------------------------------------------------------
277  
278 sub AddLogging
279 {
280 my $code = shift;
281  
282 my $add_code =<<EOF;
283 # The location of the News Clipper debug and error log files. Set to "STDOUT"
284 # or "STDERR" to send to standard output or standard error.
285 'debug_log_file' => "\$home/.NewsClipper/logs/debug.log",
286 'run_log_file' => "\$home/.NewsClipper/logs/run.log",
287  
288 # These values allow you to configure the old logs that are saved. After the
289 # log file reaches the max_log_file_size (in bytes), it is renamed and a new
290 # one is started. If there the max_number_of_log_files has been reached, then
291 # the oldest one is deleted before the logs are rotated. On Unix systems the
292 # old log files are zipped.
293 'max_number_of_log_files' => 7,
294 'max_log_file_size' => 1000000,
295  
296 EOF
297  
298 $code =~ s/(tag_text.*?\n)(\);)/$1$add_code$2/s;
299  
300 return $code;
301 }
302  
303 # -----------------------------------------------------------------------------
304  
305 sub UpdateOptionNames
306 {
307 my $code = shift;
308  
309 $code =~ s/forNewsClipperVersion/for_news_clipper_version/g;
310 $code =~ s/regKey/registration_key/g;
311 $code =~ s/inputFiles/input_files/g;
312 $code =~ s/outputFiles/output_files/g;
313 $code =~ s/ftpFiles/ftp_files/g;
314 $code =~ s/handlerlocations/handler_locations/g;
315 $code =~ s/modulepath/module_path/g;
316 $code =~ s/cachelocation/cache_location/g;
317 $code =~ s/maxcachesize/max_cache_size/g;
318 $code =~ s/scriptTimeout/script_timeout/g;
319 $code =~ s/socketTries/socket_tries/g;
320 $code =~ s/socketTimeout/socket_timeout/g;
321  
322 return $code;
323 }
324  
325 # -----------------------------------------------------------------------------
326  
327 sub ExtractImgCache()
328 {
329 my $code = shift;
330  
331 my $cacheImg;
332  
333 $code =~ s/\n*((# [^\n]+\n)*'imgcachedir'[^\n]*\n)/$cacheImg .= $1;"\n"/se;
334 $code =~ s/\n*((# [^\n]+\n)*'imgcacheurl'[^\n]*\n)/$cacheImg .= $1;"\n"/se;
335 $code =~ s/\n*((# [^\n]+\n)*'maximgcacheage'[^\n]*\n)/$cacheImg .= $1;"\n"/se;
336  
337 return ($code,$cacheImg);
338 }
339  
340 #-------------------------------------------------------------------------------
341 sub WriteConfig($$)
342 {
343 my $configFile = shift;
344 my $code = shift;
345  
346 my $backup;
347  
348 for (my $i = 1; 1; $i++)
349 {
350 $backup = $configFile;
351 $backup =~ s/$/.bak/i;
352 $backup .= $i unless $i == 1;
353 last unless -e $backup;
354 }
355  
356 rename $configFile,$backup or
357 warn "Couldn't not rename $configFile\n to $backup. Skipping...\n"
358 and return;
359 open NEW, ">$configFile" or die "Can't open $configFile for writing: $!\n";
360 print NEW $code;
361 close NEW;
362  
363 print<<" EOF";
364 - Config file $configFile
365 has been converted and saved. Check it to make sure everything look reasonable.
366 (The old configuration was renamed to $backup.)
367 EOF
368 }
369