blib/lib/PAR/WebStart.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/t/basic.t ↑ Summary lib/t/make_par.t »
1 package PAR::WebStart;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use LWP::Simple qw(getstore is_success);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 require File::Spec;
6 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use PAR::WebStart::PNLP;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use PAR::WebStart::Util qw(verifyMD5);
12 use Config;
13 use constant WIN32 => PAR::WebStart::Util::WIN32;
14
15 our $VERSION = '0.20';
16
17 sub new {
18 my ($class, %args) = @_;
19 my $file = $args{file};
20 die "Please supply the pnlp file" unless ($file and -e $file);
21 my $obj = PAR::WebStart::PNLP->new(file => $file);
22 my $cfg = $obj->parse();
23 die "Error: $obj->{ERROR}" unless ($cfg);
24
25 my $tmpdir = $ENV{PAR_TEMP} ||
26 File::Spec->catdir(File::Spec->tmpdir(), 'par');
27 unless (-d $tmpdir) {
28 mkdir($tmpdir) or die qq{Failed to mkdir "$tmpdir": $!};
29 }
30
31 my $par_command = {};
32 if (WIN32) {
33 die qq{Could not find "par"}
34 unless ($par_command->{par} = which('par'));
35 ($par_command->{par_pl} = $par_command->{par}) =~ s/\.bat$/.pl/i;
36 die qq{Could not find "par.pl"}
37 unless (-f $par_command->{par_pl});
38 if ($cfg->{wperl}->{seen}) {
39 die qq{Could not find "wperl"}
40 unless ($par_command->{wperl} = which('wperl'));
41 }
42 }
43 else {
44 die qq{Could not find "par.pl"}
45 unless ($par_command->{par_pl} = which('par.pl'));
46 }
47
48 my %config = (os => $Config{osname},
49 arch => $Config{archname},
50 version => $],
51 perl_version => $Config{PERL_VERSION},
52 );
53 my $self = {pnlp => $file, cfg => $cfg, ERROR => '', %config,
54 tmpdir => $tmpdir, pars => [], par_command => $par_command,
55 cached_pars => {} };
56 bless $self, $class;
57 }
58
59 sub fetch_pars {
60 my $self = shift;
61 my $cfg = $self->{cfg};
62
63 if (my $version = $cfg->{perlws}->{version}) {
64 if ($VERSION < $version) {
65 $self->{ERROR} = qq{PAR::WebStart version '$version' required, but only '$VERSION' seen};
66 return;
67 }
68 }
69
70 my $prereqs = $cfg->{module};
71 if ($prereqs and ref($prereqs) eq 'ARRAY' ) {
72 return unless $self->check_prereqs($prereqs);
73 }
74
75 if (my $resources = $cfg->{resources}) {
76 unless ($self->check_platform($resources)) {
77 $self->{ERROR} = 'Resource specification not intended for this platform';
78 return;
79 }
80 }
81 my $par = $cfg->{par};
82 unless ($par and ref($par) eq 'ARRAY') {
83 $self->{ERROR} = 'No par archives specified';
84 return;
85 }
86 my $par_files = [];
87 foreach my $file(@$par) {
88 next unless $self->check_platform($file);
89 push @$par_files, $file->{href};
90 }
91 if (scalar(@$par_files) == 0) {
92 $self->{ERROR} = 'No suitable par files found for this platform';
93 return;
94 }
95
96 my $tmpdir = $self->{tmpdir};
97 my $codebase = $cfg->{pnlp}->{codebase};
98 $codebase =~ s{/$}{};
99
100 foreach my $par (@$par_files) {
101 my $md5 = $par . '.md5';
102 my $remote_par = $codebase . '/' . $par;
103 my $remote_md5 = $codebase . '/' . $md5;
104 my $local_par = File::Spec->catfile($tmpdir,
105 basename($par, qr{\.par}));
106 my $local_md5 = File::Spec->catfile($tmpdir,
107 basename($md5, qr{\.md5}));
108 unless (is_success(getstore($remote_md5, $local_md5))) {
109 $self->{ERROR} = qq{Failed to get "$remote_md5"};
110 return;
111 }
112 if (-e $local_par) {
113 my $status = verifyMD5(md5 => $local_md5, file => $local_par);
114 if ($status and $status =~ /^1$/) {
115 my $base = basename($local_par, qr{\.par});
116 push @{$self->{pars}}, $base;
117 $self->{cached_pars}->{$base}++;
118 next;
119 }
120 }
121 unless (is_success(getstore($remote_par, $local_par))) {
122 $self->{ERROR} = qq{Failed to get "$remote_par"};
123 return;
124 }
125
126 my $status = verifyMD5(md5 => $local_md5, file => $local_par);
127 unless ($status and $status =~ /^1$/) {
128 $self->{ERROR} = $status;
129 return;
130 }
131
132 push @{$self->{pars}}, basename($local_par, qr{\.par});
133 }
134
135 if ($cfg->{icon}) {
136 my $icon = $cfg->{icon}->{href};
137 my $remote_icon = $codebase . '/' . $icon;
138 my $local_icon = File::Spec->catfile($tmpdir,
139 basename($icon, qr{\..*}));
140 unless (is_success(getstore($remote_icon, $local_icon))) {
141 $self->{ERROR} = qq{Failed to get "$remote_icon"};
142 return;
143 }
144 }
145
146 unless ($cfg->{'allow-unsigned-pars'}->{seen}) {
147 $self->verify_sig() or return;
148 }
149 return 1;
150 }
151
152 sub check_platform {
153 my ($self, $hash) = @_;
154 return if ($hash->{version} and $hash->{version} > $self->{version});
155 foreach my $key(qw(os arch perl_version)) {
156 return if ($hash->{$key} and $hash->{$key} ne $self->{$key});
157 }
158 return 1;
159 }
160
161 sub check_prereqs {
162 my ($self, $prereqs) = @_;
163 my @wanted = ();
164 foreach my $ref(@{$prereqs}) {
165 my $mod = $ref->{value};
166 eval "require $mod";
167 next unless $@;
168 push @wanted, $mod;
169 }
170 if (@wanted) {
171 my $needed = join ', ', @wanted;
172 $self->{ERROR} = <<"END";
173 The following modules are needed but were not found:
174 $needed
175 Please consider installing them first.
176 END
177 return;
178 }
179 return 1;
180 }
181
182
183 sub verify_sig {
184 my $self = shift;
185 my $tmpdir = $self->{tmpdir};
186 chdir($tmpdir) or do {
187 $self->{ERROR} = qq{Cannot chdir to "$tmpdir": $!};
188 return;
189 };
190 my @args = ();
191 my $par_command = $self->{par_command};
192 if (WIN32) {
193 if ($par_command->{wperl}) {
194 push @args, ($par_command->{wperl}, $par_command->{par_pl});
195 }
196 else {
197 push @args, $par_command->{par};
198 }
199 }
200 else {
201 push @args, $par_command->{par_pl};
202 }
203 push @args, '-v';
204 no warnings;
205 my ($fh, $filename) = tempfile(UNLINK => 1);
206 open my $oldout, ">&STDOUT" or die "Cannot dup STDOUT: $!";
207 open OLDERR, ">&", \*STDERR or die "Cannot dup STDERR: $!";
208 open STDOUT, '>', $filename or die "Cannot redirect STDOUT: $!";
209 open STDERR, ">&STDOUT" or die "Cannot dup STDERR: $!";
210 select STDERR; $| = 1;
211 select STDOUT; $| = 1;
212
213 my $pars = $self->{pars};
214 my $cached_pars = $self->{cached_pars};
215 foreach my $par(@$pars) {
216 next if $cached_pars->{$par};
217 system(@args, $par);
218 }
219
220 seek($fh, 0, 1);
221 open STDOUT, ">&", $oldout or die "Cannot dup \$oldout: $!";
222 open STDERR, ">&OLDERR" or die "Cannot dup OLDERR: $!";
223 my $failure = 0;
224 my $text = '';
225 while (my $line = <$fh>) {
226 $text .= $line;
227 $failure++ if ($line =~ /Mismatched content between SIGNATURE/i);
228 }
229 if ($failure) {
230 $self->{ERROR} = $text;
231 return;
232 }
233 return 1;
234 }
235
236 sub run_command {
237 my $self = shift;
238
239 my @args = ();
240 my $par_command = $self->{par_command};
241 if (WIN32) {
242 if ($par_command->{wperl}) {
243 push @args, ($par_command->{wperl}, $par_command->{par_pl});
244 }
245 else {
246 push @args, $par_command->{par};
247 }
248 }
249 else {
250 push @args, $par_command->{par_pl};
251 }
252
253 my $pars = $self->{pars};
254 my $number_of_pars = scalar(@$pars);
255 if ($number_of_pars == 1) {
256 push @args, $pars->[0];
257 }
258 else {
259 for my $i (1 .. $number_of_pars-1) {
260 push @args, "-A$pars->[$i]";
261 }
262 push @args, $pars->[0];
263 }
264
265 my $cfg = $self->{cfg};
266 my @extra_args = ();
267 foreach my $arg(@{$cfg->{argument}}) {
268 push @extra_args, $arg->{value};
269 }
270 push @args, @extra_args if @extra_args;
271
272 return \@args;
273 }
274
275 1;
276
277 __END__
« lib/t/basic.t ↑ Summary lib/t/make_par.t »