ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
(Generate patch)

Comparing COMP/SCRAM/src/URL/URLhandler.pm (file contents):
Revision 1.1 by williamc, Mon Aug 23 08:56:49 1999 UTC vs.
Revision 1.6 by williamc, Wed Sep 29 08:56:39 1999 UTC

# Line 1 | Line 1
1 < # url handler -> returns the location of the file
2 < #
3 < # Interface
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,location)        : dowload to the specified directory
7 < # setcvsco("checkout_cmd") : Default cvs is "co" change with this cmd
8 < # 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:
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 < # usedb($data)     : Set databasecontainer from which to search for tags
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 URLhandler;
17 > package URL::URLhandler;
18   require 5.004;
19   use Utilities::AddDir;
20 + use URL::URL_base;
21   use Carp;
22  
23   sub new {
# Line 42 | Line 36 | sub init {
36          if (! defined $cache  ) { $cache="/tmp/SCRAMcache" }; # default cache
37          AddDir::adddir($cache);
38          $self->{cache}=$cache;
39 <        $self->{cvsco}="co";
40 <        @{$self->{urltypes}} = qw(label file cvs http);
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 $filename=shift;
61 >        my $dirname=shift;
62          my $rest;
63          my $type;
64          my $url;
65          my $version;
66 +        my $rv="";
67  
68 <        if ( ! defined $filename ) {
69 <          $filename="";
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;
# Line 68 | Line 78 | sub get ($@) {
78          else {
79             $type='label';
80             $rest=$url.":".$version;
81 <        }
82 <        foreach $ty ( @{$self->{urltypes}} ) {
73 <           do { return &$ty($self, $rest, $filename); $supported='yes'; last;}
74 <                        , if $type eq $ty;
75 <        }
76 <        if ( ! ( $supported=~/yes/ )) {
81 >        }
82 >        if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
83             print "URLhandler: Unsupported type $type\n";
84             carp;
85          }
86 < }
87 <
88 < sub usedb {
89 <        my $self=shift;
90 <        my $db=shift;
91 <        
92 <        $self->{db}=$db;
93 <        print "URLhandler warning: db containers not yet Implemented\n";
94 < }
95 <
96 < #
97 < # setcvsco --- to change nature of cvs co (e.g "export" rather than "co")
98 < #
99 < sub setcvsco {
94 <        my $self=shift;
95 <        $self->{cvsco}=shift;
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, $dirname);
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 ( ! grep( $type, @{$self->{urltypes}}) ) {
109 >        if ( ! exists $self->{urlmodules}{$type} ) {
110            print "URLhandler error: Unsupported type $type\n";
111            return 1;
112          }
113          else {
114 <          &{$type."_setbase"}($self,@args);
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 ( ! grep( $type, @{$self->{urltypes}}) ) {
127 >        if ( ! exists $self->{urlmodules}{$type} ) {
128            print "URLhandler error: Unsupported type $type\n";
129            return 1;
130          }
131          else {
132 <          &{$type."_unsetbase"}($self, @args);
133 <        }
134 < }
135 <
136 < # ------------------------- 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);
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 <        }
139 <        close LOOKUP;
140 <        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;
138 >          else {
139 >           print "URLhandler error: Unable to unset type $type\n";
140 >           return 1;
141            }
179          return $urlfile;
180        }
181        else {
182           print "URLhandler: Unable to find file $urlfile : $!\n";
183           die "";
142          }
143   }
144  
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
145   # ------------------------ General Support Routines ----------------------------
146  
147   sub cachefilename {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines