WE_Frontend/Installer.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« WE/DB/OnlineUser.pm ↑ Summary WebEditor/OldController.pm »
1 # -*- perl -*-
2  
3 #
4 # $Id: Installer.pm,v 1.6 2004/06/10 13:18:02 eserte Exp $
5 # Author: Slaven Rezic
6 #
7 # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8 # Copyright (C) 2002 Slaven Rezic.
9 # This is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License, see the file COPYING.
11  
12 #
13 # Mail: slaven@rezic.de
14 # WWW: http://we-framework.sourceforge.net
15 #
16  
17 package WE_Frontend::Installer;
18  
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use vars qw($VERSION $magicfile $magiccontent);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 $VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/);
22  
23 2 ▶use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 __PACKAGE__->mk_accessors(qw(Main));
25  
26 0 ▶use CGI qw(:standard);
Subroutine BEGIN: called CC=1 SCAR=2.6
27 #use CGI::Carp qw(fatalsToBrowser);
28 use File::Path;
29 use File::Basename;
30 use File::stat;
31  
32 use WE::Util::Functions qw(_save_pwd);
33  
34 $magicfile = "magic";
35 $magiccontent = "Elvis is alive";
36  
37 =head1 NAME
38  
39 WE_Frontend::Installer - handle servicepack creation and handling
40  
41 =head1 SYNOPSIS
42  
43 use WE_Frontend::Installer;
44 WE_Frontend::Installer->create_servicepack($tarfile);
45  
46 =head1 DESCRIPTION
47  
48 This module handles servicepack creation and handling.
49  
50 =head2 METHODS and FUNCTIONS
51  
52 =over 4
53  
54 =item new
55  
56 Return a new C<WE_Frontend::Installer> object.
57  
58 =cut
59  
60 sub new {
61 bless {}, $_[0];
62 }
63  
64 =item objectify($self)
65  
66 Return a new C<WE_Frontend::Installer> object, if it does not already
67 exist in C<$self>. Should be called as a static method.
68  
69 =cut
70  
71 sub objectify {
72 my $self = shift;
73 if (!$self || $self eq __PACKAGE__) {
74 require WE_Frontend::MainAny;
75 $self = new WE_Frontend::Installer;
76 my $main = WE_Frontend::MainAny->new;
77 if (!$main) {
78 die "Can't make \$main object";
79 }
80  
81 $self->Main($main);
82 }
83 $self;
84 }
85  
86 sub Config { shift->Main->Config(@_) }
87  
88 =item main
89  
90 Create a HTML page for uploading and installing a service pack. This
91 calls either upload_form or handle_tar.
92  
93 =cut
94  
95 sub main {
96 my $self = shift;
97 $self = objectify($self);
98  
99 print header, "<html><body bgcolor='#ffffff'>";
100  
101 eval {
102 if (!param('tarfile')) {
103 $self->upload_form;
104 } else {
105 $self->handle_tar;
106 }
107 };
108 if ($@) {
109 print "Folgende Fehler sind aufgetreten:",
110 br,pre(escapeHTML($@)),p;
111 }
112  
113 print "<hr>";
114 print '<br><a href="we_redisys.cgi?goto=siteeditorframe">';
115 print "zurück zum Site-Editor</a><br></body></html>";
116 }
117  
118 =item upload_form
119  
120 Create a HTML page for uploading a service pack.
121  
122 =cut
123  
124 sub upload_form {
125 my $self = shift;
126 my $scriptname = script_name();
127 print qq~
128 <script>
129 function check() {
130 return confirm("Das Servicepack wird im Verzeichnis @{[ $self->Config->paths->rootdir ]} installiert. Fortsetzen?");
131 }
132 </script>
133 <form action="$scriptname" method="POST" ENCTYPE="multipart/form-data" onsubmit="return check()"><table><tr>
134 <td>Servicepack-Datei: </td>
135 <td><input type=file name="tarfile">
136 <td><input type=submit value="upload"></td>
137 </tr></table></form><br>~;
138 }
139  
140 =item upload_form
141  
142 Create a HTML page for installing a previously uploaded service pack.
143  
144 =cut
145  
146 sub handle_tar {
147 my $self = shift;
148 require Archive::Tar;
149 my $uploadfile = param('tarfile');
150 my $tmpdir = tmpdir();
151 if (!defined $tmpdir) {
152 die "Cannot find suitable temporary directory";
153 }
154 my $extrdir = "$tmpdir/webeditor_service";
155 if (-d $extrdir) {
156 rmtree([$extrdir], 0, 1);
157 }
158 mkdir $extrdir, 0775;
159 if (!-d $extrdir) {
160 die "Cannot create extraction directory $extrdir";
161 }
162 chdir $extrdir or die "Can't chdir to $extrdir: $!";
163  
164 my $tarfilename = "$extrdir/service.tar.gz";
165 open(SP,">$tarfilename") or die "Can't writeopen $tarfilename: $!";
166 binmode SP;
167 while (<$uploadfile>) {
168 print SP $_;
169 }
170 close SP;
171 print "File-Upload abgeschlossen.<br>";
172  
173 my $tar = Archive::Tar->new();
174 $tar->read($tarfilename);
175  
176 my $is_servicepack = 0;
177 foreach my $m ($magicfile, "./$magicfile") {
178 if ($tar->get_content($m) =~ /\Q$magiccontent/) {
179 $is_servicepack++;
180 last;
181 }
182 }
183  
184 if ($is_servicepack) {
185 # XXX $tar->extract geht nicht?!
186 if (!$tar->extract_archive($tarfilename)) {
187 print "Extrahieren von $tarfilename fehlgeschlagen: ". $tar->error();
188 goto CLEANUP;
189 } else {
190 print "Dateien extrahiert.<br>\n";
191 };
192 } else {
193 print "<font color=red>Das scheint kein gültiges Servicepack zu sein!</font><br>";
194 goto CLEANUP;
195 }
196  
197 unlink "$extrdir/$magicfile";
198 $self->install($extrdir);
199  
200 CLEANUP:
201 unlink $tarfilename;
202 }
203  
204 =item install($dir)
205  
206 Install the contents of directory C<$dir> to the rootdir of the
207 system.
208  
209 =cut
210  
211 sub install {
212 my($self, $dir) = @_;
213  
214 if (-e "$dir/install.pl") {
215 if (-x "$dir/install.pl") {
216 system("$dir/install.pl");
217 if ($?/256!=0) {
218 print "Fehler beim Ausführen von install.pl!<br>\n";
219 }
220 } else {
221 print "install.pl ist nicht ausführbar.<br>\n";
222 }
223 } else {
224 print "Kopieren:\n<br>";
225 my(@f) = glob("$dir/*");
226 @f = grep { $_ !~ /\.tar\.gz$/ } @f; # tar.gz-Dateien ausschließen
227 my @cmd = ('cp', '-Rf', @f, $self->Config->paths->rootdir);
228 print join(" ",@cmd), "<br>";
229 system(@cmd);
230 }
231  
232 CLEANUP: 1;
233 # XXX missing cleanup of $dir
234 }
235  
236 sub tmpdir {
237 foreach my $d ("/tmp", "/var/tmp", "/usr/tmp", "/temp", "C:/temp", "C:/windows/temp", "D:/temp") {
238 next if !defined $d;
239 next if !-d $d || !-w $d;
240 return $d;
241 }
242 undef;
243 }
244  
245 =item WE_Frontend::Installer->create_servicepack($destfile, %args)
246  
247 =item $self->create_servicepack($destfile, %args)
248  
249 Create a service pack file. Ignores all WEsiteinfo*.pm files.
250  
251 The %args hash may contain the following key-value pairs:
252  
253 =over 4
254  
255 =item -wesiteinfo
256  
257 If -wesiteinfo is specified, then use this file as the WEsiteinfo.pm
258 file for the target site. Most times there is a WEsiteinfo.pm file for
259 local development and a WEsiteinfo_customer.pm file for the customer
260 site.
261  
262 =item -since date
263  
264 Only include files newer than C<date>. L<Date::Parse> is used for
265 parsing the date string.
266  
267 =item -v
268  
269 Set to 1 to generate verbose messages.
270  
271 =back
272  
273 =cut
274  
275 sub create_servicepack {
276 my($self, $destfile, %args) = @_;
277  
278 $self = objectify($self);
279  
280 if (!defined $destfile) {
281 die "Destfile not given";
282 }
283  
284 my $since;
285 if (defined $args{-since}) {
286 require Date::Parse;
287 $since = Date::Parse::str2time($args{-since});
288 if (!defined $since) {
289 die "Could not parse the date $args{-since}";
290 }
291 }
292 my $v = $args{-verbose};
293  
294 require Archive::Tar;
295 require File::Find;
296  
297 require 5.006; # this perl includes a version of File::Find which can
298 # follow symlinks
299  
300 my $tar = new Archive::Tar;
301  
302 my $is_new = sub {
303 my $file = shift;
304 return (!defined $since || stat($file)->mtime > $since);
305 };
306  
307 my @files;
308 my $wanted = sub {
309 if (-d $_ && (/^(RCS|CVS|\.svn|headlines|photos)$/ ||
310 $File::Find::name =~ m;(we_data/content|html/.+);)) {
311 $File::Find::prune = 1;
312 return;
313 }
314 if (-f $_ && (/^(\.cvsignore|WEsiteinfo.*\.pm|.*~|\.\#.*)$/ ||
315 $File::Find::name =~ m;( we_data/.*\.db$ |
316 we_data/.*\.lock$
317 );x)
318 ) {
319 return;
320 }
321 if (-f $_) {
322 return if !$is_new->($_);
323 push @files, $File::Find::name;
324 }
325 };
326  
327 my $rootdir = $self->Config->paths->rootdir;
328 my $cgidir = $self->Config->paths->cgidir;
329 _save_pwd {
330 chdir $rootdir or die "Can't chdir to $rootdir: $!";
331  
332 File::Find::find({wanted => $wanted, follow => 1 }, ".");
333  
334 # Hmmm... add_files does not work?!
335 # But nevertheless I need resolved symbolic links, so this is the
336 # only possibility.
337 foreach my $f (@files) {
338 warn "Add $f ...\n" if $v;
339 _tar_add_file($tar, $f);
340 }
341 };
342  
343 if ($args{-wesiteinfo}) {
344 my $as = $cgidir;
345 $as =~ s|^$rootdir/*||;
346 $as .= "/WEsiteinfo.pm";
347 if ($is_new->($args{-wesiteinfo})) {
348 warn "Add $as ...\n" if $v;
349 _tar_add_file($tar, $args{-wesiteinfo}, $as);
350 }
351 }
352  
353 $tar->add_data($magicfile, $magiccontent);
354  
355 $tar->write($destfile, 9)
356 or die "Can't write to $destfile: " . $tar->error;
357  
358 }
359  
360 sub _tar_add_file {
361 my($tar, $f, $as) = @_;
362 open(F, $f) or die "Can't open file $f: $!";
363 local $/ = undef;
364 my $buf = <F>;
365 close F;
366 $as = $f if !defined $as;
367 my $s = stat $f;
368 my %stat = (mode => $s->mode,
369 mtime => $s->mtime);
370 $tar->add_data($as, $buf, \%stat);
371 }
372  
373 1;
374  
375 __END__
« WE/DB/OnlineUser.pm ↑ Summary WebEditor/OldController.pm »