From 970bbb3887e4d1ca89ee1ca117defaa85c6845e7 Mon Sep 17 00:00:00 2001 From: Olivier Blin Date: Tue, 28 Oct 2008 23:47:21 +0000 Subject: extract progress code in a new MDV::Draklive::Progress object --- draklive | 37 +++++++------------------------------ lib/MDV/Draklive/Progress.pm | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 lib/MDV/Draklive/Progress.pm diff --git a/draklive b/draklive index 4dbcea6..3066715 100755 --- a/draklive +++ b/draklive @@ -28,7 +28,6 @@ use fs; use modules; use detect_devices; use run_program; -use POSIX qw(strftime); use Cwd 'getcwd'; use Getopt::Long; use Pod::Usage; @@ -38,6 +37,7 @@ use MDV::Draklive::Utils; use MDV::Draklive::Live; use MDV::Draklive::Media; use MDV::Draklive::StorageFS; +use MDV::Draklive::Progress; my $dir_distrib_sqfs = { mountpoint => '/distrib', @@ -184,29 +184,6 @@ sub nls_modules { if_($fs !~ /^ext/, map { "nls_$_" } (map { "iso8859_$_" } 1..7, 9, 13..15), 'utf8'); } -sub progress_start { - my ($total, $time, $o_exp_divide) = @_; - { - total => $total, - current => 0, - start_time => $time, - exp_divide => $o_exp_divide, - maxl => length($total) - $o_exp_divide, - }; -} - -sub progress_show { - my ($progress, $time) = @_; - my $elapsed_time = $time - $progress->{start_time}; - my $eta = $progress->{current} ? int($elapsed_time*$progress->{total}/$progress->{current}) : -1; - printf("\r%3d%% (%$progress->{maxl}s/%-$progress->{maxl}s), %8s/%8s (ETA)", - int(100*$progress->{current}/$progress->{total}), - (map { substr($_, 0, length($_)-$progress->{exp_divide}) } $progress->{current}, $progress->{total}), - (map { POSIX::strftime("%H:%M:%S", gmtime($_)) } $elapsed_time, $eta)); -} - -sub progress_end() { print "\n" } - my $loop_number = 0; my %loop; %loop = ( @@ -223,14 +200,14 @@ my %loop; my $src = $root . $dir->{build_from}; my $total = directory_usage($src); print "have to process " . int($total/1000000) . " MB\n"; - my $progress = progress_start($total, time(), 6); + my $progress = MDV::Draklive::Progress->new($total, time(), 6); my $exclude_file = tmpnam(); output_p($exclude_file, map { $root . "$_\n" } grep { -e $root . $_ } @{$dir->{exclude} || []}); my $sort = $live->{settings}{config_root} . '/' . $dir->{sort}; run_foreach(sub { if (/^mksquashfs: file .*, uncompressed size (\d+) bytes\s*(?:DUPLICATE|LINK)?$/) { $progress->{current} += $1; - progress_show($progress, time()); + $progress->show(time()); } }, 'mksquashfs', $src, $dest, '-noappend', '-no-progress', '-info', '-lzma', @@ -238,7 +215,7 @@ my %loop; '-ef', $exclude_file, if_(-f $sort, '-sort', $sort), ) or die "unable to run mksquashfs\n"; - progress_end(); + $progress->end; unlink $exclude_file; }, mount => sub { @@ -1103,13 +1080,13 @@ sub create_cdrom_master { $dest = get_cdrom_master_path($live, $opts); mkdir_p(dirname($dest)); } - my $progress = progress_start(100, time()); + my $progress = MDV::Draklive::Progress->new(100, time()); my $in_progress; autoflush STDOUT 1; run_foreach(sub { if (/^\s*([0-9.]+)%\s*done,/) { $progress->{current} = int($1); - progress_show($progress, time()); + $progress->show(time()); $in_progress = 1; } else { print "\n" if $in_progress; @@ -1143,7 +1120,7 @@ sub create_cdrom_master { ), ) or die "unable to run genisoimage\n"; autoflush STDOUT 0; - progress_end(); + $progress->end; if ($dest) { my $dir = dirname($dest); my $filename = basename($dest); diff --git a/lib/MDV/Draklive/Progress.pm b/lib/MDV/Draklive/Progress.pm new file mode 100644 index 0000000..128ffe3 --- /dev/null +++ b/lib/MDV/Draklive/Progress.pm @@ -0,0 +1,31 @@ +package MDV::Draklive::Progress; + +use POSIX qw(strftime); + +sub new { + my ($class, $total, $time, $o_exp_divide) = @_; + bless { + total => $total, + current => 0, + start_time => $time, + exp_divide => $o_exp_divide, + maxl => length($total) - $o_exp_divide, + }, $class; +} + +sub show { + my ($progress, $time) = @_; + my $elapsed_time = $time - $progress->{start_time}; + my $eta = $progress->{current} ? int($elapsed_time*$progress->{total}/$progress->{current}) : -1; + printf("\r%3d%% (%$progress->{maxl}s/%-$progress->{maxl}s), %8s/%8s (ETA)", + int(100*$progress->{current}/$progress->{total}), + (map { substr($_, 0, length($_)-$progress->{exp_divide}) } $progress->{current}, $progress->{total}), + (map { POSIX::strftime("%H:%M:%S", gmtime($_)) } $elapsed_time, $eta)); +} + +sub end { + my ($_progress) = @_; + print "\n"; +} + +1; -- cgit v1.2.1