X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=bin%2Fsup-sync;h=5e89dac9cd8c479555a5f0b486a350a90bd438fd;hb=e127eef693956080b06bc8c0157071aa0c8549cb;hp=4a0482a9951e6967999ecf5b56e262edf3f77304;hpb=d66683c3375b48810d8f6ca49805943f9997c84f;p=sup diff --git a/bin/sup-sync b/bin/sup-sync index 4a0482a..5e89dac 100755 --- a/bin/sup-sync +++ b/bin/sup-sync @@ -9,9 +9,7 @@ PROGRESS_UPDATE_INTERVAL = 15 # seconds class Float def to_s; sprintf '%.2f', self; end - def to_time_s - infinite? ? "unknown" : super - end + def to_time_s; infinite? ? "unknown" : super end end class Numeric @@ -21,6 +19,10 @@ class Numeric end end +class Set + def to_s; to_a * ',' end +end + def time startt = Time.now yield @@ -54,10 +56,10 @@ by running "sup-add --help". Options controlling WHICH messages sup-sync operates on: EOS opt :new, "Operate on new messages only. Don't scan over the entire source. (Default.)", :short => :none - opt :changed, "Scan over the entire source for messages that have been deleted, altered, or moved from another source. (In the case of mbox sources, this includes all messages AFTER an altered message.)" + opt :changed, "Scan over the entire source for messages that have been deleted, altered, or moved from another source." opt :restored, "Operate only on those messages included in a dump file as specified by --restore which have changed state." opt :all, "Operate on all messages in the source, regardless of newness or changedness." - opt :start_at, "For --changed and --all, start at a particular offset.", :type => :int + opt :start_at, "For --changed, --restored and --all, start at a particular offset.", :type => :int text < :none opt :archive, "When using the default source state, mark messages as archived.", :short => "-x" opt :read, "When using the default source state, mark messages as read." - opt :extra_labels, "When using the default source state, also apply these user-defined labels. Should be a comma-separated list.", :type => String, :short => :none + opt :extra_labels, "When using the default source state, also apply these user-defined labels (a comma-separated list)", :default => "", :short => :none text < true do |m, offset, entry| + Redwood::PollManager.each_message_from source do |m| 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 + old_m = index.build_message m.id + + case target + when :changed + ## skip this message 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 old_m && old_m.source.id == m.source.id && old_m.source_info == m.source_info + when :restored + ## skip if we're operating on restored messages, and this one + ## ain't (or we wouldn't be making a change) + next unless old_m && restored_state[m.id] && restored_state[m.id] != old_m.labels + when :new + ## nothing to do; we'll consider all messages starting at the start offset, which + ## hasn't been changed. + when :all + ## nothing to do; we'll consider all messages starting at the start offset, which + ## was reset to the beginning above. 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].symbolistize if entry - - ## skip if we're operating on restored messages, and this one - ## ain't. - next if target == :restored && (!restored_state[m.id] || (index_state && restored_state[m.id].sort_by { |s| s.to_s } == index_state.sort_by { |s| s.to_s })) - - ## m.labels is the default source labels. tweak these according - ## to default source state modification flags. - m.labels -= [:inbox] if opts[:archive] - m.labels -= [:unread] if opts[:read] - 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 - when :asis - m.labels = ((m.labels - [:unread, :inbox]) + index_state).uniq if index_state - when :restore - ## if the entry exists on disk - if restored_state[m.id] - m.labels = restored_state[m.id] + ## tweak source labels according to commandline arguments if necessary + m.labels.delete :inbox if opts[:archive] + m.labels.delete :unread if opts[:read] + m.labels += opts[:extra_labels].to_set_of_symbols(",") + + ## decide what to do based on message labels and the operation we're performing + dothis, new_labels = case + when (op == :restore) && restored_state[m.id] + if old_m && (old_m.labels != restored_state[m.id]) + num_restored += 1 + [:update_message_state, restored_state[m.id]] + elsif old_m.nil? num_restored += 1 - elsif index_state - m.labels = index_state + m.labels = restored_state[m.id] + :add_message + else + # labels are the same; don't do anything + end + when op == :discard + if old_m && (old_m.labels != m.labels) + [:update_message_state, m.labels] + else + # labels are the same; don't do anything + end + else + ## duplicate behavior of poll mode: if index_state is non-nil, this is a newer + ## version of an older message, so merge in any new labels except :unread and + ## :inbox. + ## + ## TODO: refactor such that this isn't duplicated + if old_m + m.labels = old_m.labels + (m.labels - [:unread, :inbox]) + :update_message + else + :add_message end - when :discard - ## nothin! use default source labels end - if index_state.nil? - puts "Adding message #{source}##{offset} from #{m.from} with state {#{m.labels * ', '}}" if opts[:verbose] + ## now, actually do the operation + case dothis + when :add_message + puts "Adding new message #{source}##{m.source_info} with labels #{m.labels}" if opts[:verbose] + index.add_message m unless opts[:dry_run] 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] + when :update_message + puts "Updating message #{source}##{m.source_info}; labels #{old_m.labels} => #{m.labels}; offset #{old_m.source_info} => #{m.source_info}" if opts[:verbose] + index.update_message m unless opts[:dry_run] + num_updated += 1 + when :update_message_state + puts "Changing flags for #{source}##{m.source_info} from #{m.labels} to #{new_labels}" if opts[:verbose] + m.labels = new_labels + index.update_message_state m unless opts[:dry_run] num_updated += 1 end - opts[:dry_run] ? nil : m + 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) + 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 end - $stderr.puts "Scanned #{num_scanned}, added #{num_added}, updated #{num_updated} messages from #{source}." - $stderr.puts "Restored state on #{num_restored} (#{100.0 * num_restored / num_scanned}%) messages." if num_restored > 0 + + puts "Scanned #{num_scanned}, added #{num_added}, updated #{num_updated} messages from #{source}." + puts "Restored state on #{num_restored} (#{100.0 * num_restored / num_scanned}%) messages." if num_restored > 0 end ## delete any messages in the index that claim they're from one of ## these sources, but that we didn't see. - ## - ## kinda crappy code here, because we delve directly into the Ferret - ## API. - ## - ## TODO: move this to Index, i suppose. - if (target == :all || target == :changed) && !opts[:start_at] - $stderr.puts "Deleting missing messages from the index..." + if (target == :all || target == :changed) + puts "Deleting missing messages from the index..." num_del, num_scanned = 0, 0 sources.each do |source| raise "no source id for #{source}" unless source.id - q = "+source_id:#{source.id}" - q += " +source_info: >= #{opts[:start_at]}" if opts[:start_at] - index.index.search_each(q, :limit => :all) do |docid, score| + index.each_message :source_id => source.id, :load_spam => true, :load_deleted => true, :load_killed => true do |m| num_scanned += 1 - mid = index.index[docid][:message_id] - unless seen[mid] - puts "Deleting #{mid}" if opts[:verbose] - index.index.delete docid unless opts[:dry_run] + unless seen[m.id] + next unless m.source_info >= opts[:start_at] if opts[:start_at] + puts "Deleting #{m.id}" if opts[:verbose] + index.delete m.id unless opts[:dry_run] num_del += 1 end end end - $stderr.puts "Deleted #{num_del} / #{num_scanned} messages" + puts "Deleted #{num_del} / #{num_scanned} messages" end index.save if opts[:optimize] - $stderr.puts "Optimizing index..." - optt = time { index.index.optimize unless opts[:dry_run] } - $stderr.puts "Optimized index of size #{index.size} in #{optt}s." + puts "Optimizing index..." + optt = time { index.optimize unless opts[:dry_run] } + puts "Optimized index of size #{index.size} in #{optt}s." end rescue Redwood::FatalSourceError => e $stderr.puts "Sorry, I couldn't communicate with a source: #{e.message}"