1 |
# url handler -> returns the location of the file
|
2 |
#
|
3 |
# Interface
|
4 |
# ---------
|
5 |
# new() :
|
6 |
# new(cachedir) : A new urlhandler with a defined default cahce directory
|
7 |
# get(url) : download from the specified url to the default cache
|
8 |
# get(url,location) : dowload to the specified directory
|
9 |
# setcvsco("checkout_cmd") : Default cvs is "co" change with this cmd
|
10 |
# setbase(type,@args) : set the url defaults for a specific url type
|
11 |
# arguments are dependent on type
|
12 |
# http:
|
13 |
# file:
|
14 |
# cvs: servername,servertype [ ,user,passkey ]
|
15 |
# label:
|
16 |
# unsetbase(type) : deactivate a previously set base
|
17 |
# usedb($data) : Set databasecontainer from which to search for tags
|
18 |
#
|
19 |
# ----------------------------------------------------------------------
|
20 |
# returns file location - or crashes out
|
21 |
# can pass a file name for the item to be stored as
|
22 |
# if not then stores in a default cache.
|
23 |
|
24 |
package URLhandler;
|
25 |
require 5.004;
|
26 |
use Utilities::AddDir;
|
27 |
use Carp;
|
28 |
|
29 |
sub new {
|
30 |
my $class=shift;
|
31 |
my $cache=shift;
|
32 |
$self={};
|
33 |
bless $self, $class;
|
34 |
$self->init($cache);
|
35 |
return $self;
|
36 |
}
|
37 |
|
38 |
sub init {
|
39 |
use Utilities::AddDir;
|
40 |
my $self=shift;
|
41 |
my $cache=shift;
|
42 |
if (! defined $cache ) { $cache="/tmp/SCRAMcache" }; # default cache
|
43 |
AddDir::adddir($cache);
|
44 |
$self->{cache}=$cache;
|
45 |
$self->{cvsco}="co";
|
46 |
@{$self->{urltypes}} = qw(label file cvs http);
|
47 |
$self->{filebase}="";
|
48 |
}
|
49 |
|
50 |
sub get ($@) {
|
51 |
my $self=shift;
|
52 |
my $origurl=shift;
|
53 |
my $filename=shift;
|
54 |
my $rest;
|
55 |
my $type;
|
56 |
my $url;
|
57 |
my $version;
|
58 |
|
59 |
if ( ! defined $filename ) {
|
60 |
$filename="";
|
61 |
}
|
62 |
chomp $origurl;
|
63 |
# get our version info from the url (after last ??)
|
64 |
( $url, $version) = split /\?\?/, $origurl;
|
65 |
if ( $url=~/:/ ) {
|
66 |
($type,$rest) = split /:/, $url;
|
67 |
}
|
68 |
else {
|
69 |
$type='label';
|
70 |
$rest=$url.":".$version;
|
71 |
}
|
72 |
foreach $ty ( @{$self->{urltypes}} ) {
|
73 |
do { return &$ty($self, $rest, $filename); $supported='yes'; last;}
|
74 |
, if $type eq $ty;
|
75 |
}
|
76 |
if ( ! ( $supported=~/yes/ )) {
|
77 |
print "URLhandler: Unsupported type $type\n";
|
78 |
carp;
|
79 |
}
|
80 |
}
|
81 |
|
82 |
sub usedb {
|
83 |
my $self=shift;
|
84 |
my $db=shift;
|
85 |
|
86 |
$self->{db}=$db;
|
87 |
print "URLhandler warning: db containers not yet Implemented\n";
|
88 |
}
|
89 |
|
90 |
#
|
91 |
# setcvsco --- to change nature of cvs co (e.g "export" rather than "co")
|
92 |
#
|
93 |
sub setcvsco {
|
94 |
my $self=shift;
|
95 |
$self->{cvsco}=shift;
|
96 |
}
|
97 |
|
98 |
sub setbase {
|
99 |
my $self=shift;
|
100 |
my $type=shift;
|
101 |
my @args=@_;
|
102 |
|
103 |
# Check type is supported
|
104 |
if ( ! grep( $type, @{$self->{urltypes}}) ) {
|
105 |
print "URLhandler error: Unsupported type $type\n";
|
106 |
return 1;
|
107 |
}
|
108 |
else {
|
109 |
&{$type."_setbase"}($self,@args);
|
110 |
}
|
111 |
}
|
112 |
|
113 |
sub unsetbase {
|
114 |
my $self=shift;
|
115 |
my $type=shift;
|
116 |
|
117 |
# Check type is supported
|
118 |
if ( ! grep( $type, @{$self->{urltypes}}) ) {
|
119 |
print "URLhandler error: Unsupported type $type\n";
|
120 |
return 1;
|
121 |
}
|
122 |
else {
|
123 |
&{$type."_unsetbase"}($self, @args);
|
124 |
}
|
125 |
}
|
126 |
|
127 |
# ------------------------- Type Support Routines ----------------------------
|
128 |
#
|
129 |
# label:
|
130 |
#
|
131 |
sub label {
|
132 |
my $self=shift;
|
133 |
my $label=shift;
|
134 |
my $filename=shift;
|
135 |
my $returnval="";
|
136 |
|
137 |
open ( LOOKUP, "$ENV{SCRAM_LOOKUPDB}" )
|
138 |
|| die "URLhandler: Unable to open DataBase $ENV{SCRAM_LOOKUPDB} $!";
|
139 |
while ( <LOOKUP> ) {
|
140 |
next if /^#/;
|
141 |
if ( $_=~s/^$label\:// ) {
|
142 |
$returnval = urlhandler($_,$filename);
|
143 |
}
|
144 |
}
|
145 |
close LOOKUP;
|
146 |
if ( $returnval ne "" ) {
|
147 |
return $returnval;
|
148 |
}
|
149 |
($proj,$ver)=split /:/, $label;
|
150 |
print "Error : Unknown project name or version (".$proj." ".$ver.")\n";
|
151 |
carp;
|
152 |
}
|
153 |
|
154 |
sub label_setbase {
|
155 |
my $self=shift;
|
156 |
}
|
157 |
|
158 |
sub label_unsetbase {
|
159 |
my $self=shift;
|
160 |
}
|
161 |
|
162 |
#
|
163 |
# file:
|
164 |
#
|
165 |
sub file {
|
166 |
my $self=shift;
|
167 |
use File::Copy;
|
168 |
my $urlfile=shift;
|
169 |
my $filename=shift;
|
170 |
|
171 |
if ( $self->{filebase} ne "" ) { # add a base if it exists
|
172 |
$urlfile=$self->{filebase}."/".$urlfile;
|
173 |
}
|
174 |
if ( -e "$urlfile" ) {
|
175 |
if ( $filename ne "" ) {
|
176 |
copy ( $urlfile, $filename ) || return $urlfile;
|
177 |
return $filename;
|
178 |
}
|
179 |
return $urlfile;
|
180 |
}
|
181 |
else {
|
182 |
print "URLhandler: Unable to find file $urlfile : $!\n";
|
183 |
die "";
|
184 |
}
|
185 |
}
|
186 |
|
187 |
#
|
188 |
#
|
189 |
#
|
190 |
sub file_setbase {
|
191 |
my $self=shift;
|
192 |
my $filebase=shift;
|
193 |
|
194 |
if ( -d $filebase ) {
|
195 |
$self->{filebase}=$filebase;
|
196 |
push @{$self->{filebasestack}}, $self->{filebase};
|
197 |
}
|
198 |
else {
|
199 |
print "Directory Does Not Exist \n";
|
200 |
carp;
|
201 |
}
|
202 |
}
|
203 |
|
204 |
sub file_unsetbase {
|
205 |
my $self=shift;
|
206 |
pop @{$self->{filebasestack}};
|
207 |
$self->{filebase}=@{$self->{filebasestack}}
|
208 |
[$#{$self->{filebasestack}}];
|
209 |
}
|
210 |
|
211 |
#
|
212 |
# cvs:
|
213 |
#
|
214 |
|
215 |
sub cvs {
|
216 |
my $self=shift;
|
217 |
my $url=shift;
|
218 |
my $dirname=shift;
|
219 |
|
220 |
my $cvscmd;
|
221 |
my $module;
|
222 |
my $version="";
|
223 |
|
224 |
# Where should we co to?
|
225 |
if ( $dirname eq "" ) {
|
226 |
$dirname=$self->{cache};
|
227 |
}
|
228 |
|
229 |
# Split up our url into its components
|
230 |
if ( $url=~/\?/ ) {
|
231 |
($module, $version)= split /\?/, $url;
|
232 |
}
|
233 |
print $url." -----\n";
|
234 |
|
235 |
if ( $version ne "" ) {
|
236 |
$cvscmd=$self->{cvsco}." -r $version";
|
237 |
}
|
238 |
else {
|
239 |
$cvscmd=$self->{cvsco};
|
240 |
}
|
241 |
#
|
242 |
# Check to see we have a server and if so attempt the checkout
|
243 |
#
|
244 |
if ( ! defined $self->{cvsobject} ) {
|
245 |
print "urlhandler error: undefined cvs server for $module\n";
|
246 |
return 1;
|
247 |
}
|
248 |
else {
|
249 |
chdir $dirname or carp "Unable to Enter Directory $dirname $!\n";
|
250 |
$self->{cvsobject}->invokecvs($cvscmd, $module);
|
251 |
}
|
252 |
}
|
253 |
|
254 |
sub cvs_setbase {
|
255 |
my $self=shift;
|
256 |
my $base=shift;
|
257 |
my $auth=shift;
|
258 |
my $user=shift;
|
259 |
my $passkey=shift;
|
260 |
use Utilities::CVSmodule;
|
261 |
|
262 |
# Push a new cvs object onto the cvs stack
|
263 |
$self->{cvsobject}=CVSmodule->new();
|
264 |
push @{$self->{cvsobjectstack}},$self->{cvsobject};
|
265 |
$self->{cvsobject}->set_base($base);
|
266 |
$self->{cvsobject}->set_auth($auth);
|
267 |
if ( $user ne "" ) {
|
268 |
$self->{cvsobject}->set_user($user);
|
269 |
}
|
270 |
if ( $passkey ne "" ) {
|
271 |
$self->{cvsobject}->set_passkey($passkey);
|
272 |
}
|
273 |
}
|
274 |
|
275 |
sub cvs_unsetbase {
|
276 |
my $self=shift;
|
277 |
pop @{$self->{cvsobjectstack}};
|
278 |
$self->{cvsobject}=@{$self->{cvsobjectstack}}
|
279 |
[$#{$self->{cvsobjectstack}}];
|
280 |
}
|
281 |
|
282 |
#
|
283 |
# http:
|
284 |
#
|
285 |
|
286 |
sub http {
|
287 |
my $self=shift;
|
288 |
my $urlfile=shift;
|
289 |
my $filename=shift;
|
290 |
# use LWP::Simple;
|
291 |
print "Hello $filename, $urlfile\n";
|
292 |
# open (STORE, ">$filename") || carp "unable to open file $filename $!\n";
|
293 |
# print STORE (get 'http:'.$urlfile);
|
294 |
close STORE;
|
295 |
}
|
296 |
|
297 |
sub http_setbase {
|
298 |
my $self=shift;
|
299 |
}
|
300 |
|
301 |
sub http_unsetbase {
|
302 |
my $self=shift;
|
303 |
}
|
304 |
|
305 |
# ------------------------ General Support Routines ----------------------------
|
306 |
|
307 |
sub cachefilename {
|
308 |
my $self=shift;
|
309 |
use File::Basename;
|
310 |
use Utilities::AddDir;
|
311 |
my $filebase=dirname($rest);
|
312 |
$cache="/tmp/williamc/urlhandler$$";
|
313 |
adddir($cache);
|
314 |
$filename=$cache."/".$filebase;
|
315 |
}
|
316 |
|