This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
forked from openSUSE/patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_repomd
executable file
·303 lines (276 loc) · 6.59 KB
/
convert_to_repomd
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/perl
#
# convert the crappy stuff to repomd for easier handling
# Author: Ludwig Nussel, 2011
#
# TODO:
# 1. include all archs at the same time via extended syntax
# 2. include translations
# 4. generate yast2 style sel files from the repomd files
# 5. generate patterns-openSUSE-* packages from repomd files
# 6. get rid of the old crap
# 7. profit!
#
# http://de.opensuse.org/Standards/RPM_Metadata/repomd
# http://en.opensuse.org/openSUSE:Standards_Rpm_Metadata_patterns
use strict;
#use YAML qw/Dump Load DumpFile LoadFile/;
#use Data::Dumper;
my $version = "12.1";
my $release = "99";
my $arch = 'x86_64';
my @want = @ARGV;
my %sel;
opendir (my $dh, "data") || die "$!";
%sel = map { $_ => undef } grep { !/\.\.?/ } readdir($dh);
closedir $dh;
sub read_pkg_list($;$)
{
my $fn = shift;
my $forcetype = shift;
my $fh;
unless (open ($fh, '<', "$fn")) {
warn "$fn: $!";
next;
}
my $data = {};
my $nested = 0;
my $type;
my $exclarch;
my $onlyarch;
while (<$fh>) {
chomp;
s/\/\/.*//;
if (s/^#if\s*//) {
++$nested;
die "nesting not supported\n" if $nested > 1;
my @d = split(/\|\|/);
for (@d) {
my $neg = s/^!//;
die "invalid define \"$_\"\n" unless s/^ *defined//;
s/ +//g;
s/\(?__(.+)__\)?/\1/;
if ($neg) {
push @$exclarch, $_;
} else {
push @$onlyarch, $_;
}
}
next;
}
if (/^#else/) {
my $x = $exclarch;
$exclarch = $onlyarch;
$onlyarch = $x;
next;
}
if (/^#endif/) {
--$nested;
$exclarch = undef;
$onlyarch = undef;
die "too many endif\n" if $nested < 0;
next;
}
if (/^#/) {
warn "unknown preprocessor directive \"$_\" ignored";
next;
}
s/^ +//; s/ +$//;
next if /^$/;
if ($forcetype) {
$type = $forcetype;
} else {
if (/^\+P(cn|rc|rq|sg)/) {
$type = $1;
next;
}
if (/^-P(cn|rc|rq|sg)/) {
$type = undef;
next;
}
}
if ($type) {
my $c = undef;
$c->{'excl'} = [ @$exclarch ] if $exclarch;
$c->{'arch'} = [ @$onlyarch ] if $onlyarch;
$data->{$type}->{$_} = $c;
} else {
warn "line without type: $_";
}
}
close $fh;
if ($forcetype) {
return $data->{$forcetype};
}
return $data;
}
for my $f (keys %sel) {
next if $f =~ /^\.\.?/;
my $fn = "data/$f";
$sel{$f} = read_pkg_list($fn);
}
#print STDERR Dump(\%sel);
my %toinstall;
opendir (my $dh, "toinstall") || die "$!";
%toinstall = map { $_ => undef } grep { !/\.\.?/ } readdir($dh);
closedir $dh;
sub slurp($) {
my $fh;
return undef unless (open($fh, '<', $_[0]));
my @c = grep { chomp; $_ } <$fh>;
close $fh;
if (@c > 1) {
return [ @c ];
} else {
return $c[0];
}
}
for my $f (keys %toinstall) {
for my $tag (qw/category icon order role sel visible/) {
my $fn = "toinstall/$f/$tag";
unless (-r $fn) {
warn "$f lacks tag '$tag'";
next;
}
$toinstall{$f}->{$tag} = slurp($fn);
}
for my $tag (qw/conflicts extends freshens obsoletes provides recommends requires supplements/) {
my $fn = "toinstall/$f/$tag";
next unless -r $fn;
$toinstall{$f}->{$tag} = read_pkg_list($fn, $tag);
}
}
#print Dump(\%toinstall);
### now dump repomd patterns
sub archok($)
{
my $h = shift;
return 1 unless $h && ref $h eq 'HASH';
if ($h->{'arch'}) {
return 1 if grep { $_ eq $arch} @{$h->{'arch'}};
return 0;
}
if ($h->{'excl'}) {
return 0 if grep { $_ eq $arch} @{$h->{'excl'}};
return 1;
}
return 1;
}
sub tag($$)
{
my $tag = shift;
my $value = shift;
sprintf("<%s>%s</%s>\n", $tag, $value, $tag);
}
sub rpmentry($)
{
my $n = shift;
my @e = split(/ +/, $n);
my %flagmap = (
'<' => 'LT',
'<=' => 'LE',
'>' => 'GT',
'>=' => 'GE',
'=' => 'EQ',
'!=' => 'NE',
);
$e[0] =~ s/^patterns-openSUSE-/pattern:/;
my $s = sprintf ("<rpm:entry name=\"%s\"", $e[0]);
if (@e > 1) {
$s .= sprintf " flags=\"%s\"", $flagmap{$e[1]};
my @v = split(/-/, $e[2], 2);
$s .= sprintf " ver=\"%s\"", $v[0];
$s .= sprintf " rel=\"%s\"", $v[1] if @v > 1;
}
$s .= "/>\n";
}
my @patterns;
@want = keys %toinstall unless @want;
for my $f (@want) {
unless (exists $toinstall{$f}) {
warn "toinstall/$f does not exist, ignored.";
next;
}
#print STDERR Dumper($toinstall{$f});
my $s = '<pattern xmlns="http://novell.com/package/metadata/suse/pattern" xmlns:rpm="http://linux.duke.edu/metadata/rpm" xmlns:suse="http://localhost/">';
$s .= tag("name", $f);
$s .= sprintf '<version epoch="0" ver="%s" rel="%s" />', $version, $release;
$s .= "<uservisible/>\n" if (($toinstall{$f}->{'visible'}||'') eq 'true');
$s .= tag("category", $toinstall{$f}->{'category'});
$s .= tag("icon", $toinstall{$f}->{'icon'});
$s .= tag("order", $toinstall{$f}->{'order'});
$s .= tag("summary", $toinstall{$f}->{'role'});
my %deps;
for my $tag (qw/conflicts extends freshens obsoletes provides recommends requires supplements/) {
next unless exists $toinstall{$f}->{$tag};
my $dep = '';
for my $entry (keys %{$toinstall{$f}->{$tag}}) {
my $match = archok($toinstall{$f}->{$tag}->{$entry});
$dep .= "<!--\n" unless $match;
$dep .= rpmentry("patterns-openSUSE-$entry");
$dep .= "-->\n" unless $match;
}
$deps{$tag} = $dep;
}
$toinstall{$f}->{'sel'} = [] unless exists $toinstall{$f}->{'sel'};
$toinstall{$f}->{'sel'} = [ $toinstall{$f}->{'sel'} ] unless ref $toinstall{$f}->{'sel'} eq 'ARRAY';
for my $n (@{$toinstall{$f}->{'sel'}}) {
if (!exists $sel{$n}) {
warn "data/$n included from $f does not exist, ignored";
next;
}
my %tagmap = (
"conflicts" => "cn",
"recommends" => "rc",,
"requires" => "rq",,
"suggests" => "sg",,
);
for my $tag (keys %tagmap) {
next unless exists $sel{$n}->{$tagmap{$tag}};
my $dep = '';
while (my ($k, $v) = each %{$sel{$n}->{$tagmap{$tag}}}) {
my $match = archok($v);
$dep .= "<!--\n" unless $match;
$dep .= rpmentry($k);
$dep .= "-->\n" unless $match;
}
$deps{$tag} ||= '';
$deps{$tag} .= $dep;
}
}
while (my ($tag, $v) = each %deps) {
$s .= "<rpm:$tag>\n";
$s .= $v;
$s .= "</rpm:$tag>\n";
}
$s .= "</pattern>\n";
push @patterns, $s;
}
mkdir "repodata" unless -d "repodata";
my $fh;
unless (open($fh, '>', "repodata/patterns.xml")) {
die "xxx xxx: $!\n";
}
printf $fh "<patterns count=\"%d\">\n", scalar @patterns;
for my $s (@patterns) {
print $fh $s;
}
print $fh "</patterns>\n";
close $fh;
unless (open($fh, '>', "repodata/repomd.xml")) {
die "xxx repomd.xml: $!\n";
next;
}
print $fh <<EOF
<?xml version="1.0" ?>
<repomd xmlns="http://linux.duke.edu/metadata/repo"
xmlns:rpm="http://linux.duke.edu/metadata/rpm"
xmlns:suse="http://novell.com/package/metadata/suse/common">
<revision>1</revision>
<data type="patterns">
<location href="patterns.xml"/>
</data>
</repomd>
EOF
;
close $fh;