summaryrefslogtreecommitdiffstats
path: root/pm/MGATools/rpmsrate.pm
blob: 4a41ce05ff842b428489dc0ab69c8d34b8db3789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package MGATools::rpmsrate;

use strict;
use MDK::Common qw(any cat_or_die member);

require Exporter;
use URPM;
our @ISA = qw(Exporter);
our @EXPORT = qw(cleanrpmsrate);

=head1 NAME

Mageia rpmsrate tools

=head1 SYNOPSYS

    require MGATools::rpmsrate;

=head1 DESCRIPTION

<MGATools::rpmsrate> includes Mageia rpmsrate tools.

=head1 COPYRIGHT

Copyright (C) 2000,2001,2002,2003,2004 Mandriva <warly@mandriva.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=cut

# function copied from Mkcd::Tools
sub cleanrpmsrate {
    my ($rpmsrate, $output, $norpmsrate, $reprpms, $urpm) = @_;
    $norpmsrate ||= [];
    my $LOG; open $LOG, ">&STDERR";
    my (@rpmsrate, %potloc);

    # must preread to get locale guessed packages
    # postfix is just used not to break the diff when checking if the result is correct
    foreach (cat_or_die($rpmsrate)) {
	chomp;
	s/#.*//;
	#s/\s*$//;
	/^(\s*)$/ and push @rpmsrate, [ '', 0, '', [] ] and next;
	if (/^(\S+)(.*)$/) {
	    push @rpmsrate, [ 0, 0, $1, [], $2 ];
	    next;
	}
	if (/^(\s*)([1-5])?(\s?[0-9A-Z_]+)$/) {
	    push @rpmsrate, [ $1, $2, $3, [] ];
	    next;
	}
	my ($indent, $r, $flags, $data) = /^(\s*)([1-5])?(\s*(?:(?:(?:!\s*)?[0-9A-Z_]+(?:"[^"]*")?(?:\s+(?:\|\|\s+)?)*)+\s+)|\s*)(.*)$/;
	my ($postfix) = $data =~ /(\s*)$/;
	my @data;
	my $i;
	foreach ([$data =~ /(?:^|\s)(\S+)-(?:\S+)\s+\1-(?:\S+)(?:\s|$)/g], [split ' ', $data]) {
	    $data[$i++] = [ @$norpmsrate ? any { my $r = $_; $r if !any { $r =~ /$_/ } @$norpmsrate } @$_ : @$_ ];
	}
	$potloc{$_} = [] foreach  @{$data[0]};
	push @rpmsrate, [ $indent,$r, $flags, $data[1], $postfix ];
    }

    my (%rpms, $text);
    my (%rate, %section, %keyword);
    my (%locale, %localized_pkg);
    my @plain_flags = qw(INSTALL LIVE);
    my $kernel_like = "((?:(?:NVIDIA_)?kernel|NVIDIA_nforce|cm2020).*)";
    my $dkms_like = "(.*)([-_])kernel-([0-9]+(?:\.[0-9]+){2,3}-[0-9]+(?:.[^.]+){0,2}md[vk])(.*)";
    my $rpmsrate_dkms_like = "(.*[-_]kernel)(.*)";
    my $urpm2 = URPM->new;

    foreach my $dir (keys %$reprpms) {
	foreach (@{$reprpms->{$dir}}) {
	    my $rpm = "$_.rpm";
	    my $key = $_;
	    s/-[^-]+-[^-]+\.[^.]+$// or next;
	    any { $rpm =~ /$_/ } @$norpmsrate and next;

	    if (/(.*?)([_-]*[\d._]+)(-.*)?-devel$/ || /^$kernel_like(-[^.]+(?:\.[^.]+){3,6}md[vk])$/) { 
		if (!$rpms{"$1$3"} || URPM::ranges_overlap("== $2", "> $rpms{'$1$3'}")) {
		    $rpms{"$1$3"} = [ $2, $1, $3 ];
		}
		if (/^$kernel_like-(\d+\.\d+)(.*)/) {
		    $rpms{"$1-$2"} = [ $3, "$1-$2" ];
		}
	    } elsif (/^$dkms_like$/) {
		my $vname = "$1$2kernel$4";
		if (!$rpms{$vname}) {
		    $rpms{$vname} = [ $3, $vname ];
		} elsif (URPM::ranges_overlap("== $3", "> $rpms{$vname}[0]")) {
		    $rpms{$vname} = [ $3, $vname ];
		}
	    } elsif (my ($pg, $loc) = /^(.*)-([^-+]+)$/) {
		if ($potloc{$pg}) {
		    my $pkg;
		    $pkg = $urpm->{rpm}{$urpm->{rpmkey}{key}{$key}} if ref $urpm;

		    # if package is not urpmi' synthesis, try to parse it directly:
		    if (!$pkg) {
			my $id = $urpm2->parse_rpm("$dir/$rpm");
			$pkg = $urpm2->{depslist}[$id];
		    }

		    if (!$pkg) {
			print "ERROR cleanrpmsrate: parse_rpm $dir/$rpm ($key) failed\n";
			next;
		    }

		    # some i18n packages does not require the same locale, e.g. kde-i18n-nb and nn requires locales-no 
		    # if (grep { s/locales-// && $loc =~ /^$_(_|$)/ } @{$header{REQUIRENAME}}) {
		    if (any { /^locales-...?$/ } $pkg->requires) {
			push @{$locale{$pg}}, $loc;
			$localized_pkg{"$pg-$loc"} = 1;
		    }
		}
	    }
	}
    }

    my (%done, @flags, @tree_rate, $prev_level);
    foreach (@rpmsrate) {
	if (!$_->[0]) {
	    $text .= "$_->[2]$_->[4]\n";
	    @flags = $_->[2] if $_->[2];
	    next;
	}

	my ($indent, $r, $flags, $data, $postfix) = @$_;
	my $level = (length $indent)/2 - 1;
	my $rate;
	if ($r) {
	    #print "tree_rate[$level] = $r\n";
	    $rate = $r;
	    $tree_rate[$level] = $r;
	} else {
	    if (@$data) {
		if ($level > $prev_level) {
		    $level--;
	        } else {
		    # fix a syntax error in rpmsrate such as
		    # A
		    #   1 toto
		    #   B tata <---
		    #     4 titi
		    @$data = ();
	        }
	    }
	    $rate = $tree_rate[$level];
	}

	$prev_level = $level;
	@flags = @flags[0 .. $level];
	push @flags, split(' ', $flags);
	#push @flags, grep { s/\s//; !/(\|\||[A-Z_]+"[^"]+")/ } split(' ', $flags);

	if (!@$data) {
	    $text .= "$indent$r$flags$postfix\n";
	    next;
	}

	my @k;
	my $flat_path = join ' ', @flags;
	foreach (@$data) {
	    my $c = $_;
	    next if ref $done{$_} && any { $flat_path eq $_ } @{$done{$_}};
	    die "FATAL: too complicated flags for duplicate entry $c ($flat_path and " . join ',', @{$done{$_}} 
	         if !member($flags[0], @plain_flags) && @flags > 1 && any { 
			 my ($f) = $flat_path =~ /^[^ ]+ (.*)/;
			 !/^[^ ]+ (.*)/ || $1 ne $f;
		 } @{$done{$_}};

	    my ($d) = /(.*)-[^-]+/;
	    my ($a, $b, $e, $do);

	    if ((!member($flags[0], @plain_flags) && s/(-devel)// ? ($b = "-devel") : /^$kernel_like/) && (($rpms{$_}) || (defined $rpms{"lib$_"} and $a = "lib") || (defined $rpms{"lib64$_"} and $a = "lib64") || (s/^lib(.*?)[_-]*[\d._]*(-.*)?$/$1$2/g && defined $rpms{"lib64$_"} and $a = 'lib64'))) {
		$e = $rpms{"$a$_"}[1] . $rpms{"$a$_"}[0] . $rpms{"$a$_"}[2] . $b;
		$do = 1;
	    } elsif ((!member($flags[0], @plain_flags) && /^$rpmsrate_dkms_like$/ && $rpms{"$1$2"})) {
		$e = "$1-" . $rpms{"$1$2"}[0] . $2;
		$do = 1;
	    }

	    if ($do) {
		$keyword{$c} = $e;
	       	if (!ref $done{$e} || member($flags[0], @plain_flags) && !(any { $flat_path eq $_ } @{$done{$e}}) || $flat_path =~ /DRIVER|HW/) {
		    push @{$done{$e}}, $flat_path;
		    push @k, $e;
		}
	    }

	    if ($locale{$d} && $localized_pkg{$c}) {
		foreach (sort @{$locale{$d}}) {
		    next if member($flat_path, @{$done{"$d-$_"}});
		    push @{$done{"$d-$_"}}, $flat_path; 
		    push @k , "$d-$_";
		}
		next;
	    }
	    push @k, $c;
	    push @{$done{$c}}, $flat_path;
	} 

	$text .= "$indent$r$flags@k$postfix\n" if @k;
	@rate{@k} = ($rate) x @k;
	my $path;
	foreach (@flags) {
	    $path .= $path ? "/$_" : $_;
	    push @{$section{$path}}, @k;
	}
    }

    if (%rpms || $output || %locale) {
	if (%$reprpms || $output) {
	    $output ||= $rpmsrate;
	    if (open my $A, ">$output") {
		print $A $text;
	    } else { 
		print $LOG "ERROR cleanrpmsrate: cannot open $rpmsrate for writing\n";
		print $text;
	    }
	}
    }

    [\%rate, \%section, \%keyword];
}

1