1 |
+ |
# |
2 |
+ |
# Tool.pm - Store basic tool properties |
3 |
+ |
# |
4 |
+ |
# Originally Written by Christopher Williams |
5 |
+ |
# |
6 |
+ |
# Description |
7 |
+ |
# ----------- |
8 |
+ |
# Stores basic tool features |
9 |
+ |
# |
10 |
+ |
# Interface |
11 |
+ |
# --------- |
12 |
+ |
# new() : A new FeatureTool object |
13 |
+ |
# name() : get/set the tools name |
14 |
+ |
# version() : get set the tool version |
15 |
+ |
# url() : get set the tool url |
16 |
+ |
# features() : return a list of features defined (ordered in relation to |
17 |
+ |
# their first definition) |
18 |
+ |
# listtype(type) : return a list of all features of the specified type |
19 |
+ |
# addfeature(name,@value) : add value(s) to the named feature |
20 |
+ |
# setfeature(name,@value) : set value(s) for the named feature |
21 |
+ |
# getfeature(name) : return a list of elements belonging to the named feature |
22 |
+ |
# (with embedded dependencies) |
23 |
+ |
# type(name[,type]) : get/set the type associated with a feature |
24 |
+ |
# addrequirement() : add a requirement at the current parse position |
25 |
+ |
# dependencies() : return a list of dependency objects |
26 |
+ |
# store(location) : Save object to given file |
27 |
+ |
# restore(location) : Restore object from specified file |
28 |
+ |
# reset() : Clean out all the features |
29 |
+ |
# equals(toolobj) : return 1 if the tools correspond in url version etc. |
30 |
+ |
|
31 |
+ |
package BuildSystem::Tool; |
32 |
+ |
use Utilities::Verbose; |
33 |
+ |
require 5.004; |
34 |
+ |
@ISA=qw(Utilities::Verbose); |
35 |
+ |
|
36 |
+ |
sub new { |
37 |
+ |
my $class=shift; |
38 |
+ |
my $self={}; |
39 |
+ |
bless $self, $class; |
40 |
+ |
return $self; |
41 |
+ |
} |
42 |
+ |
|
43 |
+ |
sub name { |
44 |
+ |
my $self=shift; |
45 |
+ |
|
46 |
+ |
@_?$self->{name}=shift |
47 |
+ |
:$self->{name}; |
48 |
+ |
} |
49 |
+ |
|
50 |
+ |
sub version { |
51 |
+ |
my $self=shift; |
52 |
+ |
|
53 |
+ |
@_?$self->{version}=shift |
54 |
+ |
:$self->{version}; |
55 |
+ |
} |
56 |
+ |
|
57 |
+ |
sub url { |
58 |
+ |
my $self=shift; |
59 |
+ |
|
60 |
+ |
@_?$self->{url}=shift |
61 |
+ |
:$self->{url}; |
62 |
+ |
} |
63 |
+ |
|
64 |
+ |
sub equals { |
65 |
+ |
my $self=shift; |
66 |
+ |
my $tool=shift; |
67 |
+ |
|
68 |
+ |
my $rv=0; |
69 |
+ |
if ( ($tool->url() eq $self->url() ) && |
70 |
+ |
($tool->name() eq $self->name() ) && |
71 |
+ |
($tool->version() eq $self->version()) ) { |
72 |
+ |
$rv=1; |
73 |
+ |
} |
74 |
+ |
return $rv; |
75 |
+ |
} |
76 |
+ |
|
77 |
+ |
sub listtype { |
78 |
+ |
my $self=shift; |
79 |
+ |
my $type=shift; |
80 |
+ |
|
81 |
+ |
my @list=(); |
82 |
+ |
foreach $v ( $self->features() ) { |
83 |
+ |
if ( $self->type($v) eq $type ) { |
84 |
+ |
push @list, $v; |
85 |
+ |
} |
86 |
+ |
} |
87 |
+ |
return @list; |
88 |
+ |
} |
89 |
+ |
|
90 |
+ |
sub type { |
91 |
+ |
my $self=shift; |
92 |
+ |
my $name=shift; |
93 |
+ |
|
94 |
+ |
if ( @_ ) { |
95 |
+ |
$self->{featuretypes}{$name}=shift; |
96 |
+ |
} |
97 |
+ |
return ((defined $self->{featuretypes}{$name})? |
98 |
+ |
$self->{featuretypes}{$name}:""); |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
sub addfeature { |
102 |
+ |
my $self=shift; |
103 |
+ |
my $name=shift; |
104 |
+ |
my @value=@_; |
105 |
+ |
|
106 |
+ |
$self->_newfeature($name,@value); |
107 |
+ |
push @{$self->{features}{$name}}, @value; |
108 |
+ |
} |
109 |
+ |
|
110 |
+ |
sub setfeature { |
111 |
+ |
my $self=shift; |
112 |
+ |
my $name=shift; |
113 |
+ |
my @value=@_; |
114 |
+ |
|
115 |
+ |
$self->_newfeature($name,@value); |
116 |
+ |
@{$self->{features}{$name}}=@value; |
117 |
+ |
} |
118 |
+ |
|
119 |
+ |
sub reset { |
120 |
+ |
my $self=shift; |
121 |
+ |
undef $self->{features}; |
122 |
+ |
undef $self->{'features_ordered'}; |
123 |
+ |
undef $self->{reqobjs}; |
124 |
+ |
undef $self->{requireposition}; |
125 |
+ |
} |
126 |
+ |
|
127 |
+ |
sub _newfeature { |
128 |
+ |
my $self=shift; |
129 |
+ |
my $name=shift; |
130 |
+ |
my @value=@_; |
131 |
+ |
|
132 |
+ |
# if it dosnt exist , make sure we first mark all the current reqs |
133 |
+ |
# get inserted beforehand |
134 |
+ |
if ( ! exists $self->{features}{$name} ) { |
135 |
+ |
# add feature name to our ordered list |
136 |
+ |
push @{$self->{'features_ordered'}},$name; |
137 |
+ |
for ( $i=0; $i<=$#{$self->{reqobjs}}; $i++ ) { |
138 |
+ |
$self->_recordpos( $name, $i); |
139 |
+ |
} |
140 |
+ |
} |
141 |
+ |
} |
142 |
+ |
|
143 |
+ |
sub features { |
144 |
+ |
my $self=shift; |
145 |
+ |
|
146 |
+ |
# return (keys %{$self->{features}}); |
147 |
+ |
return @{$self->{'features_ordered'}}; |
148 |
+ |
} |
149 |
+ |
|
150 |
+ |
sub getfeature { |
151 |
+ |
my $self=shift; |
152 |
+ |
my $name=shift; |
153 |
+ |
|
154 |
+ |
my @rv=(); |
155 |
+ |
my @rep; |
156 |
+ |
# make sure we insert requirements at the right place |
157 |
+ |
for ( my $i=-1; $i<=$#{$self->{features}{$name}}; $i++ ) { |
158 |
+ |
if ( $i>=0 ) { |
159 |
+ |
push @rv, $self->{features}{$name}[$i]; |
160 |
+ |
} |
161 |
+ |
my @rep=$self->_testpos($name, $i); |
162 |
+ |
my $repobj; |
163 |
+ |
foreach $repobj ( @rep ) { |
164 |
+ |
push @rv, $repobj->getfeature($name); |
165 |
+ |
} |
166 |
+ |
} |
167 |
+ |
return @rv; |
168 |
+ |
} |
169 |
+ |
|
170 |
+ |
sub clearfeature { |
171 |
+ |
my $self=shift; |
172 |
+ |
my $name=shift; |
173 |
+ |
|
174 |
+ |
undef @{$self->{features}{$name}}; |
175 |
+ |
} |
176 |
+ |
|
177 |
+ |
sub addrequirement { |
178 |
+ |
my $self=shift; |
179 |
+ |
my $reqobj=shift; |
180 |
+ |
|
181 |
+ |
push @{$self->{reqobjs}}, $reqobj; |
182 |
+ |
foreach $key ( keys %{$self->{features}} ) { |
183 |
+ |
$self->_recordpos($key,$#{$self->{reqobjs}}); |
184 |
+ |
} |
185 |
+ |
} |
186 |
+ |
|
187 |
+ |
sub dependencies { |
188 |
+ |
my $self=shift; |
189 |
+ |
return @{$self->{recobjs}} |
190 |
+ |
} |
191 |
+ |
|
192 |
+ |
sub store { |
193 |
+ |
my $self=shift; |
194 |
+ |
my $location=shift; |
195 |
+ |
|
196 |
+ |
my $fh=FileHandle->new(); |
197 |
+ |
$self->verbose("opening $location for output"); |
198 |
+ |
$fh->open(">".$location) or die "Unable to open $location for output". |
199 |
+ |
$!."\n"; |
200 |
+ |
print $fh "name:".$self->name().":_sys\n"; |
201 |
+ |
print $fh "version:".$self->version().":_sys\n"; |
202 |
+ |
print $fh "url:".$self->url().":_sys\n"; |
203 |
+ |
foreach $f ( $self->features()) { |
204 |
+ |
foreach $val ( $self->getfeature($f) ) { |
205 |
+ |
print $fh $f.":".$val.":".$self->type($f)."\n"; |
206 |
+ |
} |
207 |
+ |
} |
208 |
+ |
undef $fh; |
209 |
+ |
} |
210 |
+ |
|
211 |
+ |
sub restore { |
212 |
+ |
my $self=shift; |
213 |
+ |
my $location=shift; |
214 |
+ |
|
215 |
+ |
my $fh=FileHandle->new(); |
216 |
+ |
$fh->open("<".$location) or die "Unable to open $location for output". |
217 |
+ |
$!."\n"; |
218 |
+ |
my ($tool,$type,$variable,$value); |
219 |
+ |
while ( <$fh> ) { |
220 |
+ |
chomp; |
221 |
+ |
next if /^#/; |
222 |
+ |
next if /^\s*$/; |
223 |
+ |
($variable, $value, $type)=split /:/; |
224 |
+ |
next if ( $variable=~/\&/ ); |
225 |
+ |
$product=~tr[A-Z][a-z]; |
226 |
+ |
if ( $type eq "_sys" ) { |
227 |
+ |
if ( $self->can($variable)) { |
228 |
+ |
$self->$variable($value); |
229 |
+ |
} |
230 |
+ |
} |
231 |
+ |
else { |
232 |
+ |
$self->addfeature($variable,$value); |
233 |
+ |
if ( $type ne "" ) { |
234 |
+ |
$self->type($variable,$type); |
235 |
+ |
} |
236 |
+ |
} |
237 |
+ |
} |
238 |
+ |
undef $fh; |
239 |
+ |
} |
240 |
+ |
|
241 |
+ |
sub _recordpos { |
242 |
+ |
my $self=shift; |
243 |
+ |
my $name=shift; |
244 |
+ |
my $recref=shift; |
245 |
+ |
|
246 |
+ |
push @{$self->{requireposition}{$name}{$#{$self->{features}{$name}}}} |
247 |
+ |
,$recref; |
248 |
+ |
} |
249 |
+ |
|
250 |
+ |
# return array of objects that correspond to the required position |
251 |
+ |
sub _testpos { |
252 |
+ |
my $self=shift; |
253 |
+ |
my $name=shift; |
254 |
+ |
my $ref=shift; |
255 |
+ |
|
256 |
+ |
my @rv=(); |
257 |
+ |
if ( exists $self->{requireposition}{$name}{$ref} ) { |
258 |
+ |
foreach $recref ( @{$self->{requireposition}{$name}{$ref}} ) { |
259 |
+ |
push @rv, $self->{reqobjs}[$recref]; |
260 |
+ |
} |
261 |
+ |
} |
262 |
+ |
return @rv; |
263 |
+ |
} |
264 |
+ |
|
265 |
+ |
|