/
Arch.pm
1311 lines (1039 loc) · 39.1 KB
/
Arch.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 CPANPLUS::Dist::Arch;
use warnings;
use strict;
use base qw(CPANPLUS::Dist::Base Exporter);
use File::Spec::Functions qw(catfile catdir);
use Module::CoreList qw();
use CPANPLUS::Error qw(error msg);
use Digest::MD5 qw();
use Pod::Select qw();
use List::Util qw(first);
use File::Path qw(make_path);
use File::Copy qw(copy);
use File::stat qw(stat);
use DynaLoader qw();
use IPC::Cmd qw(can_run);
use version qw(qv);
use English qw(-no_match_vars);
use Carp qw(carp croak confess);
use Cwd qw();
our $VERSION = '0.20';
our @EXPORT = qw();
our @EXPORT_OK = qw(dist_pkgname dist_pkgver);
our @EXPORT_TAGS = ( ':all' => \@EXPORT_OK );
#-----------------------------------------------------------------------------
# CLASS CONSTANTS
#-----------------------------------------------------------------------------
my $MKPKGCONF_FQP = '/etc/makepkg.conf';
my $CPANURL = 'http://search.cpan.org';
my $ROOT_USER_ID = 0;
my $CFG_VALUE_MATCH = '\A \s* (%s) \s* = \s* (.*?) \s* (?: \#.* )? \z';
my $NONROOT_WARNING = <<'END_MSG';
In order to install packages as a non-root user (highly recommended)
you must have a sudo-like command specified in your CPANPLUS
configuration.
END_MSG
# META.yml abstract entries we should ignore.
my @BAD_METAYML_ABSTRACTS
= ( q{~}, 'Module abstract (<= 44 characters) goes here' );
# Patterns to use when using pacman for finding library owners.
my $PACMAN_FINDOWN = qr/\A[^ ]+ is owned by ([\w-]+) ([\w.-]+)/;
my $PACMAN_FINDOWN_ERR = qr/\Aerror:/;
# Override a package's name to conform to packaging guidelines.
# Copied entries from CPANPLUS::Dist::Pacman and alot more
# from searching for packages with perl in their name in
# [extra] and [community]
my $PKGNAME_OVERRIDES =
{ map { split /[\s=]+/ } split /\s*\n+\s*/, <<'END_OVERRIDES' };
libwww-perl = perl-libwww
glade-perl-two = perl-glade-two
aceperl = perl-ace
Gnome2-GConf = gconf-perl
Gtk2-GladeXML = glade-perl
Glib = glib-perl
Gnome2 = gnome-perl
Gnome2-VFS = gnome-vfs-perl
Gnome2-Canvas = gnomecanvas-perl
Gtk2 = gtk2-perl
XML-LibXML = libxml-perl
mod_perl = mod_perl
Pango = pango-perl
XML-Parser = perlxml0
SDL_Perl = sdl_perl
shorewall-perl = shorewall-perl
END_OVERRIDES
# This var tells us whether to use a template module or our internal code:
my $TT_MOD_NAME;
my @TT_MOD_SEARCH = qw/ Template Template::Alloy Template::Tiny /;
# Crude template for our PKGBUILD script
my $PKGBUILD_TEMPL = <<'END_TEMPL';
# Contributor: [% packager %]
# Generator : CPANPLUS::Dist::Arch [% version %]
pkgname='[% pkgname %]'
pkgver='[% pkgver %]'
pkgrel='[% pkgrel %]'
pkgdesc="[% pkgdesc %]"
arch=('i686' 'x86_64')
license=('PerlArtistic' 'GPL')
options=('!emptydirs')
depends=([% depends %])
url='[% url %]'
source=('[% source %]')
md5sums=('[% md5sums %]')
build() {
DIST_DIR="${srcdir}/[% distdir %]"
export PERL_AUTOINSTALL=--skipdeps PERL_MM_USE_DEFAULT=1
{ cd "$DIST_DIR" &&
[% IF is_makemaker %]
perl Makefile.PL INSTALLDIRS=vendor &&
make &&
[% IF skiptest %]#[% END %]make test &&
make DESTDIR="$pkgdir" install;
[% END %]
[% IF is_modulebuild %]
perl Build.PL --installdirs=vendor --destdir="$pkgdir" &&
perl Build &&
[% IF skiptest %]#[% END %]perl Build test &&
perl Build install;
[% END %]
} || return 1;
find "$pkgdir" -name .packlist -o -name perllocal.pod -delete
}
END_TEMPL
=for Weird "perl Build" Syntax
We use "perl Build" above instead of the normal "./Build" in order to
make the yaourt packager happy. Yaourt runs the PKGBUILD under the /tmp
directory and makepkg will fail if /tmp is a seperate partition mounted
with noexec. Thanks to xenoterracide on the AUR for mentioning the
problem.
=cut
#----------------------------------------------------------------------
# CLASS GLOBALS
#----------------------------------------------------------------------
our ($Is_dependency, $PKGDEST, $PACKAGER, $DEBUG);
$PACKAGER = 'Anonymous';
$DEBUG = $ENV{DIST_ARCH_DEBUG};
sub _DEBUG
{
print STDERR '***DEBUG*** ', @_, "\n" if $DEBUG;
}
#---HELPER FUNCTION---
# Purpose: Expand environment variables and tildes like bash would.
#---------------------
sub _shell_expand
{
my $dir = shift;
$dir =~ s/ \A ~ / $ENV{HOME} /xmse; # tilde = homedir
$dir =~ s/ (?<!\\) \$ (\w+) / $ENV{$1} || q{} /xmseg; # expand env vars
$dir =~ s/ \\ [a-zA-Z] / /xmsg;
$dir =~ s/ \\ (.) / $1 /xmsg; # escaped special
# chars
return $dir;
}
READ_CONF:
{
# Read makepkg.conf to see if there are system-wide settings
my $mkpkgconf;
if ( ! open $mkpkgconf, '<', $MKPKGCONF_FQP ) {
error "Could not read $MKPKGCONF_FQP: $!";
last READ_CONF;
}
my %cfg_vars = ( 'PKGDEST' => \$PKGDEST,
'PACKAGER' => \$PACKAGER );
my $cfg_field_match = sprintf $CFG_VALUE_MATCH,
join '|', keys %cfg_vars;
CFG_LINE:
while (<$mkpkgconf>) {
chomp;
next CFG_LINE unless ( my ($name, $value) = /$cfg_field_match/xmso );
${ $cfg_vars{$name} } =
( $value =~ m/\A"(.*)"\z/
? _shell_expand( $1 ) # expand double quotes
: ( $value =~ m/\A'(.*)'\z/
? $1 # dont single quotes
: _shell_expand( $value )));
}
close $mkpkgconf or error "close on makepkg.conf: $!";
}
#-----------------------------------------------------------------------------
# PUBLIC CPANPLUS::Dist::Base Interface
#-----------------------------------------------------------------------------
=for Interface Methods
See CPANPLUS::Dist::Base's documentation for a description of the
purpose of these functions. All of these "interface" methods override
Base's default actions in order to create our packages.
=cut
#---INTERFACE METHOD---
# Purpose : Checks if we have makepkg and pacman installed
# Returns : 1 - if we have the tools needed to make a pacman package.
# 0 - if we don't think so.
#----------------------
sub format_available
{
for my $prog ( qw/ makepkg pacman / ) {
if ( ! can_run($prog) ) {
error "CPANPLUS::Dist::Arch needs to run $prog, to work properly";
return 0;
}
}
return 1;
}
#---INTERFACE METHOD---
# Purpose : Initializes our object internals to get things started
# Returns : 1 always
#----------------------
sub init
{
my $self = shift;
$self->status->mk_accessors( qw{ pkgname pkgver pkgbase pkgdesc
pkgurl pkgsize pkgarch pkgrel
builddir destdir
pkgbuild_templ tt_init_args } );
return 1;
}
#---INTERFACE METHOD---
# Purpose : Prepares the files and directories we will need to build a
# package. Also prepares any data we expect to have later,
# on a per-object basis.
# Return : 1 if ok, 0 on error.
# Postcond : Sets $self->status->prepare to 1 or 0 on success or
# failure.
#----------------------
sub prepare
{
my $self = shift;
my $status = $self->status; # Private hash
my $module = $self->parent; # CPANPLUS::Module
my $intern = $module->parent; # CPANPLUS::Internals
my $conf = $intern->configure_object; # CPANPLUS::Configure
my $distcpan = $module->status->dist_cpan; # CPANPLUS::Dist::MM or
# CPANPLUS::Dist::Build
$self->_prepare_status;
$status->prepared(0);
# Call CPANPLUS::Dist::Base's prepare to resolve our pre-reqs.
return $self->SUPER::prepare(@_);
}
#---HELPER FUNCTION---
sub _find_built_pkg
{
my ($self, $pkg_type, $destdir) = @_;
my $status = $self->status;
my $pkgfile = catfile( $destdir,
join '-',
( $status->pkgname,
$status->pkgver,
$status->pkgrel,
join '.',
( $pkg_type eq q{bin}
? ( $status->pkgarch, 'pkg' )
: 'src' ),
'tar',
));
_DEBUG "Searching for file starting with $pkgfile";
my ($found) =
( grep { -f $_ }
map { "$pkgfile.$_" } qw/ xz gz / );
_DEBUG ( $found ? "Found $found" : "No package file found!" );
return $found;
}
#---INTERFACE METHOD---
# Purpose : Creates the pacman package using the 'makepkg' command.
#----------------------
sub create
{
my ($self, %opts) = (shift, @_);
my $status = $self->status; # Private hash
my $module = $self->parent; # CPANPLUS::Module
my $intern = $module->parent; # CPANPLUS::Internals
my $conf = $intern->configure_object; # CPANPLUS::Configure
my $distcpan = $module->status->dist_cpan; # CPANPLUS::Dist::MM or
# CPANPLUS::Dist::Build
# Create directories for building and delivering the new package.
MKDIR_LOOP:
for my $dir ( $status->pkgbase, $status->destdir ) {
if ( -e $dir ) {
die "$dir exists but is not a directory!" unless ( -d _ );
die "$dir exists but is read-only!" unless ( -w _ );
next MKDIR_LOOP;
}
make_path( $dir, { verbose => $opts{verbose} ? 1 : 0 });
}
my $pkg_type = $opts{pkg} || $opts{pkgtype} || 'bin';
$pkg_type = lc $pkg_type;
unless ( $pkg_type =~ /^(?:bin|src)$/ ) {
error qq{Invalid package type requested: "$pkg_type"
Package type must be 'bin' or 'src'};
return 0;
}
if ( $opts{verbose} ) {
my %fullname = ( bin => 'binary', src => 'source' );
msg "Creating a $fullname{$pkg_type} pacman package";
}
if ( $pkg_type eq 'bin' ) {
# Use CPANPLUS::Dist::Base to make packages for pre-requisites...
# (starts the packaging process for any missing ones)
my @ok_resolve_args = qw/ verbose target force prereq_build /;
my %resolve_args = map { ( exists $opts{$_} ?
($_ => $opts{$_}) : () ) } @ok_resolve_args;
local $Is_dependency = 1; # only top level pkgs explicitly installed
$distcpan->_resolve_prereqs( %resolve_args,
'format' => ref $self,
'prereqs' => $module->status->prereqs );
}
# Prepare our file name paths for pkgfile and source tarball...
my $srcfile_fqp = $status->pkgbase . '/' . $module->package;
$status->destdir( $opts{destdir} ) if $opts{destdir};
my $destdir = $status->destdir;
$destdir = Cwd::abs_path( $destdir );
# Prepare our 'makepkg' package building directory,
# namely the PKGBUILD and source tarball files...
if ( ! -e $srcfile_fqp ) {
my $tarball_fqp = $module->_status->fetch;
link $tarball_fqp, $srcfile_fqp
or error "Failed to create link to $tarball_fqp: $OS_ERROR";
}
$self->create_pkgbuild( $self->status->pkgbase, $opts{skiptest} );
# Package it up!
local $ENV{PKGDEST} = $destdir;
my $oldcwd = Cwd::getcwd();
chdir $status->pkgbase or die "chdir: $OS_ERROR";
my $makepkg_cmd = join q{ }, ( 'makepkg',
'-f', # should we force rebuilding?
( $EUID == 0 ? '--asroot' : () ),
( $pkg_type eq 'src' ? '--source' : () ),
( $opts{nocolor} ? '--nocolor' : () ),
( $opts{quiet} ? '2>&1 >/dev/null'
: () ),
);
# I tried to use IPC::Cmd here, but colors didn't work...
system $makepkg_cmd;
if ( $CHILD_ERROR ) {
error ( $CHILD_ERROR & 127
? sprintf "makepkg failed with signal %d", $CHILD_ERROR & 127
: sprintf "makepkg returned abnormal status: %d",
$CHILD_ERROR >> 8 );
return 0;
}
chdir $oldcwd or die "chdir: $OS_ERROR";
$status->dist( $self->_find_built_pkg( $pkg_type, $destdir ));
return $status->created( 1 );
}
#---INTERFACE METHOD---
# Purpose : Installs the package file (.pkg.tar.gz) using sudo and
# pacman.
# Comments : Called automatically on pre-requisite packages
#----------------------
sub install
{
my ($self, %opts) = (shift, @_);
my $status = $self->status; # Private hash
my $module = $self->parent; # CPANPLUS::Module
my $intern = $module->parent; # CPANPLUS::Internals
my $conf = $intern->configure_object; # CPANPLUS::Configure
my $pkgfile_fqp = $status->dist;
unless ( $pkgfile_fqp ) {
error << 'END_ERROR';
Path to package file has not been set.
Someone is using CPANPLUS::Dist::Arch incorrectly.
Tell them to call create() before install().
END_ERROR
return 0;
}
die "Package file $pkgfile_fqp was not found" if ( ! -f $pkgfile_fqp );
my @pacmancmd = ( 'pacman', '-U', $pkgfile_fqp,
( $Is_dependency ? '--asdeps' : '--asexplicit' ),
);
# Make sure the user has access to install a package...
my $sudocmd = $conf->get_program('sudo');
if ( $EFFECTIVE_USER_ID != $ROOT_USER_ID ) {
if ( $sudocmd ) {
unshift @pacmancmd, $sudocmd;
# $pacmancmd = "$sudocmd pacman -U $pkgfile_fqp";
}
else {
error $NONROOT_WARNING;
return 0;
}
}
system @pacmancmd;
if ( $CHILD_ERROR ) {
error ( $CHILD_ERROR & 127
? sprintf qq{'@pacmancmd' failed with signal %d},
$CHILD_ERROR & 127
: sprintf qq{'@pacmancmd' returned abnormal status: %d},
$CHILD_ERROR >> 8
);
return 0;
}
return $status->installed(1);
}
#-----------------------------------------------------------------------------
# EXPORTED FUNCTIONS
#-----------------------------------------------------------------------------
sub dist_pkgname
{
croak "Must provide arguments to dist_pkgname" if ( @_ == 0 );
my ($distname) = @_;
# Override this package name if there is one specified...
return $PKGNAME_OVERRIDES->{$distname}
if $PKGNAME_OVERRIDES->{$distname};
# Package names should be lowercase and consist of alphanumeric
# characters only (and hyphens!)...
$distname = lc $distname;
$distname =~ tr/_/-/;
$distname =~ tr/-a-z0-9//cd;
$distname =~ tr/-/-/s;
# Delete leading or trailing hyphens...
$distname =~ s/\A-//;
$distname =~ s/-\z//;
die qq{Dist name '$distname' completely violates packaging standards}
if ( ! $distname );
if ( $distname !~ / (?: \A perl ) | (?: -perl \z ) /xms ) {
$distname = "perl-$distname";
}
return $distname;
}
sub dist_pkgver
{
croak "Must provide arguments to pacman_version" if ( @_ == 0 );
my ($version) = @_;
# Package versions should be numbers and decimal points only...
$version =~ tr/-/./;
$version =~ tr/_0-9.-//cd;
# Developer packages have a ..._## at the end though...
unless (( $version =~ tr/_/_/ == 1 ) && ( $version =~ /\d_\d+$/ )) {
$version =~ tr/_//d; # Delete underscores otherwise.
}
$version =~ tr/././s;
$version =~ s/[.]$//;
$version =~ s/^[.]//;
return $version;
}
=for Letters In Versions
Letters aren't allowed in versions because makepkg doesn't handle them
in dependencies. Example:
* CAM::PDF requires Text::PDF 0.29
* Text::PDF 0.29a was built/installed
* makepkg still complains about perl-text-pdf>=0.29 is missing ... ?
So ... no more letters in versions.
=cut
#-----------------------------------------------------------------------------
# PUBLIC METHODS
#-----------------------------------------------------------------------------
sub set_destdir
{
croak 'Invalid arguments to set_destdir' if ( @_ != 2 );
my ($self, $destdir) = @_;
$self->status->destdir($destdir);
return $destdir;
}
sub get_destdir
{
my $self = shift;
return $self->status->destdir;
}
sub get_pkgpath
{
my $self = shift;
return $self->status->dist;
}
sub get_cpandistdir
{
my ($self) = @_;
my $module = $self->parent;
my $distdir = $module->status->dist_cpan->status->distdir;
$distdir =~ s{^.*/}{};
return $distdir;
}
sub get_pkgname
{
return shift->status->pkgname;
}
sub get_pkgver
{
return shift->status->pkgver;
}
sub get_pkgrel
{
my ($self) = @_;
return $self->status->pkgrel;
}
sub set_pkgrel
{
my ($self, $new_pkgrel) = @_;
return $self->status->pkgrel( $new_pkgrel );
}
sub get_pkgvars
{
croak 'Invalid arguments to get_pkgvars' if ( @_ != 1 );
my $self = shift;
my $status = $self->status;
croak 'prepare() must be called before get_pkgvars()'
unless ( $status->prepared );
return ( pkgname => $status->pkgname,
pkgver => $status->pkgver,
pkgrel => $status->pkgrel,
pkgdesc => $status->pkgdesc,
depends => scalar $self->_translate_cpan_deps,
url => $self->_get_disturl,
source => $self->_get_srcurl,
md5sums => $self->_calc_tarballmd5,
depshash => { $self->_translate_cpan_deps },
);
}
sub get_pkgvars_ref
{
croak 'Invalid arguments to get_pkgvars_ref' if ( @_ != 1 );
my $self = shift;
return { $self->get_pkgvars };
}
sub set_tt_init_args
{
my $self = shift;
croak 'set_tt_init_args() must be given a hash as an argument'
unless @_ % 2 == 0;
return $self->status->tt_init_args( { @_ } );
}
sub get_tt_module
{
_load_tt_module() unless defined $TT_MOD_NAME;
return $TT_MOD_NAME;
}
sub set_pkgbuild_templ
{
my ($self, $template) = @_;
return $self->status->pkgbuild_templ( $template );
}
sub get_pkgbuild_templ
{
my ($self) = @_;
return $self->status->pkgbuild_templ() || $PKGBUILD_TEMPL;
}
sub get_pkgbuild
{
croak 'Invalid arguments to get_pkgbuild' if ( @_ < 1 );
my ($self, $skiptest) = @_;
my $status = $self->status;
my $module = $self->parent;
my $conf = $module->parent->configure_object;
croak 'prepare() must be called before get_pkgbuild()'
unless $status->prepared;
my %pkgvars = $self->get_pkgvars;
# Quote our package desc for bash.
$pkgvars{pkgdesc} =~ s/ ([\$\"\`]) / \\$1 /gxms;
# !'s are much more annoying...
$pkgvars{pkgdesc} =~ s/ \! /"'!'"/xms;
my $templ_vars = { packager => $ENV{PACKAGER} || $PACKAGER,
version => $VERSION,
%pkgvars,
distdir => $self->get_cpandistdir(),
skiptest => $skiptest || $conf->get_conf('skiptest'),
};
my $dist_type = $module->status->installer_type;
@{$templ_vars}{'is_makemaker', 'is_modulebuild'} =
( $dist_type eq 'CPANPLUS::Dist::MM' ? (1, 0) :
$dist_type eq 'CPANPLUS::Dist::Build' ? (0, 1) :
die "unknown Perl module installer type: '$dist_type'" );
my $templ_text = $status->pkgbuild_templ || $PKGBUILD_TEMPL;
return scalar $self->_process_template( $templ_text, $templ_vars );
}
sub create_pkgbuild
{
croak 'Invalid arguments to create_pkgbuild' if ( @_ < 2 );
my ($self, $destdir, $skiptest) = @_;
croak qq{Invalid directory passed to create_pkgbuild: "$destdir" ...
Directory does not exist or is not writeable}
unless ( -d $destdir && -w _ );
my $pkgbuild_text = $self->get_pkgbuild( $skiptest );
my $fqpath = catfile( $destdir, 'PKGBUILD' );
open my $pkgbuild_file, '>', $fqpath
or die "failed to open new PKGBUILD: $OS_ERROR";
print $pkgbuild_file $pkgbuild_text;
close $pkgbuild_file
or die "failed to close new PKGBUILD: $OS_ERROR";
return;
}
#-----------------------------------------------------------------------------
# PRIVATE INSTANCE METHODS
#-----------------------------------------------------------------------------
#---HELPER FUNCTION---
sub _is_main_module
{
my ($mod_name, $dist_name) = @_;
$mod_name =~ tr/:/-/s;
return (lc $mod_name) eq (lc $dist_name);
}
#---INSTANCE METHOD---
# Usage : my $deps_str = $self->_translate_cpan_deps()
# Purpose : Convert CPAN prerequisites into pacman package dependencies
# Returns : String to be appended after 'depends=' in PKGBUILD file,
# without parenthesis.
#---------------------
sub _translate_cpan_deps
{
croak 'Invalid arguments to _translate_cpan_deps method'
if @_ != 1;
my ($self) = @_;
my %pkgdeps;
my $module = $self->parent;
my $backend = $module->parent;
my $prereqs = $module->status->prereqs;
CPAN_DEP_LOOP:
for my $modname ( keys %{$prereqs} ) {
my $depver = $prereqs->{$modname};
# Sometimes a perl version is given as a prerequisite
if ( $modname eq 'perl' ) {
$pkgdeps{perl} = $depver;
next CPAN_DEP_LOOP;
}
# Ignore modules included with this version of perl...
# NOTE: If 'provides' are given version numbers in the perl
# package we won't need to check this.
# (But we still do, owell. It avoids redundancy.)
my $bundled_version = $Module::CoreList::version{ 0+$] }->{$modname};
if ( defined $bundled_version ) {
next CPAN_DEP_LOOP if ( qv($bundled_version) >= qv($depver) );
}
# Translate the module's distribution name into a package name...
my $modobj = $backend->module_tree( $modname )
or next CPAN_DEP_LOOP;
my $pkgname = dist_pkgname( $modobj->package_name );
# If two module prereqs are in the same distribution ("package") file
# then try to choose the one with the same name as the file...
if ( exists $pkgdeps{$pkgname} ) {
next CPAN_DEP_LOOP unless _is_main_module( $modname, $pkgname );
}
$pkgdeps{$pkgname} = ( qv($depver) == 0 ? 0 : dist_pkgver( $depver ));
}
# Always require perl.
$pkgdeps{perl} ||= 0;
# Merge in the XS C library package deps...
my $xs_deps = $self->_translate_xs_deps;
XSDEP_LOOP:
while ( my ($name, $ver) = each %$xs_deps ) {
# TODO: report this error?
next XSDEP_LOOP if ( exists $pkgdeps{$name} );
$pkgdeps{$name} = $ver;
}
# Return a hash if in list context or return a string representing
# the depends= line in the PKGBUILD if caller wants a scalar.
return %pkgdeps if ( wantarray );
return ( join ' ',
map { $pkgdeps{$_} ? qq{'${_}>=$pkgdeps{$_}'} : qq{'$_'} }
sort keys %pkgdeps );
}
#---HELPER FUNCTION---
sub _metayml_pkgdesc
{
my ($mod_obj) = @_;
my $metayml;
unless ( open $metayml, '<',
catfile( $mod_obj->status->extract, 'META.yml' )) {
_DEBUG( "Could not open META.yml to get pkgdesc: $!" );
return undef;
}
while ( <$metayml> ) {
chomp;
if ( my ($pkgdesc) = / \A abstract: \s* (.+) \s* \z /xms ) {
_DEBUG qq{Found pkgdesc "$pkgdesc" in META.yml};
# Ignore enclosing quotes...
$pkgdesc = $2 if ( $pkgdesc =~ / \A (['"]) (.*) \1 \z /xms );
# Ignore certain values we don't like...
for my $bad ( @BAD_METAYML_ABSTRACTS ) {
return undef if $pkgdesc eq $bad;
}
return $pkgdesc;
}
}
return undef;
}
#---HELPER FUNCTION---
sub _pod_pkgdesc
{
my ($mod_obj) = @_;
my $podselect = Pod::Select->new;
my $modname = $mod_obj->name;
$podselect->select('NAME');
=for POD Search
We use the package name because there is usually a module file
with the exact same name as the package file.
We want the main module's description, just in case the user requested
a lesser module in the same package file.
Assume the main .pm or .pod file is under lib/Module/Name/Here.pm
=cut
my $mainmod_path = $mod_obj->package_name;
$mainmod_path =~ tr{-}{/}s;
my $mainmod_file = $mainmod_path;
$mainmod_file =~ s{\A.*/}{};
$mainmod_path =~ s{/$mainmod_file}{};
my $base_path = $mod_obj->status->extract;
my @possible_pods = (# check directly inside the extracted folder
# and deep inside the lib directory
map { ( catfile( $base_path, 'lib', $_ ),
catfile( $base_path, $_ )) }
map { ( catfile( $mainmod_path, $_ ), $_ ) }
# .pm files and .pod files
map { "${mainmod_file}.$_" }
qw/pod pm/ );
PODSEARCH:
for my $podfile_path ( @possible_pods ) {
next PODSEARCH unless ( -e $podfile_path );
_DEBUG "Searching the POD inside $podfile_path for pkgdesc...";
my $name_section = q{};
open my $podfile, '<', $podfile_path
or next PODSEARCH;
open my $podout, '>', \$name_section
or die "failed open on filehandle to string: $!";
$podselect->parse_from_filehandle( $podfile, $podout );
close $podfile;
close $podout or die "failed close on filehandle to string: $!";
next PODSEARCH unless ( $name_section );
# Remove formatting codes.
$name_section =~ s{ [IBCLEFSXZ] <(.*?)> }{$1}gxms;
$name_section =~ s{ [IBCLEFSXZ] <<(.*?)>> }{$1}gxms;
# The short desc is on a line beginning with 'Module::Name - '
if ( $name_section =~ / ^ \s* $modname [ -]+ ([^\n]+) /xms ) {
_DEBUG qq{Found pkgdesc "$1" in POD};
return $1;
}
}
return undef;
}
#---HELPER FUNCTION---
sub _readme_pkgdesc
{
my ($mod_obj) = @_;
my $mod_name = $mod_obj->name;
open my $readme, '<', catfile( $mod_obj->status->extract, 'README' )
or return undef;
LINE:
while ( <$readme> ) {
chomp;
# limit ourselves to a NAME section
next LINE unless ( (/^NAME/ ... /^[A-Z]+/) &&
/ ^ \s* ${mod_name} [\s\-]+ (.+) $ /oxms );
_DEBUG qq{Found pkgdesc "$1" in README};
return $1;
}
return undef;
}
#---INSTANCE METHOD---
# Usage : $pkgdesc = $self->_prepare_pkgdesc();
# Purpose : Tries to find a module's "abstract" short description for
# use as a package description.
# Postcond : Sets the $self->status->pkgdesc accessor to the found
# package description.
# Returns : The package short description.
# Comments : We search through the META.yml file, the main module's .pm file,
# .pod file, and then the README file.
#---------------------
sub _prepare_pkgdesc
{
croak 'Invalid arguments to _prepare_pkgdesc method' if @_ != 1;
my ($self) = @_;
my ($status, $module, $pkgdesc) = ($self->status, $self->parent);
my @pkgdesc_srcs =
(
# Registered modules have their description stored in the object.
sub { $module->description },
# First, try to find the short description in the META.yml file...
\&_metayml_pkgdesc,
# Next, parse the source file or pod file for a NAME section...
\&_pod_pkgdesc,
# Last, try to find it in in the README file...
\&_readme_pkgdesc,
);
PKGDESC_LOOP:
for my $pkgdesc_src ( @pkgdesc_srcs ) {
$pkgdesc = $pkgdesc_src->( $module ) and last PKGDESC_LOOP;
}
return $status->pkgdesc( $pkgdesc || q{} );
}
#---INSTANCE METHOD---
# Usage : $self->_prepare_status()
# Purpose : Prepares all the package-specific accessors in our $self->status
# accessor object (of the class Object::Accessor).
# Postcond : Accessors assigned to: pkgname pkgver pkgbase pkgarch
# destdir
# Returns : The object's status accessor.
#---------------------
sub _prepare_status
{
croak 'Invalid arguments to _prepare_status method' if @_ != 1;
my $self = shift;
my $status = $self->status; # Private hash
my $module = $self->parent; # CPANPLUS::Module
my $conf = $module->parent->configure_object;
my $our_base = catdir( $conf->get_conf('base'),
( sprintf "%vd", $PERL_VERSION ),
'pacman' );
$status->destdir( $ENV{PKGDEST} ||
$PKGDEST ||
catdir( $our_base, 'pkg' ) );
my ($pkgver, $pkgname)
= ( dist_pkgver( $module->package_version ),
dist_pkgname( $module->package_name));
my $pkgbase = catdir( $our_base, 'build', "$pkgname-$pkgver" );
my $pkgarch = `uname -m`;
chomp $pkgarch;
foreach ( $pkgname, $pkgver, $pkgbase, $pkgarch ) {
die "A package variable is invalid" unless defined;
}
$status->pkgname( $pkgname );
$status->pkgver ( $pkgver );
$status->pkgbase( $pkgbase );
$status->pkgarch( $pkgarch );
$status->pkgrel ( 1 );
$status->tt_init_args( {} );
$self->_prepare_pkgdesc();