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::Mail qw(send_mail_message);
89 use Debbugs::MIME qw(create_mime_message);
92 use Debbugs::Estraier qw(:add);
96 use vars qw($DEBUG $VERBOSE);
98 # XXX parse config file
100 my %options = (debug => 0,
112 GetOptions(\%options,'url|u=s','user|U=s','passwd|P=s',
113 'spool|s=s','conf|C=s','cron!','timestamp=s',
114 'debug|d+','help|h|?','man|m');
118 if (not defined $options{conf}) {
119 $ERRORS .= "--url must be set\n" if not defined $options{url};
120 $ERRORS .= "--user must be set\n" if not defined $options{user};
121 $ERRORS .= "--passwd must be set\n" if not defined $options{passwd};
125 my $conf_fh = new IO::File $options{conf},'r'
126 or die "Unable to open $options{conf} for reading";
130 my ($key,$value) = split /\s*[:=]\s*/,$_,2;
131 $options{$key} = $value if defined $key and not defined $options{$key};
133 $ERRORS .= "url must be set\n" if not defined $options{url};
134 $ERRORS .= "user must be set\n" if not defined $options{user};
135 $ERRORS .= "passwd must be set\n" if not defined $options{passwd};
137 $ERRORS .= "--spool must be set if --cron is used\n" if
138 not defined $options{spool} and $options{cron};
139 pod2usage($ERRORS) if length $ERRORS;
141 pod2usage() if $options{help};
142 pod2usage({verbose=>2}) if $options{man};
145 $DEBUG = $options{debug};
147 $Debbugs::Estraier::DEBUG = $DEBUG;
150 my $node = new Search::Estraier::Node (url => $options{url},
151 user => $options{user},
152 passwd => $options{passwd},
154 $Debbugs::Config::gSpoolDir = $options{spool} if defined $options{spool};
156 if ($options{cron}) {
158 my $start_time = time;
161 check_pid($options{timestamp});
162 # read timestamp file
163 if (defined $options{timestamp}) {
164 my $timestamp_fh = new IO::File $options{timestamp},'r' or
165 die "Unable to open timestamp $options{timestamp}: $!";
166 while (<$timestamp_fh>) {
168 my ($key,$value) = split /\s+/,$_,2;
169 $timestamps{$key} = $value;
172 for my $hash (map {sprintf '%02d',$_ } 0..99) {
174 print STDERR "Examining $_\n" if $DEBUG > 1;
175 return if not /^(\d+)\.log$/;
177 my $stat = stat $_ or next;
179 return if exists $timestamps{$File::Find::dir} and
180 ($timestamps{$File::Find::dir} > $stat->mtime);
181 $seen_dirs{$File::Find::dir} = $start_time;
182 print STDERR "Adding $bug_num\n" if $DEBUG;
184 add_bug_log($node,$bug_num);
187 print STDERR "Adding $bug_num failed with $@\n";
190 map {(-d "$options{spool}/$_/$hash")?
191 "$options{spool}/$_/$hash":()}
194 # write timestamp file
195 if (defined $options{timestamp}) {
196 %timestamps = (%timestamps,%seen_dirs);
197 my $timestamp_fh = new IO::File $options{timestamp},'w' or
198 die "Unable to open timestamp $options{timestamp}: $!";
199 foreach my $key (keys %timestamps) {
200 print {$timestamp_fh} $key,' ',
201 $timestamps{$key}||'',qq(\n);
205 unlink("$options{timestamp}.pid");
208 while (my $bug_num = <STDIN>) {
210 add_bug_log($node,$bug_num);
216 my ($timestamp) = @_;
217 if (-e "${timestamp}.pid") {
218 my $time_fh = new IO::File "${timestamp}.pid", 'r' or die "Unable to read pidfile";
220 my $pid = <$time_fh>;
222 print STDERR "Another cron is running" and exit 0;
225 my $time_fh = new IO::File "${timestamp}.pid", 'w' or
226 die "Unable to read pidfile";