ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Configuration/ConfigArea.pm
Revision: 1.10
Committed: Thu Feb 24 09:55:13 2000 UTC (25 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.9: +5 -1 lines
Log Message:
New parse level added for tool setup

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