]> git.cworth.org Git - sup/blob - bin/sup-tweak-labels
Merge branch 'master' into next
[sup] / bin / sup-tweak-labels
1 #!/usr/bin/env ruby
2
3 require 'rubygems'
4 require 'trollop'
5 require "sup"
6
7 class Float
8   def to_s; sprintf '%.2f', self; end
9    def to_time_s
10      infinite? ? "unknown" : super
11    end
12 end
13
14 class Numeric
15   def to_time_s
16     i = to_i
17     sprintf "%d:%02d:%02d", i / 3600, (i / 60) % 60, i % 60
18   end
19 end
20
21 def time
22   startt = Time.now
23   yield
24   Time.now - startt
25 end
26
27 opts = Trollop::options do
28   version "sup-tweak-labels (sup #{Redwood::VERSION})"
29   banner <<EOS
30 Batch modification of message state for messages already in the index.
31
32 Usage:
33   sup-tweak-labels [options] <source>*
34
35 where <source>* is zero or more source URIs. Supported source URI schemes can
36 be seen by running "sup-add --help".
37
38 Options:
39 EOS
40   opt :add, "One or more labels (comma-separated) to add to every message from the specified sources", :type => String
41   opt :remove, "One or more labels (comma-separated) to remove from every message from the specified sources, if those labels are present", :type => String
42   opt :query, "A Sup search query", :type => String
43
44   text <<EOS
45
46 Other options:
47 EOS
48   opt :verbose, "Print message ids as they're processed."
49   opt :very_verbose, "Print message names and subjects as they're processed."
50   opt :all_sources, "Scan over all sources.", :short => :none
51   opt :dry_run, "Don't actually modify the index. Probably only useful with --verbose.", :short => "-n"
52   opt :version, "Show version information", :short => :none
53 end
54 opts[:verbose] = true if opts[:very_verbose]
55
56 add_labels = (opts[:add] || "").split(",").map { |l| l.intern }.uniq
57 remove_labels = (opts[:remove] || "").split(",").map { |l| l.intern }.uniq
58
59 Trollop::die "nothing to do: no labels to add or remove" if add_labels.empty? && remove_labels.empty?
60
61 Redwood::start
62 begin
63   index = Redwood::Index.new
64   index.load
65
66   source_ids = 
67     if opts[:all_sources]
68       index.sources
69     else
70       ARGV.map do |uri|
71         index.source_for uri or Trollop::die "Unknown source: #{uri}. Did you add it with sup-add first?"
72       end
73   end.map { |s| s.id }
74   Trollop::die "nothing to do: no sources" if source_ids.empty?
75
76   query = "+(" + source_ids.map { |id| "source_id:#{id}" }.join(" OR ") + ")"
77   if add_labels.empty?
78     ## if all we're doing is removing labels, we can further restrict the
79     ## query to only messages with those labels
80     query += " +(" + remove_labels.map { |l| "label:#{l}" }.join(" ") + ")"
81   end
82   query += ' ' + opts[:query] if opts[:query]
83
84   qobj, opts = Redwood::Index.parse_user_query_string query
85   query = Redwood::Index.build_query opts.merge(:qobj => qobj)
86
87   results = index.ferret.search query, :limit => :all
88   num_total = results.total_hits
89
90   $stderr.puts "Found #{num_total} documents across #{source_ids.length} sources. Scanning..."
91
92   num_changed = num_scanned = 0
93   last_info_time = start_time = Time.now
94   results.hits.each do |hit|
95     num_scanned += 1
96     id = hit.doc
97
98     m = index.build_message id
99     old_labels = m.labels.clone
100
101     m.labels += add_labels
102     m.labels -= remove_labels
103     m.labels = m.labels.uniq
104
105     unless m.labels.sort_by { |s| s.to_s } == old_labels.sort_by { |s| s.to_s }
106       num_changed += 1
107       puts "From #{m.from}, subject: #{m.subj}" if opts[:very_verbose]
108       puts "#{m.id}: {#{old_labels.join ','}} => {#{m.labels.join ','}}" if opts[:verbose]
109       puts if opts[:very_verbose]
110       index.sync_message m unless opts[:dry_run]
111     end
112
113     if Time.now - last_info_time > 60
114       last_info_time = Time.now
115       elapsed = last_info_time - start_time
116       pctdone = 100.0 * num_scanned.to_f / num_total.to_f
117       remaining = (100.0 - pctdone) * (elapsed.to_f / pctdone)
118       $stderr.puts "## #{num_scanned} (#{pctdone}%) read; #{elapsed.to_time_s} elapsed; #{remaining.to_time_s} remaining"
119     end
120   end
121   $stderr.puts "Scanned #{num_scanned} / #{num_total} messages and changed #{num_changed}."
122
123   unless num_changed == 0
124     $stderr.puts "Optimizing index..."
125     index.ferret.optimize unless opts[:dry_run]
126   end
127
128 rescue Exception => e
129   File.open("sup-exception-log.txt", "w") { |f| f.puts e.backtrace }
130   raise
131 ensure
132   index.save
133   Redwood::finish
134   index.unlock
135 end
136