/
Multipkg.pm
1473 lines (1210 loc) · 37.9 KB
/
Multipkg.pm
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Seco::Multipkg;
# Copyright (c) 2011 Yahoo! Inc. All rights reserved.
use strict;
use constant MULTIPKG_VERSION => '__MULTIPKG_BUILD_VERSION__';
use File::Spec;
use File::Basename;
use Cwd;
use base qw/Seco::Class/;
BEGIN {
__PACKAGE__->_accessors(
startdir => undef,
directory => undef,
confdir => '__MULTIPKG_CONFIG_DIR__',
info => undef,
cleanup => 0,
cwd => undef,
overrides => {},
meta => undef,
force => 0,
platform => undef,
warn_on_error => 0,
verbose => 0
);
__PACKAGE__->_requires(qw/directory/);
}
sub _init {
my $self = shift;
my $cwd = getcwd;
$self->cwd($cwd);
$self->{directory} =~ s/\/$//;
$self->{directory} = File::Spec->rel2abs( $self->{directory} );
return $self->error( "No such directory " . $self->directory . "." )
unless ( -d $self->directory );
$self->{confdir} =~ s/\/$//;
$self->{confdir} = File::Spec->rel2abs( $self->{confdir} );
return $self->error( "No such directory " . $self->confdir )
unless ( -d $self->confdir );
$self->info(
Seco::Multipkg::Info->new(
overrides => $self->overrides,
platform => $self->platform,
directory => $self->directory,
confdir => $self->confdir,
verbose => $self->verbose,
meta => $self->meta
)
);
}
sub build {
my $self = shift;
my $builder;
$builder = Seco::Multipkg::Builder::Gem->new(
verbose => $self->verbose,
info => $self->info,
force => $self->force,
cwd => $self->cwd
) if ( $self->info->data->{packagetype} eq 'gem' );
$builder = Seco::Multipkg::Builder::Rpm->new(
verbose => $self->verbose,
info => $self->info,
force => $self->force,
cwd => $self->cwd
) if ( $self->info->data->{packagetype} eq 'rpm' );
$builder = Seco::Multipkg::Builder::Deb->new(
verbose => $self->verbose,
info => $self->info,
force => $self->force,
cwd => $self->cwd
) if ( $self->info->data->{packagetype} eq 'deb' );
$builder = Seco::Multipkg::Builder::Tarball->new(
verbose => $self->verbose,
info => $self->info,
force => $self->force,
cwd => $self->cwd
) if ( $self->info->data->{packagetype} eq 'tarball' );
$builder->build;
$builder->copyroot;
$builder->transform;
$builder->verify_data
or $builder->forceok("Finished package contains no files");
# build action log
my $multipkg_build_meta = {
actionlog => [
{ 'time' => time(),
'type' => 'build',
'actor' => $self->info->data->{'whoami'},
'actions' => [
{ 'summary' => 'Seco::Multipkg build complete',
'text' => "multipkg version: " . MULTIPKG_VERSION . "\n",
},
],
},
],
};
$self->info->mergemeta($multipkg_build_meta);
my $pkg = $builder->makepackage;
if ( $self->cleanup ) {
$builder->cleanup;
}
else {
warn "Not cleaning up: " . $builder->tmpdir . "\n";
}
return $pkg;
}
package Seco::Multipkg::Builder;
use File::Find qw/find/;
use File::Temp qw/tempfile tempdir/;
use File::Copy;
use IPC::Open3;
use FileHandle;
use Fcntl ':mode';
use constant MULTIPKG_VERSION => '__MULTIPKG_BUILD_VERSION__';
use base qw/Seco::Class/;
BEGIN {
__PACKAGE__->_accessors(
info => undef,
verbose => 0,
tmpdir => undef,
force => 0,
builddir => undef,
cwd => undef,
installdir => undef
);
__PACKAGE__->_requires(qw/info/);
}
sub _init {
my $self = shift;
$self->{_vars} = {};
$self->tmpdir( tempdir( CLEANUP => 0 ) );
$self->infomsg( "Using tmpdir " . $self->tmpdir );
my $builddir = $self->tmpdir . "/build";
mkdir($builddir) or die "Unable to mkdir $builddir: $!";
$self->builddir($builddir);
my $installdir = $self->tmpdir . "/install";
mkdir($installdir) or die "Unable to mkdir $installdir: $!";
chmod 0755, $installdir;
$self->installdir($installdir);
$self->setrelease;
$self->{_rules} = [ $self->get_file_rules ];
}
sub setrelease {
my $self = shift;
# When release is not provided, we must be making a test build.
# Use "0.time()" as release version to avoid ever conflicting with an actual
# build from source checkout
$self->info->data->{release} = sprintf "0.%u", time()
unless ( defined $self->info->data->{release} );
}
sub pkgverid {
my $self = shift;
return join '-', $self->info->data->{'name'},
$self->info->data->{'version'},
$self->info->data->{'release'};
}
sub forceok {
my $self = shift;
my $msg = shift;
die "FATAL (use --force to override): $msg\n" unless ( $self->force );
warn "WARN: $msg\n";
}
sub template_file {
my $self = shift;
my $from = shift;
my $to = shift;
my $chmod = shift;
my $str = $self->template_string($from);
open my $f, ">$to";
print $f $str;
close $f;
chmod $chmod, $to if ( defined $chmod );
}
# XXX: dumb variable substitution, intended for use for expanding relative
# strings in index.yaml parameters, etc
sub substvars {
my $self = shift;
my $buf = shift;
for my $v ( keys %{ $self->{_vars} } ) {
my $retxt = quotemeta( '$(' . $v . ')' );
my $re = qr/$retxt/;
$buf =~ s/$re/$self->{_vars}->{$v}/gm;
}
return $buf;
}
sub template_string {
my $self = shift;
my $from = shift;
open my $f, "<$from" or die "$from: $!";
my $ret = '';
my $skipping = 0;
for (<$f>) {
# XXX: use of '%' prefix potentially conflicts with RPM specfile syntax
if (/^%%ifscript\(([A-Za-z\.]+)\)$/) {
my $script = $1;
# increase nesting count for every nested %%if statement when we are inside an
# %%if which evaluated false.
if ($skipping) {
$skipping++;
next;
}
# start skipping lines once we hit a false %%if statement.
if ( !$self->info->scripts->{$script} ) {
$skipping++;
}
next;
}
# reduce the nesting count for each %%endif until we leave the original %%if
# that evaluated false.
if (/^%%endif$/) {
if ($skipping) {
$skipping--;
}
next;
}
# skip lines if we are inside an %if that evaluated false.
if ($skipping) {
next;
}
while (/%([A-Za-z_-]+)%/) {
my $match = $1;
if ( defined( my $repl = $self->info->data->{$match} ) ) {
s/%$match%/$repl/;
}
else {
s/%$match%/UNKNOWN/;
$self->infomsg("WARN: $match not defined for $from");
}
}
while (/%%([A-Za-z\.]+)%%/) {
my $match = $1;
if ( ( my $f = $self->info->scripts->{$match} )
and ( -f $f ) )
{
my $script = $self->template_string($f);
s/%%$match%%/$script/;
}
else {
s/%%$match%%//;
}
}
$ret .= $_;
}
close $f;
return $ret;
}
sub makepackage {
my $self = shift;
die "makepackage not implemented";
}
sub install_gemspec {
my $self = shift;
# shove the file list into $self->info->data->{filelist}
my $installdir = $self->installdir;
my $buildprefix = $self->info->data->{buildprefix};
my @filelist;
my $geminstalldir = [$self->runcmd("gem environment gemdir")]->[0];
return $self->error("Can't run: $@") if($@);
chomp $geminstalldir;
my $fullinstalldir = $geminstalldir . "/gems/" .
$self->info->data->{name} . "-" .
$self->info->data->{version};
foreach ($self->listdir("$installdir/$fullinstalldir")) {
if (-f "$installdir/$fullinstalldir/$_") {
s#^$installdir/$fullinstalldir/##;
s#^$buildprefix/##;
push @filelist, "\"$_\"";
}
}
$self->info->data->{filelist} = join ",", @filelist;
# generate the gemspec file based on that
my $name = $self->info->data->{'name'};
my $version = $self->info->data->{'version'};
$self->runcmd("mkdir -p $installdir/$geminstalldir/specifications");
$self->template_file($self->info->confdir . "/templates/gemspec.template",
"$installdir/$geminstalldir/specifications/" .
"$name-$version.gemspec");
}
sub cleanup {
my $self = shift;
# my $tmpdir = $ENV{TMPDIR} || '/tmp';
# $tmpdir =~ s/\/$//;
# die "Unsafe to clean up " . $self->tmpdir
# unless ( $self->tmpdir =~ /^$tmpdir\/\w/ );
$self->infomsg( "Cleaning up " . $self->tmpdir );
system( "rm -rf " . $self->tmpdir );
}
sub _listfile {
my ( $path, $parent, $found_root ) = @_;
# the root must be a directory
if ( $path eq $parent ) {
lstat($parent) or die "can't lstat parent ($parent)";
die "parent ($parent) is not a directory" unless ( -d _ );
$$found_root = 1;
return;
}
# otherwise the path must be a child of the parent
die "path ($path) outside parent ($parent)"
unless ( substr( $path, 0, length($parent) + 1 ) eq "$parent/" );
my $name = substr( $path, length($parent) + 1 );
die "invalid path ($path)" unless ( length($name) );
# return relative path
return $name;
}
# Strips the leading directory path and returns the relative path name of the
# contents of '$dir'
sub listdir {
my $self = shift;
my $dir = shift;
my @ret = ();
my $found_root = 0;
find(
{ wanted => sub { push @ret, _listfile( $_, $dir, \$found_root ); },
no_chdir => 1,
},
$dir
);
die "find failed on path ($dir)" unless ($found_root);
# preserve daemontools log dirs, not needed with new logrun
#@ret = ($dir) if(scalar @ret == 0 and $dir eq 'main');
return @ret;
}
# XXX: maybe keep a "global log of all command output" in the builder object,
# so we can toss that into the generated package too for tracability?
sub runcmd {
my $self = shift;
my $cmd = shift;
my $count = shift;
$count ||= 10;
$self->infomsg("RUNNING: $cmd");
my @last = ();
my $writer = FileHandle->new;
my $reader = FileHandle->new;
my $pid = open3( $writer, $reader, undef, $cmd );
close $writer;
while (<$reader>) {
print if ( $self->verbose );
push @last, $_;
shift @last if ( @last > $count );
}
close $reader;
waitpid $pid, 0;
die "Build failed: @last" if ( $? >> 8 );
return @last;
}
sub fetch {
my $self = shift;
my $target;
# build cpan module
if ( $target = $self->info->data->{'cpan-module'} ) {
eval { require Seco::CPAN; };
die "Seco::CPAN required to install cpan modules" if ($@);
$self->infomsg("Fetching $target from CPAN");
my $agent = Seco::CPAN->new(
depositdir => ( $self->tmpdir . "/cpan" ),
tmpdir => $self->tmpdir
);
my $hash = $agent->pull($target)
or die "Unable to pull $target: $!";
my $name = lc $hash->{name};
my $loc = $hash->{tarball};
my $version = $hash->{version};
if ( $name ne $self->info->data->{name} ) {
$self->forceok( "Package wants to be called $name, "
. "you asked for "
. $self->info->data->{name}
. "\n" );
}
$self->info->data->{sourcetar} = $loc;
$self->info->data->{version} = $version;
}
# build from http
elsif ( $target = $self->info->data->{'http'} ) {
eval { require Seco::HTTP; };
die "Seco::HTTP required to build package from web: $@" if ($@);
$self->infomsg("Fetching $target");
my $agent = Seco::HTTP->new(
xfercmd => $self->info->data->{xfercmd},
depositdir => ( $self->tmpdir . "/source" ),
tmpdir => $self->tmpdir
);
my $hash = $agent->pull($target)
or die "Unable to pull $target: $!";
my $loc = $hash->{tarball};
$self->info->data->{sourcetar} = $loc;
}
}
sub build {
my $self = shift;
chdir $self->info->directory;
my $realbuild = $self->builddir;
# fetch source from remote
$self->fetch();
# build the source if there is any
if ( $self->info->data->{sourcedir} and -d $self->info->data->{sourcedir} ) {
$self->infomsg( "Building from " . $self->info->data->{sourcedir} );
system( "cd "
. $self->info->data->{sourcedir} . " && "
. "tar cf - . | tar xf - -C $self->{builddir}" );
}
elsif ( $self->info->data->{sourcetar}
and -f $self->info->data->{sourcetar} )
{
$self->infomsg( "Building from " . $self->info->data->{sourcetar} );
system( "tar zxf $self->{info}->{data}->{sourcetar} " . "-C $self->{builddir}" );
my $d;
opendir $d, $self->builddir;
foreach ( readdir $d ) {
next if /^\./;
if ( -d $self->builddir . "/$_" ) {
$realbuild = $self->builddir . "/$_";
last;
}
}
closedir $d;
}
else {
return;
}
my $destdir = $self->installdir;
my $prefix = $self->info->data->{buildprefix}?$self->info->data->{buildprefix}:"/usr";
my $perl = $self->info->data->{perl}?$self->info->data->{perl}:"/usr/bin/perl";
chdir $realbuild;
$self->{_vars}{BUILDDIR} = $realbuild;
my $patchdir = $self->info->directory . "/patches";
if ( -d $patchdir ) {
$self->infomsg("Applying patches");
my $d;
opendir $d, $patchdir;
my @patches = sort { $a cmp $b }
grep { $_ !~ /^\./ and -f "$patchdir/$_" } readdir $d;
closedir $d;
for my $patch (@patches) {
$self->infomsg("Applying $patch");
$self->runcmd("patch --ignore-whitespace -p 1 -d . < $patchdir/$patch");
}
}
$self->infomsg("Building source");
if($self->info->data->{gem} and
$self->info->scripts->{gembuild}) {
# FATAL ON ERRORS
$self->runcmd( "PERL=$perl INSTALLROOT=$destdir DESTDIR=$destdir "
. "PREFIX=$prefix PKGVERID="
. $self->pkgverid . " "
. "PACKAGEVERSION=" . $self->info->data->{version} . " "
. "PACKAGENAME=" . $self->info->data->{name} . " "
. $self->info->scripts->{build} );
return $self->error("Error running: $@") if($@);
} else {
# FATAL ON ERRORS
$self->runcmd( "PERL=$perl INSTALLROOT=$destdir DESTDIR=$destdir "
. "PREFIX=$prefix PKGVERID="
. $self->pkgverid . " "
. "PACKAGEVERSION=" . $self->info->data->{version} . " "
. "PACKAGENAME=" . $self->info->data->{name} . " "
. $self->info->scripts->{build} );
return $self->error("Error running: $@") if($@);
}
chdir $self->cwd;
}
# verifies that installroot got some data
sub verify_data {
my $self = shift;
my $root = shift;
$root = $self->installdir unless defined($root);
my $dir;
opendir $dir, $root;
while ( my $f = readdir $dir ) {
next if ( $f eq '.' or $f eq '..' );
return 1 if ( -f "$root/$f" );
if ( -d "$root/$f" ) {
return 1 if $self->verify_data("$root/$f");
}
}
closedir $dir;
return 0;
}
# change #!/usr/bin/wrongperl to the one you asked for in index.yaml
sub shebangmunge {
my $self = shift;
my $dirname = shift;
if ( !defined($dirname) ) {
$dirname = $self->installdir;
}
my $dir;
opendir $dir, $dirname or die "Can't open $dirname";
while ( my $f = readdir $dir ) {
next if ( $f eq '.' or $f eq '..' );
if ( -f "$dirname/$f" ) {
my $out = `file $dirname/$f`;
next unless ( $out =~ /text/ );
open my $g, "$dirname/$f";
my $firstline = <$g>;
next unless ( $firstline =~ m#^\#\!(/.*/)(\w+)\s+?(.*)# );
my ( $path, $interpreter, $options ) = ( $1, $2, $3 );
if ( $self->info->data->{$interpreter} ) {
$self->infomsg("SHEBANG-MUNGING $interpreter: $dirname/$f");
my $newinterp = $self->info->data->{$interpreter};
$firstline =~ s/$path$interpreter/$newinterp/;
open my $h, ">" . $self->tmpdir . "/temp-munge"
or die "Can't open " . $self->tmpdir . "/temp-munge";
print $h $firstline;
while (<$g>) {
print $h $_;
}
close $g;
close $h;
my $mode = ( stat("$dirname/$f") )[2];
rename $self->tmpdir . "/temp-munge" => "$dirname/$f";
chmod $mode, "$dirname/$f";
}
}
elsif ( -d "$dirname/$f" ) {
$self->shebangmunge("$dirname/$f");
}
}
closedir $dir;
}
# process filetransforms (s/// on files) and dirtransforms
# (moving directories around)
sub transform {
my $self = shift;
my $installdir = $self->installdir;
$self->shebangmunge if ( $self->info->data->{shebangmunge} );
if ( $self->info->data->{filetransforms} ) {
for my $file ( keys %{ $self->info->data->{filetransforms} } ) {
my ( $fh, $filename ) = tempfile( 'tempXXXXX', DIR => $self->tmpdir );
$self->infomsg("TRANSFORMING: $installdir/$file");
open my $g, "$installdir/$file" or next;
while (<$g>) {
for my $trans ( @{ $self->info->data->{filetransforms}->{$file} } ) {
for my $from ( keys %$trans ) {
my $to = $trans->{$from};
s/$from/$to/g;
}
}
print $fh $_;
}
close $g;
close $fh;
rename $filename => "$installdir/$file";
}
}
if ( $self->info->data->{dirtransforms} ) {
foreach my $transform ( @{ $self->info->data->{dirtransforms} } ) {
my $from = $transform->{from};
my $to = $transform->{to};
$self->runcmd("mkdir -p '$installdir/$to'");
$self->runcmd("mv $installdir/$from/* $installdir/$to || echo fine");
$self->runcmd( "rmdir -p --ignore-fail-on-non-empty " . "$installdir/$from || echo fine" );
}
}
if($self->info->scripts->{transform}) {
$self->runcmd( "INSTALLDIR=$installdir "
. "PKGVERID="
. $self->pkgverid . " "
. $self->info->scripts->{transform} );
}
}
sub copyroot {
my $self = shift;
my $installdir = $self->installdir;
# copy root/ or root.tar.gz into installdir
if ( $self->info->data->{rootdir} and -d $self->info->data->{rootdir} ) {
$self->infomsg( "Using " . $self->info->data->{rootdir} );
system( "cd "
. $self->info->data->{rootdir} . " && "
. "tar cf - --exclude \.svn . | tar xf - -C $self->{installdir}" );
}
elsif ( $self->info->data->{roottar}
and -f $self->info->data->{roottar} )
{
$self->infomsg( "Using " . $self->info->data->{roottar} );
system( "tar zxf $self->{info}->{data}->{roottar} " . " -C $self->{installdir}" );
}
# install daemontools service
if ( $self->info->scripts->{run}
and $self->info->scripts->{logrun} )
{
$self->info->data->{service} ||= $self->info->data->{name};
my $service = $self->info->data->{service};
$self->infomsg("Installing daemontools service $service");
for (
"etc", "etc/service",
"etc/service/$service", "etc/service/$service/log",
"etc/service/$service/log/main"
)
{
mkdir "$installdir/$_" unless ( -d "$installdir/$_" );
}
copy( $self->info->scripts->{run}, "$installdir/etc/service/$service/run" );
copy( $self->info->scripts->{logrun}, "$installdir/etc/service/$service/log/run" );
chmod 0755, "$installdir/etc/service/$service/run", "$installdir/etc/service/$service/log/run";
my $uid = ( getpwnam('nobody') )[2];
my $gid = ( getpwnam('nobody') )[3];
chown $uid, $gid, "$installdir/etc/service/$service/log/main";
}
}
sub _find_attributes {
my $name = shift;
my $rules = shift;
# return the last match
my @found = ( grep { $_->{name} eq $name } (@$rules) );
return pop(@found);
}
sub _find_match_attributes {
my $name = shift;
my $rules = shift;
# return the last shell pattern match
my @found = ( grep { _fnmatch( $_->{name}, $name ) } (@$rules) );
return pop(@found);
}
# pure-perl implementation of File::FnMatch::fnmatch with 'FNM_PATHNAME' argument
sub _fnmatch {
my $pat = shift;
my $str = shift;
#dots are literal dots
$pat =~ s{\.}{\\\.}g;
#internal slashes must be escaped
$pat =~ s{/}{\\/}g;
#asterisks are everything but slash
$pat =~ s{\*}{[^\/]*}g;
#anchor it
$pat =~ s{^}{\^};
$pat =~ s{$}{\$};
return $str =~ m/$pat/;
}
sub _find_parent_attributes {
my $name = shift;
my $rules = shift;
my @found;
for my $r (@$rules) {
# syntax for "include all children of dir" is '/dir/...'
next unless ( $r->{name} =~ /^(.+)\/\.\.\.$/ );
my $parent = quotemeta($1);
push @found, $r
if ( $name =~ /^$parent\/.+/ );
}
return pop @found;
}
sub get_file_rules {
my $self = shift;
my @rules;
for my $p ( @{ $self->info->data->{files} } ) {
for my $f ( sort keys %$p ) {
# remove any leading /, as a convenience for writing the files
# configuration (->listdir() returns all contents using relative
# paths)
( my $n = $f ) =~ s/^\/*//;
# XXX: it could be useful to call ->substvars() here, so that a
# single set of file rules would work for both rpm and yinst, etc
$p->{$f}{name} = $n;
if ( exists( $p->{$f}{perm} ) ) {
die "perm was not octal for $f"
unless ( $p->{$f}{perm} =~ /^0/ );
}
push @rules, $p->{$f};
}
}
return @rules;
}
# Look up attribute
sub get_file_attributes {
my $self = shift;
my $name = shift;
my $attribs = undef;
# first try to find a rule for the exact path name
$attribs = _find_attributes( $name, $self->{_rules} );
return $attribs if ( defined($attribs) );
# next try matching via shell patterns
$attribs = _find_match_attributes( $name, $self->{_rules} );
return $attribs if ( defined($attribs) );
# finally try matching for subtrees
$attribs = _find_parent_attributes( $name, $self->{_rules} );
return $attribs;
}
package Seco::Multipkg::Builder::Rpm;
use base qw/Seco::Multipkg::Builder/;
use POSIX qw/strftime/;
BEGIN {
__PACKAGE__->_accessors( stagedir => undef );
}
sub makepackage {
my $self = shift;
mkdir $self->tmpdir . "/rpm";
mkdir $self->tmpdir . "/rpm/rpmtop";
mkdir $self->tmpdir . "/rpm/rpmtop/BUILD";
mkdir $self->tmpdir . "/rpm/rpmtop/RPMS";
mkdir $self->tmpdir . "/rpm/rpmtemp";
mkdir $self->tmpdir . "/rpm/rpmbuild";
$self->info->data->{rpmtemprepo} = $self->tmpdir . "/rpm";
if ( !$self->info->data->{arch} ) {
$self->info->data->{arch} = `arch`;
chomp $self->info->data->{arch};
}
$self->template_file( $self->info->confdir . "/templates/spec.template",
$self->tmpdir . "/spec" );
open my $f, ">>" . $self->tmpdir . "/spec";
print $f "%files\n";
print $f "%defattr(-,root,root)\n";
my $installdir = $self->installdir;
if($self->info->data->{gem}) {
$self->install_gemspec;
}
foreach ( $self->listdir($installdir) ) {
my $path = "$installdir/$_";
next if m{/\.packlist$}; # XXX: cleanup in build, not here
# get attributes string
my ( $rpmattr, $is_removed ) = $self->get_rpm_file_attributes($_);
# skip directories unless .keep exists, in which case use %dir
# skip .keep files
next if (/\.keep$/);
lstat($path) or die "can't lstat $path";
# ignore files pruned via the 'remove' attribute
if ($is_removed) {
# non-directories must actually be deleted, else RPM's check-files
# script will abort the build
unless ( -d _ ) {
unlink($path) or die "cannot remove $path : $!";
}
next;
}
if ( -d _ ) {
print $f $rpmattr . "\%dir /$_\n" if ( -e "$path/.keep" );
}
else {
print $f $rpmattr . "/$_\n";
}
}
print $f "\n%changelog\n";
$self->writechangelog($f);
close $f;
open my $g, ">" . $self->tmpdir . "/spec.tmp";
open $f, "<" . $self->tmpdir . "/spec";
while ( my $line = <$f> ) {
print $g $line
unless ( $line =~ /^Conflicts:\s*$/
or $line =~ /^Requires:\s*$/
or $line =~ /^Obsoletes:\s*$/
or $line =~ /^Provides:\s*$/ );
}
close $f;
close $g;
rename $self->tmpdir . "/spec.tmp" => $self->tmpdir . "/spec";
# remove any '.packlist' files created by makemaker
$self->runcmd( "find " . $self->tmpdir . " -name .packlist -exec rm {} \\;" );
# remove .keep files
$self->runcmd( "find " . $self->tmpdir . " -name .keep -exec rm {} \\;" );
my $rpm;
my @ten;
# FATAL ON ERRORS
my $setarch = '';
if($self->info->data->{arch} eq 'i686') {
$setarch = " setarch " . $self->info->data->{arch};
}
@ten = $self->runcmd(
"INSTALLROOT=" . $self->installdir . $setarch .
" rpmbuild -bb --define '_topdir " . $self->tmpdir . "/rpm/rpmtop" .
"' --buildroot " . $self->installdir . " " .
$self->tmpdir . "/spec" );
# return $self->error("Can't run: $@") if($@);
for my $rpmline (@ten) {
if ( $rpmline =~ /Wrote: (.*\.rpm)/ ) {
$rpm = $1;
}
}
unless ($rpm) {
return $self->error("Can't find rpm name");
}
my $myrpm = File::Basename::basename $rpm;
File::Copy::copy( $rpm, $self->cwd . "/$myrpm" )
or die "Unable to copy rpm to cwd: $!";
return $myrpm;
}
sub get_rpm_file_attributes {
my $self = shift;
my $name = shift;
my $attr = $self->get_file_attributes($name);
return ( '', 0 ) unless ( defined($attr) );
# convert attribs to rpm syntax
my @rpmattrs;
if ( exists( $attr->{config} ) ) {
push @rpmattrs, '%config';
}
if ( exists( $attr->{owner} ) || exists( $attr->{group} ) || exists( $attr->{perm} ) ) {
my $mode = ( exists( $attr->{perm} ) ) ? "$attr->{perm}" : '-';
my $user = ( exists( $attr->{owner} ) ) ? $attr->{owner} : 'root';
my $group = ( exists( $attr->{group} ) ) ? $attr->{group} : 'root';
push @rpmattrs, "\%attr($mode, $user, $group)";
}
my $is_removed = ( exists( $attr->{remove} ) ) ? $attr->{remove} : 0;
my $rpmattr = (@rpmattrs) ? ( join( ' ', @rpmattrs ) . ' ' ) : '';
return ( $rpmattr, $is_removed );
}
sub writechangelog {
my $self = shift;
my $output = shift;
my $meta = $self->info->{'meta'};
my $fd;
if ( ref($output) ) {
$fd = $output;
}
else {
open $fd, '>', $output or die "can't open for writing: $output";
}
# rpm changelog happens in reverse
for my $c ( reverse( @{ $meta->{'actionlog'} } ) ) {
my @gmt = gmtime( $c->{'time'} );
my $timestr = strftime( '%a %b %d %Y %H:%M:%S', @gmt );
printf $fd "* \%s \%s \n", $timestr, $c->{'actor'};
for my $m ( @{ $c->{'actions'} } ) {
printf $fd "- \%s\n", $m->{'summary'};
for ( split( "\n", $m->{'text'} ) ) {
s/\%/\%\%/g; # XXX: need to escape '%' in spec file
printf $fd " - \%s\n", $_;
}
}
print $fd "\n";
}
close($fd) unless ref($output);
}
package Seco::Multipkg::Builder::Deb;
use base qw/Seco::Multipkg::Builder/;
BEGIN {
}
sub makepackage {
my $self = shift;