ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Configuration/ConfigArea.pm
Revision: 1.13
Committed: Mon Mar 13 10:23:19 2000 UTC (25 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: ProtoEnd
Branch point for: V0_9branch
Changes since 1.12: +8 -0 lines
Log Message:
Add stroeconfigobject method

File Contents

# User Rev Content
1 williamc 1.1 #
2     # ConfigArea.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7 williamc 1.9 # -----------
8     # creates and manages a configuration area
9     #
10     # Options
11     # -------
12     # ConfigArea_location
13     # ConfigArea_name
14 williamc 1.1 #
15     # Interface
16     # ---------
17     # new(ActiveConfig) : A new ConfigArea object
18     # setup() : setup the configuration area
19     # location([dir]) : set/return the location of the area
20     # version([version]) : set/return the version of the area
21     # name([name]) : set/return the name of the area
22     # store(location) : store data in file location
23     # restore(location) : restore data from file location
24     # meta() : return a description string of the area
25     # addconfigitem(url) : add a new item to the area
26 williamc 1.13 # storeconfigobject(confiItemobj) : store a ready made ConfigItem in the local
27     # area
28 williamc 1.1 # configitem(@keys) : return a list of fig items that match
29     # the keys - all if left blank
30 williamc 1.4 # parentstore() : set/return the parent ObjectStore
31 williamc 1.9 # basearea(ConfigArea) : Set/Get the base area
32     # freebase() : Remove any link to a base area
33 williamc 1.5 # bootstrapfromlocation([location]): bootstrap the object based on location.
34     # no location specified - cwd used
35     # searchlocation([startdir]) : returns the location directory. search starts
36     # from cwd if not specified
37 williamc 1.6 # defaultdirname() : return the default directory name string
38     # copy(location) : make a copy of the current area at the
39 williamc 1.9 # specified location - defaults to cwd/default
40     # if not specified . ConfigArea_name,
41     # ConfigArea_location also override .
42     # Return an object representing the area
43     # satellite() : make a satellite area based on $self
44     # arch([archobj]) : Set/get the architecture object
45     # structure(name) : return the object corresponding to the
46     # structure name
47     # structurelist() : return list of structure objectS
48     # downloadtotop(dir,url) : download the url to a dir in the config area
49     #
50 williamc 1.1
51     package Configuration::ConfigArea;
52     use ActiveDoc::ActiveDoc;
53     require 5.004;
54     use Utilities::AddDir;
55     use ObjectUtilities::ObjectStore;
56 williamc 1.8 use Configuration::ConfigStore;
57 williamc 1.9 use Configuration::ActiveDoc_arch;
58 williamc 1.6 use Cwd;
59 williamc 1.9 @ISA=qw(Configuration::ActiveDoc_arch ObjectUtilities::StorableObject);
60 williamc 1.1
61     sub init {
62     my $self=shift;
63 williamc 1.5
64 williamc 1.1 $self->newparse("init");
65     $self->newparse("download");
66     $self->newparse("setup");
67 williamc 1.10 $self->newparse("setup_tools");
68     $self->addarchtags("setup_tools");
69     $self->addarchtags("setup");
70 williamc 1.1 $self->addtag("init","project",\&Project_Start,$self,
71 williamc 1.7 \&Project_text,$self,"", $self );
72 williamc 1.1 $self->addurltags("download");
73 williamc 1.9 $self->addtag("download","download",\&Download_Start,$self,
74     "", $self, "",$self);
75 williamc 1.1 $self->addtag("download","use",\&Use_download_Start,$self,
76     "", $self, "",$self);
77     $self->addurltags("setup");
78 williamc 1.12 $self->addurltags("setup_tools");
79 williamc 1.10 $self->addtag("setup_tools","use",\&Use_Start,$self, "", $self, "",$self);
80 williamc 1.9 $self->addtag("setup","structure",\&Structure_Start,$self,
81     "", $self, "",$self);
82 williamc 1.8
83     # data init
84     $self->{admindir}=".SCRAM";
85 williamc 1.1 }
86    
87 williamc 1.9 sub basearea {
88     my $self=shift;
89    
90     my $area;
91     if ( @_ ) {
92     $area=shift;
93     $self->config()->store($area,"BaseArea");
94     }
95     else {
96 williamc 1.12 ($area)=$self->config()->find("BaseArea");
97 williamc 1.9 }
98     return $area;
99     }
100    
101     sub freebase {
102     my $self=shift;
103     $self->config()->delete("BaseArea");
104     }
105 williamc 1.7
106 williamc 1.6 sub defaultdirname {
107     my $self=shift;
108 williamc 1.8 my $name=$self->name();
109     my $vers=$self->version();
110 williamc 1.9 $vers=~s/^$name\_//;
111 williamc 1.8 $name=$name."_".$vers;
112     return $name;
113 williamc 1.9 }
114 williamc 1.8
115 williamc 1.6
116 williamc 1.1 sub setup {
117     my $self=shift;
118    
119 williamc 1.7 # --- find out the location - default is cwd
120 williamc 1.9 my $location=$self->option("ConfigArea_location");
121 williamc 1.7 if ( ! defined $location ) {
122     $location=cwd();
123     }
124     elsif ( $location!~/^\// ) {
125 williamc 1.6 $location=cwd()."/".$location;
126     }
127 williamc 1.1
128     # --- find area directory name , default name projectname_version
129 williamc 1.9 my $name=$self->option("ConfigArea_name");
130 williamc 1.1 if ( ! defined $name ) {
131 williamc 1.6 $name=$self->defaultdirname();
132 williamc 1.1 }
133     $self->location($location."/".$name);
134    
135     # make a new store handler
136 williamc 1.4 $self->_setupstore();
137 williamc 1.1
138     # --- download everything first
139 williamc 1.2 $self->parse("download");
140 williamc 1.1
141     # --- and parse the setup file
142     $self->parse("setup");
143 williamc 1.10 $self->parse("setup_tools");
144 williamc 1.3
145 williamc 1.5 # --- store bootstrap info
146 williamc 1.8 $self->store($self->location()."/".$self->{admindir}."/ConfigArea.dat");
147 williamc 1.5
148 williamc 1.3 # --- store self in original database
149 williamc 1.4 $self->parentconfig()->store($self,"ConfigArea",$self->name(),
150 williamc 1.3 $self->version());
151     }
152    
153 williamc 1.9 sub structure {
154     my $self=shift;
155     my $vr=shift;
156     return $self->{structures}{$vr};
157     }
158    
159     sub structurelist {
160     my $self=shift;
161     return ( keys %{$self->{structures}} );
162     }
163    
164 williamc 1.3 sub _setupstore {
165     my $self=shift;
166    
167 williamc 1.8 # --- make a new ConfigStore at the location and add it to the db list
168     my $ad=Configuration::ConfigStore->new($self->location().
169 williamc 1.9 "/".$self->{admindir}, $self->arch());
170 williamc 1.3
171 williamc 1.4 $self->parentconfig($self->config());
172     # $self->config(Configuration::ConfigureStore->new());
173     # $self->config()->db("local",$ad);
174     # $self->config()->db("parent",$self->parentconfig());
175     # $self->config()->policy("cache","local");
176     $self->config($ad);
177     $self->config()->basedoc($self->parentconfig()->basedoc());
178     }
179    
180 williamc 1.5 sub bootstrapfromlocation {
181     my $self=shift;
182    
183     if ( ! defined $self->location(@_) ) {
184     $self->error("Unable to locate the top of local configuration area");
185     }
186 williamc 1.12 $self->verbose("Found top ".$self->location());
187 williamc 1.5 $self->_setupstore();
188 williamc 1.12 my $infofile=$self->location()."/".$self->{admindir}."/ConfigArea.dat";
189     if ( -e $infofile ) {
190     $self->restore($infofile);
191     }
192     else {
193     $self->error("Area corrupted - cannot find $infofile");
194     }
195 williamc 1.5 }
196    
197 williamc 1.4 sub parentconfig {
198     my $self=shift;
199     @_?$self->{parentconfig}=shift
200     :$self->{parentconfig};
201 williamc 1.1 }
202    
203     sub store {
204     my $self=shift;
205     my $location=shift;
206    
207     my $fh=$self->openfile(">".$location);
208 williamc 1.4 $self->savevar($fh,"location", $self->location());
209     $self->savevar($fh,"url", $self->url());
210     $self->savevar($fh,"name", $self->name());
211     $self->savevar($fh,"version", $self->version());
212 williamc 1.1 $fh->close();
213 williamc 1.9
214     $self->_storestructures();
215     }
216    
217     sub satellite {
218     my $self=shift;
219     my $newarea=$self->copy(@_);
220     $newarea->_makesatellites();
221     return $newarea;
222 williamc 1.1 }
223    
224 williamc 1.6 sub copy {
225     my $self=shift;
226     use File::Basename;
227     # create the area
228    
229 williamc 1.9 my $destination;
230     if ( @_ ) {
231     $destination=shift;
232     }
233     else {
234     my($location,$name)=$self->_defaultoptions();
235     $destination=$location."/".$name
236     }
237     #AddDir::adddir(dirname($destination)."/".$self->{admindir});
238     #AddDir::adddir($destination."/".$self->{admindir});
239 williamc 1.6
240 williamc 1.9 # copy across the admin dir
241     $temp=$self->location()."/".$self->{admindir};
242     AddDir::copydir($temp,"$destination/".$self->{admindir});
243 williamc 1.6 # create a new object based on the new area
244     my $newarea=ref($self)->new($self->parentconfig());
245     $newarea->bootstrapfromlocation($destination);
246     # save it with the new location info
247 williamc 1.9 $newarea->store($self->location()."/".$self->{admindir}.
248     "/ConfigArea.dat");
249     return $newarea;
250 williamc 1.6 }
251    
252 williamc 1.1 sub restore {
253     my $self=shift;
254 williamc 1.4 my $location=shift;
255 williamc 1.1
256     my $fh=$self->openfile("<".$location);
257 williamc 1.4 my $varhash={};
258     $self->restorevars($fh,$varhash);
259 williamc 1.5 if ( ! defined $self->location() ) {
260     $self->location($$varhash{"location"});
261     }
262 williamc 1.4 $self->_setupstore();
263     $self->url($$varhash{"url"});
264     $self->name($$varhash{"name"});
265     $self->version($$varhash{"version"});
266 williamc 1.1 $fh->close();
267 williamc 1.9
268     $self->_restorestructures();
269 williamc 1.1 }
270    
271     sub name {
272     my $self=shift;
273    
274     @_?$self->{name}=shift
275     :$self->{name};
276     }
277    
278     sub version {
279     my $self=shift;
280    
281     @_?$self->{version}=shift
282     :$self->{version};
283     }
284    
285     sub location {
286     my $self=shift;
287    
288 williamc 1.5 if ( @_ ) {
289 williamc 1.6 $self->{location}=shift;
290 williamc 1.5 }
291     elsif ( ! defined $self->{location} ) {
292     # try and find the release location
293 williamc 1.9 $self->{location}=$self->searchlocation();
294 williamc 1.5 }
295     return $self->{location};
296     }
297    
298     sub searchlocation {
299     my $self=shift;
300    
301     #start search in current directory if not specified
302     my $thispath;
303     @_?$thispath=shift
304 williamc 1.6 :$thispath=cwd();
305 williamc 1.5
306     my $rv=0;
307    
308 williamc 1.6 Sloop:{
309     do {
310     # print "Searching $thispath\n";
311 williamc 1.8 if ( -e "$thispath/".$self->{admindir} ) {
312 williamc 1.6 # print "Found\n";
313 williamc 1.5 $rv=1;
314 williamc 1.6 last Sloop;
315 williamc 1.5 }
316 williamc 1.6 } while ( ($thispath=~s/(.*)\/.*/$1/)=~/./ ) };
317 williamc 1.5
318     return $rv?$thispath:undef;
319 williamc 1.1 }
320    
321     sub meta {
322     my $self=shift;
323    
324     my $string=$self->name()." ".$self->version()." located at :\n ".
325     $self->location;
326     }
327    
328     sub configitem {
329     my $self=shift;
330    
331 williamc 1.5 return ($self->config()->find("ConfigItem",@_));
332 williamc 1.1 }
333    
334     sub addconfigitem {
335     my $self=shift;
336     my $url=shift;
337    
338     my $docref=$self->activatedoc($url);
339     # Set up the document
340     $docref->setup();
341 williamc 1.8 $docref->save();
342 williamc 1.4 # $self->config()->storepolicy("local");
343 williamc 1.13 }
344    
345     sub storeconfigobject {
346     my $self=shift;
347     my $obj=shift;
348     $obj->save($self->config());
349 williamc 1.1 }
350    
351 williamc 1.9 sub downloadtotop {
352     my $self=shift;
353     my $url=shift;
354     my $dir=shift;
355    
356     # only download once
357     if ( ! -e $self->location()."/".$dir ) {
358     $self->{urlhandler}->download($url,$self->location()."/".$dir);
359     }
360     }
361    
362     sub _makesatellites {
363     my $self=shift;
364     foreach $st ( values %{$self->{structures}} ) {
365     $st->setupsatellite()
366     }
367     }
368    
369     sub _storestructures {
370     my $self=shift;
371     foreach $struct ( values %{$self->{structures}} ) {
372     $self->config()->store($struct, "Structures", $struct->name());
373     }
374     }
375    
376     sub _restorestructures {
377     my $self=shift;
378     my @strs=$self->config()->find("Structures");
379     foreach $struct ( @strs ) {
380     $struct->parent($self);
381     $self->{structures}{$struct->name()}=$struct;
382     }
383     }
384    
385     sub _defaultoptions {
386     my $self=shift;
387     my $name;
388     my $location;
389    
390     # --- find out the location - default is cwd
391     $location=$self->option("ConfigArea_location");
392     if ( ! defined $location ) {
393     $location=cwd();
394     }
395     elsif ( $location!~/^\// ) {
396     $location=cwd()."/".$location;
397     }
398    
399     # --- find area directory name , default name projectname_version
400     $name=$self->option("ConfigArea_name");
401     if ( ! defined $name ) {
402     $name=$self->defaultdirname();
403     }
404     return ($location,$name);
405     }
406 williamc 1.1 # -------------- Tags ---------------------------------
407     # -- init parse
408     sub Project_Start {
409     my $self=shift;
410     my $name=shift;
411     my $hashref=shift;
412    
413     $self->checktag($name,$hashref,'name');
414     $self->checktag($name,$hashref,'version');
415    
416     $self->name($$hashref{'name'});
417     $self->version($$hashref{'version'});
418     }
419    
420    
421     sub Project_text {
422     my $self=shift;
423     my $name=shift;
424     my $string=shift;
425    
426     print $string;
427     }
428    
429     # ---- download parse
430    
431 williamc 1.9 sub Download_Start {
432     my $self=shift;
433     my $name=shift;
434     my $hashref=shift;
435    
436     $self->checktag($name,$hashref,'url');
437     $self->checktag($name,$hashref,'location');
438     if ( $$hashref{'location'}!~/^\w/ ) {
439     $self->parseerror("location must start with an".
440     " alphanumeric character");
441     }
442     print "Downloading .... ".$$hashref{'url'}."\n";
443     $self->downloadtotop($$hashref{'url'},$$hashref{'location'});
444     }
445    
446 williamc 1.1 sub Use_download_Start {
447     my $self=shift;
448     my $name=shift;
449     my $hashref=shift;
450    
451     $self->checktag($name,$hashref,'url');
452     print "Downloading .... ".$$hashref{'url'}."\n";
453     $self->getfile($$hashref{'url'});
454     }
455    
456     # --- setup parse
457 williamc 1.9
458     sub Structure_Start {
459     my $self=shift;
460     my $name=shift;
461     my $hashref=shift;
462    
463     $self->checktag($name,$hashref,'name');
464 williamc 1.11 if ( !(( exists $$hashref{'type'}) || ( exists $$hashref{'url'})) ) {
465 williamc 1.9 $self->parseerror("No url or type given in <$name> tag");
466     }
467     if ( ! exists $self->{structures}{$$hashref{'name'}} ) {
468     if ( exists $$hashref{'type'}) {
469     # create a new object of the specified type
470     eval "require $$hashref{'type'} ";
471     if ( $@ ) {
472     $self->parseerror("Unable to instantiate type=".
473     $$hashref{'type'}." in <$name> .".$@);
474     }
475     $self->{structures}{$$hashref{'name'}}=
476     $$hashref{'type'}->new($self->config());
477     $self->{structures}{$$hashref{'name'}}->name($$hashref{'name'});
478     $self->{structures}{$$hashref{'name'}}->parent($self);
479     $self->{structures}{$$hashref{'name'}}->vars($hashref);
480 williamc 1.11 $self->{structures}{$$hashref{'name'}}->arch($self->arch());
481 williamc 1.9 }
482     else { # its an activedoc
483     $self->{structures}{$$hashref{'name'}}=
484     $self->activatedoc($$hashref{'url'});
485     }
486     $self->{structures}{$$hashref{'name'}}->setupbase();
487     }
488     else {
489     $self->parseerror("Multiply defined Structure - ".
490     $$hashref{'name'});
491     }
492     }
493 williamc 1.1
494     sub Use_Start {
495     my $self=shift;
496     my $name=shift;
497     my $hashref=shift;
498    
499     $self->checktag($name,$hashref,'url');
500     $self->addconfigitem($$hashref{'url'});
501     }
502