1 |
# url handler -> returns the location of thefilu# Interface
|
2 |
# ---------
|
3 |
# new() :
|
4 |
# new(cachedir) : A new urlhandler with a defined default cahce directory
|
5 |
# get(url) : download from the specified url to the default cache
|
6 |
# get(url,dirlocation) : download to the specified directory
|
7 |
# setbase(type,variablehash) : set the url defaults for a specific url type
|
8 |
# arguments are dependenton type
|
9 |
# unsetbase(type) : deactivate a previously set base
|
10 |
# setcache(dir) : set the default cache location
|
11 |
#
|
12 |
# ----------------------------------------------------------------------
|
13 |
# returns file location - or crashes out
|
14 |
# can pass a file name for the item to be stored as
|
15 |
# if not then stores in a default cache.
|
16 |
|
17 |
package URL::URLhandler;
|
18 |
require 5.004;
|
19 |
use Utilities::AddDir;
|
20 |
use URL::URL_base;
|
21 |
use Carp;
|
22 |
|
23 |
sub new {
|
24 |
my $class=shift;
|
25 |
my $cache=shift;
|
26 |
$self={};
|
27 |
bless $self, $class;
|
28 |
$self->init($cache);
|
29 |
return $self;
|
30 |
}
|
31 |
|
32 |
sub init {
|
33 |
use Utilities::AddDir;
|
34 |
my $self=shift;
|
35 |
my $cache=shift;
|
36 |
if (! defined $cache ) { $cache="/tmp/SCRAMcache" }; # default cache
|
37 |
AddDir::adddir($cache);
|
38 |
$self->{cache}=$cache;
|
39 |
use URL::URL_cvs;
|
40 |
use URL::URL_cvsfile;
|
41 |
use URL::URL_file;
|
42 |
$self->{urlmodules}={
|
43 |
'cvsfile' => 'URL::URL_cvsfile',
|
44 |
'cvs' => 'URL::URL_cvs',
|
45 |
'file' => 'URL::URL_file'
|
46 |
};
|
47 |
$self->{filebase}="";
|
48 |
$self->setbase("file", {}); # Base file as default
|
49 |
}
|
50 |
|
51 |
sub setcache {
|
52 |
my $self=shift;
|
53 |
my $cache=shift;
|
54 |
|
55 |
$self->{cache}=$cache;
|
56 |
}
|
57 |
|
58 |
sub get ($@) {
|
59 |
my $self=shift;
|
60 |
my $origurl=shift;
|
61 |
my $dirname=shift;
|
62 |
my $rest;
|
63 |
my $type;
|
64 |
my $url;
|
65 |
my $version;
|
66 |
my $rv="";
|
67 |
|
68 |
if ( ! defined $dirname ) {
|
69 |
$dirname=$self->{cache};
|
70 |
}
|
71 |
chdir $dirname or carp "Unable to Enter Directory $dirname $!\n";
|
72 |
chomp $origurl;
|
73 |
# get our version info from the url (after last ??)
|
74 |
( $url, $version) = split /\?\?/, $origurl;
|
75 |
if ( $url=~/:/ ) {
|
76 |
($type,$rest) = split /:/, $url;
|
77 |
}
|
78 |
else {
|
79 |
$type='label';
|
80 |
$rest=$url.":".$version;
|
81 |
}
|
82 |
if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
|
83 |
print "URLhandler: Unsupported type $type\n";
|
84 |
carp;
|
85 |
}
|
86 |
else {
|
87 |
if ( $#{$self->{urlostack}{$type}} < 0 ) {
|
88 |
print "URLhandler : base not set for type $type \n";
|
89 |
}
|
90 |
else {
|
91 |
$rv=
|
92 |
eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}->
|
93 |
get($rest);
|
94 |
}
|
95 |
}
|
96 |
if ( $rv ne "" ) {
|
97 |
$rv=$dirname."/".$rv;
|
98 |
}
|
99 |
return $rv;
|
100 |
}
|
101 |
|
102 |
sub setbase {
|
103 |
my $self=shift;
|
104 |
my $type=shift;
|
105 |
my @args=@_;
|
106 |
my $oref;
|
107 |
|
108 |
# Check type is supported
|
109 |
if ( ! exists $self->{urlmodules}{$type} ) {
|
110 |
print "URLhandler error: Unsupported type $type\n";
|
111 |
return 1;
|
112 |
}
|
113 |
else {
|
114 |
# A new URL object - pushed onto the stack
|
115 |
$oref=eval{$self->{urlmodules}{$type}}->new();
|
116 |
push @{$self->{urlostack}{$type}}, $oref;
|
117 |
$oref->setbase(@args);
|
118 |
}
|
119 |
}
|
120 |
|
121 |
sub unsetbase {
|
122 |
my $self=shift;
|
123 |
my $type=shift;
|
124 |
my $oref;
|
125 |
|
126 |
# Check type is supported
|
127 |
if ( ! exists $self->{urlmodules}{$type} ) {
|
128 |
print "URLhandler error: Unsupported type $type\n";
|
129 |
return 1;
|
130 |
}
|
131 |
else {
|
132 |
# pop off the stack and call the unset base method
|
133 |
if ( $#{$self->{urlostack}{$type}} >=0 ) {
|
134 |
$oref=pop @{$self->{urlostack}{$type}};
|
135 |
$oref->unsetbase();
|
136 |
undef $oref;
|
137 |
}
|
138 |
else {
|
139 |
print "URLhandler error: Unable to unset type $type\n";
|
140 |
return 1;
|
141 |
}
|
142 |
}
|
143 |
}
|
144 |
|
145 |
# ------------------------ General Support Routines ----------------------------
|
146 |
|
147 |
sub cachefilename {
|
148 |
my $self=shift;
|
149 |
use File::Basename;
|
150 |
use Utilities::AddDir;
|
151 |
my $filebase=dirname($rest);
|
152 |
$cache="/tmp/williamc/urlhandler$$";
|
153 |
adddir($cache);
|
154 |
$filename=$cache."/".$filebase;
|
155 |
}
|
156 |
|