[svk-commit] r3035 - in trunk/lib/SVK: .

nobody at bestpractical.com nobody at bestpractical.com
Thu Jul 31 18:24:34 EDT 2008


Author: alexmv
Date: Thu Jul 31 18:24:34 2008
New Revision: 3035

Modified:
   trunk/lib/SVK/Command/Mirror.pm
   trunk/lib/SVK/Mirror.pm

Log:
 * Move remote fetching code into SVK::Mirror->bootstrap from the SVK::Command::Mirror
 * Add a user-agent on the remote fetch
 * Error catching for failed requests
 * Untabify

Modified: trunk/lib/SVK/Command/Mirror.pm
==============================================================================
--- trunk/lib/SVK/Command/Mirror.pm	(original)
+++ trunk/lib/SVK/Command/Mirror.pm	Thu Jul 31 18:24:34 2008
@@ -85,7 +85,7 @@
     }
 
     if (defined (my $narg = $self->narg)) {
-	return unless $narg == (scalar @arg + 1);
+        return unless $narg == (scalar @arg + 1);
     }
 
     return ($self->arg_depotpath ($path), @arg);
@@ -160,70 +160,28 @@
 
     die loc("No such dump file: %1.\n", $self->{bootstrap})
         unless $self->{bootstrap} eq '-' ||
-	$self->{bootstrap} eq 'auto' || -f ($self->{bootstrap});
+        $self->{bootstrap} =~ m{^(file|https?|ftp)://} ||
+        $self->{bootstrap} eq 'auto' || -f ($self->{bootstrap});
 
     if (!$m) {
-	$self->SUPER::run($target,$uri, @options);
-	($m, $mpath) = $target->is_mirrored;
+        $self->SUPER::run($target,$uri, @options);
+        ($m, $mpath) = $target->is_mirrored;
     }
     # XXX: make sure the mirror is fresh and not synced at all
 
     die loc("%1 is not a mirrored path.\n", $target->depotpath) if !$m;
     die loc("%1 is inside a mirrored path.\n", $target->depotpath) if $mpath;
 
-    my $hint;
     if ( $self->{bootstrap} eq 'auto' ) {
         my $ra = $m->_backend->_new_ra;
         $ra->reparent( $ra->get_repos_root() );
         my %prop = %{ ( $ra->get_file( '', $ra->get_latest_revnum, undef ) )[1] };
         $m->_backend->_ra_finished($ra);
-        if ( $hint = $prop{'svk:dump-url'} ) {
-            $logger->info( loc( "Downloading dump file: %1", $hint ) );
-            $self->{bootstrap} = File::Temp->new;
-
-            require LWP::UserAgent;
-            my $ua = LWP::UserAgent->new;
-            my ( $received_size, $next_update ) = ( 0, 0 );
-            my $did_set_target = 0;
-            # XXX: switch to a default notify object that takes care
-            # of quiet and gui variants.
-            my $progress = SVK::Notify->new->progress(
-                    min => 0,
-                    max => 1024,
-            );
-            $ua->get(
-                $hint,
-                ':content_cb' => sub {
-                    my ( $data, $cb_response, $protocol ) = @_;
-
-                    if ($progress) {
-                        unless ($did_set_target) {
-                            if ( my $content_length = $cb_response->content_length ) {
-                                $progress->attr(max => $content_length);
-                                $did_set_target = 1;
-                            }
-                            else {
-                                $progress->attr(max => 
-                                                  $received_size + 2 * length $data );
-                            }
-                        }
-                    }
-                    $received_size += length $data;
-                    print { $self->{bootstrap} } $data;
-		    if ($progress && $received_size >= $next_update) {
-			local $| = 1;
-			print STDERR $progress->report( "%45b %p\r", $received_size );
-			$next_update = $received_size + 1;
-		    }
-                },
-                ':read_size_hint' => 16384,
-            );
-        }
-
+        $self->{bootstrap} = $prop{'svk:dump-url'};
     }
 
     $logger->info( loc("Bootstrapping mirror from dump") );
-    $m->bootstrap($self->{bootstrap}, $hint); # load from dumpfile
+    $m->bootstrap($self->{bootstrap}); # load from dumpfile
     print loc("Mirror path '%1' synced from dumpfile.\n", $target->depotpath);
     return;
 }

Modified: trunk/lib/SVK/Mirror.pm
==============================================================================
--- trunk/lib/SVK/Mirror.pm	(original)
+++ trunk/lib/SVK/Mirror.pm	Thu Jul 31 18:24:34 2008
@@ -59,6 +59,7 @@
 use SVK::I18N;
 use SVK::Util qw(uri_escape uri_unescape);
 use Scalar::Util 'weaken';
+use File::Temp qw//;
 
 use base 'Class::Accessor::Fast';
 use SVK::Notify;
@@ -245,6 +246,48 @@
 
     if ($dumpfile eq '-') {
         $fh = \*STDIN;
+    } elsif ($dumpfile =~ m{^(file|https?|ftp)://}) {
+        $logger->info( loc( "Downloading dump file: %1", $dumpfile ) );
+        my ($tmp, $path) = File::Temp::tempfile;
+
+        require LWP::UserAgent;
+        my $ua = LWP::UserAgent->new(agent => "SVK-bootstrap/$SVK::VERSION");
+        my ( $received_size, $next_update ) = ( 0, 0 );
+        my $did_set_target = 0;
+        # XXX: switch to a default notify object that takes care
+        # of quiet and gui variants.
+        my $progress = SVK::Notify->new->progress(
+                min => 0,
+                max => 1024,
+        );
+        my $response = $ua->get(
+            $dumpfile,
+            ':content_cb' => sub {
+                my ( $data, $cb_response, $protocol ) = @_;
+                if ($progress) {
+                    unless ($did_set_target) {
+                        if ( my $content_length = $cb_response->content_length ) {
+                            $progress->attr(max => $content_length);
+                            $did_set_target = 1;
+                        }
+                        else {
+                            $progress->attr(max => 
+                                              $received_size + 2 * length $data );
+                        }
+                    }
+                }
+                $received_size += length $data;
+                print { $tmp } $data;
+                if ($progress && $received_size >= $next_update) {
+                    local $| = 1;
+                    print STDERR $progress->report( "%45b %p\r", $received_size );
+                    $next_update = $received_size + 1;
+                }
+            },
+            ':read_size_hint' => 16384,
+        );
+        die $response->status_line unless $response->is_success;
+        open $fh, "<", $path or die $!;
     }
     else {
         open $fh, '<', $dumpfile or die $!;


More information about the svk-commit mailing list