ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Configuration/ConfigArea.pm
Revision: 1.13.2.6.2.7.2.2
Committed: Thu Sep 21 14:17:51 2000 UTC (24 years, 7 months ago) by williamc
Content type: text/plain
Branch: V0_15branch
Changes since 1.13.2.6.2.7.2.1: +6 -3 lines
Log Message:
bugfix to new changes

File Contents

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