forked from gentoo-perl/g-cpan
/
g-cpan
executable file
·1217 lines (1012 loc) · 40.2 KB
/
g-cpan
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 -w
package main;
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
#
# modules to use - these will need to be marked as
# dependencies, and installable by portage
use warnings;
use strict;
use File::Spec;
use File::Path;
use File::Basename;
use File::Copy;
use Term::ANSIColor;
use Cwd qw(getcwd abs_path cwd);
use YAML;
use YAML::Node;
use Carp;
use IO::File;
use DirHandle;
# Module load & configure
use Getopt::Long;
Getopt::Long::Configure("bundling");
use Log::Agent;
use Log::Agent::Driver::File;
use Log::Agent::Driver::Silent;
our $VERSION = "0.16.4";
my $prog = basename($0);
##### Establish our tmpdir
unless ($ENV{TMPDIR}) { $ENV{TMPDIR} = '/var/tmp/g-cpan' }
my %dep_list = ();
my @perl_dirs = ("perl-gcpan", "dev-perl", "perl-core", "perl-text", "perl-tools", "perl-xml", "perl-dev", "dev-lang",);
###############################
# Command line interpretation #
###############################
# Declare options
# First, the main switches
my @main_switches = \my ($search, $list, $install, $generate, $buildpkgonly);
# Then, additional switches
my @additional_switches = \my ($upgrade, $pretend, $buildpkg, $ask, $cpan_reload);
# Then, the normal options
my ($debug, $verbose, $log);
# Set colors here so we can use them at will anywhere :)
my $green = color("bold green");
my $white = color("bold white");
my $cyan = color("bold cyan");
my $reset = color("reset");
#Get & Parse them
GetOptions(
'verbose|v' => \$verbose,
'search|s' => \$search,
'install|i' => \$install,
'upgrade|u' => \$upgrade,
'list|l' => \$list,
'log|L' => \$log,
'pretend|p' => \$pretend,
'buildpkg|b' => \$buildpkg,
'buildpkgonly|B' => \$buildpkgonly,
'ask|a' => \$ask,
'generate|g' => \$generate,
'debug|d' => \$debug,
'cpan_reload' => \$cpan_reload,
'help|h' => sub { exit_usage(); }
) or exit_usage();
use Gentoo;
use Gentoo::UI::Console;
if ($log)
{
open my $log_test, q{>>}, "/var/log/$prog/$prog.err"
or fatal(print_err("You don't have permission to perform logging to /var/log/$prog/$prog.err: $!"));
close($log_test);
my $log_driver = Log::Agent::Driver::File->make(
-prefix => "$prog",
-magic_open => 0,
-stampfmt => 'date',
-channels => {
'error' => "/var/log/$prog/$prog.err",
'output' => "/var/log/$prog/$prog.log",
}
);
logconfig(-driver => $log_driver);
}
else
{
my $log_driver = Log::Agent::Driver::Silent->make();
logconfig(-driver => $log_driver);
}
print_warn("*WARNING* - logging debug output can create large logs") if ($log && $debug);
if (($install || $ask || $buildpkg || $buildpkgonly || $upgrade) && $> > 0 && !$pretend)
{
print_err("INSUFFICIENT PERMISSIONS TO RUN EMERGE");
logerr("ERROR - INSUFFICIENT PERMISSIONS TO RUN EMERGE");
exit();
}
if (!$install && defined($ask))
{
$install = 1;
}
# Array containing the original values passed for installing
my %passed_to_install;
@ARGV > 0
and %passed_to_install = map { $_ => 1 } @ARGV;
# Array that will contain the portage friendly version of the values passed to install
my %really_install;
# Output error if more than one main switch is activated
#
if ((grep { defined $$_ } @main_switches) > 1)
{
print_err("You can't combine actions with each other.\n");
print_out("${white}Please consult ${cyan}$prog ${green}--help${reset} or ${cyan}man $prog${reset} for more information\n\n");
exit();
}
if (!grep { defined $$_ } @main_switches, @additional_switches)
{
print_err("You haven't told $prog what to do.\n");
print_out("${white}Please consult ${cyan}$prog ${green}--help${reset} or ${cyan}man $prog${reset} for more information\n\n");
exit();
}
# Output error if no arguments
if (@ARGV == 0 && !(defined $upgrade || defined $list || defined $cpan_reload))
{
print_err("Not even one module name or expression given!\n");
print_out("${white}Please consult ${cyan}$prog ${green}--help${reset} for more information\n\n");
exit();
}
######################
# CPAN Special Stuff #
######################
my $GentooCPAN = Gentoo->new();
# Don't do autointalls via ExtUtils::AutoInstall
$ENV{PERL_EXTUTILS_AUTOINSTALL} = "--skipdeps";
$ENV{PERL_MM_USE_DEFAULT} = 1;
# Do we need to generate a config ?
eval "use CPAN::Config;";
my $needs_cpan_stub = $@ ? 1 : 0;
# Test Replacement - ((A&B)or(C&B)) should be the same as ((A or C) and B)
if (($needs_cpan_stub || $> > 0) && !-f "$ENV{HOME}/.cpan/CPAN/MyConfig.pm")
{
# In case match comes from the UID test
$needs_cpan_stub = 1;
print_warn("No CPAN Config found, auto-generating a basic one");
# Generate a fake config for CPAN
$GentooCPAN->makeCPANstub();
}
else
{
$needs_cpan_stub = 0;
}
use CPAN;
{
foreach (qw[build sources])
{
if (-d "$ENV{TMPDIR}/.cpan/$_")
{
my $test_file = $ENV{TMPDIR} . "/.cpan/$_/test";
my $test_tmp = IO::File->new($test_file, '>');
if (defined($test_tmp))
{
undef $test_tmp;
unlink($test_file);
}
else
{
print_err("No WRITE permissions in $ENV{TMPDIR}/.cpan/$_!!");
print_err("Please run $prog as a user with sufficient permissions");
print_err("or correct permissions in $ENV{TMPDIR}");
exit;
}
}
}
}
##########
# main() #
##########
my $gcpan_run = Gentoo->new(
'cpan_reload' => $cpan_reload,
'DEBUG' => $debug,
);
# Grab some configuration options for g-cpan
my $GCPAN_CAT = $gcpan_run->getEnv('GCPAN_CAT');
$GCPAN_CAT = 'perl-gcpan' unless defined($GCPAN_CAT) and length($GCPAN_CAT) > 0;
# Push back to the env so we can use during cleanup
$ENV{GCPAN_CAT} = $GCPAN_CAT;
my $GCPAN_OVERLAY = $gcpan_run->getEnv('GCPAN_OVERLAY') || 0;
# Ensure it's in the category list
if(length(grep {/$GCPAN_CAT/} @perl_dirs) == 0) {
push @perl_dirs, $GCPAN_CAT;
}
# Confirm that there is an /etc/portage/categories file
# and that we have an entry for perl-gcpan in it.
my $cat_file = "/etc/portage/categories";
if (-f "$cat_file")
{
#
# Use braces to localize the $/ assignment, so we don't get bitten later.
#
local $/ = undef;
my $cat_read = IO::File->new($cat_file, '<');
if (defined $cat_read)
{
my $data = <$cat_read>;
undef $cat_read;
autoflush STDOUT 1;
unless ($data =~ m{$GCPAN_CAT}gmxi)
{
my $cat_write = IO::File->new($cat_file, '>>');
if (defined $cat_write)
{
print $cat_write "$GCPAN_CAT\n";
undef $cat_write;
autoflush STDOUT 1;
}
else
{
print_err("Insufficient permissions to edit /etc/portage/categories");
print_err("Please run $prog as a user with sufficient permissions");
exit;
}
}
}
}
else
{
my $cat_write = IO::File->new($cat_file, '>');
if (defined $cat_write)
{
print $cat_write $GCPAN_CAT;
}
else
{
print_err("Insufficient permissions to edit /etc/portage/categories");
print_err("Please run $prog as a user with sufficient permissions");
exit;
}
}
# Reset the object in case we created a new category.
$gcpan_run = Gentoo->new(
'cpan_reload' => $cpan_reload,
'DEBUG' => $debug,
);
# If passed --cpan_reload and nothing else, just reload and exit.
if (@ARGV == 0 && defined $cpan_reload) {
CPAN::Index->force_reload();
exit;
}
# Get the main portdir
my $PORTAGE_DIR = $gcpan_run->getEnv('PORTDIR');
$gcpan_run->{portage_bases}{$PORTAGE_DIR} = 1;
# Grab the keywords - we'll need these when we build the ebuild
my $keywords = $gcpan_run->getEnv('ACCEPT_KEYWORDS');
if ($keywords =~ m{ACCEPT_KEYWORDS}) { $keywords="" }
$keywords ||= do
{
open my $tmp, '<', "$PORTAGE_DIR/profiles/arch.list"
or fatal(print_err("ERROR READING $PORTAGE_DIR/profiles/arch.list: $!"));
join ' ', map { chomp; s/^#.*$//g; $_ } <$tmp>;
};
$ENV{ACCEPT_KEYWORDS} = $keywords;
# Get the overlays
my $overlay = $gcpan_run->getEnv('PORTDIR_OVERLAY') || undef;
# Get the DISTDIR - we'd like store the tarballs in here the one time
$gcpan_run->{sources} = ($gcpan_run->getEnv('DISTDIR'));
# Make sure we have write access to the DISTDIR
if ( $generate
|| $install
|| $pretend
|| $buildpkg
|| $buildpkgonly
|| $ask
|| $upgrade)
{
my $test_dist_writes = $gcpan_run->{sources} . '/test-gcpan';
my $test_distdir = IO::File->new($test_dist_writes, '>');
if ($test_distdir)
{
undef $test_distdir;
unlink $test_dist_writes;
}
else
{
undef $test_distdir;
fatal(print_err("No write access to DISTDIR: $!"));
}
}
if ($overlay)
{
if ($overlay =~ m{\S*\s+\S*}x)
{
my @overlays = split ' ', $overlay;
foreach (@overlays)
{
$gcpan_run->{portage_bases}{$_} = 1 if (-d $_);
}
}
else
{
if (-d $overlay) { $gcpan_run->{portage_bases}{$overlay} = 1 }
}
unless (keys %{$gcpan_run->{portage_bases}} > 1)
{
fatal(print_err("DEFINED OVERLAYS DON'T EXIST!"));
}
}
elsif ($generate || $list || $pretend)
{
print_err("The option you have chosen isn't supported without a configured overlay.\n");
exit();
}
# Set portage_categories to our defined list of perl_dirs
$gcpan_run->{portage_categories} = \@perl_dirs;
# Taking care of Searches.
if ($search)
{
foreach my $expr (@ARGV)
{
my $tree_expr = $expr;
$tree_expr =~ s/::/-/gxms;
scanTree(lc($tree_expr));
if (defined($gcpan_run->{portage}{lc($tree_expr)}{found}))
{
print_info("$gcpan_run->{portage}{lc($tree_expr)}{category}/$gcpan_run->{portage}{lc($tree_expr)}{name}");
my $tdesc = strip_ends($gcpan_run->{portage}{lc($tree_expr)}{DESCRIPTION});
my $thp = strip_ends($gcpan_run->{portage}{lc($tree_expr)}{HOMEPAGE});
print_info("DESCRIPTION: $tdesc");
print_info("HOMEPAGE: $thp");
}
else
{
print_info("No ebuild exists, pulling up CPAN listings for $expr");
my @search_results;
# Assume they gave us module-name instead of module::name
# which is bad, because CPAN can't convert it ;p
$verbose and print_info("Searching for $expr on CPAN");
# Let's define a CPAN::Frontend to use our printing methods
spinner_start();
if (!@search_results)
{
$expr =~ s{-}{::}gx;
my @hold = CPAN::Shell->i("/$expr/");
#if (grep { /\S{2,}/ } @hold)
#{
# push @search_results, @hold;
#}
}
# remove the spin
spinner_stop();
# UPDATE - this block doesn't work; the call to CPAN::Shell above doesn't return anything
# now, @search_results should contain the matching modules strings, if any
#if (@search_results)
#{
#print_info("Result(s) found :");
#foreach (@search_results)
#{
#print_out("$_\n");
#}
#}
#else
#{
#print_warn('no result found.');
#}
}
}
exit;
}
if ($list || $upgrade)
{
if ($upgrade && @ARGV > 0)
{
%passed_to_install = map { $_ => 1 } @ARGV;
}
else
{
my @overlays = split ' ', $overlay;
foreach my $overlay_dir (@overlays)
{
next unless (-d $overlay_dir);
my $gcpan_dir = File::Spec->catdir($overlay_dir, $GCPAN_CAT);
#$list and print_info("OVERLAY: $gcpan_dir");
print_info("OVERLAY: $gcpan_dir");
if (opendir PDIR, $gcpan_dir)
{
while (my $file = readdir PDIR)
{
next if ($file eq '.' || $file eq '..');
$list and print_info("$GCPAN_CAT/$file");
$upgrade and $passed_to_install{$file} = 1;
}
closedir PDIR;
}
else
{
print_warn("Couldn't open folder $gcpan_dir: $!");
}
}
}
}
if ( $generate
|| $install
|| $pretend
|| $buildpkg
|| $buildpkgonly
|| $ask
|| $upgrade)
{
if (keys (%passed_to_install)) {
generatePackageInfo($_) foreach (keys %passed_to_install);
}
}
if (($install || $pretend || $buildpkg || $buildpkgonly || $upgrade || $ask)
&& !( $generate))
{
if (keys %really_install)
{
my @ebuilds = (keys %really_install);
$verbose and print_info("Calling emerge for @ebuilds\n");
my @flags;
if ($pretend && $pretend > 0) { push @flags, '--pretend' }
if ($ask && $ask > 0) { push @flags, '--ask' }
if ($buildpkg && $buildpkg > 0) { push @flags, '--buildpkg' }
if ($buildpkgonly && $buildpkgonly > 0) { push @flags, '--buildpkgonly' }
if ($upgrade && $upgrade > 0) { push @flags, '--update' }
$verbose and print_info("Calling: emerge @flags @ebuilds");
$gcpan_run->emerge_ebuild(@ebuilds, @flags);
}
else
{
if ($upgrade)
{
print_info('Everything was up to date, nothing to do!');
}
else
{
print_err('Nothing to install!!');
}
}
}
sub generatePackageInfo
{
# Since all we are concerned with is the final name of the package, this
# should be a safe substitution
my ($ebuild_wanted) = @_;
$ebuild_wanted =~ m{ExtUtils(::|-)MakeMaker}ix
and print_info('Skipping ExtUtils::MakeMaker dependency'), next;
#In the event this is an upgrade, go ahead and do the lame s/-/::/
$upgrade and $ebuild_wanted =~ s/-/::/gxms;
# Grab specific info for this module
spinner_start();
unless (defined($gcpan_run->{portage}{lc($ebuild_wanted)}{found}))
{
# First, check to see if this came with the core perl install
my $pkgdbdir = "/var/db/pkg/dev-lang/";
my $s_perl = new DirHandle($pkgdbdir);
my $eb = $ebuild_wanted;
$eb =~ s{::}{/}gxs;
$eb = '/' . $eb;
while (defined(my $read = $s_perl->read))
{
if ((-d $pkgdbdir . "/" . $read) and ($read =~ m{^perl}x))
{
open(FH, "<$pkgdbdir/$read/CONTENTS") || die("Cannot open $read\'s CONTENTS");
my @data = <FH>;
close(FH);
foreach (@data)
{
my $thisfile = (split(/ /, $_))[1];
$thisfile =~ s{\.([A-Za-z]{1,3})$}{};
if ( ($thisfile =~ m{\Q$eb\E$}x) && !defined($passed_to_install{$eb}) )
{
spinner_stop();
print_info("$ebuild_wanted is part of the core perl install");
return;
}
}
spinner_stop();
last;
}
}
unless (defined($upgrade) or defined($passed_to_install{$ebuild_wanted}))
{
# If we're still here, then we didn't come with perl
$gcpan_run->getCPANInfo($ebuild_wanted);
}
}
spinner_stop();
if (!$gcpan_run->{cpan}{lc($ebuild_wanted)} && !defined($gcpan_run->{portage}{lc($ebuild_wanted)}{found}))
{
# Fallback to trying the /-/::/ trick - we avoid this the first time
# in case the module actually employs a - in its name
$ebuild_wanted =~ s/-/::/gxms;
$verbose and print_info("Getting CPAN Info for $ebuild_wanted");
spinner_start();
$gcpan_run->getCPANInfo($ebuild_wanted);
spinner_stop();
}
# If we found something on cpan, transform the portage_name
# It's possible to not find something on cpan at this point - we're just
# trying to pre-seed the portage_name
if ($gcpan_run->{cpan}{lc($ebuild_wanted)})
{
spinner_start();
$gcpan_run->{cpan}{lc($ebuild_wanted)}{portage_name} = $gcpan_run->transformCPAN($gcpan_run->{cpan}{lc($ebuild_wanted)}{src_uri}, 'n');
$gcpan_run->{cpan}{lc($ebuild_wanted)}{portage_version} = $gcpan_run->transformCPAN($gcpan_run->{cpan}{lc($ebuild_wanted)}{src_uri}, 'v');
spinner_stop();
}
else
{
print_err("$ebuild_wanted is not a CPAN module!");
}
# Save a copy of the originally requested name for later use
my $original_ebuild = $ebuild_wanted;
# Simple transform of name to something portage friendly
$ebuild_wanted =~ s/::/-/gxms;
# Scan portage for the ebuild name
if ( ($upgrade && !defined($passed_to_install{$ebuild_wanted}))
|| (!$upgrade && defined($passed_to_install{$ebuild_wanted}))
|| (!$upgrade && !defined($gcpan_run->{portage}{lc($ebuild_wanted)}{found})))
{
# Ebuild wasn't found - scan for the nice version of the module name
if (lc($gcpan_run->{cpan}{lc($original_ebuild)}{portage_name}) eq 'perl') { return }
scanTree($gcpan_run->{cpan}{lc($original_ebuild)}{portage_name});
# We had success in finding this module under a different name
if (defined($gcpan_run->{portage}{lc($gcpan_run->{cpan}{lc($original_ebuild)}{portage_name})}{found}))
{
$verbose and print_info('Found ebuild for CPAN name ' . $gcpan_run->{cpan}{lc($original_ebuild)}{portage_name});
$ebuild_wanted = $gcpan_run->{cpan}{lc($original_ebuild)}{portage_name};
}
}
else
{
$gcpan_run->{cpan}{lc($original_ebuild)}{portage_name} = $ebuild_wanted;
}
# Second round - we've looked for the package in portage two different
# ways now, time to get serious and create it ourselves
if (!defined($gcpan_run->{portage}{lc($ebuild_wanted)}{found}))
{
# Generate info - nothing found currently in the tree
$debug and $gcpan_run->debug;
if ($gcpan_run->{cpan}{lc($original_ebuild)}{portage_name}
&& lc($gcpan_run->{cpan}{lc($original_ebuild)}{portage_name}) ne 'perl')
{
# We have a cpan package that matches the request.
# Let's unpack it and get all the deps out of it.
spinner_start();
$gcpan_run->unpackModule($gcpan_run->{cpan}{lc($original_ebuild)}{name});
# Force re-compute of the information, as MANPAGE is now valid.
$gcpan_run->getCPANInfo($original_ebuild);
spinner_stop();
foreach my $dep (keys %{$gcpan_run->{cpan}{lc($original_ebuild)}{depends}})
{
defined $dep && $dep ne '' or next;
#next if (defined $dep && $dep ne '');
$dep eq 'perl' and delete $gcpan_run->{cpan}{lc($original_ebuild)}{depends}{$dep};
$dep =~ m{ExtUtils(::|-)MakeMaker}ix and print_info("Skipping ExtUtils::MakeMaker dependency"), next;
# Make sure we have information relevant to each of the deps
$verbose and print_info("Checking on dependency $dep for $original_ebuild");
$passed_to_install{$dep} or generatePackageInfo($dep);
# Remove dep from list of modules to install later on - no
# more dup'd installs!
defined $passed_to_install{$dep} and delete $really_install{$dep};
# Reindex one last time for anything we build after the fact
scanTree($gcpan_run->{cpan}{lc($dep)}{portage_name});
}
# Write ebuild here?
$debug and $gcpan_run->debug;
my @overlays;
if ($GCPAN_OVERLAY)
{
push @overlays, $GCPAN_OVERLAY
}
elsif ($overlay)
{
@overlays = split ' ', $overlay
}
else
{
push @overlays, "/var/tmp/g-cpan"
and $ENV{PORTDIR_OVERLAY} = "/var/tmp/g-cpan";
}
foreach my $target_dir (@overlays)
{
if (-d $target_dir)
{
my $gcpan_dir = File::Spec->catdir($target_dir, $GCPAN_CAT);
if (!-d $gcpan_dir)
{
$verbose and print_info("Create directory '$gcpan_dir'");
mkdir($gcpan_dir, 0755)
or fatal(print_err("Couldn't create folder $gcpan_dir: $!"));
}
my $ebuild_dir = File::Spec->catdir($gcpan_dir, $gcpan_run->{cpan}{lc($original_ebuild)}{portage_name});
if (!-d $ebuild_dir)
{
$verbose and print_info("Create directory '$ebuild_dir'");
mkdir($ebuild_dir, 0755)
or fatal(print_err("Couldn't create folder $gcpan_dir: $!"));
}
my $files_dir = File::Spec->catdir($ebuild_dir, 'files');
if (!-d $files_dir)
{
$verbose and print_info("Create directory '$files_dir'");
mkdir($files_dir, 0755)
or fatal(print_err("Couldn't create folder $gcpan_dir: $!"));
}
my $ebuild = File::Spec->catdir($ebuild_dir,
$gcpan_run->{cpan}{lc($original_ebuild)}{portage_name} . '-' . $gcpan_run->{cpan}{lc($original_ebuild)}{portage_version} . '.ebuild');
# Break out if we already have an ebuild (upgrade or
# mistake in the code)
if (!-f $ebuild)
{
print_info('Generating ebuild for ' . $gcpan_run->{cpan}{lc($original_ebuild)}{name});
my $EBUILD = IO::File->new($ebuild, '>')
or fatal(print_err("Couldn't open(write) file $ebuild: $!"));
my $module_author = $gcpan_run->{'cpan'}{lc($original_ebuild)}{'src_uri'};
$module_author =~ s/.\/..\/(.*)\/[^\/]+$/$1/g;
my $module_section = '';
if($module_author =~ /\//) {
$module_section = $module_author;
my @module_bits = split /\//, $module_author, 2;
$module_author = $module_bits[0];
$module_section = sprintf "MODULE_SECTION=\"%s\"\n", $module_bits[1];
}
# Detect the file extension, upstream usually uses .tar.gz, but not always
my $module_a_ext = '';
foreach my $ext (qw( tgz tbz2 tar.bz2 tar.xz tar.Z zip )) {
if ( $gcpan_run->{'cpan'}{lc($original_ebuild)}{'src_uri'} =~ m/\.\Q$ext\E$/ ) {
$module_a_ext = sprintf 'MODULE_A_EXT="%s"', $ext;
}
}
my $module_version = $gcpan_run->{cpan}{lc($original_ebuild)}{version};
my $description = $gcpan_run->{'cpan'}{lc($original_ebuild)}{'description'};
print $EBUILD <<"HERE";
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# This ebuild generated by $prog $VERSION
EAPI="2"
MODULE_AUTHOR="$module_author"
MODULE_VERSION="$module_version"
$module_a_ext
$module_section
inherit perl-module
DESCRIPTION="$description"
LICENSE="|| ( Artistic GPL-1 GPL-2 GPL-3 )"
SLOT="0"
KEYWORDS="$keywords"
IUSE=""
HERE
if (my @deps = keys %{$gcpan_run->{cpan}{lc($original_ebuild)}{depends}})
{
print $EBUILD "DEPEND=\"";
my %seen_deps;
foreach my $dep (@deps)
{
defined $dep && $dep ne '' or next;
my $portage_name = $gcpan_run->{cpan}{lc($dep)}{portage_name};
next unless defined $portage_name;
$portage_name = lc($portage_name);
$portage_name =~ m{\S}x or next;
# Last ditch call to scanTree to make sure we
# have info
scanTree($portage_name);
next if ( defined($seen_deps{$portage_name}) && $seen_deps{$portage_name} > 0 );
$seen_deps{$portage_name} = 1;
next
unless (defined($gcpan_run->{portage}{$portage_name}{category})
&& defined($gcpan_run->{portage}{$portage_name}{name}) && ($gcpan_run->{portage}{$portage_name}{name} =~ m/\S/));
$portage_name eq 'perl' || lc($portage_name) eq lc($gcpan_run->{cpan}{lc($original_ebuild)}{portage_name})
and next;
my ($eb_version, $cpan_version) =
stripdown($gcpan_run->{portage}{lc($portage_name)}{version}, $gcpan_run->{cpan}{lc($dep)}{portage_version});
#my $eb_version = stripdown($gcpan_run->{portage}{lc($portage_name)}{version});
#my $cpan_version = defined($gcpan_run->{cpan}{lc($dep)}{portage_version})? stripdown($gcpan_run->{cpan}{lc($dep)}{portage_version}): "0";
if ( $gcpan_run->{portage}{$portage_name}{name} eq "module-build")
{
print $EBUILD ""
. "virtual/perl-Module-Build\n";
}
elsif ( defined($gcpan_run->{cpan}{lc($dep)}{portage_version})
&& $gcpan_run->{cpan}{lc($original_ebuild)}{depends}{$dep} ne '0'
&& int($eb_version) >= int($cpan_version)
&& $gcpan_run->{cpan}{lc($original_ebuild)}{depends}{$dep} =~ m{\d}gx
&& $gcpan_run->{portage}{$portage_name}{name} ne "module-build")
{
print $EBUILD "\>\="
. $gcpan_run->{portage}{$portage_name}{category} . '/'
. $gcpan_run->{portage}{$portage_name}{name} . '-';
if (defined($eb_version))
{
print $EBUILD $gcpan_run->{portage}{lc($portage_name)}{version};
}
else
{
print $EBUILD $gcpan_run->{cpan}{lc($dep)}{portage_version};
}
print $EBUILD "\n\t";
}
else
{
print $EBUILD ""
. $gcpan_run->{portage}{$portage_name}{category} . "/"
. $gcpan_run->{portage}{$portage_name}{name} . "\n\t";
}
}
print $EBUILD "dev-lang/perl\"\n";
if (defined($buildpkg) or defined($buildpkgonly)) {
print $EBUILD "\npkg_postinst() {\n";
print $EBUILD "elog \"If you redistribute this package, please remember to\"\n";
print $EBUILD "elog \"update /etc/portage/categories with an entry for perl-gpcan\"\n";
print $EBUILD "}\n";
}
undef $EBUILD;
autoflush STDOUT 1;
}
if (-f $gcpan_run->{cpan}{lc($original_ebuild)}{cpan_tarball})
{
$verbose and print_ok("Copying $gcpan_run->{cpan}{lc($original_ebuild)}{cpan_tarball} to $gcpan_run->{sources}");
copy($gcpan_run->{cpan}{lc($original_ebuild)}{cpan_tarball}, $gcpan_run->{sources});
}
print_info("Ebuild generated for $ebuild_wanted");
$gcpan_run->generate_digest($ebuild);
if (
!$upgrade
|| ($upgrade
&& defined($passed_to_install{$gcpan_run->{'cpan'}->{lc($original_ebuild)}->{'name'}}))
)
{
my $portage_name = $gcpan_run->{'cpan'}->{lc($original_ebuild)}->{'portage_name'};
$really_install{$portage_name} = 1;
}
last;
}
else
{
$upgrade and print_info("$ebuild_wanted already up to date") and last;
my $portage_name = $gcpan_run->{'cpan'}->{lc($original_ebuild)}->{'portage_name'};
$really_install{$portage_name} = 1;
}
}
}
}
}
else
{
print_ok("Ebuild already exists for $ebuild_wanted (".$gcpan_run->{'portage'}{lc($ebuild_wanted)}{'category'}."/".$gcpan_run->{'portage'}{lc($ebuild_wanted)}{'name'}.")");
if ( defined $passed_to_install{$ebuild_wanted} || defined $passed_to_install{$original_ebuild} )
{ $really_install{$gcpan_run->{portage}{lc($ebuild_wanted)}{'name'}} = 1 }
}
return;
}
sub scanTree
{
my ($module) = @_;
$module or return;
if ($module =~ /pathtools/gimx) { $module = "File-Spec" }
foreach my $portage_root (keys %{$gcpan_run->{portage_bases}})
{
if (-d $portage_root)
{
$verbose and print_ok("Scanning $portage_root for $module");
$gcpan_run->getAvailableVersions($portage_root, $module);
}
# Pop out of the loop if we've found the module
defined($gcpan_run->{portage}{lc($module)}{found}) and last;
}
return;
}
sub strip_ends
{
my $key = shift;
if (defined($ENV{$key}))
{
$ENV{$key} =~ s{\\n}{ }gxms;
$ENV{$key} =~ s{\\|\'|\\'|\$|\s*$}{}gmxs;
#$ENV{$key} =~ s{\'|^\\|\$|\s*\\.\s*|\\\n$}{}gmxs;
return $ENV{$key};
}
else
{
$key =~ s{\\n}{ }gxms;
#$key =~ s{\'|^\\|\$|\s*\\.\s*|\\\n$}{}gmxs;
$key =~ s{(\'|\\|\\'|\$|\s*$)}{}gmxs;
return $key;
}
}
sub stripdown
{
my ($eb, $mod) = @_;
$eb =~ s{_|-|\D+}{}gmxi;
$mod =~ s{_|-|\D+}{}gmxi;
if ($eb =~ m{^\.}x) { $eb = "00$eb" }
if ($mod =~ m{^\.}x) { $mod = "00$mod" }
my $e_in = "";
my $m_in = "";
my (@eb_ver) = split(/\./, $eb);
my (@mod_ver) = split(/\./, $mod);
my $num_e = @eb_ver;
my $num_m = @mod_ver;
if ($num_e == $num_m)
{
for (my $x = 0; $x <= ($num_e - 1); $x++)
{
if (length($eb_ver[$x]) > length($mod_ver[$x]))
{
while (length($eb_ver[$x]) > length($mod_ver[$x]))
{
$mod_ver[$x] .= "0";
}
}
if (length($mod_ver[$x]) > length($eb_ver[$x]))
{
while (length($mod_ver[$x]) > length($eb_ver[$x]))
{
$eb_ver[$x] .= "0";
}
}
$e_in .= "$eb_ver[$x]";
$m_in .= "$mod_ver[$x]";
}
}
elsif ($num_e > $num_m)
{
for (my $x = 0; $x <= ($num_e - 1); $x++)
{
unless ($mod_ver[$x])
{
$mod_ver[$x] = "00";
}
if (length($eb_ver[$x]) > length($mod_ver[$x]))
{
while (length($eb_ver[$x]) > length($mod_ver[$x]))
{
$mod_ver[$x] .= "0";
}
}
if (length($mod_ver[$x]) > length($eb_ver[$x]))
{
while (length($mod_ver[$x]) > length($eb_ver[$x]))
{
$eb_ver[$x] .= "0";
}
}
$e_in .= "$eb_ver[$x]";
$m_in .= "$mod_ver[$x]";
}
}
elsif ($num_e < $num_m)
{
for (my $x = 0; $x <= ($num_m - 1); $x++)
{
unless ($eb_ver[$x])
{
$eb_ver[$x] = "00";
}
if (length($eb_ver[$x]) > length($mod_ver[$x]))
{
while (length($eb_ver[$x]) > length($mod_ver[$x]))
{
$mod_ver[$x] .= "0";
}
}
if (length($mod_ver[$x]) > length($eb_ver[$x]))
{
while (length($mod_ver[$x]) > length($eb_ver[$x]))
{
$eb_ver[$x] .= "0";
}
}
$e_in .= "$eb_ver[$x]";
$m_in .= "$mod_ver[$x]";
}
}
$e_in =~ s{\.$}{}x;
$m_in =~ s{\.$}{}x;
return ($eb, $mod);
}
# cab - Takes care of system's sanity
END
{
#Clean out the /tmp tree we were using
#I know this looks weird, but since clean_up is invoked on a search, where OVERLAYS isn't ever defined,
# we first need to see if it exists, then need to remove only if it has content (the old exists vs. defined)
if (defined($ENV{TMPDIR}))
{
$verbose and print_ok('Cleaning temporary space');
my ($startdir) = cwd();