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

  ViewVC Help
Powered by ViewVC 1.1.5