]> git.donarmstrong.com Git - debbugs.git/blob - bin/add_bug_to_estraier
* Add a bin directory
[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::Mail qw(send_mail_message);
89 use Debbugs::MIME qw(create_mime_message);
90
91 use Search::Estraier;
92 use Debbugs::Estraier qw(:add);
93 use File::Find;
94
95 use vars qw($DEBUG $VERBOSE);
96
97 # XXX parse config file
98
99 my %options = (debug           => 0,
100                help            => 0,
101                man             => 0,
102                url             => undef,
103                user            => undef,
104                passwd          => undef,
105                spool           => undef,
106                conf            => undef,
107                cron            => 0,
108                timestamp       => undef,
109               );
110
111 GetOptions(\%options,'url|u=s','user|U=s','passwd|P=s',
112            'spool|s=s','conf|C=s','cron!','timestamp=s',
113            'debug|d+','help|h|?','man|m');
114
115 my $ERRORS = '';
116
117 if (not defined $options{conf}) {
118      $ERRORS .= "--url must be set\n" if not defined $options{url};
119      $ERRORS .= "--user must be set\n" if not defined $options{user};
120      $ERRORS .= "--passwd must be set\n" if not defined $options{passwd};
121 }
122 else {
123      # Read the conf file
124      my $conf_fh = new IO::File $options{conf},'r'
125           or die "Unable to open $options{conf} for reading";
126      while (<$conf_fh>) {
127           chomp;
128           next if /^\s*\#/;
129           my ($key,$value) = split /\s*[:=]\s*/,$_,2;
130           $options{$key} = $value if not defined $options{$key};
131      }
132      $ERRORS .= "url must be set\n" if not defined $options{url};
133      $ERRORS .= "user must be set\n" if not defined $options{user};
134      $ERRORS .= "passwd must be set\n" if not defined $options{passwd};
135 }
136 $ERRORS .= "--spool must be set if --cron is used\n" if
137      not defined $options{spool} and $options{cron};
138 pod2usage($ERRORS) if length $ERRORS;
139
140 pod2usage() if $options{help};
141 pod2usage({verbose=>2}) if $options{man};
142
143
144 $DEBUG = $options{debug};
145
146 $VERBOSE = 0;
147
148 my $node =  new Search::Estraier::Node (url    => $options{url},
149                                         user   => $options{user},
150                                         passwd => $options{passwd},
151                                        );
152 $Debbugs::Config::gSpoolDir = $options{spool} if defined $options{spool};
153
154 if ($options{cron}) {
155      my %timestamps;
156      my $start_time = time;
157      my %seen_dirs;
158      # read timestamp file
159      if (defined $options{timestamp}) {
160           my $timestamp_fh = new IO::File $options{timestamp},'r' or
161                die "Unable to open timestamp $options{timestamp}: $!";
162           while (<$timestamp_fh>) {
163                chomp;
164                my ($key,$value) = split /\s+/,$_,2;
165                $timestamps{$key} = $value;
166           }
167      }
168      find(sub {
169                print STDERR "Examining $_\n" if $DEBUG;
170                return if not /^(\d+)\.log$/;
171                my $bug_num = $1;
172                my $stat = stat $_ or next;
173                return unless -f _;
174                return if exists $timestamps{$File::Find::dir} and
175                     $timestamps{$File::Find::dir} > $stat->mtime;
176                $seen_dirs{$File::Find::dir} = $start_time;
177                add_bug_log($node,$1);
178           },
179           map {(-d "$options{spool}/$_")?"$options{spool}/$_":()}
180           qw(db-h archived db),
181          );
182      # write timestamp file
183      if (defined $options{timestamp}) {
184           %timestamps = (%timestamps,%seen_dirs);
185           my $timestamp_fh = new IO::File $options{timestamp},'w' or
186                die "Unable to open timestamp $options{timestamp}: $!";
187           foreach my $key (keys %timestamps) {
188                print {$timestamp_fh} $key,' ',
189                     $timestamps{$key}||'',qq(\n);
190           }
191      }
192 }
193 else {
194      while (my $bug_num = <STDIN>) {
195           chomp $bug_num;
196           add_bug_log($node,$bug_num);
197      }
198 }
199
200
201
202 __END__