ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Configuration/ConfigArea.pm
Revision: 1.13.2.6.2.1
Committed: Fri Aug 4 08:31:37 2000 UTC (24 years, 9 months ago) by williamc
Content type: text/plain
Branch: HPWbranch
Changes since 1.13.2.6: +180 -14 lines
Log Message:
Add setup and linking

File Contents

# Content
1 #
2 # ConfigArea.pm
3 #
4 # Written by Christopher Williams
5 #
6 # Description
7 # -----------
8 # creates and manages a configuration area
9 #
10 # Notes
11 # -------
12 # Persistency - remember to call the save method to make changes persistent
13 #
14 # Interface
15 # ---------
16 # new() : A new ConfigArea object
17 # name() : get/set project name
18 # setup(dir[,areaname]) : setup a fresh area in dir
19 # version() : get/set project version
20 # location([dir]) : set/return the location of the work area
21 # bootstrapfromlocation([location]) : bootstrap the object based on location.
22 # no location specified - cwd used
23 # return 0 if succesful 1 otherwise
24 # requirementsdoc() : get set the requirements doc
25 # searchlocation([startdir]) : returns the location directory. search starts
26 # from cwd if not specified
27 # scramversion() : return the scram version associated with
28 # area
29 # configurationdir() : return the location of the project
30 # configuration directory
31 # copy(location) : copy a configuration
32 # copysetup(location) : copy the architecture specific tool setup
33 # returns 0 if successful, 1 otherwise
34 # copyenv($ref) : copy the areas environment into the hashref
35 # toolbox() : return the areas toolbox object
36 # save() : save changes permanently
37 # linkto(location) : link the current area to that at location
38 # unlinkarea() : destroy link (autosave)
39 # linkarea([ConfigArea]) : link the current area to the apec Area Object
40 # - temporary
41 # align() : adjust hard paths to suit local loaction
42
43 package Configuration::ConfigArea;
44 require 5.004;
45 use URL::URLcache;
46 use Utilities::AddDir;
47 use Utilities::Verbose;
48 use Cwd;
49 @ISA=qw(Utilities::Verbose);
50
51 sub new {
52 my $class=shift;
53 my $self={};
54 bless $self, $class;
55
56 # data init
57 $self->{admindir}=".SCRAM";
58 $self->{cachedir}="cache";
59 undef $self->{linkarea};
60
61 return $self;
62 }
63
64 sub cache {
65 my $self=shift;
66 if ( @_ ) {
67 $self->{cache}=shift;
68 }
69 elsif ( ! defined $self->{cache} ) {
70 my $loc=$self->location()."/".$self->{admindir}."/".$self->{cachedir};
71 $self->{cache}=URL::URLcache->new($loc);
72 }
73 return $self->{cache};
74 }
75
76 sub name {
77 my $self=shift;
78 @_?$self->{name}=shift
79 :$self->{name};
80 }
81
82 sub version {
83 my $self=shift;
84 @_?$self->{version}=shift
85 :$self->{version};
86 }
87
88 sub setup {
89 my $self=shift;
90 my $location=shift;
91 my $areaname;
92
93 # -- check we have a project name and version
94 my $name=$self->name();
95 my $vers=$self->version();
96 if ( ( ! defined $name ) && ( ! defined $version )) {
97 $self->error("Set ConfigArea name and version before setup");
98 }
99
100 # -- check arguments and set location
101 if ( ! defined $location ) {
102 $self->error("ConfigArea: Cannont setup area without a location");
103 }
104 if ( @_ ) {
105 $areaname=shift;
106 }
107 if ( (! defined $areaname) || ( $areaname eq "" ) ) {
108 # -- make up a name from the project name and version
109 $vers=~s/^$name\_//;
110 $areaname=$name."_".$vers;
111 }
112 my $arealoc=$location."/".$areaname;
113 my $workloc=$arealoc."/".$self->{admindir};
114 $self->verbose("Building at $arealoc");
115 $self->location($arealoc);
116
117 # -- create top level structure and work area
118 AddDir::adddir($workloc);
119
120 # -- add a cache
121 $self->cache();
122
123 # -- Save Environment File
124 $self->_SaveEnvFile();
125
126 }
127
128 sub configurationdir {
129 my $self=shift;
130 if ( @_ ) {
131 $self->{configurationdir}=shift;
132 }
133 return (defined $self->{configurationdir})?$self->{configurationdir}:undef;
134 }
135
136 sub toolbox {
137 my $self=shift;
138 if ( ! defined $self->{toolbox} ) {
139 $self->{toolbox}=BuildSystem::ToolBox->new($self);
140 }
141 return $self->{toolbox};
142 }
143
144 sub requirementsdoc {
145 my $self=shift;
146 if ( @_ ) {
147 $self->{reqdoc}=shift;
148 }
149 return (defined $self->{reqdoc})?$self->{reqdoc}:undef;
150 }
151
152 sub scramversion {
153 my $self=shift;
154 if ( ! defined $self->{scramversion} ) {
155 my $filename=$self->location()."/".$self->configurationdir()."/".
156 "scram_version";
157 if ( -f $filename ) {
158 use FileHandle;
159 $fh=FileHandle->new();
160 open ($fh, "<".$filename);
161 my $version=<$fh>;
162 chomp $version;
163 $self->{scramversion}=$version;
164 undef $fh;
165 }
166 }
167 return $self->{scramversion};
168 }
169
170 sub bootstrapfromlocation {
171 my $self=shift;
172
173 my $rv=0;
174
175 my $location;
176 if ( ! defined ($location=$self->searchlocation(@_)) ) {
177 $rv=1;
178 $self->verbose("Unable to locate the top of local configuration area");
179 }
180 else {
181 $self->location($location);
182 $self->verbose("Found top ".$self->location());
183 my $infofile=$self->location()."/".$self->{admindir}."/ConfigArea.dat";
184 $self->_LoadEnvFile();
185 }
186 return $rv;
187 }
188
189 sub location {
190 my $self=shift;
191
192 if ( @_ ) {
193 $self->{location}=shift;
194 }
195 elsif ( ! defined $self->{location} ) {
196 # try and find the release location
197 $self->{location}=$self->searchlocation();
198 }
199 return $self->{location};
200 }
201
202 sub searchlocation {
203 my $self=shift;
204
205 #start search in current directory if not specified
206 my $thispath;
207 if ( @_ ) {
208 $thispath=shift
209 }
210 else {
211 $thispath=cwd();
212 }
213
214 my $rv=0;
215
216 # chop off any files - we only want dirs
217 if ( -f $thispath ) {
218 $thispath=~s/(.*)\/.*/$1/;
219 }
220 Sloop:{
221 do {
222 $self->verbose("Searching $thispath");
223 if ( -e "$thispath/".$self->{admindir} ) {
224 $self->verbose("Found\n");
225 $rv=1;
226 last Sloop;
227 }
228 } while ( ($thispath=~s/(.*)\/.*/$1/)=~/./ ) };
229
230 return $rv?$thispath:undef;
231 }
232
233 sub satellite {
234 my $self=shift;
235
236 # -- create the sat object
237 my $sat=Configuration::ConfigArea->new();
238 $sat->name($self->name());
239 $sat->version($self->version());
240 $sat->requirementsdoc($self->requirementsdoc());
241 $sat->configurationdir($self->configurationdir());
242 $sat->setup(@_);
243
244 # -- copy across the cache
245 copy($self->cache()->location(),$sat->cache()->location());
246 # and make sure in reinitialises
247 undef ($sat->{cache});
248
249 # -- link it to this area
250 $sat->linkarea($self);
251
252 }
253
254 sub copy {
255 my $self=shift;
256 my $destination=shift;
257
258 # copy across the admin dir
259 my $temp=$self->location()."/".$self->{admindir};
260 AddDir::copydir($temp,"$destination/".$self->{admindir});
261 }
262
263 sub align {
264 my $self=shift;
265 use File::Copy;
266
267 $self->_LoadEnvFile();
268 my $Envfile=$self->location()."/".$self->{admindir}."/Environment";
269 my $tmpEnvfile=$Envfile.".bak";
270 my $rel=$self->{ENV}{RELEASETOP};
271 my $local=$self->location();
272
273 rename( $Envfile, $tmpEnvfile );
274 use FileHandle;
275 my $fh=FileHandle->new();
276 my $fout=FileHandle->new();
277 open ( $fh, "<".$tmpEnvfile ) or
278 $self->error("Cannot find Environment file. Area Corrupted? ("
279 .$self->location().")\n $!");
280 open ( $fout, ">".$Envfile ) or
281 $self->error("Cannot find Environment file. Area Corrupted? ("
282 .$self->location().")\n $!");
283 while ( <$fh> ) {
284 $_=~s/\Q$rel\L/$local/g;
285 print $fout $_;
286 }
287 undef $fh;
288 undef $fout;
289 }
290
291 sub copysetup {
292 my $self=shift;
293 my $dest=shift;
294
295 my $rv=1;
296 # copy across the admin dir
297 my $temp=$self->location()."/".$self->{admindir}."/".$self->arch();
298 my $temp2=$dest."/".$self->{admindir}."/".$self->arch();
299 if ( $temp ne $temp2 ) {
300 if ( -d $temp ) {
301 AddDir::copydir($temp,$temp2);
302 $rv=0;
303 }
304 }
305 return $rv;
306 }
307
308 sub copyenv {
309 my $self=shift;
310 my $hashref=shift;
311
312 foreach $elem ( keys %{$self->{ENV}} ) {
313 $$hashref{$elem}=$self->{ENV}{$elem};
314 }
315 }
316
317 sub arch {
318 my $self=shift;
319 return $ENV{SCRAM_ARCH};
320 }
321
322 sub linkto {
323 my $self=shift;
324 my $location=shift;
325 if ( -d $location ) {
326 my $area=Configuration::ConfigArea->new();
327 $area->bootstrapfromlocation($location);
328 $self->linkarea($area);
329 }
330 else {
331 $self->error("ConfigArea : Unable to link to non existing directory ".
332 $location);
333 }
334 }
335
336 sub unlinkarea {
337 my $self=shift;
338 undef $self->{linkarea};
339 $self->{linkarea}=undef;
340 $self->save();
341 }
342
343 sub linkarea {
344 my $self=shift;
345 my $area=shift;
346 if ( defined $area ) {
347 $self->{linkarea}=$area;
348 }
349 return (defined $self->{linkarea} && $self->{linkarea} ne "")?
350 $self->{linkarea}:undef;
351 }
352
353 sub save {
354 my $self=shift;
355 $self->_SaveEnvFile();
356 }
357
358 # ---- support routines
359
360 sub _SaveEnvFile {
361 my $self=shift;
362 use FileHandle;
363 my $fh=FileHandle->new();
364 open ( $fh, ">".$self->location()."/".$self->{admindir}."/".
365 "Environment" ) or
366 $self->error("Cannot Open Environment file to Save ("
367 .$self->location().")\n $!");
368
369 print $fh "SCRAM_PROJECTNAME=".$self->name()."\n";
370 print $fh "SCRAM_PROJECTVERSION=".$self->version()."\n";
371 print $fh "projconfigdir=".$self->configurationdir()."\n";
372 print $fh "SCRAM_ProjReqsDoc=".$self->requirementsdoc()."\n";
373 if ( defined $self->linkarea() ) {
374 my $area=$self->linkarea()->location();
375 if ( $area ne "" ) {
376 print $fh "RELEASETOP=".$area."\n";
377 }
378 }
379 undef $fh;
380 }
381
382
383 sub _LoadEnvFile {
384 my $self=shift;
385
386 use FileHandle;
387 my $fh=FileHandle->new();
388 open ( $fh, "<".$self->location()."/".$self->{admindir}."/".
389 "Environment" ) or
390 $self->error("Cannot find Environment file. Area Corrupted? ("
391 .$self->location().")\n $!");
392 while ( <$fh> ) {
393 chomp;
394 next if /^#/;
395 next if /^\s*$/ ;
396 ($name, $value)=split /=/;
397 eval "\$self->{ENV}{${name}}=\"$value\"";
398 }
399 undef $fh;
400
401 # -- set internal variables appropriately
402 if ( defined $self->{ENV}{"SCRAM_PROJECTNAME"} ) {
403 $self->name($self->{ENV}{"SCRAM_PROJECTNAME"});
404 }
405 if ( defined $self->{ENV}{"SCRAM_PROJECTVERSION"} ) {
406 $self->version($self->{ENV}{"SCRAM_PROJECTVERSION"});
407 }
408 if ( defined $self->{ENV}{"projconfigdir"} ) {
409 $self->configurationdir($self->{ENV}{projconfigdir});
410 }
411 if ( defined $self->{ENV}{"SCRAM_ProjReqsDoc"} ) {
412 $self->requirementsdoc($self->{ENV}{SCRAM_ProjReqsDoc});
413 }
414 if ( ( defined $self->{ENV}{"RELEASETOP"} ) &&
415 ($self->{ENV}{"RELEASETOP"} ne $self->location())) {
416 $self->linkto($self->{ENV}{"RELEASETOP"});
417 }
418 else {
419 $self->{ENV}{"RELEASETOP"}=$self->location();
420 }
421 }