]> git.donarmstrong.com Git - debbugs.git/blob - bin/debbugs-loadsql
only remove the progress bar if $p is defined
[debbugs.git] / bin / debbugs-loadsql
1 #! /usr/bin/perl
2 # debbugs-loadsql is part of debbugs, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2012 by Don Armstrong <don@donarmstrong.com>.
6
7
8 use warnings;
9 use strict;
10
11 use Getopt::Long qw(:config no_ignore_case);
12 use Pod::Usage;
13
14 =head1 NAME
15
16 debbugs-loadsql -- load debbugs sql database
17
18 =head1 SYNOPSIS
19
20 debbugs-loadsql [options]
21
22  Options:
23   --quick, -q only load changed bugs
24   --service, -s service name
25   --sysconfdir, -c postgresql service config dir
26   --spool-dir debbugs spool directory
27   --debug, -d debugging level (Default 0)
28   --help, -h display this help
29   --man, -m display manual
30
31 =head1 OPTIONS
32
33 =over
34
35 =item B<--quick, -q>
36
37 Only load changed bugs
38
39 =item B<--service,-s>
40
41 Postgreql service to use; defaults to debbugs
42
43 =item B<--sysconfdir,-c>
44
45 System configuration directory to use; if not set, defaults to the
46 postgresql default. [Operates by setting PGSYSCONFDIR]
47
48 =item B<--spool-dir>
49
50 Debbugs spool directory; defaults to the value configured in the
51 debbugs configuration file.
52
53 =item B<--debug, -d
54
55 Debug verbosity.
56
57 =item B<--help, -h>
58
59 Display brief useage information.
60
61 =item B<--man, -m>
62
63 Display this manual.
64
65 =back
66
67
68 =cut
69
70
71 use vars qw($DEBUG);
72
73 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list);
74 use Debbugs::Config qw(:config);
75 use Debbugs::Status qw(read_bug split_status_fields);
76 use Debbugs::Log;
77 use Debbugs::DB;
78 use Debbugs::DB::Load qw(load_bug handle_load_bug_queue);
79 use DateTime;
80 use File::stat;
81
82
83 my %options = (debug           => 0,
84                help            => 0,
85                man             => 0,
86                verbose         => 0,
87                quiet           => 0,
88                quick           => 0,
89                service         => 'debbugs',
90                progress        => 0,
91               );
92
93
94 GetOptions(\%options,
95            'quick|q',
96            'service|s',
97            'sysconfdir|c',
98            'progress!',
99            'spool_dir|spool-dir=s',
100            'debug|d+','help|h|?','man|m');
101
102 pod2usage() if $options{help};
103 pod2usage({verbose=>2}) if $options{man};
104
105 $DEBUG = $options{debug};
106
107 my @USAGE_ERRORS;
108 $options{verbose} = $options{verbose} - $options{quiet};
109
110 if ($options{progress}) {
111     eval "use Term::ProgressBar";
112     push @USAGE_ERRORS, "You asked for a progress bar, but Term::ProgressBar isn't installed" if $@;
113 }
114
115
116 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
117
118 if (exists $options{sysconfdir}) {
119     if (not defined $options{sysconfdir} or not length $options{sysconfdir}) {
120         delete $ENV{PGSYSCONFDIR};
121     } else {
122         $ENV{PGSYSCONFDIR} = $options{sysconfdir};
123     }
124 }
125
126 if (exists $options{spool_dir} and defined $options{spool_dir}) {
127     $config{spool_dir} = $options{spool_dir};
128 }
129 chdir($config{spool_dir}) or die "chdir $config{spool_dir} failed: $!";
130
131 my $verbose = $options{debug};
132
133 my $initialdir = "db-h";
134
135 if (defined $ARGV[0] and $ARGV[0] eq "archive") {
136     $initialdir = "archive";
137 }
138
139 if (not lockpid($config{spool_dir}.'/lock/debbugs-loadsql')) {
140      if ($options{quick}) {
141           # If this is a quick run, just exit
142           print STDERR "Another debbugs-loadsql is running; stopping\n" if $verbose;
143           exit 0;
144      }
145      print STDERR "Another debbugs-loadsql is running; stopping\n";
146      exit 1;
147 }
148
149
150 # connect to the database; figure out how to handle errors properly
151 # here.
152 my $schema = Debbugs::DB->connect('dbi:Pg:service='.$options{service}) or
153     die "Unable to connect to database: ";
154
155 my $time = 0;
156 my $start_time = time;
157
158
159 my @dirs = (@ARGV?@ARGV : $initialdir);
160 my $cnt = 0;
161 my %tags;
162 my %severities;
163 my %queue;
164 my $tot_dirs = @ARGV ? @ARGV : 0;
165 my $done_dirs = 0;
166 my $avg_subfiles = 0;
167 my $completed_files = 0;
168 my $p;
169 if ($options{progress}) {
170     $p = eval "Term::ProgressBar->new({count => 1,ETA=>q(linear)})";
171     warn "Unable to initialize progress bar: $@" if not $p;
172 }
173 while (my $dir = shift @dirs) {
174     printf "Doing dir %s ...\n", $dir if $verbose;
175
176     opendir(DIR, "$dir/.") or die "opendir $dir: $!";
177     my @subdirs = readdir(DIR);
178     closedir(DIR);
179
180     my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
181     $tot_dirs -= @dirs;
182     push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
183     $tot_dirs += @dirs;
184     if ($avg_subfiles == 0) {
185         $avg_subfiles = @list;
186     }
187
188     $p->target($avg_subfiles*($tot_dirs-$done_dirs)+$completed_files+@list) if $p;
189     $avg_subfiles = ($avg_subfiles * $done_dirs + @list) / ($done_dirs+1);
190     $done_dirs += 1;
191
192     for my $bug (@list) {
193         $completed_files++;
194         $p->update($completed_files) if $p;
195         print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
196         my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
197         if (not defined $stat) {
198             print STDERR "Unable to stat $bug $!\n";
199             next;
200         }
201         next if $stat->mtime < $time;
202         my $data = read_bug(bug => $bug,
203                             location => $initialdir);
204         eval {
205             load_bug(db => $schema,
206                      data => split_status_fields($data),
207                      tags => \%tags,
208                      severities => \%severities,
209                      queue => \%queue);
210         };
211         if ($@) {
212             use Data::Dumper;
213             print STDERR Dumper($data) if $DEBUG;
214             die "failure while trying to load bug $bug\n$@";
215         }
216     }
217 }
218 $p->remove() if $p;
219 handle_load_bug_queue(db => $schema,
220                       queue => \%queue);
221
222
223
224 __END__