-
Notifications
You must be signed in to change notification settings - Fork 0
/
jppbootstrap
executable file
·406 lines (356 loc) · 10.3 KB
/
jppbootstrap
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/perl -w
use Getopt::Long;
use RPM::Source::Editor;
use RPM::Source::Macros;
use File::Path;
use strict;
use warnings;
my $release='0.1';
my $verbose=0;
my $outdir='.';
my $changelog='- bootstrap pack of jars created with jppbootstrap script
- temporary package to satisfy circular dependencies';
my $force_cleanup;
my (@skiprequires,@skipprovides);
my (@addprovides,@addrequires,@addobsoletes,$opt_epoch);
my ($altrpm,$altspec);
my ($opt_arch_mode);
my $altgroup='Development/Java';
my $release_suffix='jpp';
my %ARCH_MAP = qw/i386 %ix86 i486 %ix86 i586 %ix86 i686 %ix86 athlon %ix86/;
my $result = GetOptions (
"arch-mode" => \$opt_arch_mode,
"changelog=s" => \$changelog,
"outdir=s" => \$outdir,
"release=s" => \$release,
"relsuffix=s" => \$release_suffix,
"alt=s" => \$altrpm,
"parent=s" => \$altrpm,
"epoch=s" => \$opt_epoch,
"group=s" => \$altgroup,
"skip-requires=s" => \@skiprequires,
"skip-provides=s" => \@skipprovides,
"add-provides=s" => \@addprovides,
"add-requires=s" => \@addrequires,
"add-obsoletes=s" => \@addobsoletes,
"verbose+" => \$verbose);
die unless @ARGV;
my $macros=RPM::Source::Macros->new();
my $packager=$macros->macro_subst('%{packager}');
$packager=undef if $packager eq '%{packager}';
my %skip;
$skip{PROVIDENAME}={map {$_=>1} split(',',join(',',@skipprovides))};
$skip{REQUIRENAME}={map {$_=>1} split(',',join(',',@skiprequires))};
if (not $opt_arch_mode) {
foreach (@ARGV) {
die "Oops: $_ not found" unless -e $_;
&package($_);
}
} else {
&package(@ARGV);
}
sub package {
my ($donor, $trippletname, $bootstrap, $SOURCESDIR);
foreach my $rpmfile (@_) {
$donor=RPM::Donor::Query->new(RPM=>$rpmfile);
my $next_trippletname=$donor->query('NAME')."-".$donor->query('VERSION')."-".$donor->query('RELEASE');
my $arch=$donor->query('ARCH');
unless ($bootstrap) {
$trippletname=$next_trippletname;
$bootstrap=RPM::SpecFile::Bootstrap->new(-tmpdir=>$trippletname.$$);
} else {
die "tripplet mismatch: $trippletname $next_trippletname" unless $trippletname eq $next_trippletname;
}
$SOURCESDIR=$bootstrap->get_sourcesdir();
my $srcname;
unless ($opt_arch_mode) {
$srcname=$trippletname.'.cpio';
$bootstrap->set_noarch_source($srcname);
} else {
my $arch_subst=$ARCH_MAP{$arch};
$arch_subst//=$arch;
warn "ARCH=$arch SUBST=$arch_subst\n";
$srcname=$trippletname.'.'.$arch.'.cpio';
$bootstrap->set_arch_source($srcname, $arch_subst);
}
`rpm2cpio $rpmfile > $SOURCESDIR/$srcname`;
}
my $SPECSDIR=$bootstrap->get_specsdir();
my $specfile=$SPECSDIR."/".$donor->query('NAME').'.spec';
&write_spec_file($specfile, $donor, $bootstrap);
system ('add_changelog', '-e',$changelog,$specfile)==0 || $bootstrap->cleanup_and_exit();
mkpath([$outdir]) unless -d $outdir;
system ('rpmbuild', '--define', '_sourcedir '.$SOURCESDIR, '--define', '_srcrpmdir '.$outdir, '-bs', $specfile)==0 || $bootstrap->cleanup_and_exit();
$bootstrap->cleanup();
}
sub print_tag {
my ($fn,$tag,$tagval)=@_;
print $fn ucfirst(lc($tag)).": ".$tagval."\n" if defined $tagval && $tagval ne '(none)';
}
sub write_spec_file {
my ($specfile, $donor, $bootstrap)=@_;
my $parent;
if ($altrpm || $altspec) {
$parent=RPM::Source::Editor->new(SOURCERPM => $altrpm, SPECFILE => $altspec, VERBOSE=> $verbose);
}
my $is_noarch=$bootstrap->get_noarch_source();
open SPEC, '>', $specfile;
print SPEC q!# fedora builds i586 w/o -fPIC
%set_verify_elf_method relaxed!."\n" if not $is_noarch;
print SPEC q!
# sometimes commpress gets crazy (see maven-scm-javadoc for details)
%set_compress_method none!."\n\n";
foreach my $tag (qw/
NAME
VERSION
SUMMARY
LICENSE
URL
/) {
my $tagval=$donor->query($tag);
&print_tag(\*SPEC, $tag,$tagval);
}
$release='alt'.$release unless $release=~/^alt/;
$release=$release.$release_suffix if $release_suffix and not $release=~/${release_suffix}$/;
print SPEC 'Group: '.$altgroup.'
Release: '.$release."\n\n";
my $epoch=$donor->query('EPOCH');
if ($parent) {
my $parentepoch=$parent->main_section->get_tag('Epoch');
if (defined $parentepoch and (not defined $epoch or $epoch eq '(none)' or $parentepoch> $epoch)) {
$epoch=$parentepoch;
}
}
$epoch=$opt_epoch if defined $opt_epoch;
&print_tag(\*SPEC, 'EPOCH',$epoch);
print SPEC "Packager: $packager\n" if $packager;
my $pkgname=$donor->query('NAME');
foreach my $triplet (
[qw/PROVIDENAME PROVIDEFLAGS PROVIDEVERSION/]
) {
my @tagval=$donor->triquery(@$triplet);
@tagval=grep {$_!~/^.+\(/ or /^(?:mvn|osgi|npm)\(/} @tagval;
my $tag=$triplet->[0];
#print STDERR "DEBUG: $triplet->[0]: ",join(' ',@tagval),"\n";
my $tagname=ucfirst(lc($tag));
$tagname=~s/name/s/;
foreach my $val (@tagval) {
my $namepart=$val=~s/\s.*$//r;
print SPEC $tagname.": ",$val,"\n" if ($namepart ne $pkgname) and not $skip{$tag}->{$namepart};
}
}
foreach my $tag (qw/
REQUIRENAME
/) {
my @tagval=$donor->multiquery($tag);
@tagval=grep {$_!~/^.+\(/ or /^mvn\(/ or /^osgi\(/} @tagval;
#print STDERR "DEBUG: $tag: ",join(' ',@tagval),"\n";
my $tagname=ucfirst(lc($tag));
$tagname=~s/name/s/;
foreach my $val (@tagval) {
#(npm(sprintf-js) >= 1 with npm(sprintf-js) < 2)
$val=$1 if $val=~m/^\((\S+)\s/;
#print STDERR $tagname, ': ', $val, "\n";
print SPEC $tagname.": ".$val."\n" if $val ne $pkgname and not $skip{$tag}->{$val};
}
}
my %arch2src;
if ($is_noarch) {
my $noarch_source=$bootstrap->get_noarch_source();
print SPEC '
BuildArch: noarch
Source: '.$noarch_source."\n\n";
} else {
my $src_counter=0;
my $arch_source_hash=$bootstrap->get_arch_source_hash();
foreach my $arch (sort keys(%$arch_source_hash)) {
$arch2src{$arch}=$src_counter;
print SPEC 'Source'.$src_counter.': '.$arch_source_hash->{$arch}."\n";
$src_counter++;
}
}
print SPEC 'Provides: '.join(' ',@addprovides)."\n" if @addprovides;
print SPEC 'Requires: '.join(' ',@addrequires)."\n" if @addrequires;
print SPEC 'Obsoletes: '.join(' ',@addobsoletes)."\n" if @addobsoletes;
foreach my $tag (qw/DESCRIPTION/) {
my $query = $donor->query($tag);
print SPEC "\n".'%'.lc($tag)."\n".$query."\n" if $query ne '';
}
if ($is_noarch) {
print SPEC q!
%prep
cpio -idmu --quiet --no-absolute-filenames < %{SOURCE0}
%build
cpio --list < %{SOURCE0} | sed -e 's,^\.,,' > %name-list
!;
} else {
my $arch_source_hash=$bootstrap->get_arch_source_hash();
print SPEC "\n".'%prep'."\n";
foreach my $arch (sort keys(%$arch_source_hash)) {
my $src_counter=$arch2src{$arch};
print SPEC '%ifarch ',$arch,"\n",
'cpio -idmu --quiet --no-absolute-filenames < %{SOURCE',$src_counter,"}\n",
'%endif'."\n";
}
print SPEC "\n".'%build'."\n";
foreach my $arch (sort keys(%$arch_source_hash)) {
my $src_counter=$arch2src{$arch};
print SPEC '%ifarch ',$arch,"\n",
'cpio --list < %{SOURCE',$src_counter,q!} | sed -e 's,^\.,,' > %name-list!,"\n",
'%endif'."\n";
}
}
print SPEC q!
%install
mkdir -p $RPM_BUILD_ROOT
for i in usr var etc; do
[ -d $i ] && mv $i $RPM_BUILD_ROOT/
done
!;
#PRETRANS
#POSTTRANS
foreach my $sec (qw/
PREIN
PREUN
POSTIN
POSTUN
/) {
my $secname=lc($sec);
$secname=~s/in$//;
my $val=$donor->query($sec);
print SPEC '
%'.$secname."\n".$val."\n" if $val and $val ne "(none)";
}
print SPEC q!
%files -f %name-list
%changelog
!;
if ($parent) {
my @changelog_alt=@{$parent->get_section('changelog')->get_bodyref()};
shift @changelog_alt; # %changelog header
print SPEC @changelog_alt;
}
close(SPEC);
}
#sub eval {
# my $tag = shift;
# #%packager
# return `rpmquery --eval '$tag' 2>/dev/null`
#}
package RPM::SpecFile::Bootstrap;
use strict;
use warnings;
use File::Path qw(make_path remove_tree);
sub new {
my $class=shift;
my $self = {
-force_cleanup => 0,
@_,
};
my $tmpdir=$self->{-tmpdir};
unless ($tmpdir) {
die "Oops! no -tmpdir";
} else {
make_path($tmpdir);
}
return bless $self, $class;
}
sub get_sourcesdir {
my $self=shift;
my $SOURCESDIR=$self->{-sourcesdir};
unless ($SOURCESDIR) {
my $tmpdir=$self->{-tmpdir};
$SOURCESDIR="$tmpdir/SOURCES";
$self->{-sourcesdir}=$SOURCESDIR;
mkdir $SOURCESDIR;
}
return $SOURCESDIR;
}
sub get_specsdir {
my $self=shift;
my $SPECSDIR=$self->{-specsdir};
unless ($SPECSDIR) {
my $tmpdir=$self->{-tmpdir};
$SPECSDIR="$tmpdir/SPECS";
$self->{-specsdir}=$SPECSDIR;
mkdir $SPECSDIR;
}
return $SPECSDIR;
}
sub set_noarch_source {
my ($self,$srcname)=@_;
$self->{-noarch_source}=$srcname;
}
sub get_noarch_source {
my ($self)=@_;
return $self->{-noarch_source};
}
sub set_arch_source {
my ($self,$srcname,$arch)=@_;
$self->{-arch_source}->{$arch}=$srcname;
}
sub get_arch_source_hash {
my ($self)=@_;
return $self->{-arch_source};
}
sub cleanup {
my $self=shift;
my $tmpdir=$self->{-tmpdir};
remove_tree($tmpdir);
}
sub cleanup_and_exit {
my $self=shift;
my $tmpdir=$self->{-tmpdir};
&cleanup($tmpdir) if $self->{-force_cleanup};
exit 1;
}
package RPM::Donor::Query;
use strict;
use warnings;
sub new {
my $class=shift;
my $self = {
@_,
};
return bless $self, $class;
}
sub query{
my $self=shift;
my $tag=shift;
my $query=`rpmquery -p --queryformat '\%{$tag}' $self->{RPM}`;
chomp $query;
return $query;
}
sub multiquery {
my $self=shift;
my $tag=shift;
return grep {$_} split (/\n/, `rpmquery -p --queryformat '[\%{$tag}\n]' $self->{RPM}`);
}
sub triquery {
my ($self,$tag1,$tag2,$tag3)=@_;
my @triplets=grep {$_} split (/\n/, `rpmquery -p --queryformat '[\%{$tag1} %{$tag2} %{$tag3}\n]' $self->{RPM}`);
my @out;
foreach my $triplet (@triplets) {
my @triple=split (/\s+/, $triplet);
my $rel=&__rpmflag_to_string($triple[1]);
if ($rel) {
push @out, $triple[0].' '.$rel.' '.$triple[2];
} else {
push @out, $triple[0];
}
}
return @out;
}
sub __rpmflag_to_string {
my $flag=shift;
$flag||=0;
$flag &= 0xf;
return '' if $flag == 0;
return '<' if $flag == 2;
return '>' if $flag == 4;
return '=' if $flag == 8;
return '<=' if $flag == 10;
return '>=' if $flag == 12;
return '';
}
__END__