ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Configuration/ConfigArea.pm
Revision: 1.14
Committed: Mon Aug 28 08:35:14 2000 UTC (24 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.13: +322 -347 lines
Log Message:
remove Interface.pm

File Contents

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