/[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 1249 - (hide annotations) (download)
Mon Sep 8 18:58:06 2008 UTC (4 years, 8 months ago) by lucas
File MIME type: text/plain
File size: 9620 byte(s)
bugs import cleanup
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 neronus-guest 971 use Debbugs::Packages qw{binarytosource};
20 neronus-guest 968 use Debbugs::Config qw{:globals};
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 neronus-guest 1068
30 neronus-guest 968 # 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 neronus-guest 973 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 neronus-guest 1068 sub run_usertags {
96     my ($config, $source, $dbh) = @_;
97 neronus-guest 945 my %src_config = %{$config->{$source}};
98 neronus-guest 1068 my $table = $src_config{'usertags-table'} or die "usertags-table not specified for source $source";
99     our $timing;
100     our $t;
101 neronus-guest 919
102    
103 lucas 1055 $t = time();
104 neronus-guest 968 # Free usertags table
105 neronus-guest 1068 $dbh->do("DELETE FROM $table") or die
106     "Couldn't empty $table: $!";
107 lucas 1055 print "Deleting usertags: ",(time() - $t),"s\n" if $timing;
108     $t = time();
109 neronus-guest 971 # read and insert user tags
110 neronus-guest 968 my @users = get_bugs_users();
111     foreach my $user (@users) {
112 neronus-guest 1053 #read_usertags(\%tags, $user);
113     my $u = Debbugs::User->new($user);
114     my %tags = %{$u->{tags}};
115 neronus-guest 968 $user = $dbh->quote($user);
116     foreach my $tag (keys %tags) {
117     my $qtag = $dbh->quote($tag);
118 neronus-guest 1068 map { $dbh->do("INSERT INTO $table VALUES ($user, $qtag, $_)") or die $! } @{$tags{$tag}};
119 neronus-guest 968 }
120     }
121 neronus-guest 1068 }
122    
123     sub run {
124     my ($config, $source, $dbh) = @_;
125    
126     our $t;
127     our $timing;
128 lucas 1249
129     run_usertags($config, $source, $dbh);
130 lucas 1055 print "Inserting usertags: ",(time() - $t),"s\n" if $timing;
131     $t = time();
132 neronus-guest 1068
133     my %src_config = %{$config->{$source}};
134     my $table = $src_config{table};
135     my $archived_table = $src_config{'archived-table'};
136    
137 neronus-guest 973 my @modified_bugs;
138 lucas 1243
139 neronus-guest 973 if($src_config{archived}) {
140 lucas 1249 # 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 neronus-guest 973 } else {
149     @modified_bugs = get_bugs();
150     }
151 lucas 1059 my @modified_bugs2;
152     if ($src_config{debug}) {
153 lucas 1249 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 lucas 1059 }
157     @modified_bugs = @modified_bugs2;
158     }
159    
160 lucas 1055 print "Fetching list of ",scalar(@modified_bugs), " bugs to insert: ",(time() - $t),"s\n" if $timing;
161     $t = time();
162 neronus-guest 973
163 neronus-guest 1068 foreach my $prefix ($table, $archived_table) {
164 lucas 1243 foreach my $postfix (qw{_merged_with _found_in _fixed_in _tags}, '') {
165 neronus-guest 1068 my $sth = $dbh->prepare("DELETE FROM $prefix$postfix WHERE id = \$1");
166     map {
167     $sth->execute($_) or die $!;
168     } @modified_bugs;
169     }
170 neronus-guest 973 }
171 lucas 1055 print "Deleting bugs: ",(time() - $t),"s\n" if $timing;
172     $t = time();
173 neronus-guest 919
174 neronus-guest 971 # Used to chache binary to source mappings
175 neronus-guest 939 my %binarytosource = ();
176 neronus-guest 945 my $location = $src_config{archived} ? 'archive' : 'db_h';
177 neronus-guest 1068 $table = $src_config{archived} ? $archived_table : $table;
178 neronus-guest 919 # Read all bugs
179 neronus-guest 1068 my $insert_bugs_handle = $dbh->prepare("INSERT INTO $table 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 VALUES (\$1, \$2)");
181     my $insert_bugs_fixed_handle = $dbh->prepare("INSERT INTO ${table}_fixed_in VALUES (\$1, \$2)");
182     my $insert_bugs_merged_handle = $dbh->prepare("INSERT INTO ${table}_merged_with VALUES (\$1, \$2)");
183     my $insert_bugs_tags_handle = $dbh->prepare("INSERT INTO ${table}_tags 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 neronus-guest 973 foreach my $bug_nr (@modified_bugs) {
189 neronus-guest 930 # Fetch bug using Debbugs
190 neronus-guest 946 # 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 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);
193 neronus-guest 942 # Yeah, great, why does get_bug_status not accept a location?
194 neronus-guest 945 my %bug = %{get_bug_status(bug => $bug_nr, status => $bug_ref)};
195 neronus-guest 936
196 neronus-guest 930 # Convert data where necessary
197 neronus-guest 1077 my @found_versions = @{$bug{found_versions}};
198     my @fixed_versions = @{$bug{fixed_versions}};
199     my @tags = split / /, $bug{keywords};
200 neronus-guest 939
201 neronus-guest 945 # 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 neronus-guest 1068 #$bug{$_} = "$bug{$_}::abstime";
206     $bug{$_} = int($bug{$_});
207 neronus-guest 945 } else {
208 neronus-guest 1068 $bug{$_} = 0;
209 neronus-guest 945 }
210     } qw{date log_modified};
211 neronus-guest 939
212 neronus-guest 945
213 neronus-guest 939 if(not exists $binarytosource{$bug{package}}) {
214 neronus-guest 982 $binarytosource{$bug{package}} = (binarytosource($bug{package}))[0];
215 neronus-guest 939 }
216     my $source = $binarytosource{$bug{package}};
217    
218 neronus-guest 929 if(not defined $source) {
219 lucas 1029 # 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 neronus-guest 1077 $source = $bug{package};
223 neronus-guest 1078 }
224 neronus-guest 929
225 neronus-guest 922 #Calculate bug presence in distributions
226 neronus-guest 1068 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 neronus-guest 922 } else {
230 neronus-guest 1068 $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 neronus-guest 922 }
259    
260 neronus-guest 920 # Insert data into bugs table
261 neronus-guest 1068 $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 neronus-guest 920
265     # insert data into bug_fixed_in and bug_found_in tables
266 neronus-guest 973 foreach my $version (without_duplicates(@found_versions)) {
267 neronus-guest 1068 $insert_bugs_found_handle->execute($bug_nr, $version) or die $!;
268 neronus-guest 920 }
269 neronus-guest 973 foreach my $version (without_duplicates(@fixed_versions)) {
270 neronus-guest 1068 $insert_bugs_fixed_handle->execute($bug_nr, $version) or die $!;
271 neronus-guest 920 }
272 neronus-guest 973 foreach my $mergee (without_duplicates(split / /, $bug{mergedwith})) {
273 neronus-guest 1068 $insert_bugs_merged_handle->execute($bug_nr, $mergee) or die $!;
274 neronus-guest 921 }
275 neronus-guest 983 foreach my $tag (without_duplicates(@tags)) {
276 neronus-guest 1068 $insert_bugs_tags_handle->execute($bug_nr, $tag) or die $!;
277 neronus-guest 983 }
278 neronus-guest 919 }
279 lucas 1249 print "Inserting bugs: ",(time() - $t),"s\n" if $timing;
280 neronus-guest 1068 }
281 neronus-guest 919
282 neronus-guest 1068 sub main {
283     if(@ARGV != 3) {
284     print STDERR "Usage: $0 <config> <command> <source>\n";
285     exit 1;
286     }
287    
288 neronus-guest 1077 our $t = time();
289 neronus-guest 1070 our $timing;
290    
291 neronus-guest 1068 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 lucas 1243 $dbh->do('SET CONSTRAINTS ALL DEFERRED');
301 neronus-guest 1068
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 neronus-guest 919 $dbh->commit();
310 lucas 1055 print "Committing bugs: ",(time() - $t),"s\n" if $timing;
311 neronus-guest 919 }
312    
313     main();

  ViewVC Help
Powered by ViewVC 1.1.5