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 |
|
|
# Options
|
11 |
|
|
# -------
|
12 |
|
|
# ConfigArea_location
|
13 |
|
|
# ConfigArea_name
|
14 |
williamc |
1.1 |
#
|
15 |
|
|
# Interface
|
16 |
|
|
# ---------
|
17 |
williamc |
1.13.2.1 |
# new() : A new ConfigArea object
|
18 |
|
|
# location([dir]) : set/return the location of the work area
|
19 |
williamc |
1.13.2.3 |
# bootstrapfromlocation([location]) : bootstrap the object based on location.
|
20 |
|
|
# no location specified - cwd used
|
21 |
|
|
# return 0 if succesful 1 otherwise
|
22 |
williamc |
1.5 |
# searchlocation([startdir]) : returns the location directory. search starts
|
23 |
|
|
# from cwd if not specified
|
24 |
williamc |
1.6 |
# defaultdirname() : return the default directory name string
|
25 |
williamc |
1.13.2.1 |
# scramversion() : return the scram version associated with
|
26 |
|
|
# area
|
27 |
|
|
# configurationdir() : return the location of the project
|
28 |
|
|
# configuration directory
|
29 |
williamc |
1.13.2.2 |
# copy(location) : copy a configuration
|
30 |
|
|
# copysetup(location) : copy the architecture specific tool setup
|
31 |
|
|
# returns 0 if successful, 1 otherwise
|
32 |
|
|
# copyenv($ref) : copy the areas environment into the hashref
|
33 |
williamc |
1.1 |
|
34 |
|
|
package Configuration::ConfigArea;
|
35 |
|
|
require 5.004;
|
36 |
|
|
use Utilities::AddDir;
|
37 |
williamc |
1.13.2.1 |
use Utilities::Verbose;
|
38 |
williamc |
1.6 |
use Cwd;
|
39 |
williamc |
1.13.2.1 |
@ISA=qw(Utilities::Verbose);
|
40 |
williamc |
1.1 |
|
41 |
williamc |
1.13.2.1 |
sub new {
|
42 |
|
|
my $class=shift;
|
43 |
|
|
my $self={};
|
44 |
|
|
bless $self, $class;
|
45 |
williamc |
1.9 |
|
46 |
williamc |
1.13.2.2 |
# data init
|
47 |
|
|
$self->{admindir}=".SCRAM";
|
48 |
williamc |
1.7 |
|
49 |
williamc |
1.13.2.2 |
return $self;
|
50 |
williamc |
1.9 |
}
|
51 |
williamc |
1.8 |
|
52 |
williamc |
1.13.2.1 |
sub configurationdir {
|
53 |
williamc |
1.1 |
my $self=shift;
|
54 |
williamc |
1.13.2.1 |
if ( @_ ) {
|
55 |
|
|
$self->{configurationdir}=shift;
|
56 |
williamc |
1.6 |
}
|
57 |
williamc |
1.13.2.1 |
if ( ! defined $self->{configurationdir} ) {
|
58 |
|
|
$self->_LoadEnvFile();
|
59 |
|
|
$self->{configurationdir}=$self->{ENV}{projconfigdir};
|
60 |
|
|
}
|
61 |
|
|
return $self->{configurationdir};
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
sub scramversion {
|
65 |
|
|
my $self=shift;
|
66 |
|
|
if ( ! defined $self->{scramversion} ) {
|
67 |
williamc |
1.13.2.2 |
my $filename=$self->location()."/".$self->configurationdir()."/".
|
68 |
williamc |
1.13.2.1 |
"scram_version";
|
69 |
|
|
if ( -f $filename ) {
|
70 |
|
|
use FileHandle;
|
71 |
|
|
$fh=FileHandle->new();
|
72 |
|
|
open ($fh, "<".$filename);
|
73 |
|
|
my $version=<$fh>;
|
74 |
|
|
chomp $version;
|
75 |
|
|
$self->{scramversion}=$version;
|
76 |
|
|
undef $fh;
|
77 |
|
|
}
|
78 |
williamc |
1.1 |
}
|
79 |
williamc |
1.13.2.1 |
return $self->{scramversion};
|
80 |
williamc |
1.4 |
}
|
81 |
|
|
|
82 |
williamc |
1.5 |
sub bootstrapfromlocation {
|
83 |
|
|
my $self=shift;
|
84 |
williamc |
1.13.2.2 |
|
85 |
|
|
my $rv=0;
|
86 |
williamc |
1.5 |
|
87 |
williamc |
1.13.2.3 |
my $location;
|
88 |
|
|
if ( ! defined ($location=$self->searchlocation(@_)) ) {
|
89 |
williamc |
1.13.2.2 |
$rv=1;
|
90 |
|
|
$self->verbose("Unable to locate the top of local configuration area");
|
91 |
|
|
}
|
92 |
|
|
else {
|
93 |
williamc |
1.13.2.3 |
$self->location($location);
|
94 |
williamc |
1.13.2.2 |
$self->verbose("Found top ".$self->location());
|
95 |
|
|
my $infofile=$self->location()."/".$self->{admindir}."/ConfigArea.dat";
|
96 |
|
|
$self->_LoadEnvFile();
|
97 |
williamc |
1.5 |
}
|
98 |
williamc |
1.13.2.2 |
return $rv;
|
99 |
williamc |
1.1 |
}
|
100 |
|
|
|
101 |
|
|
sub location {
|
102 |
|
|
my $self=shift;
|
103 |
|
|
|
104 |
williamc |
1.5 |
if ( @_ ) {
|
105 |
williamc |
1.6 |
$self->{location}=shift;
|
106 |
williamc |
1.5 |
}
|
107 |
|
|
elsif ( ! defined $self->{location} ) {
|
108 |
|
|
# try and find the release location
|
109 |
williamc |
1.9 |
$self->{location}=$self->searchlocation();
|
110 |
williamc |
1.5 |
}
|
111 |
|
|
return $self->{location};
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
sub searchlocation {
|
115 |
|
|
my $self=shift;
|
116 |
|
|
|
117 |
|
|
#start search in current directory if not specified
|
118 |
|
|
my $thispath;
|
119 |
williamc |
1.13.2.3 |
if ( @_ ) {
|
120 |
|
|
$thispath=shift
|
121 |
|
|
}
|
122 |
|
|
else {
|
123 |
|
|
$thispath=cwd();
|
124 |
|
|
}
|
125 |
williamc |
1.5 |
|
126 |
|
|
my $rv=0;
|
127 |
|
|
|
128 |
williamc |
1.13.2.3 |
# chop off any files - we only want dirs
|
129 |
|
|
if ( -f $thispath ) {
|
130 |
|
|
$thispath=~s/(.*)\/.*/$1/;
|
131 |
|
|
}
|
132 |
williamc |
1.6 |
Sloop:{
|
133 |
|
|
do {
|
134 |
williamc |
1.13.2.1 |
$self->verbose("Searching $thispath");
|
135 |
williamc |
1.8 |
if ( -e "$thispath/".$self->{admindir} ) {
|
136 |
williamc |
1.13.2.1 |
$self->verbose("Found\n");
|
137 |
williamc |
1.5 |
$rv=1;
|
138 |
williamc |
1.6 |
last Sloop;
|
139 |
williamc |
1.5 |
}
|
140 |
williamc |
1.6 |
} while ( ($thispath=~s/(.*)\/.*/$1/)=~/./ ) };
|
141 |
williamc |
1.5 |
|
142 |
|
|
return $rv?$thispath:undef;
|
143 |
williamc |
1.1 |
}
|
144 |
|
|
|
145 |
williamc |
1.13.2.2 |
sub copy {
|
146 |
|
|
my $self=shift;
|
147 |
|
|
my $destination=shift;
|
148 |
|
|
|
149 |
|
|
# copy across the admin dir
|
150 |
|
|
my $temp=$self->location()."/".$self->{admindir};
|
151 |
|
|
AddDir::copydir($temp,"$destination/".$self->{admindir});
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
sub copysetup {
|
155 |
|
|
my $self=shift;
|
156 |
|
|
my $dest=shift;
|
157 |
|
|
|
158 |
|
|
my $rv=1;
|
159 |
|
|
# copy across the admin dir
|
160 |
|
|
my $temp=$self->location()."/".$self->{admindir}."/".$self->arch();
|
161 |
|
|
my $temp2=$dest."/".$self->{admindir}."/".$self->arch();
|
162 |
|
|
if ( $temp ne $temp2 ) {
|
163 |
|
|
if ( -d $temp ) {
|
164 |
|
|
AddDir::copydir($temp,$temp2);
|
165 |
|
|
$rv=0;
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
return $rv;
|
169 |
|
|
}
|
170 |
|
|
|
171 |
|
|
sub copyenv {
|
172 |
|
|
my $self=shift;
|
173 |
|
|
my $hashref=shift;
|
174 |
|
|
|
175 |
|
|
foreach $elem ( keys %{$self->{ENV}} ) {
|
176 |
|
|
$$hashref{$elem}=$self->{ENV}{$elem};
|
177 |
|
|
}
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
sub arch {
|
181 |
|
|
my $self=shift;
|
182 |
|
|
return $ENV{SCRAM_ARCH};
|
183 |
|
|
}
|
184 |
|
|
|
185 |
williamc |
1.13.2.1 |
# ---- support routines
|
186 |
|
|
sub _LoadEnvFile {
|
187 |
williamc |
1.1 |
my $self=shift;
|
188 |
|
|
|
189 |
williamc |
1.13.2.1 |
use FileHandle;
|
190 |
|
|
my $fh=FileHandle->new();
|
191 |
williamc |
1.13.2.2 |
open ( $fh, "<".$self->location()."/".$self->{admindir}."/".
|
192 |
|
|
"Environment" ) or
|
193 |
williamc |
1.13.2.3 |
$self->error("Cannot find Environment file. Area Corrupted? ("
|
194 |
|
|
.$self->location().")\n $!");
|
195 |
williamc |
1.13.2.1 |
while ( <$fh> ) {
|
196 |
|
|
chomp;
|
197 |
|
|
next if /^#/;
|
198 |
|
|
next if /^\s*$/ ;
|
199 |
|
|
($name, $value)=split /=/;
|
200 |
|
|
eval "\$self->{ENV}{${name}}=\"$value\"";
|
201 |
williamc |
1.9 |
}
|
202 |
williamc |
1.13.2.1 |
undef $fh;
|
203 |
williamc |
1.1 |
}
|
204 |
|
|
|
205 |
williamc |
1.13.2.1 |
sub _savevar {
|
206 |
|
|
my $self=shift;
|
207 |
|
|
my $fh=shift;
|
208 |
williamc |
1.9 |
my $name=shift;
|
209 |
williamc |
1.13.2.1 |
my $val=shift;
|
210 |
|
|
print $fh "#".$name."\n";
|
211 |
|
|
print $fh $val."\n";
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
sub _restorevars {
|
215 |
|
|
my $self=shift;
|
216 |
|
|
my $fh=shift;
|
217 |
|
|
my $varhash=shift;
|
218 |
|
|
|
219 |
|
|
while ( <$fh>=~/^#(.*)/ ) {
|
220 |
|
|
$name=$1;
|
221 |
|
|
chomp $name;
|
222 |
|
|
$value=<$fh>;
|
223 |
|
|
chomp $value;
|
224 |
|
|
$$varhash{$name}=$value;
|
225 |
|
|
#print "Restoring ".$name."=".$value."\n";
|
226 |
|
|
}
|
227 |
williamc |
1.1 |
}
|