9 def to_s; sprintf '%.2f', self; end
11 infinite? ? "unknown" : super
18 sprintf "%d:%02d:%02d", i / 3600, (i / 60) % 60, i % 60
28 opts = Trollop::options do
29 version "sup-tweak-labels (sup #{Redwood::VERSION})"
31 Batch modification of message state for messages already in the index.
34 sup-tweak-labels [options] <source>*
36 where <source>* is zero or more source URIs. Supported source URI schemes can
37 be seen by running "sup-add --help".
41 opt :add, "One or more labels (comma-separated) to add to every message from the specified sources", :default => ""
42 opt :remove, "One or more labels (comma-separated) to remove from every message from the specified sources, if those labels are present", :default => ""
43 opt :query, "A Sup search query", :type => String
49 opt :verbose, "Print message ids as they're processed."
50 opt :very_verbose, "Print message names and subjects as they're processed."
51 opt :all_sources, "Scan over all sources.", :short => :none
52 opt :dry_run, "Don't actually modify the index. Probably only useful with --verbose.", :short => "-n"
53 opt :version, "Show version information", :short => :none
55 opts[:verbose] = true if opts[:very_verbose]
57 add_labels = opts[:add].to_set_of_symbols ","
58 remove_labels = opts[:remove].to_set_of_symbols ","
60 Trollop::die "nothing to do: no labels to add or remove" if add_labels.empty? && remove_labels.empty?
61 Trollop::die "no sources specified" if ARGV.empty?
64 index = Redwood::Index.init
65 index.lock_interactively or exit
69 source_ids = if opts[:all_sources]
70 Redwood::SourceManager.sources
73 Redwood::SourceManager.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
76 Trollop::die "nothing to do: no sources" if source_ids.empty?
78 query = "+(" + source_ids.map { |id| "source_id:#{id}" }.join(" OR ") + ")"
80 ## if all we're doing is removing labels, we can further restrict the
81 ## query to only messages with those labels
82 query += " +(" + remove_labels.map { |l| "label:#{l}" }.join(" ") + ")"
84 query += ' ' + opts[:query] if opts[:query]
86 parsed_query = index.parse_query query
87 parsed_query.merge! :load_spam => true, :load_deleted => true, :load_killed => true
88 ids = Enumerable::Enumerator.new(index, :each_id, parsed_query).map
91 $stderr.puts "Found #{num_total} documents across #{source_ids.length} sources. Scanning..."
93 num_changed = num_scanned = 0
94 last_info_time = start_time = Time.now
98 m = index.build_message id
99 old_labels = m.labels.dup
101 m.labels += add_labels
102 m.labels -= remove_labels
104 unless m.labels == old_labels
106 puts "From #{m.from}, subject: #{m.subj}" if opts[:very_verbose]
107 puts "#{m.id}: {#{old_labels.to_a.join ','}} => {#{m.labels.to_a.join ','}}" if opts[:verbose]
108 puts if opts[:very_verbose]
109 index.update_message_state m unless opts[:dry_run]
112 if Time.now - last_info_time > 60
113 last_info_time = Time.now
114 elapsed = last_info_time - start_time
115 pctdone = 100.0 * num_scanned.to_f / num_total.to_f
116 remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone)
117 $stderr.puts "## #{num_scanned} (#{pctdone}%) read; #{elapsed.to_time_s} elapsed; #{remaining.to_time_s} remaining"
120 $stderr.puts "Scanned #{num_scanned} / #{num_total} messages and changed #{num_changed}."
122 unless num_changed == 0
123 $stderr.puts "Optimizing index..."
124 index.optimize unless opts[:dry_run]
127 rescue Exception => e
128 File.open("sup-exception-log.txt", "w") { |f| f.puts e.backtrace }