ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/BuildSystem/ToolSettingValidator.pm
Revision: 1.4
Committed: Thu Mar 3 18:57:58 2005 UTC (20 years, 2 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_3-p4, HEAD_SM_071214, forV1_1_0, v103_xml_071106, V1_0_3-p3, V1_0_3-p2, V1_1_0, v110p1, V110p6, V110p5, V110p4, V110p3, before110xmlBRmerge, V110p2, V110p1, V1_0_4p1, V1_0_3-p1, V1_0_3, V1_0_2, V1_0_2_p1, v102p1, V1_0_1
Branch point for: HEAD_BRANCH_SM_071214, v200branch, v103_with_xml, v103_branch
Changes since 1.3: +29 -11 lines
Log Message:
Added warn feature so that missing dirs can prompt a warning rather than stopping for user-input.

File Contents

# Content
1 #____________________________________________________________________
2 # File: BuildSystem::ToolSettingValidator.pm
3 #____________________________________________________________________
4 #
5 # Author: Shaun Ashby <Shaun.Ashby@cern.ch>
6 # Update: 2004-10-14 10:16:21+0200
7 # Revision: $Id: ToolSettingValidator.pm,v 1.3 2004/12/10 15:29:50 sashby Exp $
8 #
9 # Copyright: 2004 (C) Shaun Ashby
10 #
11 #--------------------------------------------------------------------
12 package BuildSystem::ToolSettingValidator;
13 require 5.004;
14
15 use Exporter;
16 @ISA=qw(Exporter);
17 @EXPORT_OK=qw( );
18
19 sub new()
20 ###############################################################
21 # new #
22 ###############################################################
23 # modified : Thu Oct 14 10:16:25 2004 / SFA #
24 # params : #
25 # : #
26 # function : #
27 # : #
28 ###############################################################
29 {
30 my $proto=shift;
31 my $class=ref($proto) || $proto;
32 my $self={};
33
34 # Store the list of known environments:
35 my ($environments,$toolname,$interactive) = @_;
36
37 $self->{TOOLNAME} = $toolname;
38 $self->{ENVIRONMENT} = $environments->{ENVIRONMENT};
39 $self->{RUNTIME} = $environments->{RUNTIME};
40 $self->{VARDATA} = {}; # Somewhere to store the variables
41 $self->{LOCALENV} = \%ENV;
42 $self->{STATUS} = { 0 => $main::good."[OK]".$main::normal, 1 => $main::error."[FAIL]".$main::normal, 2 => $main::good."[OK (but currently missing)]".$main::normal };
43 $self->{INVALIDPATHERRORMSG} = $main::error."Invalid path...please try again!".$main::normal;
44
45 # Are we interactive or not?
46 $self->{INTERACTIVE} = $interactive;
47
48 bless $self,$class;
49 return $self;
50 }
51
52 sub findvalue()
53 {
54 my $self=shift;
55 my ($name, $data) = @_;
56 my $stringtoeval;
57 my $path;
58
59 # We pass in this var to checdefaults(). The data (hash) used in the
60 # path checking may contain a key 'handler': if this is set to 'warnonly', we
61 # don't prompt for this path setting if it happens to be incorrect:
62 my $handlertype;
63
64 # See if there's a default/value in our data element:
65 if ($self->checkdefaults($data,\$stringtoeval,\$handlertype))
66 {
67 # OK, there's a def/val.
68 $path = $self->_expandvars($stringtoeval);
69
70 if ($self->validatepath($path,$handlertype) )
71 {
72 # Save in VARDATA:
73 $self->savevalue($name,$path);
74 }
75 else
76 {
77 # Prompt user for a value:
78 $path = $self->promptuser($name);
79 }
80 }
81 else
82 {
83 # Path was invalid. Fall back to prompting:
84 $path = $self->promptuser($name);
85 }
86
87 # Return the path:
88 return $path;
89 }
90
91 sub ifindvalue()
92 {
93 my $self=shift;
94 my ($name, $data) = @_;
95 my $stringtoeval;
96 my ($dpath,$path);
97 # We pass in this var to checdefaults(). The data (hash) used in the
98 # path checking may contain a key 'handler': if this is set to 'warnonly', we
99 # don't prompt for this path setting if it happens to be incorrect:
100 my $handlertype;
101
102 # See if there's a default/value in our data element:
103 if ($self->checkdefaults($data,\$stringtoeval,\$handlertype))
104 {
105 # OK, there's a def/val.
106 $dpath = $self->_expandvars($stringtoeval);
107
108 if ($self->validatepath($dpath,$handlertype))
109 {
110 $path = $self->promptuser($name,$dpath);
111 # Save in VARDATA:
112 $self->savevalue($name, $path);
113 }
114 else
115 {
116 # Prompt user for a value:
117 $path = $self->promptuser($name);
118 }
119 }
120 else
121 {
122 # Path was invalid. Fall back to prompting:
123 $path = $self->promptuser($name);
124 }
125
126 # Return the path:
127 return $path;
128 }
129
130 sub savevalue()
131 {
132 my $self = shift;
133 my ($varname, $path) = @_;
134
135 if ($varname && $path)
136 {
137 $self->{VARDATA}->{$varname} = $path;
138 }
139
140 return;
141 }
142
143 sub environment()
144 {
145 my $self = shift;
146 my ($type, $varname) = @_;
147
148 if ($type && $varname)
149 {
150 if (exists($self->{uc($type)}->{$varname}))
151 {
152 # Return the tag data:
153 return $self->{uc($type)}->{$varname};
154 }
155 else
156 {
157 # No tag data so return 0:
158 return 0;
159 }
160 }
161 elsif ($type)
162 {
163 # Return all environments of type $type:
164 return $self->{uc($type)};
165 }
166 else
167 {
168 print "SCRAM: Unknown tag type/var name","\n";
169 }
170 }
171
172 sub validatepath()
173 {
174 my $self = shift;
175 my ($pathvalue,$handlertype) = @_;
176 my $path;
177
178 # Either we use the pathvalue supplied or
179 # we use PATHFROMDB:
180 if ($pathvalue)
181 {
182 $path = $pathvalue;
183 }
184 else
185 {
186 $path = $self->{PATHFROMDB};
187 }
188
189 print "\tChecks ", if ($path);
190
191 if ( -f $path)
192 {
193 # File exists:
194 print $self->{STATUS}->{0}." for $path","\n";
195 return 1;
196 }
197 # This is done so that some paths can be added which include ".":
198 elsif ($path =~ /^\.:.*/ || $path =~ /^\.$/)
199 {
200 print $self->{STATUS}->{0}." for $path","\n";
201 return 1;
202 }
203 elsif ($handlertype =~ /^[Ww].*$/)
204 {
205 print $self->{STATUS}->{2}." for $path","\n";
206 return 1;
207 }
208 else
209 {
210 use DirHandle;
211 my $dh = DirHandle->new();
212
213 opendir $dh, $path or do
214 {
215 # No path:
216 print $self->{STATUS}->{1}." for $path","\n", unless ($path eq '');
217 return 0;
218 };
219
220 # Dir found:
221 print $self->{STATUS}->{0}." for $path","\n";
222 return 1;
223 }
224 }
225
226 sub checkdefaults()
227 {
228 my $self=shift;
229 my ($vardata,$pathtoevalref,$handlertyperef) = @_;
230
231 # If $vardata is actually an array (which it will
232 # be if there is more than one VAR element), dereference
233 # to get only the first hash entry (this is fine as the
234 # block of code to handle nmore than one element will loop
235 # over all elements of the array so that a hash is passed
236 # to this routine:
237 if (ref($vardata) eq 'ARRAY')
238 {
239 $data = $vardata->[0];
240 }
241 else
242 {
243 $data = $vardata;
244 }
245
246 if (exists($data->{'handler'}))
247 {
248 $$handlertyperef = $data->{'handler'};
249 }
250
251 if (exists($data->{'default'}))
252 {
253 $$pathtoevalref = $data->{'default'};
254 }
255 elsif (exists($data->{'value'}))
256 {
257 $$pathtoevalref = $data->{'value'};
258 }
259 else
260 {
261 # No value or default. Return 0:
262 return 0;
263 }
264
265 return 1;
266 }
267
268 sub pathfromdb()
269 {
270 my $self=shift;
271 return $self->{PATHFROMDB};
272 }
273
274 sub checkDB()
275 {
276 my $self = shift;
277 my ($varname) = @_;
278
279 if ($::lookupdb->checkTool($self->{TOOLNAME}))
280 {
281 $pathfromdb = $::lookupdb->lookupTag($self->{TOOLNAME}, $varname);
282
283 if ($pathfromdb ne "")
284 {
285 $self->{PATHFROMDB} = $pathfromdb;
286 return 1;
287 }
288 else
289 {
290 return 0;
291 }
292 }
293 else
294 {
295 return 0;
296 }
297 }
298
299 sub _expandvars()
300 {
301 my $self=shift;
302 my ($string) = @_;
303
304 return "" , if ( ! defined $string );
305
306 # To evaluate variables in brackets, like $(X):
307 $string =~ s{\$\((\w+)\)}
308 {
309 if (defined $self->{VARDATA}->{$1})
310 {
311 $self->_expandvars($self->{VARDATA}->{$1});
312 }
313 elsif (defined $self->{LOCALENV}->{$1})
314 {
315 $self->_expandvars($self->{LOCALENV}->{$1});
316 }
317 else
318 {
319 "\$$1";
320 }
321 }egx;
322
323 # To evaluate variables like $X:
324 $string =~ s{\$(\w+)}
325 {
326 if (defined $self->{VARDATA}->{$1})
327 {
328 $self->_expandvars($self->{VARDATA}->{$1});
329 }
330 elsif (defined $self->{LOCALENV}->{$1})
331 {
332 $self->_expandvars($self->{LOCALENV}->{$1});
333 }
334 else
335 {
336 "\$$1";
337 }
338 }egx;
339
340 # Now return false if the string wasn't properly evaluated (i.e. some $ remain), otherwise
341 # return the expanded string:
342 ($string =~ /\$/) ? return undef : return $string;
343 }
344
345 sub promptuser()
346 {
347 my $self=shift;
348 my ($varname, $default)=@_;
349 my $pathvalue;
350 my $novalid = 1;
351 my $dummy = '';
352 my $ORKEEP = '';
353 print "\n";
354
355 while ($novalid)
356 {
357 if ($self->interactive())
358 {
359 # Only mention the default stuff if default actually has
360 # a value. It might not (e.g., in the case of a new tool)
361 if ($default ne '')
362 {
363 print "Default value is $default","\n", unless ($default eq '');
364 $ORKEEP=' (or <ret> to keep)';
365 }
366
367 print "\n";
368 print $main::prompt."Please Enter a Value$ORKEEP: > ".$main::normal;
369
370 $dummy = <STDIN>;
371 chomp($dummy);
372 # If we have an empty string, return the default value:
373 if ($dummy eq '' && $default ne '')
374 {
375 return $default;
376 }
377 print "\n"
378 }
379 else
380 {
381 print "\n";
382 print $main::prompt."Please Enter a Value: > ".$main::normal;
383 $dummy = <STDIN>;
384 chomp($dummy);
385 }
386
387 print "\n";
388 # Also use _expandvars() here so that env variables
389 # can be used:
390
391 $pathvalue=$self->_expandvars($dummy);
392 if ($self->validatepath($pathvalue,"")) # No handler here;
393 {
394 $novalid = 0;
395 $self->{VARDATA}->{$varname} = $pathvalue;
396 }
397 else
398 {
399 print $self->{INVALIDPATHERRORMSG},"\n";
400 }
401 }
402
403 # Return the path:
404 return $pathvalue;
405 }
406
407 sub interactive()
408 {
409 my $self=shift;
410 return $self->{INTERACTIVE};
411 }
412
413 sub promptuserforvar()
414 {
415 my $self=shift;
416 my ($varname, $default)=@_;
417 my $value;
418 my $novalid = 1;
419 my $dummy = '';
420 my $ORKEEP = '';
421
422 print "\n";
423
424 while ($novalid)
425 {
426 if ($self->interactive())
427 {
428 # Only mention the default stuff if default actually has
429 # a value. It might not (e.g., in the case of a new tool)
430 if ($default ne '')
431 {
432 print "Default value for $varname is $default","\n", unless ($default eq '');
433 $ORKEEP=' (or <ret> to keep)';
434 }
435
436 print "\n";
437 print $main::prompt."Please Enter a Value$ORKEEP: > ".$main::normal;
438 $dummy = <STDIN>;
439 chomp($dummy);
440 # If we have an empty string, set to the default value:
441 if ($dummy eq '')
442 {
443 $dummy = $default;
444 }
445
446 print "\n";
447 }
448 else
449 {
450 print "\n";
451 print $main::prompt."Please Enter a Value: > ".$main::normal;
452 $dummy = <STDIN>;
453 chomp($dummy);
454 }
455
456 print "\n";
457 # Also use _expandvars() here so that env variables
458 # can be used:
459 $value = $self->_expandvars($dummy);
460 print "Runtime variable ",$varname," set to \"",$value,"\"\n";
461 $novalid = 0;
462 }
463
464 # Return the path:
465 return $value;
466 }
467
468 1;