ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/RuntimeFile.pm
Revision: 1.2
Committed: Fri Dec 10 13:41:36 2004 UTC (20 years, 4 months ago) by sashby
Content type: text/plain
Branch: MAIN
CVS Tags: V1_0_3-p4, 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, V1_0_0
Branch point for: v200branch, v103_with_xml, v103_branch
Changes since 1.1: +191 -0 lines
Log Message:
Merged V1_0 branch to HEAD

File Contents

# User Rev Content
1 sashby 1.2 #____________________________________________________________________
2     # File: RuntimeFile.pm
3     #____________________________________________________________________
4     #
5     # Author: Shaun Ashby <Shaun.Ashby@cern.ch>
6     # Update: 2004-05-19 15:40:10+0200
7     # Revision: $Id: RuntimeFile.pm,v 1.1.2.2 2004/05/28 15:49:07 sashby Exp $
8     #
9     # Copyright: 2004 (C) Shaun Ashby
10     #
11     #--------------------------------------------------------------------
12     package RuntimeFile;
13     require 5.004;
14     use ActiveDoc::SimpleDoc;
15     use Exporter;
16     @ISA=qw(Exporter);
17     @EXPORT_OK=qw( );
18    
19     sub new()
20     ###############################################################
21     # new() #
22     ###############################################################
23     # modified : Wed May 19 15:40:33 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     $self->{runtimefile} = shift;
35     $self->{content} = {};
36     $self->{varstore} = {};
37     $self->{thisrtinfo} = [];
38    
39     bless $self,$class;
40     return $self;
41     }
42    
43     sub read()
44     {
45     my $self=shift;
46     use Cwd;
47    
48     # Check to see that the rt file exists:
49     if ( ! -f cwd()."/".$self->{runtimefile} )
50     {
51     $::scram->scramfatal("Runtime file \"".$self->{runtimefile}."\" does not exist or is not readable!.");
52     }
53     else
54     {
55     print "Reading RT environment from file ",$self->{runtimefile},"\n", if ($ENV{SCRAM_DEBUG});
56     }
57    
58     # A new SimpleDoc object to parse the file:
59     $self->{simpledoc} = ActiveDoc::SimpleDoc->new();
60    
61     $self->{simpledoc}->newparse("RUNTIME");
62     $self->{simpledoc}->filetoparse($self->{runtimefile});
63     $self->{simpledoc}->addtag("RUNTIME","Runtime",
64     \&runtimetagOpen, $self,
65     \&runtimetagInfo, $self,
66     \&runtimetagClose, $self);
67    
68     # Parse the file:
69     $self->{simpledoc}->parse("RUNTIME");
70     delete $self->{simpledoc};
71     }
72    
73     sub content()
74     {
75     my $self = shift;
76     return $self->{content};
77     }
78    
79     sub info()
80     {
81     my $self=shift;
82     my ($rtname) = @_;
83    
84     if (exists ($self->{content}->{$rtname}))
85     {
86     if (exists ($self->{content}->{$rtname}->{'info'}))
87     {
88     print $rtname,":\n";
89     foreach my $iline (@{$self->{content}->{$rtname}->{'info'}})
90     {
91     print $iline,"\n";
92     }
93     }
94     else
95     {
96     print "No description for runtime variable \"",$rtname,"\" in file ".$self->{runtimefile}."!\n";
97     }
98     }
99     else
100     {
101     $::scram->scramerror("Runtime variable ".$rtname." is not defined in ".$self->{runtimefile}."!");
102     exit(1);
103     }
104     }
105    
106     sub runtimetagOpen()
107     {
108     my ($self, $name, $hashref) = @_;
109     $self->{simpledoc}->checktag($name, $hashref, "name");
110     $self->{thisrtname} = $hashref->{'name'};
111    
112     # Check for values (as value or default):
113     foreach my $t (qw(value)) # Only support "value"
114     {
115     if (exists ($hashref->{$t}))
116     {
117     # Try to expand the value:
118     my $thisvalue = $self->_expandvars($self->{varstore},$hashref->{$t});
119     # There were no dollar signs so we can assume
120     # that everything was evaluated properly:
121     if ($thisvalue !~ /\$/)
122     {
123     $self->{varstore}->{$hashref->{'name'}} = $thisvalue;
124     $self->{content}->{$hashref->{'name'}} = { value => $thisvalue };
125     }
126     }
127     }
128     }
129    
130     sub runtimetagInfo()
131     {
132     my ($self, $name, @infotext) = @_;
133     push(@{$self->{thisrtinfo}},@infotext);
134     }
135    
136     sub runtimetagClose()
137     {
138     my ($self, $name, $hashref) = @_;
139     $self->{content}->{$self->{thisrtname}}->{'info'} = $self->{thisrtinfo};
140     delete $self->{thisrtname};
141     $self->{thisrtinfo} = [];
142     }
143    
144     sub _expandvars
145     {
146     my $self=shift;
147     # $dataenvref is the store of tags already parsed (e.g., X_BASE, LIBDIR etc.):
148     my ($dataenvref,$string) = @_;
149    
150     return "" , if ( ! defined $string );
151     # To evaluate variables in brackets, like $(X):
152     $string =~ s{\$\((\w+)\)}
153     {
154     if (defined $dataenvref->{$1})
155     {
156     $self->_expandvars($dataenvref, $dataenvref->{$1});
157     }
158     elsif (defined $ENV{$1})
159     {
160     $self->_expandvars($dataenvref, $ENV{$1});
161     }
162     else
163     {
164     "\$$1";
165     }
166     }egx;
167    
168     # To evaluate variables like $X:
169     $string =~ s{\$(\w+)}
170     {
171     if (defined $dataenvref->{$1})
172     {
173     $self->_expandvars($dataenvref, $dataenvref->{$1});
174     }
175     elsif (defined $ENV{$1})
176     {
177     $self->_expandvars($dataenvref, $ENV{$1});
178     }
179     else
180     {
181     "\$$1";
182     }
183     }egx;
184    
185     # Now return false if the string wasn't properly evaluated (i.e. some $ remain), otherwise
186     # return the expanded string:
187     ($string =~ /\$/) ? return undef : return $string;
188    
189     }
190    
191     1;