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
6 # Copyright 2006 by Don Armstrong <don@debian.org>.
23 add_bug_to_estraier [options] < list_of_bugs_to_add
26 --debug, -d debugging level (Default 0)
27 --help, -h display this help
28 --man, -m display manual
36 Url to the estraier node
40 User to log onto the estraier node
44 Password to log onto the estraier node
48 Spool location; if not set defaults to /etc/debbugs/config
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
58 Descend through the spool and add all of the bugs to estraier
62 Use the timestamp file to only add new bugs; will lock the timestamp
63 file to avoid racing with other invocations
67 Debug verbosity. (Default 0)
71 Display brief useage information.
82 test_bts --bug 7 --host donbugs.donarmstrong.com
88 use Debbugs::Config qw(:globals);
89 use Debbugs::Mail qw(send_mail_message);
90 use Debbugs::MIME qw(create_mime_message);
93 use Debbugs::Estraier qw(:add);
97 use vars qw($DEBUG $VERBOSE);
99 # XXX parse config file
101 my %options = (debug => 0,
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');
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};
126 my $conf_fh = new IO::File $options{conf},'r'
127 or die "Unable to open $options{conf} for reading";
131 my ($key,$value) = split /\s*[:=]\s*/,$_,2;
132 $options{$key} = $value if defined $key and not defined $options{$key};
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};
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;
142 pod2usage() if $options{help};
143 pod2usage({verbose=>2}) if $options{man};
146 $DEBUG = $options{debug};
148 $Debbugs::Estraier::DEBUG = $DEBUG;
151 my $node = new Search::Estraier::Node (url => $options{url},
152 user => $options{user},
153 passwd => $options{passwd},
155 $gSpoolDir = $options{spool} if defined $options{spool};
157 if ($options{cron}) {
159 my $start_time = time;
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>) {
169 my ($key,$value) = split /\s+/,$_,2;
170 $timestamps{$key} = $value;
173 for my $hash (map {sprintf '%02d',$_ } 0..99) {
175 print STDERR "Examining $_\n" if $DEBUG > 1;
176 return if not /^(\d+)\.log$/;
178 my $stat = stat $_ or next;
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;
186 $max_message = add_bug_log($node,$bug_num);
189 print STDERR "Adding $bug_num failed with $@\n";
192 map {(-d "$options{spool}/$_/$hash")?
193 "$options{spool}/$_/$hash":()}
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);
207 unlink("$options{timestamp}.pid");
210 while (my $bug_num = <STDIN>) {
212 add_bug_log($node,$bug_num);
218 my ($timestamp) = @_;
219 if (-e "${timestamp}.pid") {
220 my $time_fh = new IO::File "${timestamp}.pid", 'r' or die "Unable to read pidfile";
222 my $pid = <$time_fh>;
224 print STDERR "Another cron is running" and exit 0;
227 my $time_fh = new IO::File "${timestamp}.pid", 'w' or
228 die "Unable to read pidfile";