]> git.donarmstrong.com Git - debbugs.git/blob - bin/add_bug_to_estraier
* Add Debbugs::SOAP::Status
[debbugs.git] / bin / add_bug_to_estraier
1 #!/usr/bin/perl
2 # add_bug_to_estraier adds a log for a bug to the estaier db, and is
3 # released under the terms of the GPL version 2, or any later version,
4 # at your option. See the file README and COPYING for more
5 # information.
6 # Copyright 2006 by Don Armstrong <don@debian.org>.
7
8
9
10 use warnings;
11 use strict;
12
13
14 use Getopt::Long;
15 use Pod::Usage;
16
17 =head1 NAME
18
19 add_bug_to_estraier
20
21 =head1 SYNOPSIS
22
23 add_bug_to_estraier [options] < list_of_bugs_to_add
24
25  Options:
26   --debug, -d debugging level (Default 0)
27   --help, -h display this help
28   --man, -m display manual
29
30 =head1 OPTIONS
31
32 =over
33
34 =item B<--url, -u>
35
36 Url to the estraier node
37
38 =item B<--user,-U>
39
40 User to log onto the estraier node
41
42 =item B<--pass,-P>
43
44 Password to log onto the estraier node
45
46 =item B<--spool,-s>
47
48 Spool location; if not set defaults to /etc/debbugs/config
49
50 =item B<--conf,-C>
51
52 Configuration file; a set of key = value pairs separated by newlines;
53 the long name of any option is the name that the configuration file
54 takes
55
56 =item B<--cron>
57
58 Descend through the spool and add all of the bugs to estraier
59
60 =item B<--timestamp>
61
62 Use the timestamp file to only add new bugs; will lock the timestamp
63 file to avoid racing with other invocations
64
65 =item B<--debug, -d>
66
67 Debug verbosity. (Default 0)
68
69 =item B<--help, -h>
70
71 Display brief useage information.
72
73 =item B<--man, -m>
74
75 Display this manual.
76
77 =back
78
79
80 =head1 EXAMPLES
81
82   test_bts --bug 7 --host donbugs.donarmstrong.com
83
84
85 =cut
86
87
88 use Debbugs::Config qw(:globals);
89 use Debbugs::Mail qw(send_mail_message);
90 use Debbugs::MIME qw(create_mime_message);
91
92 use Search::Estraier;
93 use Debbugs::Estraier qw(:add);
94 use File::Find;
95 use File::stat;
96
97 use vars qw($DEBUG $VERBOSE);
98
99 # XXX parse config file
100
101 my %options = (debug           => 0,
102                help            => 0,
103                man             => 0,
104                url             => undef,
105                user            => undef,
106                passwd          => undef,
107                spool           => undef,
108                conf            => undef,
109                cron            => 0,
110                timestamp       => undef,
111               );
112
113 GetOptions(\%options,'url|u=s','user|U=s','passwd|P=s',
114            'spool|s=s','conf|C=s','cron!','timestamp=s',
115            'debug|d+','help|h|?','man|m');
116
117 my $ERRORS = '';
118
119 if (not defined $options{conf}) {
120      $ERRORS .= "--url must be set\n" if not defined $options{url};
121      $ERRORS .= "--user must be set\n" if not defined $options{user};
122      $ERRORS .= "--passwd must be set\n" if not defined $options{passwd};
123 }
124 else {
125      # Read the conf file
126      my $conf_fh = new IO::File $options{conf},'r'
127           or die "Unable to open $options{conf} for reading";
128      while (<$conf_fh>) {
129           chomp;
130           next if /^\s*\#/;
131           my ($key,$value) = split /\s*[:=]\s*/,$_,2;
132           $options{$key} = $value if defined $key and not defined $options{$key};
133      }
134      $ERRORS .= "url must be set\n" if not defined $options{url};
135      $ERRORS .= "user must be set\n" if not defined $options{user};
136      $ERRORS .= "passwd must be set\n" if not defined $options{passwd};
137 }
138 $ERRORS .= "--spool must be set if --cron is used\n" if
139      not defined $options{spool} and $options{cron};
140 pod2usage($ERRORS) if length $ERRORS;
141
142 pod2usage() if $options{help};
143 pod2usage({verbose=>2}) if $options{man};
144
145
146 $DEBUG = $options{debug};
147
148 $Debbugs::Estraier::DEBUG = $DEBUG;
149 $VERBOSE = 0;
150
151 my $node =  new Search::Estraier::Node (url    => $options{url},
152                                         user   => $options{user},
153                                         passwd => $options{passwd},
154                                        );
155 $gSpoolDir = $options{spool} if defined $options{spool};
156
157 if ($options{cron}) {
158      my %timestamps;
159      my $start_time = time;
160      my $unlink = 0;
161      my %seen_dirs;
162      check_pid($options{timestamp});
163      # read timestamp file
164      if (defined $options{timestamp}) {
165           my $timestamp_fh = new IO::File $options{timestamp},'r' or
166                die "Unable to open timestamp $options{timestamp}: $!";
167           while (<$timestamp_fh>) {
168                chomp;
169                my ($key,$value) = split /\s+/,$_,2;
170                $timestamps{$key} = $value;
171           }
172      }
173      for my $hash (map {sprintf '%02d',$_ } 0..99) {
174           find(sub {
175                     print STDERR "Examining $_\n" if $DEBUG > 1;
176                     return if not /^(\d+)\.log$/;
177                     my $bug_num = $1;
178                     my $stat = stat $_ or next;
179                     return unless -f _;
180                     return if exists $timestamps{$File::Find::dir} and
181                          ($timestamps{$File::Find::dir} > $stat->mtime);
182                     $seen_dirs{$File::Find::dir} = $start_time;
183                     print STDERR "Adding $bug_num\n" if $DEBUG;
184                     my $max_message = 0;
185                     eval{
186                          $max_message = add_bug_log($node,$bug_num);
187                     };
188                     if ($@) {
189                          print STDERR "Adding $bug_num failed with $@\n";
190                     }
191                },
192                map {(-d "$options{spool}/$_/$hash")?
193                          "$options{spool}/$_/$hash":()}
194                qw(db-h archive),
195               );
196           # write timestamp file
197           if (defined $options{timestamp}) {
198                %timestamps = (%timestamps,%seen_dirs);
199                my $timestamp_fh = new IO::File $options{timestamp},'w' or
200                     die "Unable to open timestamp $options{timestamp}: $!";
201                foreach my $key (keys %timestamps) {
202                     print {$timestamp_fh} $key,' ',
203                          $timestamps{$key}||'',qq(\n);
204                }
205           }
206      }
207      unlink("$options{timestamp}.pid");
208 }
209 else {
210      while (my $bug_num = <STDIN>) {
211           chomp $bug_num;
212           add_bug_log($node,$bug_num);
213      }
214 }
215
216
217 sub check_pid{
218      my ($timestamp) = @_;
219      if (-e "${timestamp}.pid") {
220           my $time_fh = new IO::File  "${timestamp}.pid", 'r' or die "Unable to read pidfile";
221           local $/;
222           my $pid = <$time_fh>;
223           if (kill(0,$pid)) {
224                print STDERR "Another cron is running" and exit 0;
225           }
226      }
227      my $time_fh = new IO::File  "${timestamp}.pid", 'w' or
228           die "Unable to read pidfile";
229      print {$time_fh} $$;
230 }
231
232
233 __END__