ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/BuildSystem/ToolManager.pm
Revision: 1.4
Committed: Wed Feb 2 18:57:01 2005 UTC (20 years, 3 months ago) by sashby
Content type: text/plain
Branch: MAIN
Changes since 1.3: +10 -4 lines
Log Message:
Copy the URL cache to developer areas.

File Contents

# User Rev Content
1 sashby 1.2 #____________________________________________________________________
2     # File: ToolManager.pm
3     #____________________________________________________________________
4     #
5     # Author: Shaun Ashby <Shaun.Ashby@cern.ch>
6     # Update: 2003-11-12 15:04:16+0100
7 sashby 1.4 # Revision: $Id: ToolManager.pm,v 1.3 2005/02/02 17:41:41 sashby Exp $
8 sashby 1.2 #
9     # Copyright: 2003 (C) Shaun Ashby
10     #
11     #--------------------------------------------------------------------
12     package BuildSystem::ToolManager;
13     require 5.004;
14    
15     use Exporter;
16     use BuildSystem::ToolCache;
17     use BuildSystem::ToolParser;
18     use Utilities::AddDir;
19     use URL::URLhandler;
20     use Utilities::Verbose;
21    
22     @ISA=qw(BuildSystem::ToolCache Utilities::Verbose);
23     @EXPORT_OK=qw( );
24     #
25     #
26     sub new
27     ###############################################################
28     # new #
29     ###############################################################
30     # modified : Wed Nov 12 10:34:10 2003 / SFA #
31     # params : #
32     # : #
33     # function : #
34     # : #
35     ###############################################################
36     {
37     my $proto=shift;
38     my $class=ref($proto) || $proto;
39     my $self=$class->SUPER::new(); # Inherit from ToolCache
40     my $projectarea=shift;
41    
42     bless $self,$class;
43    
44     $self->{arch}=shift;
45     $self->{topdir}=$projectarea->location();
46     $self->{configdir}=$self->{topdir}."/".$projectarea->configurationdir();
47     $self->{cache}=$projectarea->cache(); # Download tool cache
48     $self->{toolfiledir}=$self->{topdir}."/.SCRAM/InstalledTools";
49     $self->{datastore}=$self->{topdir}."/.SCRAM";
50     $self->{archstore}=$self->{topdir}."/.SCRAM/".$ENV{SCRAM_ARCH};
51    
52     # Make sure our tool download dir exists:
53     AddDir::adddir($self->{toolfiledir});
54     AddDir::adddir($self->{archstore});
55    
56     # Set the tool cache file to read/write:
57     $self->name($projectarea->toolcachename());
58    
59     # Check for the downloaded tools cache:
60     if (exists($self->{cache}))
61     {
62     $self->{urlhandler}=URL::URLhandler->new($self->{cache});
63     }
64    
65     return $self;
66     }
67    
68     sub clone()
69     {
70     my $self=shift;
71     my $projectarea=shift;
72 sashby 1.3
73 sashby 1.2 # Change cache settings to reflect the new location:
74     $self->{topdir}=$projectarea->location();
75    
76     $self->{configdir}=$self->{topdir}."/".$projectarea->configurationdir();
77     $self->{toolfiledir}=$self->{topdir}."/.SCRAM/InstalledTools";
78     $self->{datastore}=$self->{topdir}."/.SCRAM";
79     $self->{archstore}=$self->{topdir}."/.SCRAM/".$ENV{SCRAM_ARCH};
80    
81     # Change the cache name:
82     $self->name($projectarea->toolcachename());
83     $self->cloned_tm(1);
84    
85     return $self;
86     }
87    
88     sub arch_change_after_copy()
89     {
90     my $self=shift;
91     my ($newarch, $cachename)=@_;
92     # Make changes to arch-specific settings when copying tool manager
93     # object to another arch during setup:
94     $self->{arch} = $newarch;
95     $self->{archstore} = $self->{topdir}."/.SCRAM/".$newarch;
96     # Change the name of the cache to reflect new (arch-specific) location:
97     $self->name($cachename);
98     }
99    
100     sub interactive()
101     {
102     my $self=shift;
103     # Interactive mode on/off:
104     @_ ? $self->{interactive} = shift
105     : ((defined $self->{interactive}) ? $self->{interactive} : 0);
106     }
107    
108     sub setupalltools()
109     {
110     my $self = shift;
111     my ($arealocation,$setupopt) = @_;
112     my (@localtools);
113     my $selected;
114    
115     # Get the selected tool list. Handle the case where there might not be
116     # any selected tools: //FIXME: need to handle case where there are no
117     # selected tools (not very often but a possibility):
118     my $sel = $self->selected();
119    
120     if (defined ($sel))
121     {
122     $selected = [ keys %{$sel} ];
123     }
124    
125     # Setup option "setupopt" directs the setup: 1 is for booting from
126     # scratch, 0 is when just doing "scram setup" (in this case we don't
127     # want to pick up everything from any scram-managed projects):
128     if ($setupopt == 1) # We're booting from scratch
129     {
130     # Check to see if there are any SCRAM-managed projects in our local requirements:
131     my $scramprojects = $::scram->_loadscramdb();
132    
133     # Look for a match in the scram db:
134     foreach my $S (@$selected)
135     {
136     if (exists ($scramprojects->{$S}))
137     {
138     # Now check the version required exists in
139     # list of scram projects with this name:
140     while (my ($pdata,$plocation) = each %{$scramprojects->{$S}})
141     {
142     # Split the $pdata string to get the real name and the version:
143     my ($pname,$pversion) = split(":",$pdata);
144     if ($pversion eq $self->defaultversion($S))
145     {
146     # Get the tool manager for the scram project:
147     my $sa=$::scram->scramfunctions()->scramprojectdb()->getarea($pname,$pversion);
148     # Load the tool cache:
149     if ( -r $sa->toolcachename())
150     {
151     use Cache::CacheUtilities;
152     my $satoolmanager=&Cache::CacheUtilities::read($sa->toolcachename());
153     # Copy needed content from toolmanager for scram-managed project:
154     $self->inheritcontent($satoolmanager);
155     }
156     }
157     }
158     # Also add this scram-managed project to list of tools to set up:
159     push(@localtools,$S);
160     }
161     else
162     {
163     # Store other tools in ReqDoc in separate array. We will set up these tools later:
164     push(@localtools,$S);
165     }
166     }
167    
168     # Set up extra tools required in this project, in addition to
169     # any scram-managed projects
170     foreach my $localtool (@localtools)
171     {
172     # First check to see if it's already set up (i.e., was contained
173     # in list of requirements for scram project):
174     if (! $self->definedtool($localtool))
175     {
176     $self->toolsetup($arealocation,$localtool,$self->defaultversion($localtool));
177     $self->addtoselected($localtool);
178     }
179     else
180     {
181     print $localtool," already set up.","\n",if ($ENV{SCRAM_DEBUG});
182     }
183     }
184     }
185     else
186     {
187     # Just loop over all tools and setup again:
188     foreach my $localtool (@{$selected})
189     {
190     $self->toolsetup($arealocation,$localtool,$self->defaultversion($localtool));
191     }
192     }
193    
194     print "\n";
195     }
196    
197     sub coresetup()
198     {
199     my $self=shift;
200     my ($toolname, $toolversion, $toolfile) = @_;
201     my ($toolcheck, $toolparser);
202    
203     print "\n";
204     print $::bold."Setting up ",$toolname," version ",$toolversion,": ".$::normal,"\n";
205    
206     # New ToolParser object for this tool if there isn't one already.
207     # Look in array of raw tools to see if this tool has a ToolParser object:
208     $toolcheck=0;
209    
210     map
211     {
212     if ($_->toolname() eq $toolname) {$toolcheck = 1; $toolparser = $_;}
213     } $self->rawtools();
214    
215     # Tool not known so we create a new ToolParser object and parse it:
216     if ($toolcheck != 1)
217     {
218     $toolparser = BuildSystem::ToolParser->new();
219     # We only want to store the stuff relevant for one particular version:
220     $toolparser->parse($toolname, $toolversion, $toolfile);
221     # Store the ToolParser object in the cache:
222     $self->store($toolparser);
223     }
224    
225     # Next, set up the tool:
226     my $store = $toolparser->processrawtool($self->interactive());
227     # Make sure that we have this tool in the list of selected tools (just in case this tool was
228     # set up by hand afterwards):
229     $self->addtoselected($toolname);
230     # Store the ToolData object in the cache:
231     $self->storeincache($toolparser->toolname(),$store);
232     return $self;
233     }
234    
235     sub toolsetup()
236     {
237     my $self=shift;
238     my ($arealocation, $toolname, $toolversion, $toolurl) = @_;
239     my ($urlcache, $url, $filename, $tfname);
240     my $toolfile;
241 sashby 1.4
242 sashby 1.2 $toolname =~ tr[A-Z][a-z];
243     $toolversion ||= $self->defaultversion($toolname);
244     $urlcache=URL::URLcache->new($arealocation."/.SCRAM/cache"); # Download tool cache
245 sashby 1.4
246 sashby 1.2 # Check for the downloaded tools cache:
247     if (defined($urlcache))
248     {
249     $self->{urlhandler}=URL::URLhandler->new($urlcache);
250     }
251    
252     $url = $self->toolurls()->{$toolname};
253     $filename = $self->{toolfiledir}."/".$toolname;
254    
255 sashby 1.4 # If .SCRAM/InstalledTools doesn't exist, create it:
256     if (! -d $self->{toolfiledir})
257     {
258     AddDir::adddir($self->{toolfiledir});
259     }
260    
261 sashby 1.2 # First, check to see if there was a tool URL given. If so, we might need to read
262     # from http or from a file: type URL:
263     if (my ($proto, $urlv) = ($toolurl =~ /(.*):(.*)/))
264     {
265     # See what kind of URL (file:, http:, cvs:, svn:, .. ):
266     if ($proto eq 'file')
267     {
268     # If the tool url is a file and the file exists,
269     # copy it to .SCRAM/InstalledTools and set the
270     # filename accordingly:
271     if ( -f $urlv)
272     {
273     use File::Copy;
274     copy($urlv, $filename);
275     $toolfile=$filename;
276     }
277     else
278     {
279     $::scram->scramerror("Unable to set up $toolname from URL $toolurl-- $urlv does not exist!");
280     }
281     }
282     elsif ($proto eq 'http')
283     {
284     print "SCRAM: downloading $toolname from $toolurl","\n";
285     # Download from WWW first:
286     use LWP::Simple qw(&getstore);
287     my $http_response_val = &getstore($toolurl, $filename);
288    
289     # Check the HTTP status. If doc not found, exit:
290     if ($http_response_val != 200)
291     {
292     my ($server,$doc) = ($urlv =~ m|//(.*?)/(.*)|);
293     $::scram->scramerror("Unable to set up $toolname: $doc not found on $server!");
294     }
295     else
296     {
297     $toolfile=$filename;
298     }
299     }
300     elsif ($proto eq 'cvs')
301     {
302     print "SCRAM: downloading $toolname from $urlv using protocol $proto.","\n";
303     print "[ not yet supported ]","\n";
304     exit(0);
305     }
306     elsif ($proto eq 'svn')
307     {
308     print "SCRAM: downloading $toolname from $urlv using protocol $proto.","\n";
309     print "[ not yet supported ]","\n";
310     exit(0);
311     }
312     else
313     {
314     $::scram->scramerror("Unable to download $urlv! Unknown protocol \"$proto\". Bye.");
315     }
316     }
317     else
318     {
319     # Copy the downloaded tool file to InstalledTools directory:
320     if ( ! -f $filename )
321     {
322     $self->verbose("Attempting Download of $url");
323     # Get file from download cache:
324     ($url,$filename)=$self->{urlhandler}->get($url);
325     use File::Copy;
326 sashby 1.4 $tfname=$self->{toolfiledir}."/".$toolname;
327 sashby 1.2 copy($filename, $tfname);
328     $toolfile=$tfname;
329     }
330     else
331     {
332     # File already exists in the .SCRAM/InstallTools directory:
333     $toolfile=$filename;
334     }
335     }
336    
337     # Run the core setup routine:
338     $self->coresetup($toolname, $toolversion, $toolfile);
339     return $self;
340     }
341    
342     sub setupself()
343     {
344     my $self=shift;
345     my ($location)=@_;
346     # Process the file "Self" in local config directory. This is used to
347     # set all the paths/runtime settings for this project:
348     my $filename=$location."/config/Self";
349    
350     if ( -f $filename )
351     {
352     print "\n";
353     print $::bold."Setting up SELF:".$::normal,"\n";
354     # Self file exists so process it:
355     $selfparser = BuildSystem::ToolParser->new();
356     $selfparser->parse('self','SELF',$filename);
357    
358     # Next, set up the tool:
359     $store = $selfparser->processrawtool($self->interactive());
360    
361     # If we are in a developer area, also add RELEASETOP paths:
362     if (exists($ENV{RELEASETOP}))
363     {
364     print "\nAdding RELEASE area settings to self....OK","\n", if ($ENV{SCRAM_DEBUG});
365     $store->addreleasetoself();
366     }
367    
368     # Store the ToolData object in the cache:
369     $self->storeincache($selfparser->toolname(),$store);
370     print "\n";
371     }
372     else
373     {
374     print "\n";
375     print "SCRAM: No file config/Self...nothing to do.";
376     print "\n";
377     return;
378     }
379     }
380    
381     sub defaultversion()
382     {
383     my $self = shift;
384     my ($tool) = @_;
385     # Return default versions as taken from configuration:
386     return (%{$self->defaultversions()}->{$tool});
387     }
388    
389     sub storeincache()
390     {
391     my $self=shift;
392     my ($toolname,$dataobject)=@_;
393    
394     # Store ToolData object (for a set-up tool) in cache:
395     if (ref($dataobject) eq 'BuildSystem::ToolData')
396     {
397     $self->{SETUP}->{$toolname} = $dataobject;
398     }
399     else
400     {
401     $::scram->scramerror("ToolManager: BuildSystem::ToolData object reference expected.")
402     }
403     }
404    
405     sub tools()
406     {
407     my $self = shift;
408     my @tools;
409    
410     map
411     {
412     if ($_ ne "self")
413     {
414     push(@tools, $_);
415     }
416     } keys %{$self->{SETUP}};
417    
418     # Return list of set-up tools:
419     return @tools;
420     }
421    
422     sub toolsdata()
423     {
424     my $self = shift;
425     my $tooldata = [];
426     my $rawsel = $self->selected();
427    
428     foreach my $tool ( sort { %{$rawsel}->{$a}
429     <=> %{$rawsel}->{$b}}
430     keys %{$rawsel} )
431     {
432     # Return tool data objects of all set-up tools, skipping the tool "self":
433     if ($_ ne "self")
434     {
435     # Keep only tools that have really been set up:
436     if (exists $self->{SETUP}->{$tool})
437     {
438     push(@tooldata,$self->{SETUP}->{$tool});
439     }
440     }
441     }
442    
443     # Return the array of tools, in order that they appear in RequirementsDoc:
444     return @tooldata;
445     }
446    
447     sub definedtool()
448     {
449     my $self=shift;
450     my ($tool)=@_;
451    
452     # Check to see if tool X is an external tool:
453     grep ($_ eq $tool, keys %{$self->{SETUP}}) ? return 1
454     : return 0;
455     }
456    
457     sub checkifsetup()
458     {
459     my $self=shift;
460     my ($tool)=@_;
461     # Return the ToolData object if the tool has been set up:
462     (exists $self->{SETUP}->{$tool}) ? return ($self->{SETUP}->{$tool})
463     : return undef;
464     }
465    
466     sub cloned_tm()
467     {
468     my $self=shift;
469     # Has this area already been cloned and brought in-line with current location:
470     @_ ? $self->{CLONED} = $_[0]
471     : $self->{CLONED};
472     }
473    
474     sub remove_tool()
475     {
476     my $self=shift;
477     my ($toolname)=@_;
478     my $tools = $self->{SETUP};
479     my $newtlist = {};
480    
481     while (my ($tool, $tooldata) = each %$tools)
482     {
483     if ($tool ne $toolname)
484     {
485     $newtlist->{$tool} = $tooldata;
486     }
487     else
488     {
489     print "Deleting $toolname from cache.","\n";
490     }
491     }
492    
493     $self->{SETUP} = $newtlist;
494    
495     # Now remove from the RAW tool list:
496     $self->cleanup_raw($toolname);
497    
498     print "ToolManager: Updating tool cache.","\n";
499     $self->writecache();
500     }
501    
502     sub scram_projects()
503     {
504     my $self=shift;
505     my $scram_projects={};
506    
507     foreach my $t ($self->tools())
508     {
509     # Get the ToolData object:
510     my $td=$self->{SETUP}->{$t};
511     $scram_projects->{$t}=$td->variable_data(uc($t)."_BASE"), if ($td->scram_project());
512     }
513    
514     return $scram_projects;
515     }
516    
517     1;