1 |
|
# |
2 |
|
# ConfigArea.pm |
3 |
|
# |
4 |
< |
# Originally Written by Christopher Williams |
4 |
> |
# Written by Christopher Williams |
5 |
|
# |
6 |
|
# Description |
7 |
|
# ----------- |
14 |
|
# |
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 |
< |
# storeconfigobject(confiItemobj) : store a ready made ConfigItem in the local |
27 |
< |
# area |
28 |
< |
# configitem(@keys) : return a list of fig items that match |
29 |
< |
# the keys - all if left blank |
30 |
< |
# parentstore() : set/return the parent ObjectStore |
31 |
< |
# basearea(ConfigArea) : Set/Get the base area |
32 |
< |
# freebase() : Remove any link to a base area |
17 |
> |
# new() : A new ConfigArea object |
18 |
> |
# top() : return the very top directory of the area |
19 |
> |
# location([dir]) : set/return the location of the work area |
20 |
|
# bootstrapfromlocation([location]): bootstrap the object based on location. |
21 |
|
# no location specified - cwd used |
22 |
|
# searchlocation([startdir]) : returns the location directory. search starts |
23 |
|
# from cwd if not specified |
24 |
|
# defaultdirname() : return the default directory name string |
25 |
< |
# copy(location) : make a copy of the current area at the |
26 |
< |
# specified location - defaults to cwd/default |
27 |
< |
# if not specified . ConfigArea_name, |
28 |
< |
# 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 |
< |
# |
25 |
> |
# scramversion() : return the scram version associated with |
26 |
> |
# area |
27 |
> |
# configurationdir() : return the location of the project |
28 |
> |
# configuration directory |
29 |
|
|
30 |
|
package Configuration::ConfigArea; |
52 |
– |
use ActiveDoc::ActiveDoc; |
31 |
|
require 5.004; |
32 |
|
use Utilities::AddDir; |
33 |
< |
use ObjectUtilities::ObjectStore; |
56 |
< |
use Configuration::ConfigStore; |
57 |
< |
use Configuration::ActiveDoc_arch; |
33 |
> |
use Utilities::Verbose; |
34 |
|
use Cwd; |
35 |
< |
@ISA=qw(Configuration::ActiveDoc_arch ObjectUtilities::StorableObject); |
35 |
> |
@ISA=qw(Utilities::Verbose); |
36 |
|
|
37 |
< |
sub init { |
38 |
< |
my $self=shift; |
39 |
< |
|
40 |
< |
$self->newparse("init"); |
41 |
< |
$self->newparse("download"); |
66 |
< |
$self->newparse("setup"); |
67 |
< |
$self->newparse("setup_tools"); |
68 |
< |
$self->addarchtags("setup_tools"); |
69 |
< |
$self->addarchtags("setup"); |
70 |
< |
$self->addtag("init","project",\&Project_Start,$self, |
71 |
< |
\&Project_text,$self,"", $self ); |
72 |
< |
$self->addurltags("download"); |
73 |
< |
$self->addtag("download","download",\&Download_Start,$self, |
74 |
< |
"", $self, "",$self); |
75 |
< |
$self->addtag("download","use",\&Use_download_Start,$self, |
76 |
< |
"", $self, "",$self); |
77 |
< |
$self->addurltags("setup"); |
78 |
< |
$self->addurltags("setup_tools"); |
79 |
< |
$self->addtag("setup_tools","use",\&Use_Start,$self, "", $self, "",$self); |
80 |
< |
$self->addtag("setup","structure",\&Structure_Start,$self, |
81 |
< |
"", $self, "",$self); |
82 |
< |
|
83 |
< |
# data init |
84 |
< |
$self->{admindir}=".SCRAM"; |
85 |
< |
} |
86 |
< |
|
87 |
< |
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 |
< |
($area)=$self->config()->find("BaseArea"); |
97 |
< |
} |
98 |
< |
return $area; |
37 |
> |
sub new { |
38 |
> |
my $class=shift; |
39 |
> |
my $self={}; |
40 |
> |
bless $self, $class; |
41 |
> |
return $self; |
42 |
|
} |
43 |
|
|
44 |
< |
sub freebase { |
44 |
> |
sub top { |
45 |
|
my $self=shift; |
46 |
< |
$self->config()->delete("BaseArea"); |
104 |
< |
} |
46 |
> |
use File::Basename; |
47 |
|
|
48 |
< |
sub defaultdirname { |
107 |
< |
my $self=shift; |
108 |
< |
my $name=$self->name(); |
109 |
< |
my $vers=$self->version(); |
110 |
< |
$vers=~s/^$name\_//; |
111 |
< |
$name=$name."_".$vers; |
112 |
< |
return $name; |
48 |
> |
return dirname($self->location()); |
49 |
|
} |
50 |
|
|
51 |
< |
|
116 |
< |
sub setup { |
51 |
> |
sub configurationdir { |
52 |
|
my $self=shift; |
53 |
< |
|
54 |
< |
# --- find out the location - default is cwd |
120 |
< |
my $location=$self->option("ConfigArea_location"); |
121 |
< |
if ( ! defined $location ) { |
122 |
< |
$location=cwd(); |
123 |
< |
} |
124 |
< |
elsif ( $location!~/^\// ) { |
125 |
< |
$location=cwd()."/".$location; |
53 |
> |
if ( @_ ) { |
54 |
> |
$self->{configurationdir}=shift; |
55 |
|
} |
56 |
< |
|
57 |
< |
# --- find area directory name , default name projectname_version |
58 |
< |
my $name=$self->option("ConfigArea_name"); |
59 |
< |
if ( ! defined $name ) { |
60 |
< |
$name=$self->defaultdirname(); |
56 |
> |
if ( ! defined $self->{configurationdir} ) { |
57 |
> |
$self->_LoadEnvFile(); |
58 |
> |
$self->{configurationdir}=$self->{ENV}{projconfigdir}; |
59 |
> |
} |
60 |
> |
return $self->{configurationdir}; |
61 |
> |
} |
62 |
> |
|
63 |
> |
sub scramversion { |
64 |
> |
my $self=shift; |
65 |
> |
if ( ! defined $self->{scramversion} ) { |
66 |
> |
my $filename=$self->top()."/".$self->configurationdir()."/". |
67 |
> |
"scram_version"; |
68 |
> |
if ( -f $filename ) { |
69 |
> |
use FileHandle; |
70 |
> |
$fh=FileHandle->new(); |
71 |
> |
open ($fh, "<".$filename); |
72 |
> |
my $version=<$fh>; |
73 |
> |
chomp $version; |
74 |
> |
$self->{scramversion}=$version; |
75 |
> |
undef $fh; |
76 |
> |
} |
77 |
|
} |
78 |
< |
$self->location($location."/".$name); |
134 |
< |
|
135 |
< |
# make a new store handler |
136 |
< |
$self->_setupstore(); |
137 |
< |
|
138 |
< |
# --- download everything first |
139 |
< |
$self->parse("download"); |
140 |
< |
|
141 |
< |
# --- and parse the setup file |
142 |
< |
$self->parse("setup"); |
143 |
< |
$self->parse("setup_tools"); |
144 |
< |
|
145 |
< |
# --- store bootstrap info |
146 |
< |
$self->store($self->location()."/".$self->{admindir}."/ConfigArea.dat"); |
147 |
< |
|
148 |
< |
# --- store self in original database |
149 |
< |
$self->parentconfig()->store($self,"ConfigArea",$self->name(), |
150 |
< |
$self->version()); |
151 |
< |
} |
152 |
< |
|
153 |
< |
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 |
< |
sub _setupstore { |
165 |
< |
my $self=shift; |
166 |
< |
|
167 |
< |
# --- make a new ConfigStore at the location and add it to the db list |
168 |
< |
my $ad=Configuration::ConfigStore->new($self->location(). |
169 |
< |
"/".$self->{admindir}, $self->arch()); |
170 |
< |
|
171 |
< |
$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()); |
78 |
> |
return $self->{scramversion}; |
79 |
|
} |
80 |
|
|
81 |
|
sub bootstrapfromlocation { |
85 |
|
$self->error("Unable to locate the top of local configuration area"); |
86 |
|
} |
87 |
|
$self->verbose("Found top ".$self->location()); |
187 |
– |
$self->_setupstore(); |
88 |
|
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 |
– |
} |
196 |
– |
|
197 |
– |
sub parentconfig { |
198 |
– |
my $self=shift; |
199 |
– |
@_?$self->{parentconfig}=shift |
200 |
– |
:$self->{parentconfig}; |
201 |
– |
} |
202 |
– |
|
203 |
– |
sub store { |
204 |
– |
my $self=shift; |
205 |
– |
my $location=shift; |
206 |
– |
|
207 |
– |
my $fh=$self->openfile(">".$location); |
208 |
– |
$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 |
– |
$fh->close(); |
213 |
– |
|
214 |
– |
$self->_storestructures(); |
215 |
– |
} |
216 |
– |
|
217 |
– |
sub satellite { |
218 |
– |
my $self=shift; |
219 |
– |
my $newarea=$self->copy(@_); |
220 |
– |
$newarea->_makesatellites(); |
221 |
– |
return $newarea; |
222 |
– |
} |
223 |
– |
|
224 |
– |
sub copy { |
225 |
– |
my $self=shift; |
226 |
– |
use File::Basename; |
227 |
– |
# create the area |
228 |
– |
|
229 |
– |
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 |
– |
|
240 |
– |
# copy across the admin dir |
241 |
– |
$temp=$self->location()."/".$self->{admindir}; |
242 |
– |
AddDir::copydir($temp,"$destination/".$self->{admindir}); |
243 |
– |
# 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 |
– |
$newarea->store($self->location()."/".$self->{admindir}. |
248 |
– |
"/ConfigArea.dat"); |
249 |
– |
return $newarea; |
250 |
– |
} |
251 |
– |
|
252 |
– |
sub restore { |
253 |
– |
my $self=shift; |
254 |
– |
my $location=shift; |
255 |
– |
|
256 |
– |
my $fh=$self->openfile("<".$location); |
257 |
– |
my $varhash={}; |
258 |
– |
$self->restorevars($fh,$varhash); |
259 |
– |
if ( ! defined $self->location() ) { |
260 |
– |
$self->location($$varhash{"location"}); |
261 |
– |
} |
262 |
– |
$self->_setupstore(); |
263 |
– |
$self->url($$varhash{"url"}); |
264 |
– |
$self->name($$varhash{"name"}); |
265 |
– |
$self->version($$varhash{"version"}); |
266 |
– |
$fh->close(); |
267 |
– |
|
268 |
– |
$self->_restorestructures(); |
269 |
– |
} |
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}; |
89 |
|
} |
90 |
|
|
91 |
|
sub location { |
113 |
|
|
114 |
|
Sloop:{ |
115 |
|
do { |
116 |
< |
# print "Searching $thispath\n"; |
116 |
> |
$self->verbose("Searching $thispath"); |
117 |
|
if ( -e "$thispath/".$self->{admindir} ) { |
118 |
< |
# print "Found\n"; |
118 |
> |
$self->verbose("Found\n"); |
119 |
|
$rv=1; |
120 |
|
last Sloop; |
121 |
|
} |
124 |
|
return $rv?$thispath:undef; |
125 |
|
} |
126 |
|
|
127 |
< |
sub meta { |
127 |
> |
# ---- support routines |
128 |
> |
sub _LoadEnvFile { |
129 |
|
my $self=shift; |
130 |
|
|
131 |
< |
my $string=$self->name()." ".$self->version()." located at :\n ". |
132 |
< |
$self->location; |
133 |
< |
} |
134 |
< |
|
135 |
< |
sub configitem { |
136 |
< |
my $self=shift; |
137 |
< |
|
138 |
< |
return ($self->config()->find("ConfigItem",@_)); |
139 |
< |
} |
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 |
< |
$docref->save(); |
342 |
< |
# $self->config()->storepolicy("local"); |
343 |
< |
} |
344 |
< |
|
345 |
< |
sub storeconfigobject { |
346 |
< |
my $self=shift; |
347 |
< |
my $obj=shift; |
348 |
< |
$obj->save($self->config()); |
349 |
< |
} |
350 |
< |
|
351 |
< |
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(); |
131 |
> |
use FileHandle; |
132 |
> |
my $fh=FileHandle->new(); |
133 |
> |
open ( $fh, "<".$self->location()."/Environment" ); |
134 |
> |
while ( <$fh> ) { |
135 |
> |
chomp; |
136 |
> |
next if /^#/; |
137 |
> |
next if /^\s*$/ ; |
138 |
> |
($name, $value)=split /=/; |
139 |
> |
eval "\$self->{ENV}{${name}}=\"$value\""; |
140 |
|
} |
141 |
< |
return ($location,$name); |
405 |
< |
} |
406 |
< |
# -------------- 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 |
< |
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 |
< |
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'}); |
141 |
> |
undef $fh; |
142 |
|
} |
143 |
|
|
144 |
< |
# --- setup parse |
145 |
< |
|
146 |
< |
sub Structure_Start { |
459 |
< |
my $self=shift; |
144 |
> |
sub _savevar { |
145 |
> |
my $self=shift; |
146 |
> |
my $fh=shift; |
147 |
|
my $name=shift; |
148 |
< |
my $hashref=shift; |
149 |
< |
|
150 |
< |
$self->checktag($name,$hashref,'name'); |
151 |
< |
if ( !(( exists $$hashref{'type'}) || ( exists $$hashref{'url'})) ) { |
152 |
< |
$self->parseerror("No url or type given in <$name> tag"); |
153 |
< |
} |
154 |
< |
if ( ! exists $self->{structures}{$$hashref{'name'}} ) { |
155 |
< |
if ( exists $$hashref{'type'}) { |
156 |
< |
# create a new object of the specified type |
157 |
< |
eval "require $$hashref{'type'} "; |
158 |
< |
if ( $@ ) { |
159 |
< |
$self->parseerror("Unable to instantiate type=". |
160 |
< |
$$hashref{'type'}." in <$name> .".$@); |
161 |
< |
} |
162 |
< |
$self->{structures}{$$hashref{'name'}}= |
163 |
< |
$$hashref{'type'}->new($self->config()); |
164 |
< |
$self->{structures}{$$hashref{'name'}}->name($$hashref{'name'}); |
165 |
< |
$self->{structures}{$$hashref{'name'}}->parent($self); |
479 |
< |
$self->{structures}{$$hashref{'name'}}->vars($hashref); |
480 |
< |
$self->{structures}{$$hashref{'name'}}->arch($self->arch()); |
481 |
< |
} |
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 |
< |
|
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'}); |
148 |
> |
my $val=shift; |
149 |
> |
print $fh "#".$name."\n"; |
150 |
> |
print $fh $val."\n"; |
151 |
> |
} |
152 |
> |
|
153 |
> |
sub _restorevars { |
154 |
> |
my $self=shift; |
155 |
> |
my $fh=shift; |
156 |
> |
my $varhash=shift; |
157 |
> |
|
158 |
> |
while ( <$fh>=~/^#(.*)/ ) { |
159 |
> |
$name=$1; |
160 |
> |
chomp $name; |
161 |
> |
$value=<$fh>; |
162 |
> |
chomp $value; |
163 |
> |
$$varhash{$name}=$value; |
164 |
> |
#print "Restoring ".$name."=".$value."\n"; |
165 |
> |
} |
166 |
|
} |
502 |
– |
|