ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.4
Committed: Wed Aug 25 15:43:18 1999 UTC (25 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.3: +17 -9 lines
Log Message:
Return filename/dir as appropriate from get calls

File Contents

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