/[collab-qa]/udd/udd/bugs_gatherer.pl
ViewVC logotype

Contents of /udd/udd/bugs_gatherer.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1264 - (show annotations) (download)
Wed Sep 10 22:18:10 2008 UTC (4 years, 8 months ago) by kroeckx
File MIME type: text/plain
File size: 9833 byte(s)
Make all inserts reference the fields they want to insert into.
1 #!/usr/bin/perl -w
2 # Last-Modified: <Mon Aug 18 14:29:47 2008>
3
4 use strict;
5 use warnings;
6
7 use FindBin '$Bin';
8
9 # We need our own copy of Debbugs::Status for now
10 use lib $Bin, qw{/org/udd.debian.net/mirrors/bugs.debian.org/perl};
11
12 use DBI;
13 use DBI qw{:sql_types};
14 use YAML::Syck;
15 use Time::Local;
16
17 use Debbugs::Bugs qw{get_bugs};
18 use Debbugs::Status qw{read_bug get_bug_status bug_presence};
19 use Debbugs::Packages qw{binarytosource};
20 use Debbugs::Config qw{:globals};
21 use Debbugs::User;
22 #use Debbugs::User qw{read_usertags};
23
24 $YAML::Syck::ImplicitTyping = 1;
25
26 #Used for measuring time
27 our $t;
28 our $timing = 0;
29
30 # Return the list of usernames
31 sub get_bugs_users {
32 my $topdir = "$gSpoolDir/user";
33 my @ret = ();
34 # see Debbugs::User::filefromemail for why 0...6
35 for(my $i = 0; $i < 7; $i++) {
36 my $dir = "$topdir/$i";
37 opendir DIR, $dir or die "Can't open dir $dir: $!";
38 # Replace all occurences of %dd with the corresponding
39 # character represented by dd, where dd is a hexadecimal
40 # number
41 push @ret, map { s/%(..)/chr(hex($1))/ge; $_ } readdir DIR;
42 }
43 return @ret;
44 }
45
46 sub parse_time {
47 if(shift =~ /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
48 return ($1, $2, $3, $4, $5, $6);
49 }
50 return undef;
51 }
52
53
54 sub get_db_max_last_modified {
55 my $dbh = shift or die "Argument required";
56 my $sth = $dbh->prepare("SELECT MAX (last_modified) FROM bugs");
57 $sth->execute() or die $!;
58 my $date = $sth->fetchrow_array();
59 if(defined $date) {
60 my ($year, $month, $day, $hour, $minute, $second) = parse_time($date);
61 return timelocal($second, $minute, $hour, $day, $month-1, $year);
62 } else {
63 return 0;
64 }
65 }
66
67 sub get_mtime {
68 return ((stat(shift))[9]);
69 }
70
71 sub get_modified_bugs {
72 my $prune_stamp = shift;
73 die "Argument required" unless defined $prune_stamp;
74 my $top_dir = $gSpoolDir;
75 my @result = ();
76 foreach my $sub (qw(archive db-h)) {
77 my $spool = "$top_dir/$sub";
78 foreach my $subsub (glob "$spool/*") {
79 if( -d $subsub and get_mtime($subsub) > $prune_stamp ) {
80 push @result,
81 map { s{.*/(.*)\.log}{$1}; $_ }
82 grep { get_mtime("$_") > $prune_stamp }
83 glob "$subsub/*.log";
84 }
85 }
86 }
87 return \@result;
88 }
89
90 sub without_duplicates {
91 my %h = ();
92 return (grep { ($h{$_}++ == 0) || 0 } @_);
93 }
94
95 sub run_usertags {
96 my ($config, $source, $dbh) = @_;
97 my %src_config = %{$config->{$source}};
98 my $table = $src_config{'usertags-table'} or die "usertags-table not specified for source $source";
99 our $timing;
100 our $t;
101
102
103 $t = time();
104 # Free usertags table
105 $dbh->do("DELETE FROM $table") or die
106 "Couldn't empty $table: $!";
107 print "Deleting usertags: ",(time() - $t),"s\n" if $timing;
108 $t = time();
109 # read and insert user tags
110 my @users = get_bugs_users();
111 foreach my $user (@users) {
112 #read_usertags(\%tags, $user);
113 my $u = Debbugs::User->new($user);
114 my %tags = %{$u->{tags}};
115 $user = $dbh->quote($user);
116 foreach my $tag (keys %tags) {
117 my $qtag = $dbh->quote($tag);
118 map { $dbh->do("INSERT INTO $table (email, tag, id) VALUES ($user, $qtag, $_)") or die $! } @{$tags{$tag}};
119 }
120 }
121 }
122
123 sub run {
124 my ($config, $source, $dbh) = @_;
125
126 our $t;
127 our $timing;
128
129 run_usertags($config, $source, $dbh);
130 print "Inserting usertags: ",(time() - $t),"s\n" if $timing;
131 $t = time();
132
133 my %src_config = %{$config->{$source}};
134 my $table = $src_config{table};
135 my $archived_table = $src_config{'archived-table'};
136
137 my @modified_bugs;
138
139 if($src_config{archived}) {
140 # some bugs (the unarchived ones) are in both list. exclude them.
141 my %unarchived;
142 foreach my $b (get_bugs()) {
143 $unarchived{$b} = 1;
144 }
145 foreach my $b (get_bugs(archive => 1)) {
146 push(@modified_bugs, $b) if not $unarchived{$b};
147 }
148 } else {
149 @modified_bugs = get_bugs();
150 }
151 my @modified_bugs2;
152 if ($src_config{debug}) {
153 print "Running in debug mode with restricted bug list!!\n";
154 foreach my $b (@modified_bugs) {
155 push(@modified_bugs2, $b) if ($b =~ /58$/);
156 }
157 @modified_bugs = @modified_bugs2;
158 }
159
160 print "Fetching list of ",scalar(@modified_bugs), " bugs to insert: ",(time() - $t),"s\n" if $timing;
161 $t = time();
162
163 foreach my $prefix ($table, $archived_table) {
164 foreach my $postfix (qw{_merged_with _found_in _fixed_in _tags}, '') {
165 my $sth = $dbh->prepare("DELETE FROM $prefix$postfix WHERE id = \$1");
166 map {
167 $sth->execute($_) or die $!;
168 } @modified_bugs;
169 }
170 }
171 print "Deleting bugs: ",(time() - $t),"s\n" if $timing;
172 $t = time();
173
174 # Used to chache binary to source mappings
175 my %binarytosource = ();
176 my $location = $src_config{archived} ? 'archive' : 'db_h';
177 $table = $src_config{archived} ? $archived_table : $table;
178 # Read all bugs
179 my $insert_bugs_handle = $dbh->prepare("INSERT INTO $table (id, package, source, arrival, status, severity, submitter, owner, title, last_modified, affects_stable, affects_testing, affects_unstable) VALUES (\$1, \$2, \$3, \$4::abstime, \$5, \$6, \$7, \$8, \$9, \$10::abstime, \$11, \$12, \$13)");
180 my $insert_bugs_found_handle = $dbh->prepare("INSERT INTO ${table}_found_in (id, version) VALUES (\$1, \$2)");
181 my $insert_bugs_fixed_handle = $dbh->prepare("INSERT INTO ${table}_fixed_in (id, version) VALUES (\$1, \$2)");
182 my $insert_bugs_merged_handle = $dbh->prepare("INSERT INTO ${table}_merged_with (id, merged_with) VALUES (\$1, \$2)");
183 my $insert_bugs_tags_handle = $dbh->prepare("INSERT INTO ${table}_tags (id, tag) VALUES (\$1, \$2)");
184 $insert_bugs_handle->bind_param(4, undef, SQL_INTEGER);
185 $insert_bugs_handle->bind_param(10, undef, SQL_INTEGER);
186
187 $t = time();
188 foreach my $bug_nr (@modified_bugs) {
189 # Fetch bug using Debbugs
190 # Bugs which were once archived and have been unarchived again will appear in get_bugs(archive => 1).
191 # However, those bugs are not to be found in location 'archive', so we detect them, and skip them
192 my $bug_ref = read_bug(bug => $bug_nr, location => $location) or (print STDERR "Could not read file for bug $bug_nr; skipping\n" and next);
193 # Yeah, great, why does get_bug_status not accept a location?
194 my %bug = %{get_bug_status(bug => $bug_nr, status => $bug_ref)};
195
196 # Convert data where necessary
197 my @found_versions = @{$bug{found_versions}};
198 my @fixed_versions = @{$bug{fixed_versions}};
199 my @tags = split / /, $bug{keywords};
200
201 # log_modified and date are not necessarily set. If they are not available, they
202 # are assumed to be epoch (i.e. bug #4170)
203 map {
204 if($bug{$_}) {
205 #$bug{$_} = "$bug{$_}::abstime";
206 $bug{$_} = int($bug{$_});
207 } else {
208 $bug{$_} = 0;
209 }
210 } qw{date log_modified};
211
212
213 if(not exists $binarytosource{$bug{package}}) {
214 $binarytosource{$bug{package}} = (binarytosource($bug{package}))[0];
215 }
216 my $source = $binarytosource{$bug{package}};
217
218 if(not defined $source) {
219 # if source is not defined, then we $bug{package} is likely to
220 # be a source package name (or the source package has the same
221 # name as the binary package). See #480818 for ex.
222 $source = $bug{package};
223 }
224
225 #Calculate bug presence in distributions
226 my ($present_in_stable, $present_in_testing, $present_in_unstable);
227 if($src_config{archived}) {
228 $present_in_stable = $present_in_testing = $present_in_unstable = 'FALSE';
229 } else {
230 $present_in_stable =
231 bug_presence(bug => $bug_nr, status => \%bug,
232 dist => 'stable');
233 $present_in_testing =
234 bug_presence(bug => $bug_nr, status => \%bug,
235 dist => 'testing');
236 $present_in_unstable =
237 bug_presence(bug => $bug_nr, status => \%bug,
238 dist => 'unstable');
239 if(!defined($present_in_stable) or !defined($present_in_unstable) or !defined($present_in_testing)) {
240 print "NUMBER: $bug_nr\n";
241 }
242
243 if(defined($present_in_stable) and ($present_in_stable eq 'absent' or $present_in_stable eq 'fixed')) {
244 $present_in_stable = 'FALSE';
245 } else {
246 $present_in_stable = 'TRUE';
247 }
248 if(defined($present_in_testing) and ($present_in_testing eq 'absent' or $present_in_testing eq 'fixed')) {
249 $present_in_testing = 'FALSE';
250 } else {
251 $present_in_testing = 'TRUE';
252 }
253 if(defined($present_in_unstable) and ($present_in_unstable eq 'absent' or $present_in_unstable eq 'fixed')) {
254 $present_in_unstable = 'FALSE';
255 } else {
256 $present_in_unstable = 'TRUE';
257 }
258 }
259
260 # Insert data into bugs table
261 $insert_bugs_handle->execute($bug_nr, $bug{package}, $source, $bug{date}, $bug{pending},
262 $bug{severity}, $bug{originator}, $bug{owner}, $bug{subject}, $bug{log_modified},
263 $present_in_stable, $present_in_testing, $present_in_unstable) or die $!;
264
265 # insert data into bug_fixed_in and bug_found_in tables
266 foreach my $version (without_duplicates(@found_versions)) {
267 $insert_bugs_found_handle->execute($bug_nr, $version) or die $!;
268 }
269 foreach my $version (without_duplicates(@fixed_versions)) {
270 $insert_bugs_fixed_handle->execute($bug_nr, $version) or die $!;
271 }
272 foreach my $mergee (without_duplicates(split / /, $bug{mergedwith})) {
273 $insert_bugs_merged_handle->execute($bug_nr, $mergee) or die $!;
274 }
275 foreach my $tag (without_duplicates(@tags)) {
276 $insert_bugs_tags_handle->execute($bug_nr, $tag) or die $!;
277 }
278 }
279 print "Inserting bugs: ",(time() - $t),"s\n" if $timing;
280 }
281
282 sub main {
283 if(@ARGV != 3) {
284 print STDERR "Usage: $0 <config> <command> <source>\n";
285 exit 1;
286 }
287
288 our $t = time();
289 our $timing;
290
291 my $config = LoadFile($ARGV[0]) or die "Could not load configuration: $!";
292 my $command = $ARGV[1];
293 my $source = $ARGV[2];
294
295 my $dbname = $config->{general}->{dbname};
296 # Connection to DB
297 my $dbh = DBI->connect("dbi:Pg:dbname=$dbname");
298 # We want to commit the transaction as a hole at the end
299 $dbh->{AutoCommit} = 0;
300 $dbh->do('SET CONSTRAINTS ALL DEFERRED');
301
302 if($command eq 'run') {
303 run($config, $source, $dbh);
304 } else {
305 print STDERR "<command> has to be one of run, drop and setup\n";
306 exit(1)
307 }
308
309 $dbh->commit();
310 print "Committing bugs: ",(time() - $t),"s\n" if $timing;
311 }
312
313 main();

  ViewVC Help
Powered by ViewVC 1.1.5