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
|
#!/usr/bin/env perl
use warnings 'FATAL' => 'all';
use strict;
my $PROG = 'preps/perl.d/perl-dist';
my $PBPROG = 'perl-pkgbuild';
sub DBG {}
if(exists $ENV{'GENPKGDBG'}){
no warnings 'redefine';
*DBG = sub { print STDERR "$PROG: DBG: ", @_ };
}
# Visible through entire file.
my (%CoreMods, %CoreOnly);
loadcore();
package Convert;
*DBG = *main::DBG;
*vardir = *main::vardir;
use YAML::XS qw();
use version qw();
# Match unpredictible package names .
my %OVERRIDE =
('libwww-perl' => 'perl-libwww',
'aceperl' => 'perl-ace',
'mod_perl' => 'mod_perl',
'glade-perl-two' => 'perl-glade-two',
'Gnome2-GConf' => 'gconf-perl',
'Gtk2-GladeXML' => 'glade-perl',
'Glib' => 'glib-perl',
'Gnome2' => 'gnome-perl',
'Gnome2-VFS' => 'gnome-vfs-perl',
'Gnome2-Canvas' => 'gnomecanvas-perl',
'Gnome2-GConf' => 'gconf-perl',
'Gtk2' => 'gtk2-perl',
'Cairo' => 'cairo-perl',
'Pango' => 'pango-perl',
'Perl-Critic' => 'perl-critic',
'Perl-Tidy' => 'perl-tidy',
'App-Ack' => 'ack',
'TermReadKey' => 'perl-term-readkey');
sub dist2pkg
{
my($name, $ver) = @_;
return dist_pkgname($name), dist_pkgver($ver);
}
# Copied from CPANPLUS::Dist::Arch
sub dist_pkgname
{
my($distname) = @_;
return $OVERRIDE{$distname} if(exists $OVERRIDE{$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; # Delete all other chars
$distname =~ tr/-/-/s;
# Delete leading or trailing hyphens...
$distname =~ s/\A-//;
$distname =~ s/-\z//;
die qq{Dist name '$distname' completely violates packaging standards}
if(length $distname == 0);
# Don't prefix the package with perl- if it IS perl...
$distname = "perl-$distname" unless($distname eq 'perl');
return $distname;
}
sub dist_pkgver
{
my($version) = @_;
# Remove developer versions because pacman has no special logic
# to handle comparing them to regular versions such as perl uses.
$version =~ s/_[^_]+\z//;
# Package versions should be numbers and decimal points only...
$version =~ tr/-_/../;
$version =~ tr/0-9.//cd;
$version =~ tr/././s;
$version =~ s/^[.]|[.]$//g;
return $version;
}
#---HELPER FUNCTION---
# Decide if the dist. is named after the module.
sub _ismainmod
{
my($mod_name, $dist_name) = @_;
$mod_name =~ tr/:/-/s;
return lc($mod_name) eq lc($dist_name);
}
#---HELPER FUNCTION---
# Merges the right-hand deps into the left-hand deps.
sub _merge
{
my($left_deps, $right_deps) = @_;
MERGE_LOOP:
while(my($pkg, $ver) = each %$right_deps){
if($left_deps->{$pkg}){
my $leftver = version->parse($left_deps->{$pkg});
my $rightver = version->parse($ver);
next MERGE_LOOP if $leftver > $rightver;
}
$left_deps->{$pkg} = $ver;
}
return;
}
#---HELPER FUNCTION---
# Merge duplicate deps into $left always storing the greatest version there.
sub _mergedups
{
my($left, $right) = @_;
for my $name (keys %$left){
my $rver = delete $right->{$name} or next;
my $lver = $left->{$name};
my $lvo = ($lver ? version->parse($lver) : 0);
my $rvo = ($rver ? version->parse($rver) : 0);
$left->{$name} = ($lvo > $rvo ? $lvo : $rvo);
}
return;
}
sub _filterdeps(&$)
{
my($fsub, $deps) = @_;
my %fed;
my @pkgs = keys(%$deps);
for my $dname (grep { $fsub->() } @pkgs){
my $dver = delete $deps->{$dname};
$fed{$dname} = $dver if(defined $dver);
}
return \%fed;
}
#---HELPER FUNCTION---
sub _yankcheckers
{
_filterdeps { /^perl-pod-coverage$|^perl-test-/ } $_[0]
}
#---HELPER FUNCTION---
# Converts a decimal perl version (like $]) into the dotted decimal
# form that the official ArchLinux perl package uses.
sub _perldepver
{
my($perlver) = @_;
# Fix perl-style vstrings which have a leading "v".
return $perlver if($perlver =~ s/\Av//);
my @v;
return $perlver unless(@v = $perlver =~ /\A(\d+)[.](\d{1,3})(\d{0,3})\z/);
pop @v unless($v[2]);
return join q{.}, map { int } @v; # int removes zeroes
}
#---PUBLIC FUNCNTION---
# Translates CPAN module dependencies into ArchLinux package dependencies.
sub _reqs2deps
{
my($prereqs) = @_;
my(@mods, %pkgdeps);
# Filter out deps on 'perl' and any core modules that we can.
while(my ($name, $ver) = each(%$prereqs)) {
DBG("requires $name $ver\n");
my $cver = $CoreMods{$name};
if($name eq 'perl') {
$pkgdeps{'perl'} = _perldepver($ver);
}elsif($cver){
DBG("perl core has $name $cver\n");
my $vobj = eval { version->parse($ver) };
$cver = version->parse($cver);
if($vobj > $cver){
push @mods, $name;
}
}else{
push @mods, $name;
}
}
my %dists = _distsofmods(@mods);
while(my ($mod, $dist) = each %dists) {
DBG("$mod is provided by $dist\n");
my $pkgname = dist_pkgname($dist);
my $ver = $prereqs->{$mod};
# If the module is not named after the distribution, ignore its
# version which might not match the distribution.
undef $ver unless(_ismainmod($mod, $dist));
# If two module prereqs are in the same CPAN distribution then
# the version required for the main module will override.
# (because versions specified for other modules in the dist
# are 0)
$pkgdeps{$pkgname} ||= ($ver ? dist_pkgver($ver) : 0);
DBG("depends on $pkgname>=$pkgdeps{$pkgname}\n");
}
return \%pkgdeps;
}
sub prereqs
{
my($pkgname, $prereqs) = @_;
# maps perl names for different dependencies to ArchLinux's names
my %namemap = ('configure' => 'makedepends',
'build' => 'makedepends',
'test' => 'checkdepends',
'runtime' => 'depends',
);
my %pkgdeps;
while (my($perl, $arch) = each(%namemap)) {
my $reqs = $prereqs->{$perl}{'requires'};
my $deps; $deps = _reqs2deps($reqs) if($reqs);
next unless(keys %$deps);
if($pkgdeps{$arch}){
_merge($pkgdeps{$arch}, $deps);
}else{
$pkgdeps{$arch} = $deps;
}
}
# ArchLinux now has a separate array for dependencies that we only
# need for checking (aka "testing"). Older perl METAs do not
# have this separated. Force any test modules to be checkdepends.
if(!$pkgdeps{'checkdepends'} && $pkgname !~ /\Aperl-test-/){
my $checkdeps = {};
for(qw/makedepends depends/){
_merge($checkdeps, _yankcheckers($pkgdeps{$_}))
}
$pkgdeps{'checkdepends'} = $checkdeps;
}
# We at least require perl, if nothing else.
unless(grep { scalar keys %$_ > 0 } values %pkgdeps){
$pkgdeps{'depends'}{'perl'} = 0;
}
_mergedups(@pkgdeps{'makedepends', 'checkdepends'});
_mergedups(@pkgdeps{'depends', 'makedepends'});
# Convert all deps into arrays of strings.
for my $deptype (keys(%pkgdeps)){
$pkgdeps{$deptype} = _stringify($pkgdeps{$deptype});
}
return \%pkgdeps;
}
#---HELPER FUNCTION---
sub _stringify
{
my($deps) = @_;
my @depstrs;
for my $pkg (sort keys %$deps){
my $ver = $deps->{$pkg};
my $str = ($ver eq '0' ? $pkg : "$pkg>=$ver");
push @depstrs, $str;
}
return \@depstrs;
}
sub _distsofmods
{
my(@mods) = @_;
return () if(@mods == 0);
@mods = _nocore(@mods);
my $var = vardir();
open my $fh, '<', "$var/cpan.mods"
or die "$PROG: failed to open $var/cpan.mods: $!";
my %mods = map { ($_ => 1) } @mods;
my %dists;
local $/ = '';
RECLOOP:
while(my $rec = <$fh>){
last RECLOOP unless(keys %mods > 0);
my($dist, @mvs) = split(/\n/, $rec);
MODLOOP:
for(@mvs){
my($m) = split;
my $fnd = delete($mods{$m}) or next;
$dists{$m} = $dist;
}
}
my @lost = keys %mods;
return %dists unless(@lost);
for my $m (@lost){
print STDERR "$PROG: failed to find module $m\n";
}
exit 1;
}
sub _nocore
{
return grep { not $CoreOnly{$_} } @_;
}
#-----------------------------------------------------------------------------
package main;
use File::Basename qw(basename dirname);
use File::Spec::Functions qw(catfile catdir rel2abs);
use File::Find qw(find);
use JSON::XS qw(decode_json); # for META.json
use YAML::XS (); # for META.yml
use Pod::Select (); # search POD for description
sub printdata
{
my($pbvars) = @_;
while(my($name, $val) = each %$pbvars) {
if(!defined $val || $val eq q{}) {
warn "$PROG: warning: $name is undefined\n";
$val = q{};
}
print $name, "\n";
print "$_\n" for (ref $val ? @$val : $val);
print "\n";
}
# TODO: autodetect license type
printf("license\n%s\n\n", join "\n", qw/PerlArtistic GPL/);
print "options\n!emptydirs\n\n";
return;
}
sub writepb
{
my($ddir) = @_;
my $dtype = (-f "$ddir/Build.PL" ? "MB" : "MM");
if(system $PBPROG => $dtype){
return $? >> 8;
}else{
return 0;
}
}
sub main
{
my $distpath = shift or die "Usage: $PROG [path to cpan dist file]\n";
$distpath = rel2abs($distpath);
my $dir = dirname($distpath);
my $file = basename($distpath);
my $info = distinfo($file);
chsrcdir(catdir($dir, 'src'), $file);
my $distdir = extractdist($file);
my $meta = loadmeta($distdir);
my $desc = $meta->{'abstract'};
if(!$desc || $desc eq '~' || $desc eq 'unknown'){
$meta->{'abstract'} = distdesc($distdir, $info->{'mod'});
}
my($name, $ver) = Convert::dist2pkg(@{$info}{'name', 'ver'});
my $deps = Convert::prereqs($name, $meta->{'prereqs'});
my %pbvars = (
'pkgver' => $ver,
'pkgdesc' => $meta->{'abstract'},
'arch' => (xsdist($distdir) ? ['i686', 'x86_64'] : 'any'),
'_ddir' => $distdir,
%$deps,
);
chdir $dir or die "chdir: $!";
my $ret = writepb("$dir/src/$distdir");
if($ret){
print STDERR "$PROG: failed to write PKGBUILD: error $ret\n";
return 1;
}
printdata(\%pbvars);
return 0;
}
# Create the src/ directory and tarball symlink. Then chdir into it.
sub chsrcdir
{
my($srcdir, $distfile) = @_;
if(-e $srcdir){
system 'rm' => ('-fr', $srcdir);
die "failed to rm $srcdir\n" unless($? == 0);
}
mkdir $srcdir or die "mkdir $srcdir: $!";
chdir $srcdir or die "chdir $srcdir: $!";
unless(-f $distfile) {
symlink(catfile('..', $distfile), $distfile)
or die "symlink $distfile: $!";
}
return $srcdir;
}
sub distinfo
{
my($distfile) = @_;
my @c = split /-/, $distfile;
my $ver = pop @c;
my $name = join q{-}, @c;
my $mod = $name;
$mod =~ s/-/::/g;
return { 'name' => $name, 'ver' => $ver, 'mod' => $mod };
}
sub extractdist
{
my($file) = @_;
system 'bsdtar' => ('-xf', $file);
die "$PROG: bsdtar failed to extract $file\n" unless($? == 0);
opendir my $srcdir, '.' or die "opendir: $!";
my @dirs = grep { -d $_ && !/\A[.]/ } readdir $srcdir;
closedir $srcdir;
die "$PROG: many dirs (@dirs) inside the tarball $file\n" if(@dirs > 1);
die "$PROG: no dirs found in tarball $file\n" if(@dirs == 0);
return $dirs[0];
}
sub loadmeta
{
my($distdir) = @_;
for my $metaext (qw/json yml/){
my $path = "$distdir/META.$metaext";
next unless(-f $path);
open my $metafh, '<', $path or die "open: $!";
my $meta = do { local $/; <$metafh> };
close $metafh;
$meta = ($metaext eq 'json' ? decode_json($meta) :
$metaext eq 'yml' ? YAML::XS::Load($meta) :
die "internal error: unknown \$metaext: $metaext");
upgrademeta($meta);
return $meta;
}
return undef;
}
sub upgrademeta
{
my($meta) = @_;
return if(exists $meta->{'prereqs'});
my $prereqs;
$prereqs->{'configure'}{'requires'}
= delete $meta->{'configure_requires'};
$prereqs->{'build'}{'requires'} = delete($meta->{'build_requires'});
$prereqs->{'runtime'}{'requires'} = delete($meta->{'requires'});
$meta->{'prereqs'} = $prereqs;
return;
}
sub xsdist
{
my($dir) = @_;
my $isxs;
find({ 'wanted' => sub { $isxs = 1 if(/[.]xs$/) }, 'no_chdir' => 1 },
$dir);
return $isxs;
}
#-----------------------------------------------------------------------------
sub distdesc
{
my($dir, $modname) = @_;
return _poddesc($dir, $modname) || _readmedesc($dir, $modname);
}
sub _poddesc
{
my($dir, $modname) = @_;
my $podselect = Pod::Select->new;
$podselect->select('NAME');
my $modpath = $modname; $modpath =~ s{::}{/}g;
my $moddir = dirname($modpath);
my $modfile = basename($modpath);
# First check under lib/ for a "properly" pathed module, with
# nested directories. Then search desperately for a .pm file that
# matches the module's last name component.
my @possible = glob("$dir/{lib/,}{$moddir/,}$modfile.{pod,pm}");
PODSEARCH:
for my $podpath (@possible){
next PODSEARCH unless(-f $podpath);
# Read the NAME section of the POD into a scalar.
my $namesect = q{};
open my $podfile, '<', $podpath or next PODSEARCH;
open my $podout, '>', \$namesect or die "open: $!";
$podselect->parse_from_filehandle($podfile, $podout);
close $podfile;
close $podout or die "close: $!";
next PODSEARCH unless($namesect);
# Remove formatting codes.
$namesect =~ s{ [IBCLEFSXZ] <(.*?)> }{$1}gxms;
$namesect =~ s{ [IBCLEFSXZ] <<(.*?)>> }{$1}gxms;
# The short desc is on a line beginning with 'Module::Name - '
return $1 if($namesect =~ / ^ \s* $modname [ -]+ ([^\n]+) /xms);
}
return undef;
}
#---HELPER FUNCTION---
sub _readmedesc
{
my($dir, $modname) = @_;
my $path = catfile($dir, 'README');
return undef unless(-f $path);
open my $fh, '<', $path or die "open: $!";
while(<$fh>){
chomp;
next unless((/\ANAME/ ... /\A[A-Z]+/)
&& / \A \s* ${modname} [\s\-]+ (.+) \z /x);
return $1;
}
close $fh;
return undef;
}
sub vardir
{
return $ENV{'PKGVAR'}
or die "$PROG: PKGVAR env variable is unset\n";
}
sub loadcore
{
my $cmpath = vardir() . '/perlcore.mods';
unless(-f $cmpath){
print STDERR <<"END_ERR";
$PROG: error: $cmpath is missing.
******************************************************************************
This file should have been installed along with genpkg. Copy the file from
genpkg's source or generate it with the misc/perlcore script in genpkg's
source. Make sure to use the same version as the perl you are using.
******************************************************************************
END_ERR
exit 1;
}
open my $if, '<', $cmpath or die "$PROG: open $cmpath: $!";
while(<$if>){
my($m, $v, $coreonly) = split;
$CoreMods{$m} = $v;
$CoreOnly{$m} = 1 if($coreonly);
}
close $if;
}
exit main(@ARGV);
|