X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=bin%2Fsup-sync;h=9c342d25693f908e26825c3d7c4348e0b5ba84ee;hb=5b8724346cfa405b1b4f5cd358595226a79f6b2c;hp=91710d47495ab263a69e03ec709657d1ee8b66ae;hpb=000c6ea6fd44bc2080fd8f766fac77c02fb69e2e;p=sup diff --git a/bin/sup-sync b/bin/sup-sync old mode 100644 new mode 100755 index 91710d4..9c342d2 --- a/bin/sup-sync +++ b/bin/sup-sync @@ -5,6 +5,8 @@ require 'rubygems' require 'trollop' require "sup" +PROGRESS_UPDATE_INTERVAL = 15 # seconds + class Float def to_s; sprintf '%.2f', self; end def to_time_s @@ -100,7 +102,7 @@ restored_state = IO.foreach opts[:restore] do |l| l =~ /^(\S+) \((.*?)\)$/ or raise "Can't read dump line: #{l.inspect}" mid, labels = $1, $2 - dump[mid] = labels.split(" ").map { |x| x.intern } + dump[mid] = labels.symbolistize end $stderr.puts "Read #{dump.size} entries from dump file." dump @@ -122,7 +124,9 @@ begin unless target == :new if opts[:start_at] - sources.each { |s| s.seek_to! opts[:start_at] } + Trollop::die :start_at, "can only be used on one source" unless sources.size == 1 + sources.first.seek_to! opts[:start_at] + sources.first.correct_offset! if sources.first.respond_to?(:correct_offset!) else sources.each { |s| s.reset! } end @@ -137,13 +141,22 @@ begin num_scanned += 1 seen[m.id] = true + if Time.now - last_info_time > PROGRESS_UPDATE_INTERVAL + last_info_time = Time.now + elapsed = last_info_time - start_time + start = opts[:start_at] || source.start_offset + pctdone = 100.0 * (source.cur_offset - start).to_f / (source.end_offset - start).to_f + remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone) + $stderr.printf "## read %dm (about %.0f%%) @ %.1fm/s. %s elapsed, about %s remaining\n", num_scanned, pctdone, num_scanned / elapsed, elapsed.to_time_s, remaining.to_time_s + end + ## skip if we're operating only on changed messages, the message ## is in the index, and it's unchanged from what the source is ## reporting. next if target == :changed && entry && entry[:source_id].to_i == source.id && entry[:source_info].to_i == offset ## get the state currently in the index - index_state = entry[:label].split(/\s+/).map { |x| x.intern } if entry + index_state = entry[:label].symbolistize if entry ## skip if we're operating on restored messages, and this one ## ain't. @@ -153,7 +166,7 @@ begin ## to default source state modification flags. m.labels -= [:inbox] if opts[:archive] m.labels -= [:unread] if opts[:read] - m.labels += opts[:extra_labels].split(/\s*,\s*/).map { |x| x.intern } if opts[:extra_labels] + m.labels += opts[:extra_labels].strip.split(/\s*,\s*/).map { |x| x.intern } if opts[:extra_labels] ## assign message labels based on the operation we're performing case op @@ -171,16 +184,16 @@ begin ## nothin! use default source labels end - if Time.now - last_info_time > 60 + if Time.now - last_info_time > PROGRESS_UPDATE_INTERVAL last_info_time = Time.now elapsed = last_info_time - start_time pctdone = source.respond_to?(:pct_done) ? source.pct_done : 100.0 * (source.cur_offset.to_f - source.start_offset).to_f / (source.end_offset - source.start_offset).to_f remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone) - $stderr.puts "## #{num_scanned} (#{pctdone}%) read; #{elapsed.to_time_s} elapsed; #{remaining.to_time_s} remaining" + $stderr.printf "## read %dm (about %.0f%%) @ %.1fm/s. %s elapsed, about %s remaining\n", num_scanned, pctdone, num_scanned / elapsed, elapsed.to_time_s, remaining.to_time_s end if index_state.nil? - puts "Adding message #{source}##{offset} with state {#{m.labels * ', '}}" if opts[:verbose] + puts "Adding message #{source}##{offset} from #{m.from} with state {#{m.labels * ', '}}" if opts[:verbose] num_added += 1 else puts "Updating message #{source}##{offset}, source #{entry[:source_id]} => #{source.id}, offset #{entry[:source_info]} => #{offset}, state {#{index_state * ', '}} => {#{m.labels * ', '}}" if opts[:verbose] @@ -200,9 +213,7 @@ begin ## API. ## ## TODO: move this to Index, i suppose. - - - if target == :all || target == :changed + if (target == :all || target == :changed) && !opts[:start_at] $stderr.puts "Deleting missing messages from the index..." num_del, num_scanned = 0, 0 sources.each do |source|