[Bps-public-commit] Prophet branch, master, updated. 970510438405fd6ab36f2b57af796a96e1c57b39

jesse jesse at bestpractical.com
Thu Apr 16 00:18:58 EDT 2009


The branch, master has been updated
       via  970510438405fd6ab36f2b57af796a96e1c57b39 (commit)
       via  44829fecc6bdefcafa9765804dbd983bbbe7ed13 (commit)
      from  aef857c4ccf701a1dbda0034414ac82c837adc6b (commit)

Summary of changes:
 lib/Prophet/CLI/Command/Merge.pm     |   17 +++--------
 lib/Prophet/CLI/Command/Mirror.pm    |   52 ++++------------------------------
 lib/Prophet/CLI/MirrorCommand.pm     |   40 ++++++++++++++++++++++++++
 lib/Prophet/CLI/ProgressBar.pm       |   24 +++++++++++++++
 lib/Prophet/Replica/prophet_cache.pm |   14 ++++++++-
 5 files changed, 88 insertions(+), 59 deletions(-)
 create mode 100644 lib/Prophet/CLI/MirrorCommand.pm
 create mode 100644 lib/Prophet/CLI/ProgressBar.pm

- Log -----------------------------------------------------------------
commit 44829fecc6bdefcafa9765804dbd983bbbe7ed13
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Thu Apr 16 12:05:24 2009 +0800

    Extracted Prophet CLI progress bars to a role

diff --git a/lib/Prophet/CLI/Command/Merge.pm b/lib/Prophet/CLI/Command/Merge.pm
index 23e1ab3..46d703f 100644
--- a/lib/Prophet/CLI/Command/Merge.pm
+++ b/lib/Prophet/CLI/Command/Merge.pm
@@ -1,6 +1,7 @@
 package Prophet::CLI::Command::Merge;
 use Any::Moose;
 extends 'Prophet::CLI::Command';
+with 'Prophet::CLI::ProgressBar';
 
 has source => ( isa => 'Prophet::Replica', is => 'rw');
 has target => ( isa => 'Prophet::Replica', is => 'rw');
@@ -21,10 +22,12 @@ sub run {
         app_handle => $self->app_handle,
     ));
 
-
     
     return  unless $self->validate_merge_replicas($self->source => $self->target);
 
+
+
+
     $self->target->import_resolutions_from_remote_replica(
         from  => $self->source,
         force => $self->has_arg('force'),
@@ -99,17 +102,7 @@ sub _do_merge {
             $changesets++;
         };
     } else {
-        require Time::Progress;
-        my $progress = Time::Progress->new();
-        $progress->attr( max => ($source_latest - $source_last_seen));
-
-        $import_args{reporting_callback} = sub {
-            my %args = @_;
-            $changesets++;
-            print $progress->report( "%30b %p %E // ". ($args{changeset}->created || 'Undated'). " " .(sprintf("%-12s",$args{changeset}->creator||'')) ."\r" , $changesets);
-
-        };
-
+        $import_args{reporting_callback} = $self->progress_bar( max => ($source_latest - $source_last_seen), format => "%30b %p %E\r" );
     }
 
     $self->target->import_changesets( %import_args);
diff --git a/lib/Prophet/CLI/Command/Mirror.pm b/lib/Prophet/CLI/Command/Mirror.pm
index 976550f..89b7122 100644
--- a/lib/Prophet/CLI/Command/Mirror.pm
+++ b/lib/Prophet/CLI/Command/Mirror.pm
@@ -1,9 +1,9 @@
 package Prophet::CLI::Command::Mirror;
 use Any::Moose;
 use Params::Validate qw/:all/;
-use Time::Progress;
 
 extends 'Prophet::CLI::Command';
+with 'Prophet::CLI::ProgressBar';
 
 has source => ( isa => 'Prophet::Replica', is => 'rw');
 has target => ( isa => 'Prophet::Replica', is => 'rw');
@@ -17,19 +17,13 @@ sub run {
     $self->validate_args();
 
 
-    my $source = Prophet::Replica->get_handle(
-        url        => $self->arg('from'),
-        app_handle => $self->app_handle,
-    );
+    my $source = Prophet::Replica->get_handle( url        => $self->arg('from'), app_handle => $self->app_handle,);
     unless ( $source->replica_exists ) {
         print "The source replica '@{[$source->url]}' doesn't exist or is unreadable.";
         exit 1;
     }
 
-    $self->set_arg( 'to' => 'prophet_cache:' . $source->uuid);
-    my $target
-        = Prophet::Replica->get_handle( url => $self->arg('to'), app_handle => $self->app_handle );
-
+    my $target = Prophet::Replica->get_handle( url => 'prophet_cache:' . $source->uuid , app_handle => $self->app_handle );
     $target->uuid( $source->uuid );
     $target->resdb_replica_uuid( $source->resolution_db_handle->uuid );
 
@@ -47,8 +41,7 @@ sub run {
     print "Mirroring resolutions from " . $source->url . "\n";
     $target->resolution_db_handle->mirror_from(
         source => $source->resolution_db_handle,
-        reporting_callback =>
-            $self->progress_bar( max => $source->resolution_db_handle->latest_sequence_no )
+        reporting_callback => $self->progress_bar( max => $source->resolution_db_handle->latest_sequence_no )
     );
     print "\nMirroring changesets from " . $source->url . "\n";
     $target->mirror_from(
@@ -63,18 +56,6 @@ sub validate_args {
         unless $self->has_arg('from');
 }
 
-sub progress_bar { 
-    my $self = shift;
-    my %args = validate(@_, {max => 1});
-    my $bar = Time::Progress->new();
-    $bar->attr(max => $args{max});
-    my $bar_count = 0;
-    return sub {
-       print $bar->report( "%30b %p %L (%E remaining)\r", ++$bar_count );
-    }
-
-}
-
 __PACKAGE__->meta->make_immutable;
 no Any::Moose;
 
diff --git a/lib/Prophet/CLI/ProgressBar.pm b/lib/Prophet/CLI/ProgressBar.pm
new file mode 100644
index 0000000..7a72d46
--- /dev/null
+++ b/lib/Prophet/CLI/ProgressBar.pm
@@ -0,0 +1,25 @@
+package Prophet::CLI::ProgressBar;
+use Any::Moose 'Role';
+
+use Time::Progress;
+use Params::Validate ':all';
+
+sub progress_bar { 
+    my $self = shift;
+    my %args = validate(@_, {max => 1, format => { optional =>1, default => "%30b %p %L (%E remaining)\r" }});
+    my $bar = Time::Progress->new();
+
+
+    $bar->attr(max => $args{max});
+    my $bar_count = 0;
+    my $format = $args{format};
+    return sub {
+        my %args = shift;
+       print $bar->report(  $format,
+           , ++$bar_count );
+    }
+}
+no Any::Moose;
+
+1;
+

commit 970510438405fd6ab36f2b57af796a96e1c57b39
Author: Jesse Vincent <jesse at bestpractical.com>
Date:   Thu Apr 16 12:17:31 2009 +0800

    Extract mirror functionality so we can reuse it in merge

diff --git a/lib/Prophet/CLI/Command/Mirror.pm b/lib/Prophet/CLI/Command/Mirror.pm
index 89b7122..bfc7574 100644
--- a/lib/Prophet/CLI/Command/Mirror.pm
+++ b/lib/Prophet/CLI/Command/Mirror.pm
@@ -3,7 +3,7 @@ use Any::Moose;
 use Params::Validate qw/:all/;
 
 extends 'Prophet::CLI::Command';
-with 'Prophet::CLI::ProgressBar';
+with 'Prophet::CLI::MirrorCommand';
 
 has source => ( isa => 'Prophet::Replica', is => 'rw');
 has target => ( isa => 'Prophet::Replica', is => 'rw');
@@ -23,33 +23,12 @@ sub run {
         exit 1;
     }
 
-    my $target = Prophet::Replica->get_handle( url => 'prophet_cache:' . $source->uuid , app_handle => $self->app_handle );
-    $target->uuid( $source->uuid );
-    $target->resdb_replica_uuid( $source->resolution_db_handle->uuid );
-
-    if ( !$target->replica_exists && !$target->can_initialize ) {
-        die "The target replica path you specified can't be created.\n";
-    }
-
-    my %init_args = (
-        db_uuid            => $source->db_uuid,
-        replica_uuid       => $source->uuid,
-        resdb_uuid         => $source->resolution_db_handle->db_uuid,
-        resdb_replica_uuid => $source->resolution_db_handle->uuid,
-    );
-    $target->initialize(%init_args);    # XXX only do this when we need to
-    print "Mirroring resolutions from " . $source->url . "\n";
-    $target->resolution_db_handle->mirror_from(
-        source => $source->resolution_db_handle,
-        reporting_callback => $self->progress_bar( max => $source->resolution_db_handle->latest_sequence_no )
-    );
-    print "\nMirroring changesets from " . $source->url . "\n";
-    $target->mirror_from(
-        source             => $source,
-        reporting_callback => $self->progress_bar( max => $source->latest_sequence_no )
-    );
+    my $target = $self->get_mirror_for_source($source);
+    $self->sync_mirror_from_source( target=> $target, source => $source);
     print "\nDone.\n";
 }
+
+
 sub validate_args {
     my $self = shift;
     die "Please specify a --from.\n"
diff --git a/lib/Prophet/CLI/MirrorCommand.pm b/lib/Prophet/CLI/MirrorCommand.pm
new file mode 100644
index 0000000..bce57af
--- /dev/null
+++ b/lib/Prophet/CLI/MirrorCommand.pm
@@ -0,0 +1,40 @@
+package Prophet::CLI::MirrorCommand;
+use Any::Moose 'Role';
+with 'Prophet::CLI::ProgressBar';
+use Params::Validate ':all';
+
+
+
+sub get_mirror_for_source {
+    my $self = shift;
+    my ($source) = validate_pos(@_,{isa => 'Prophet::Replica'});
+    my $target = Prophet::Replica->get_handle( url => 'prophet_cache:' . $source->uuid , app_handle => $self->app_handle );
+
+    if ( !$target->replica_exists && !$target->can_initialize ) {
+        die "The target replica path you specified can't be created.\n";
+    }
+
+    $target->initialize_from_source($source);
+    return $target;
+}
+
+sub sync_mirror_from_source {
+    my $self = shift;
+    my %args = validate(@_, { target => { isa => 'Prophet::Replica::prophet_cache'}, source => { isa => 'Prophet::Replica'}});
+
+    print "Mirroring resolutions from " . $args{source}->url . "\n";
+    $args{target}->resolution_db_handle->mirror_from(
+        source => $args{source}->resolution_db_handle,
+        reporting_callback => $self->progress_bar( max => $args{source}->resolution_db_handle->latest_sequence_no )
+    );
+    print "\nMirroring changesets from " . $args{source}->url . "\n";
+    $args{target}->mirror_from(
+        source             => $args{source},
+        reporting_callback => $self->progress_bar( max => $args{source}->latest_sequence_no )
+    );
+}
+
+no Any::Moose;
+
+1;
+
diff --git a/lib/Prophet/CLI/ProgressBar.pm b/lib/Prophet/CLI/ProgressBar.pm
index 7a72d46..7a2c169 100644
--- a/lib/Prophet/CLI/ProgressBar.pm
+++ b/lib/Prophet/CLI/ProgressBar.pm
@@ -14,7 +14,6 @@ sub progress_bar {
     my $bar_count = 0;
     my $format = $args{format};
     return sub {
-        my %args = shift;
        print $bar->report(  $format,
            , ++$bar_count );
     }
diff --git a/lib/Prophet/Replica/prophet_cache.pm b/lib/Prophet/Replica/prophet_cache.pm
index d54b104..455643f 100644
--- a/lib/Prophet/Replica/prophet_cache.pm
+++ b/lib/Prophet/Replica/prophet_cache.pm
@@ -113,7 +113,6 @@ use constant can_write_records    => 0;
 sub BUILD {
     my $self = shift;
     my $args = shift;
-    warn "we're building";
     if ($self->url =~ /^prophet_cache:(.*)$/i) {
         my $uuid = $1;
         $self->uuid($uuid);
@@ -125,6 +124,19 @@ sub BUILD {
     }
 }
 
+sub initialize_from_source {
+    my $self = shift;
+    my ($source) = validate_pos(@_,{isa => 'Prophet::Replica'});
+
+
+    my %init_args = (
+        db_uuid            => $source->db_uuid,
+        replica_uuid       => $source->uuid,
+        resdb_uuid         => $source->resolution_db_handle->db_uuid,
+        resdb_replica_uuid => $source->resolution_db_handle->uuid,
+    );
+    $self->initialize(%init_args);    # XXX only do this when we need to
+}
 
 sub initialize {
     my $self = shift;

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list