1 |
#
|
2 |
# ScramFunctions.pm
|
3 |
#
|
4 |
# Written by Christopher Williams
|
5 |
#
|
6 |
# Description
|
7 |
#
|
8 |
# Interface
|
9 |
# ---------
|
10 |
# new() : A new ScramCommands object
|
11 |
# project(urlstring,dir[,areaname]): Create a project from the given url file
|
12 |
# return the area
|
13 |
# satellite(name,version,installdirectory[,areaname]): create a satellite
|
14 |
# project from the specified name version
|
15 |
# addareatoDB(ConfigArea[,name[,version]]) : add area to the db
|
16 |
# globalcache([cachedirectory])) : return the global cache object/set at dir.
|
17 |
# scramprojectdb() : return the scram project DB object
|
18 |
# areatoolbox(ConfigArea) : return the toolbox of the specified area
|
19 |
# setuptoolsinarea($area[,$toolname[,$toolversion[,toolfile]) : setup
|
20 |
# arch() : get/set the architecture string
|
21 |
# scramobjectinterface(Class) : print out the interface of a scram class
|
22 |
# webget(area,url) : get the url into the cache of the specified area
|
23 |
# getversion() : return the current scram version
|
24 |
# spawnversion(version,@args) : spawn the scram version with the given args
|
25 |
|
26 |
package Scram::ScramFunctions;
|
27 |
use URL::URLcache;
|
28 |
use URL::URLhandler;
|
29 |
use Utilities::Verbose;
|
30 |
require 5.004;
|
31 |
|
32 |
@ISA=qw(Utilities::Verbose);
|
33 |
|
34 |
sub new {
|
35 |
my $class=shift;
|
36 |
$self={};
|
37 |
bless $self, $class;
|
38 |
# -- default settings
|
39 |
$self->{cachedir}=$ENV{HOME}."/.scramrc/globalcache";
|
40 |
$self->{scramprojectsdbdir}=$ENV{SCRAM_LOOKUPDB};
|
41 |
($self->{scram_top})=( $ENV{SCRAM_HOME}=~/(.*)\//) ;
|
42 |
|
43 |
return $self;
|
44 |
}
|
45 |
|
46 |
sub project {
|
47 |
my $self=shift;
|
48 |
my $url=shift;
|
49 |
my $installarea=shift;
|
50 |
|
51 |
my $areaname="";
|
52 |
if ( @_ ) {
|
53 |
$areaname=shift;
|
54 |
}
|
55 |
require Configuration::BootStrapProject;
|
56 |
my $bs=Configuration::BootStrapProject->
|
57 |
new($self->globalcache(),$installarea);
|
58 |
$self->verbose("BootStrapping $url");
|
59 |
my $area=$bs->boot($url,$areaname);
|
60 |
if ( ! defined $area ) {
|
61 |
$self->error("Unable to create project area");
|
62 |
}
|
63 |
$area->archname($self->arch());
|
64 |
|
65 |
# -- download all tool description files
|
66 |
my $req=$self->arearequirements($area);
|
67 |
if ( defined $req ) {
|
68 |
$req->download($self->areatoolbox($area));
|
69 |
}
|
70 |
|
71 |
return $area;
|
72 |
}
|
73 |
|
74 |
sub setuptoolsinarea {
|
75 |
my $self=shift;
|
76 |
my $area=shift;
|
77 |
|
78 |
# -- initialise
|
79 |
$self->_allprojectinitsearcher();
|
80 |
|
81 |
# -- create a new toolbox object
|
82 |
my $toolbox=$self->areatoolbox($area);
|
83 |
$toolbox->searcher($self->_projsearcher());
|
84 |
|
85 |
if ( @_ ) {
|
86 |
# -- specific tool specified
|
87 |
if ( my $rv=$toolbox->toolsetup(@_) ) {
|
88 |
if ( $rv eq 1 ) {
|
89 |
print "Unknown tool $toolname @ARGV\n";
|
90 |
exit 1;
|
91 |
}
|
92 |
}
|
93 |
}
|
94 |
else {
|
95 |
# -- setup all tools specified in the requirements doc
|
96 |
my $reqs=$self->arearequirements($area);
|
97 |
$self->verbose("Setup ToolBox from Requirements doc ($reqs)");
|
98 |
$reqs->setup($toolbox);
|
99 |
}
|
100 |
}
|
101 |
|
102 |
sub satellite {
|
103 |
my $self=shift;
|
104 |
my $name=shift;
|
105 |
my $version=shift;
|
106 |
my $installarea=shift;
|
107 |
|
108 |
my $areaname=undef;
|
109 |
if ( @_ ) {
|
110 |
$areaname=shift;
|
111 |
}
|
112 |
|
113 |
# -- look up scram database for location
|
114 |
my $relarea=$self->_lookupareaindb($name,$version);
|
115 |
if ( ! defined $relarea ) {
|
116 |
$self->error("Unable to Find Project $name $version");
|
117 |
}
|
118 |
|
119 |
# -- fix for old broken areas
|
120 |
if ( (! defined $relarea->version()) || ($relarea->version() eq "") ) {
|
121 |
$relarea->version($version);
|
122 |
}
|
123 |
|
124 |
# -- create satellite
|
125 |
my $area=$relarea->satellite($installarea,$areaname);
|
126 |
$area->archname($self->arch());
|
127 |
|
128 |
# -- copy setup info - deprecated by toolbox copy method
|
129 |
#$relarea->copysetup($area->location());
|
130 |
|
131 |
# -- copy toolbox
|
132 |
my $rtb=$self->areatoolbox($relarea);
|
133 |
my $tb=$self->areatoolbox($area);
|
134 |
$rtb->copytools($tb);
|
135 |
|
136 |
# -- copy configuration directory
|
137 |
if ( ! -d $area->location()."/".$area->configurationdir() ) {
|
138 |
use Utilities::AddDir;
|
139 |
AddDir::copydir($relarea->location()."/".$relarea->configurationdir(),
|
140 |
$area->location()."/".$area->configurationdir() );
|
141 |
}
|
142 |
|
143 |
# -- copy RequirementsDoc
|
144 |
if ( ! -f $area->requirementsdoc() ) {
|
145 |
use File::Copy;
|
146 |
copy( $relarea->requirementsdoc() , $area->requirementsdoc());
|
147 |
}
|
148 |
|
149 |
return $area;
|
150 |
}
|
151 |
|
152 |
sub webget {
|
153 |
my $self=shift;
|
154 |
my $area=shift;
|
155 |
my $url=shift;
|
156 |
|
157 |
my $handler=URL::URLhandler->new($area->cache());
|
158 |
return ($handler->get($url));
|
159 |
}
|
160 |
|
161 |
sub addareatoDB {
|
162 |
my $self=shift;
|
163 |
my $area=shift;
|
164 |
my $tagname=shift;
|
165 |
my $version=shift;
|
166 |
|
167 |
# -- create defaults if necessary
|
168 |
if ( (! defined $version) || ( $version eq "") ) {
|
169 |
$version=$area->version();
|
170 |
}
|
171 |
if ( (! defined $tagname) || ( $tagname eq "") ) {
|
172 |
$tagname=$area->name();
|
173 |
}
|
174 |
|
175 |
# -- Add to the DB
|
176 |
$self->scramprojectdb()->addarea($tagname,$version,$area);
|
177 |
}
|
178 |
|
179 |
sub spawnversion {
|
180 |
my $self=shift;
|
181 |
my $version=shift;
|
182 |
|
183 |
my $rv=0;
|
184 |
my $thisversion=$self->getversion();
|
185 |
if ( defined $version ) {
|
186 |
if ( $version ne $thisversion ) {
|
187 |
# first try to use the correct version
|
188 |
if ( -d $self->{scram_top}."/".$version ) {
|
189 |
$ENV{SCRAM_HOME}=$self->{scram_top}."/".$version;
|
190 |
$ENV{TOOL_HOME}="$ENV{SCRAM_HOME}/src";
|
191 |
$self->verbose("Spawning SCRAM version $version");
|
192 |
my $rv=system("scram", @_)/256;
|
193 |
exit $rv;
|
194 |
}
|
195 |
else { # if not then simply warn
|
196 |
print "******* Warning : scram version inconsistent ********\n";
|
197 |
print "This version: $thisversion; Required version: $version\n";
|
198 |
print "*****************************************************\n";
|
199 |
print "\n";
|
200 |
}
|
201 |
}
|
202 |
}
|
203 |
else {
|
204 |
$self->error("Undefined value for version requested");
|
205 |
$rv=1;
|
206 |
}
|
207 |
return $rv;
|
208 |
}
|
209 |
|
210 |
sub globalcache {
|
211 |
my $self=shift;
|
212 |
if ( @_ ) {
|
213 |
$self->{cachedir}=shift;
|
214 |
}
|
215 |
if ( ! defined $self->{globalcache} ) {
|
216 |
$self->{globalcache}=URL::URLcache->new($self->{cachedir});
|
217 |
}
|
218 |
return $self->{globalcache};
|
219 |
}
|
220 |
|
221 |
|
222 |
sub scramprojectdb {
|
223 |
my $self=shift;
|
224 |
if ( @_ ) {
|
225 |
$self->{scramprojectsdbdir}=shift;
|
226 |
}
|
227 |
if ( ! defined $self->{scramprojectsdb} ) {
|
228 |
require Scram::ScramProjectDB;
|
229 |
$self->{scramprojectsdb}=Scram::ScramProjectDB->new(
|
230 |
$self->{scramprojectsdbdir} );
|
231 |
$self->{scramprojectsdb}->verbosity($self->verbosity());
|
232 |
}
|
233 |
return $self->{scramprojectsdb};
|
234 |
}
|
235 |
|
236 |
sub getversion {
|
237 |
my $self=shift;
|
238 |
|
239 |
my $thisversion;
|
240 |
($thisversion=$ENV{SCRAM_HOME})=~s/(.*)\///;
|
241 |
my $scram_top=$1;
|
242 |
my $scram_version=$thisversion;
|
243 |
# deal with links
|
244 |
my $version=readlink $ENV{SCRAM_HOME};
|
245 |
if ( defined $version) {
|
246 |
$scram_version=$version;
|
247 |
}
|
248 |
return $scram_version;
|
249 |
}
|
250 |
|
251 |
sub areatoolbox {
|
252 |
my $self=shift;
|
253 |
my $area=shift;
|
254 |
|
255 |
my $name=$area->location();
|
256 |
if ( ! defined $self->{toolboxes}{$name} ) {
|
257 |
# -- create a new toolbox object
|
258 |
require BuildSystem::ToolBox;
|
259 |
$self->{toolboxes}{$name}=BuildSystem::ToolBox->new($area,
|
260 |
$self->arch());
|
261 |
$self->{toolboxes}{$name}->verbosity($self->verbosity());
|
262 |
}
|
263 |
return $self->{toolboxes}{$name};
|
264 |
}
|
265 |
|
266 |
sub arearequirements {
|
267 |
my $self=shift;
|
268 |
my $area=shift;
|
269 |
|
270 |
my $name=$area->location();
|
271 |
if ( ! defined $self->{requirements}{$name} ) {
|
272 |
# -- create a new toolbox object
|
273 |
require BuildSystem::Requirements;
|
274 |
my $doc=$area->requirementsdoc();
|
275 |
my $cache=$area->cache();
|
276 |
my $db=$area->objectstore();
|
277 |
require ActiveDoc::ActiveStore;
|
278 |
my $astore=ActiveDoc::ActiveStore->new($db,$cache);
|
279 |
$self->{requirements}{$name}=
|
280 |
BuildSystem::Requirements->new($astore,"file:".$doc,
|
281 |
$ENV{SCRAM_ARCH});
|
282 |
$self->{requirements}{$name}->verbosity($self->verbosity());
|
283 |
$self->verbose("Requirements Doc (".$self->{requirements}{$name}.
|
284 |
") for area :\n $name\n initiated from $doc");
|
285 |
}
|
286 |
else {
|
287 |
$self->verbose("Requirements Doc (".$self->{requirements}{$name}.")");
|
288 |
}
|
289 |
return $self->{requirements}{$name};
|
290 |
}
|
291 |
|
292 |
sub arch {
|
293 |
my $self=shift;
|
294 |
|
295 |
@_?$self->{arch}=shift
|
296 |
:$self->{arch};
|
297 |
}
|
298 |
|
299 |
sub scramobjectinterface {
|
300 |
my $self=shift;
|
301 |
my $class=shift;
|
302 |
|
303 |
my $file;
|
304 |
($file=$class."\.pm")=~s/::/\//g;
|
305 |
$file=$ENV{TOOL_HOME}."/".$file;
|
306 |
|
307 |
if ( ! -f $file ) {
|
308 |
$self->error("Unable to find $class in ".$ENV{TOOL_HOME});
|
309 |
}
|
310 |
print "--------------------- $class ------------------\n";
|
311 |
my $fh=FileHandle->new();
|
312 |
$fh->open("<$file");
|
313 |
while ( <$fh> ) {
|
314 |
if ( $_=~/#\s*Interface/g ) {
|
315 |
$intregion=1;
|
316 |
next;
|
317 |
}
|
318 |
if ( $intregion ) { # if we are in the interface documentation
|
319 |
if ( ( $_!~/^#/ ) || ( $_=~/^#\s?-{40}/ ) ) { #moving out of Int doc
|
320 |
$intregion=0;
|
321 |
next;
|
322 |
}
|
323 |
print $_;
|
324 |
if ( $_=~/^#\s*(.*)\((.*)\)?:(.*)/ ) {
|
325 |
$interface=$1;
|
326 |
$args=$2;
|
327 |
$rest=$3;
|
328 |
next if ($interface eq "");
|
329 |
push @interfaces,$interface;
|
330 |
$interfaceargs{$interface}=$args;
|
331 |
}
|
332 |
}
|
333 |
}
|
334 |
print "\n";
|
335 |
undef $fh;
|
336 |
}
|
337 |
|
338 |
# -------------- Support Routines ------------------------------
|
339 |
|
340 |
|
341 |
sub _allprojectinitsearcher {
|
342 |
if ( ! defined $self->{projsearcher} ) {
|
343 |
my $search=_projsearcher();
|
344 |
foreach $proj ( $self->scramprojectdb()->list() ) {
|
345 |
$search->addproject($$proj[0],$$proj[1]);
|
346 |
}
|
347 |
}
|
348 |
}
|
349 |
|
350 |
sub _projsearcher {
|
351 |
if ( ! defined $self->{projsearcher} ) {
|
352 |
require Scram::ProjectSearcher;
|
353 |
$self->{projsearcher}=Scram::ProjectSearcher->new(
|
354 |
$self->scramprojectdb());
|
355 |
}
|
356 |
return $self->{projsearcher};
|
357 |
}
|
358 |
|
359 |
|
360 |
sub _lookupareaindb {
|
361 |
my $self=shift;
|
362 |
|
363 |
my $area=undef;
|
364 |
# -- Look up the area in the databse
|
365 |
my @areas=$self->scramprojectdb()->getarea(@_);
|
366 |
if ( $#areas > 0 ) { #ambigous
|
367 |
# could ask user - for now just error
|
368 |
$self->error("Ambigous request - please be more specific");
|
369 |
}
|
370 |
elsif ( $#areas != 0 ) { #not found
|
371 |
$self->error("The project requested has not been found");
|
372 |
}
|
373 |
else {
|
374 |
$area=$areas[0];
|
375 |
}
|
376 |
return $area;
|
377 |
}
|