-
Notifications
You must be signed in to change notification settings - Fork 2
/
cpan2rpm
executable file
·2170 lines (1637 loc) · 73.9 KB
/
cpan2rpm
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
#!/usr/bin/perl
#
# cpan2rpm - CPAN module RPM maker
# Copyright (C) 2001-2003 Erick Calder
#
# 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 of the License, 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
#
use vars qw($VERSION $VX);
$VERSION = "2.028";
# --- prologue ----------------------------------------------------------------
use strict;
use warnings;
use Getopt::Long;
use Sys::Hostname;
# Pod::Text was renamed to Pod::Parser at some point
use Pod::Parser;
#use Pod::Text;
my ($ME, $RPM, $TMPDIR, %RPMDIR, $CWD, %info, %meta, $ARGS);
my $tarRE = q/\.(tar\.(g?z|bz2)|tgz|zip)$/;
my $docRE = '(readme|changes|todo|license|install|\.txt|\.html)';
my $HTTPWARN = 0; # so we don't repeat warnings
my $SPECCOL = 10; # col width for generated spec files
*OLDOUT = *OLDERR = *OLDIN = "";
# --- main() ------------------------------------------------------------------
init(); # initialise stuff
for (@ARGV) {
get_mod(); # retrieve a module to work with
get_meta(); # get metadata from tarball
mk_spec(); # create a custom spec file
mk_rpm(); # build the RPM
inst_rpm(); # install it if requested
}
# --- support functionality ---------------------------------------------------
END {
chdir $CWD;
return print("$VERSION\n") if $VX;
print "-- Done --\n";
}
# Stub routine required to prevent destruction of fresh Makefile
sub MM::flush {}
sub init {
$|++; # good for system()
($ME = $0) =~ s|.*/||;
$ENV{PATH} = "/bin:/usr/bin"; # makes suexec happy
chomp($CWD = untaint(qx/pwd/)); # remember where we start
$RPM = inpath("rpmbuild");
$RPM = inpath("rpm") unless $RPM;
die "Cannot find rpmbuild/rpm in PATH" unless $RPM;
# package info defaults
%info = (
url => "http://www.cpan.org",
packager => "Arix International <cpan2rpm\@arix.com>",
group => "Applications/CPAN",
license => "Artistic",
release => 1,
buildroot => "%{_tmppath}/%{name}-%{version}-%(id -u -n)",
defattr => "-,root,root",
);
my %desc = (
# -- simple options
"name=s" => "RPM package name",
"no-prfx" => "suppresses package name prefix",
"no-depchk" => "suppresses dependency check",
"summary=s" => "package summary",
"version=s" => "override the CPAN version number",
"release=s" => "RPM release number",
"epoch=i" => "the package epoch",
"author=s" => "author information",
"packager=s" => "packager identification",
"defattr=s" => "ownership information for installed files",
"distribution=s" => "RPM distribution",
"license=s" => "licensing information",
"group=s" => "RPM group",
"url=s" => "home URL",
"buildroot=s" => "root directory to use for build",
"buildarch=s" => "package architecture",
"description=s" => "package description",
# -- aggregate options
"requires=s@" => "packages required for installation",
"provides=s@" => "modules provided by the package",
"buildrequires=s@" => "packages required for building",
"no-requires=s@" => "suppresses generation of a set of reqs",
"source|s=s@" => "specifies (multiple) sources to use",
"patch|p=s@" => "specifies (multiple) patches to apply",
"doc=s" => "adds to the spec's %doc section",
"define=s@" => "define macros",
# -- section options
"prologue=s@" => "inserts text at beginning of section",
"epilogue=s@" => "inserts text at end of section",
# -- build options
"spec-only" => "only generates spec file",
"spec=s" => "specifies the name of a spec file",
"make-maker=s" => "arguments for makefile creation",
"make=s" => "arguments passed to make",
"make-no-test" => "suppress running test suite",
"make-install=s" => "arguments for make install",
"find-provides=s" => "instructs us to use a given filter",
"find-requires=s" => "(see man page for further details)",
"tempdir=s" => "specify temporary working directory",
"req-scan-all" => "scan all files in a tarball for reqs",
"no-clean" => "suppress --clean",
"shadow-pure" => "override existing pure perl module",
"no-sign" => "prevents signing the package",
"install|i" => "install package when done",
"force" => "forces all operations",
# -- miscellaneous options
"fetch=s" => "fetch method for modules",
"modules|f=s" => "file containing module list",
"mk-rpm-dirs=s" => "creates RPM dirs for non-root users",
"sign-setup:s" => "sets up RPM to support signatures",
"upgrade" => "upgrades cpan2rpm",
"no-upgrade-chk|U" => "no version checks",
"debug:i" => "produce debugging output",
"keep-pipes" => "prevents pipe closing for web UIs",
"help|h" => "this help screen",
"V" => "cpan2rpm version",
"D" => "runs the perl debugger",
);
# get user options from config file ~/.cpan2rpm
my $cfg = "$ENV{HOME}/.$ME";
if (-r $cfg) {
eval "use Getopt::ArgvFile qw/argvFile/";
if ($@) {
print "\n-- cpan2rpm - Ver: $VERSION --\n";
my $msg = "A configuration file [$cfg] is present\n";
$msg .= "but the module [Getopt::ArgvFile] is not available.\n";
$msg .= "Either install the module or remove the config file.\n";
die $msg;
}
argvFile(home=> 1);
}
# get user options from command line
my %opts = ();
my @args = grep !/^-D$/, @ARGV;
$ARGS = join " ", map {/\s/ ? qq/'$_'/ : $_} @args;
my $ret = GetOptions(\%opts, keys %desc);
exec("$^X -d $0 " . join(" ", @args)) if $opts{D};
$VX++, exit if $opts{V};
print "\n-- cpan2rpm - Ver: $VERSION --\n";
syntax(\%desc) if defined $opts{help} || !$ret;
# a clarification on the various keys used in %info:
# dist = user entry of the form <directory>, <tarball-path>,
# <url>, <module-name> - this value may undergo translation
# to eventually evaluate to a filesystem reference
# tarball = always located in SOURCES. Proc-Daemon-1.0.tgz
# name = Proc-Daemon
# module = Proc::Daemon
# evaldir = directory where tarball is extracted
# tardir = the directory name inside the tarball
%info = (%info, %opts);
$info{debug} = 1 if defined $info{debug} && $info{debug} == 0;
print "DEBUG=ON [$info{debug}]\n" if $info{debug};
# set up local account to build packages
if ($info{"mk-rpm-dirs"}) {
mk_rpm_dirs();
exit;
}
if (defined $info{"sign-setup"}) {
sign_setup();
exit;
}
# handle temporary files
if ($TMPDIR = $info{tempdir}) {
mkdir $TMPDIR, 0755 unless -d $TMPDIR;
}
else {
my $msg = "Cannot run without File::Temp, please install ";
$msg .= "or use the --tempdir option.\n";
eval "use File::Temp qw/tempdir/";
$msg = "$@\n$msg" if $info{debug};
die $msg if $@;
$info{dist} ||= "";
$TMPDIR = tempdir(
CLEANUP => $info{"no-clean"} || -d $info{dist} ? 0 : 1
);
};
upgrade() if defined $info{upgrade};
unless ($info{"no-sign"}) {
my $sig = getrpm_macdef("_signature");
$sig =~ s/none//i;
$sig && ($info{sign} = "--sign") || print "Signatures not set up\n";
}
# deal with newer versions
chkupgrade();
# check directory permissions
$RPMDIR{BUILD} = getrpm_macdef("_builddir");
$RPMDIR{SOURCES} = getrpm_macdef("_sourcedir");
$RPMDIR{RPMS} = getrpm_macdef("_rpmdir");
$RPMDIR{SRPMS} = getrpm_macdef("_srcrpmdir");
$RPMDIR{SPECS} = getrpm_macdef("_specdir");
$RPMDIR{ARCH} = getrpm_macdef("_arch");
chkdirs();
# sets empty string if not --buildarch and --spec-only
$info{buildarch} ||= $info{"spec-only"} ? "" : $RPMDIR{ARCH};
# set module download method
my %cpan = (
cpanplus => \&get_cpan_plus,
cpan => \&get_cpan_old,
web => \&get_cpan_web
);
if ($info{fetch}) {
$info{fetch} = lc($info{fetch});
die "Invalid fetch method specified. See man page for --fetch\nStopped"
unless $cpan{ $info{fetch} };
}
$@ = "";
$info{fetch} ||= "web"; # default
if ($info{fetch} eq "cpanplus" && (eval q/use CPANPLUS::Backend/, !$@)) {
print "Fetch: CPAN+\n";
}
elsif ($info{fetch} eq "cpan" && (eval q/use CPAN 0.59/, !$@)) {
print "Fetch: CPAN\n";
}
else {
print "Fetch: HTTP\n";
$info{fetch} = "web";
}
$info{get_mod} = $cpan{$info{fetch}};
# set requirements patch override
$ENV{CPAN2RPM} = 1;
$ENV{CPAN2RPM_REQ_ALL} = $info{"req-scan-all"} || "";
if ($< && $info{install}) {
print "NON ROOT install requires sudo rpm privileges\n";
if (system("sudo rpm --version > /dev/null")) {
my $msg = "sudo failed, cannot use --install option! You can\n";
$msg .= "configure sudo with the following command:\n\n";
$msg .= " echo \"".getlogin()." ALL=/bin/rpm\" >> /etc/sudoers";
die "\n$msg\n\nStopped";
}
print "sudo precheck successful\n";
}
$info{"make-no-test"} = 1 if $info{"no-depchk"};
if ($info{modules}) {
local $_ = $info{modules};
die "Module list file does not exist!" unless -e;
die "Cannot read module list file!" unless -r;
for (split $/, readfile()) {
s/#.*//;
push @ARGV, $_ unless /^\s*$/;
}
}
syntax(\%desc, "No distribution specified!")
unless @ARGV;
}
#
# returns hash of info or ref to it in scalar mode
#
sub get_mod {
my %ret = %info;
$ret{dist} = shift || $_ || die "get_mod(): no args!";
$ret{dist} = $CWD if $ret{dist} eq ".";
print "\n-- module: $ret{dist} --\n";
#
# a url was passed
#
if (isurl($ret{dist})) {
$ret{tarball} = write_url($RPMDIR{SOURCES}, $ret{dist})
|| die "Unable to retrieve tarball";
push @{$ret{source}}, $ret{dist};
}
#
# argument passed is a local file name
#
elsif (istarball($ret{dist}, 1)) {
cp($ret{dist}, $RPMDIR{SOURCES})
|| die "get_mod(): cp [$ret{dist}] - $!"
;
($ret{tarball} = $ret{dist}) =~ s|.*/||;
}
#
# argument passed is a directory
#
elsif (-d $ret{dist}) {
chdir $ret{dist};
}
#
# assume argument passed is a Perl module name
#
else {
$ret{tarball} = $ret{get_mod}(\%ret);
}
local $_ if defined wantarray();
$_ = wantarray() ? %ret : \%ret;
}
sub get_meta {
my $info = shift || $_; local $_;
#my $pod = Pod::Text->new();
my $pod = Pod::Parser->new();
print "Metadata retrieval\n";
# extract tarball
unless (-d ($info->{evaldir} = $info->{dist})) {
my $f = "$RPMDIR{SOURCES}/$info->{tarball}";
print "Tarball extraction: [$f]\n";
$info->{evaldir} = untar($f);
}
eval "use File::Find";
if ($@) {
local $_ = "Cannot automatically determine architecture for\n";
$_ .= "package - defaulting to %_arch macro (this value\n";
$_ .= "may be overridden with the --buildarch parameter.\n";
$_ .= "Please install File::Find to allow for automatic\n";
$_ .= "arch selection.";
$_ .= "\n$@" if $info->{debug};
print;
}
else {
my $xs = 0;
find(sub { $xs = 1 if /\.(xs|c)$/i }, $info->{evaldir});
$info->{buildarch} = "noarch" if $xs == 0;
}
chdir $info->{evaldir} || die "get_meta(): $!";
$_ = "$info->{evaldir}/Build.PL";
$_ = "$info->{evaldir}/Makefile.PL" unless -e;
die qq/No PL file [$_] in tarball/ unless -e;
die qq/Cannot read PL file [$_]/ unless -r;
($info->{PL} = $_) =~ s|.*/||;
# we want to protect us from exit()ing but without modifying the
# actual Makefile.PL since we may be operating in a source directory
my $PL = readfile();
$PL =~ s/\bexit\b/return/gs;
my $t = "$info->{evaldir}/PL.$$";
writefile($t, $PL);
# now we get the arguments passed to the method that creates
# the make script (WriteMakefile || create_build_script)
{
no warnings;
# dynamically load the module (so when the PL file loads it
# we already own it and have hijacked the appropriate method
my $PLMOD = "ExtUtils::MakeMaker";
if ($info->{PL} =~ /^Build/) {
$PLMOD = "Module::Build";
# Module::Build builds itself using itself
# but we don't have $info->{name} yet, so we use the tarball name
unshift @INC, 'lib' if $info->{tarball} =~ /^Module-Build/;
}
$@ = ""; eval "use $PLMOD"; die "$PLMOD unloadable\n $@" if $@;
# grab parameters to function e.g. WriteMakefile()
my $PLFN = "ExtUtils::MakeMaker::WriteMakefile";
$PLFN = "Module::Build::new" if $info->{PL} =~ /^Build/;
eval qq/*${PLFN}_orig = \\&$PLFN/;
eval qq/*$PLFN = sub {
die "SAFETY ABORT!" if \$ENV{_DEEP_RECURSION}++ > 10;
%meta = \@_ unless %meta;
*$PLFN = \\&${PLFN}_orig;
goto &$PLFN;
};/;
local @ARGV = ();
local $0 = $t;
push @ARGV, $info->{"make-maker"} if $info->{"make-maker"};
# no input or output
std_close();
# execute the makefile
my $ok = 0;
eval {
do $t; $ok++;
};
my $err = $@;
# clean up
std_restore();
unlink $t || die "get_meta(): rm[$t] - $!";
if (!$ok) {
$err =~ s/$t/$info->{PL}/g;
die "FATAL CRASH! Could not load $info->{PL}:\n$err";
}
# map Build.PL hash keys to MakeMaker's
if ($info->{PL} =~ /^Build/) {
my %b2m = qw/
requires PREREQ_PM
module_name NAME
dist_author AUTHOR
dist_version VERSION
dist_version_from VERSION_FROM
/;
$meta{$b2m{$_}} = $meta{$_} for keys %b2m;
}
}
# make sure dependencies are installed
my @deps;
my $deps = $meta{PREREQ_PM};
for (keys %$deps) {
my $use = "use $_";
$use .= " $deps->{$_}" if $deps->{$_};
local $^W = 0;
$@ = ""; eval $use;
push @deps, "$_ >= $deps->{$_}" if $@;
}
my $msg = "Unable to build module, the following dependencies have failed:";
die "$msg\n " . join("\n ", @deps) . "\nStopped"
if @deps && !$info->{"no-depchk"};
print "Dependency check skipped (--make-no-test implied)\n"
if $info->{"no-depchk"};
# figure out package name
$info->{module} ||= $meta{DISTNAME} || $meta{NAME};
if (!$info->{module}) {
# for directories, guess at the tarball name
if (-d $info->{dist}) {
($info->{tarball} = $info->{dist}) =~ s|.*/||;
# a source directory may include a version #
$info->{tarver} = $info->{tarball} =~ s/-(\d+\.?\d*)$// ? $1 : "";
}
$info->{module} = $info->{tarball};
$info->{module} =~ s/-(\d+\.?\d*)$tarRE$//i;
$info->{module} =~ s/-/::/g;
}
($info->{name} = $info->{module}) =~ s/::/-/g unless $info->{name};
$info->{tarball} = $info->{name} if -d $info->{dist};
die "No package name available. Stopped"
unless $info->{name};
# get module description info
my $from = $meta{ABSTRACT_FROM} || $meta{VERSION_FROM};
($from = "$info->{module}.pm") =~ s/.*:// unless $from;
$from = readfile($from);
$meta{ABSTRACT} ||= "";
if (!$meta{ABSTRACT} && $from) {
$meta{ABSTRACT} = $pod->interpolate($1)
if $from =~ /=head\d\s+NAME.*?-\s*(.*?)$/ism;
}
$meta{DESCRIPTION} ||= "";
if (!$meta{DESCRIPTION} && $from) {
if ($from =~ /=head\d\s+DESCRIPTION\s+(.*?)=(head|cut)/ism) {
$meta{DESCRIPTION} = $pod->interpolate($1)
}
elsif ($from =~ /=head\d\s+SYNOPSIS\s+(.*?)=(head|cut)/ism) {
$meta{DESCRIPTION} = $pod->interpolate($1)
}
}
$info->{author} ||= $meta{AUTHOR} || "";
if (!$info->{author} && $from =~ /=head\d\s+AUTHORS?\s+(.*?)=/is) {
local $_ = $1;
my ($em) = /(\S+(@|\sat\s)\S+)/is;
$em ||= "";
$em =~ s/\.$//; # e.g. Erick Calder [email protected].
($_) = /((?:\S+\s+){1,3})\Q$em\E/is;
$_ ||= "";
s/[^a-zA-Z ]+//g; # Peter Behroozi, [email protected]
trim(); s/\s+/ /g;
$em =~ s/\s+at\s+/@/g;
$em =~ s/[()<>]//g; # Eryq ([email protected])
$_ .= " <$em>";
s/E<(lt|gt)>//ig; # Erick Calder E<lt>[email protected]<gt>
$info->{author} = $_;
}
if (!$info->{author}) {
# Extract generic author from any of the urls
for (@{$info->{source}}) {
if (isurl() && m%author.*/([A-Z\-]+)/[^/]+$%) {
$info->{author} = (lc $1).'@cpan.org';
last;
}
}
}
die "No author information found, please use --author option. Stopped"
unless $info->{author};
# extract version
$info->{version} ||= $meta{VERSION};
unless ($info->{version}) {
$info->{version} = ExtUtils::MM_Unix->parse_version($from)
if $from = $meta{VERSION_FROM};
trim($info->{version}); # parse_version() returns spaces
}
$info->{version} ||= $info->{tarver};
die "No version found, please use --version option. Stopped"
unless $info->{version};
# refix all macro dirs for %name, %version
chkdirs();
$info->{spec} ||= "$RPMDIR{SPECS}/$info->{name}.spec";
($info->{tardir} = $info->{evaldir}) =~ s|.*/||;
# for directories, guess some of the needed values
if (-d $info->{dist}) {
($info->{tardir} = $info->{module}) =~ s|::|-|g;
$info->{tardir} .= "-$info->{version}"
unless $info->{tardir} =~ /-(\d+.*)$/;
$info->{tarball} = "$info->{tardir}.tar.gz";
}
# tarballs without a subdir need one created
$info->{create} = $info->{tardir} =~ s/\+$// ? "-c" : "";
# create file-list for spec's %doc section
my %doc;
for (split $/, readfile("MANIFEST")) {
s|[/\s].*||;
# get subdirs (with some exceptions)
$doc{$_} = 1, next if -d && !/^(t|lib)$/;
# list all files that match the regexp
$doc{$_} = 1 if /$docRE/i;
}
$info->{doc} ||= "";
$info->{doc} = join " ", $info->{doc}, keys %doc
unless $info->{doc} =~ s/^=//;
$info->{doc} &&= "%doc $info->{doc}";
# fixes the #! so perl can be found (and deps get made ok)
$info->{fixin} = <<EOF;
grep -rsl '^#!.*perl' . |
grep -v '\.bak\$' |xargs --no-run-if-empty \\
%__perl -MExtUtils::MakeMaker -e 'MY->fixin(\@ARGV)'
EOF
# assemble other info
$info->{summary} = "$info->{name} - " . ($meta{ABSTRACT} || "Perl module");
$info->{description} ||= $meta{DESCRIPTION} || "None.";
push @{$info->{source}}, $info->{tarball}
unless $info->{source};
$info->{changelog} = changelog();
$info->{"find-provides"}
&&= qq/%define __find_provides $info->{"find-provides"}/;
$info->{"find-requires"} &&= qq/
%define __find_requires $info->{"find-requires"}\n
%define __perl_requires $info->{"find-requires"}
/;
# option lists
$info->{"opts-simple"} = [qw/
name summary version release epoch
vendor packager distribution license group url
buildroot buildarch
/];
$info->{"opts-agg"} = [qw/
provides requires buildrequires source patch
/];
$info->{"opts-secs"} = [qw/
prep build install clean changelog tag files
/];
# handle aggregate options
$info->{"$_-list"} = optagg($info) for @{$info->{"opts-agg"}};
$info->{"define-list"} .= "";
foreach (@{$info->{"define"}}) {
if (/^(\w)=(.*)/) {
$info->{"define-list"} .= "%define $1 $2\n";
}
else {
$info->{"define-list"} .= "%define $_\n";
}
}
# section handlers
my $prologue = $info->{prologue};
delete $info->{prologue};
for (@$prologue) {
/^(\w+):(.*)$/ || next;
my ($sec, $v) = ($1, $2);
$info->{prologue}{$sec} = $v . $/;
}
my $epilogue = $info->{epilogue};
delete $info->{epilogue};
for (@$epilogue) {
/^(\w+):(.*)$/ || next;
my ($sec, $v) = ($1, $2);
$info->{epilogue}{$sec} = $v . $/;
}
for (@{$info->{"opts-secs"}}) {
$info->{prologue}{$_} ||= "";
$info->{epilogue}{$_} ||= "";
}
# special situations
if ($info->{"no-requires"}) {
my $noreqs = "";
for (@{$info->{"no-requires"}}) {
$noreqs .= qq/-e '$_' / for split /\s*,\s*/;
}
delete $info->{"no-requires"};
$info->{"no-requires"}{"define"}
= "%define custom_find_req %{_tmppath}/%{NVR}-find-requires";
$info->{"find-requires"}
= "%define _use_internal_dependency_generator 0";
$info->{"find-requires"}
.= "\n%define __find_requires %{custom_find_req}";
$info->{"find-requires"}
.= "\n%define __perl_requires %{custom_find_req}";
local $_ = qq[cat <<EOF > %{custom_find_req}
#!/bin/sh
/usr/lib/rpm/find-requires |grep -v $noreqs
EOF
chmod 755 %{custom_find_req}
];
s/^\s+//mg;
$info->{"no-requires"}{"install"} = $_;
$info->{"no-requires"}{"clean"} = "rm -f %{custom_find_req}";
}
$info->{"no-requires"}{"define"} ||= "";
$info->{"no-requires"}{"install"} ||= "";
$info->{"no-requires"}{"clean"} ||= "";
# fix patch info
my $i = 0;
$info->{"patch-files"} = "";
$info->{"patch-apply"} = "";
for (split $/, $info->{"patch-list"}) {
s/.*:\s+//;
$info->{"patch-files"}
.= sprintf("%-*s %s\n", $SPECCOL, "Patch$i:", $_);
$info->{"patch-apply"} .= "%patch$i -p1\n";
# put patches in RPM dir if needed
cp($_, $RPMDIR{SOURCES})
|| die "Unable to copy patch [$_]: $!";
$i++;
}
# return to user's directory
chdir $CWD;
}
#
# generate s spec file
#
sub mk_spec {
my $info = shift || $_; local $_;
print "Generating spec file\n";
# clean warnings about missing keys
$info->{$_} ||= "" for keys(%info), @{$info->{"opts-simple"}};
$info->{$_} ||= "" for qw/make fixin/; # extra tags
$info->{"$_-list"} ||= "" for @{$info->{"opts-agg"}};
# strip ctrl-M's from Windoze files
$info->{$_} =~ s/\r//g for keys %info;
# generalise whenever possible
for (qw/tardir source/) {
$info->{$_} =~ s/$info->{name}/%{pkgname}/;
$info->{$_} =~ s/$info->{version}/%{version}/;
}
$info->{description} =~ s/\s+$//;
$info->{maketest} = $info->{"make-no-test"} ? 0 : 1;
$info->{vendor} = $info->{author};
if ($info->{name} eq "ExtUtils-MakeMaker") {
# MakeMaker builds itself using itself
$ENV{PERL5LIB} = $ENV{PERL5LIB}?"lib:$ENV{PERL5LIB}":"lib";
}
# Versions between 5.91 and 6.05 need PREFIX= on Makefile.PL line
my $perl = q/`%%{__perl} -MExtUtils::MakeMaker -e '%s'`/;
my $mm_maker = q#
print qq|PREFIX=%{buildroot}%{_prefix}|
if \\$ExtUtils::MakeMaker::VERSION =~ /5\.9[1-6]|6\.0[0-5]/
#;
$mm_maker =~ s/\s+/ /gs;
$info->{"make-maker"} ||= sprintf($perl, $mm_maker);
# Versions before 5.91 need PREFIX= on install line
# Versions after 6.05 need DESTDIR= on install line
my $mm_install = q#
print \\$ExtUtils::MakeMaker::VERSION <= 6.05
? qq|PREFIX=%{buildroot}%{_prefix}|
: qq|DESTDIR=%{buildroot}|
#;
$mm_install =~ s/\s+/ /gs;
$info->{"make-install"} ||= sprintf($perl, $mm_install);
$info->{"make-install"} = "destdir=%{buildroot}"
if $info->{PL} =~ /^Build/;
if ($info->{"shadow-pure"}) {
# Force pure perl installs into first @INC slot
$info->{"make-maker"} .= sprintf("; %%{__perl} -pi -e '%s' Makefile",
q|s/^(INSTALL[PVS]\w+LIB =).*/$1 \$(INSTALLARCHLIB)/|
);
# Avoid man page conflicts with default
$info->{"make-maker"} .= sprintf("; %%{__perl} -pi -e '%s' Makefile",
q|s,(INSTALLMAN3DIR =[^/]+)/.*,$1/man/man3,|
);
}
# prepend string to separate module from usual namespace
my $pkgname = $info->{name};
$info->{name} = "perl-" . $info->{name} unless $info->{"no-prfx"};
my $spec = <<ZZ;
#
# - $info->{module} -
# This spec file was automatically generated by cpan2rpm [ver: $VERSION]
ZZ
my $me; ($me = $0) =~ s|.*/||;
$spec .= <<ZZ unless $info->{module} eq $me;
# The following arguments were used:
# $ARGS
ZZ
$spec .= <<ZZ;
# For more information on cpan2rpm please visit: http://perl.arix.com/
#
%define pkgname $pkgname
%define filelist %{pkgname}-%{version}-filelist
%define NVR %{pkgname}-%{version}-%{release}
%define maketest $info->{maketest}
ZZ
$spec .= <<ZZ if $info->{"define-list"};
# user definitions
$info->{"define-list"}
ZZ
$spec =~ s/^[^\S\n]+//mg;
$spec .= $info->{"no-requires"}{"define"} . $/
if $info->{"no-requires"}{"define"};
$spec .= $info->{"find-provides"} . $/
if $info->{"find-provides"};
$spec .= $info->{"find-requires"} . $/
if $info->{"find-requires"};
# add simple tags
$spec .= "\n";
for (@{$info->{"opts-simple"}}) {
$spec .= sprintf("%-*s %s\n", $SPECCOL, "$_:", $info->{$_})
if $info->{$_};
}
$spec .= sprintf("%-*s %s\n", $SPECCOL, "prefix:", "%(echo %{_prefix})");
# add lists
$spec .= $info->{"provides-list"};
$spec .= $info->{"requires-list"};
$spec .= $info->{"buildrequires-list"};
$spec .= $info->{"source-list"};
$spec .= $info->{"patch-files"};
$spec .= $info->{epilogue}{tag};
$spec .= "\n" . q/%description/ . "\n$info->{description}\n";
$_ = <<ZZ;
#
# This package was generated automatically with the cpan2rpm
# utility. To get this software or for more information
# please visit: http://perl.arix.com/
#
ZZ
s/^[^\S\n]+//mg; $spec .= $_;
# handle sections
$_ = "%setup -q -n $info->{tardir} $info->{create}";
$_ .= $/ . $info->{'patch-apply'}
if $info->{"patch-apply"};
$_ .= $/ . "chmod -R u+w %{_builddir}/$info->{tardir}" . $/;
$spec .= mksec($info, "prep" => $_);
$_ = ($info->{PL} =~ /^Make/)
? qq/
$info->{fixin}
CFLAGS="\$RPM_OPT_FLAGS"
%{__perl} Makefile.PL $info->{"make-maker"}
%{__make} $info->{"make"}
%if %maketest
%{__make} test
%endif
/ : qq/
$info->{fixin}
%{__perl} Build.PL
%{__perl} Build
%if %maketest
%{__perl} Build test
%endif
/;
s/^\s+//mg;
$spec .= mksec($info, "build" => $_);
my $install = ($info->{PL} =~ /^Make/)
? qq/%{makeinstall} /
: qq/%{__perl} Build install /
;
$install .= $info->{"make-install"};
$_ = <<ZZ;
[ "%{buildroot}" != "/" ] && rm -rf %{buildroot}
$info->{"no-requires"}{"install"}
$install
cmd=/usr/share/spec-helper/compress_files
[ -x \$cmd ] || cmd=/usr/lib/rpm/brp-compress
[ -x \$cmd ] && \$cmd
# SuSE Linux
if [ -e /etc/SuSE-release -o -e /etc/UnitedLinux-release ]
then
%{__mkdir_p} %{buildroot}/var/adm/perl-modules
%{__cat} `find %{buildroot} -name "perllocal.pod"` \\
| %{__sed} -e s+%{buildroot}++g \\
> %{buildroot}/var/adm/perl-modules/%{name}
fi
# remove special files
find %{buildroot} -name "perllocal.pod" \\
-o -name ".packlist" \\
-o -name "*.bs" \\
|xargs -i rm -f {}
# no empty directories
find %{buildroot}%{_prefix} \\
-type d -depth \\
-exec rmdir {} \\; 2>/dev/null
%{__perl} -MFile::Find -le '
find({ wanted => \\&wanted, no_chdir => 1}, "%{buildroot}");
print "$info->{doc}";
for my \$x (sort \@dirs, \@files) {
push \@ret, \$x unless indirs(\$x);
}
print join "\\n", sort \@ret;
sub wanted {
return if /auto\$/;
local \$_ = \$File::Find::name;
my \$f = \$_; s|^\\Q%{buildroot}\\E||;
return unless length;
return \$files[\@files] = \$_ if -f \$f;
\$d = \$_;
/\\Q\$d\\E/ && return for reverse sort \@INC;
\$d =~ /\\Q\$_\\E/ && return
for qw|/etc %_prefix/man %_prefix/bin %_prefix/share|;
\$dirs[\@dirs] = \$_;
}
sub indirs {
my \$x = shift;
\$x =~ /^\\Q\$_\\E\\// && \$x ne \$_ && return 1 for \@dirs;
}
' > %filelist
[ -z %filelist ] && {
echo "ERROR: empty %files listing"
exit -1
}
ZZ
s/^ {8}//mg;
$spec .= mksec($info, "install" => $_);
$_ = qq|[ "%{buildroot}" != "/" ] && rm -rf %{buildroot}\n|;
# $_ .= qq|rm -rf %_builddir/%name-%version\n|;
$_ .= qq/$info->{"no-requires"}{"clean"}\n/
if $info->{"no-requires"}{"clean"};
$spec .= mksec($info, "clean" => $_);
$spec .= qq|$/%files -f %filelist|;
$spec .= qq|$/%defattr($info->{defattr})$/|;
$spec .= $info->{epilogue}{files};
$spec .= mksec($info, "changelog"
=> "* $info->{changelog}\n- Initial build."
);
writefile($info->{spec}, $spec);
print("SPEC: $info->{spec}\n");
exit if $info->{"spec-only"};
}