blib/lib/CPAN/Tarball/Patch.pm

filter: Statementstmt 30.4% 28 / 92 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 9 / 15 Podpod 0.0% 0 / 5 totaltotal 26.8% 37 / 138 CC 17 SCAR 30.9
CC
17
Cov
24%
patches
44.4
patch
42.0
new
12.0
CRAP 21.9
↑ Summary
1 package CPAN::Tarball::Patch;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 CPAN::Tarball::Patch - Patch a CPAN tarball, via CPAN::Distroprefs mechanism
15  
16 =head1 VERSION
17  
18 Version 0.020000
19  
20 =cut
21  
22 1 our $VERSION = '0.020000';
23  
24  
25 =head1 SYNOPSIS
26  
27 Patching a tarball using C<CPAN::Distroprefs>
28  
29 use CPAN::Tarball::Patch;
30  
31 my $patcher = CPAN::Tarball::Patch->new("$ENV{"HOME"}/.local/share/.cpan/prefs/", "$ENV{"HOME"}/.local/share/.cpan/patches/");
32  
33 $patcher->patch("CONTRA/Acme-LSD-0.04.tar.gz");
34 # The argument format is NOT flexible since it would be used for matching by CPAN::Distroprefs
35 # In other words, it should be: AUTHOR/tarball.tar.gz
36
37 As of today, this module internal functioning and API is not fixed, do not use it yet (or at your risks).
38  
39 =cut
40  
41 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
42 0 my $class = shift;
43 0 my $prefs_dir = shift;
44 0 my $patches_dir = shift;
45  
46 0 ▶ defined $prefs_dir or die "Need to pass prefs_dir parameter";
Branch: 1 branch
BranchTrueFalseTotal
defined $prefs_dir or die "Need to pass prefs_dir parameter" 0 0 0
47 0 ▶ defined $patches_dir or die "Need to pass patches_dir parameter";
Branch: 1 branch
BranchTrueFalseTotal
defined $patches_dir or die "Need to pass patches_dir parameter" 0 0 0
48  
49 0 my $self = bless {}, $class;
50 0 $self->{prefs_dir} = $prefs_dir;
51 0 $self->{patches_dir} = $patches_dir;
52 0 $self->{start_dir} = getcwd;
53  
54 0 return $self;
55 }
56  
57 0 ▶sub patches {
Subroutine patches: not called CC=8 SCAR=37.9
Pod undocumented
58 0 my $self = shift;
59 0 my $distribution = shift;
60  
61 0 ▶ defined $distribution or die "Need to pass distribution parameter";
Branch: 1 branch
BranchTrueFalseTotal
defined $distribution or die "Need to pass distribution parameter" 0 0 0
62  
63 0 my $prefs_folder = $self->{prefs_dir};
64 0 my $patches_folder = $self->{patches_dir};
65  
66 1 ▶ use CPAN::Distroprefs;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶ use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 0 my %arg = (
70 distribution => $distribution,
71 );
72  
73 0 my %ext = ( yml => 'YAML' );
74 0 my $finder = CPAN::Distroprefs->find($prefs_folder, \%ext);
75 0 my @patches = ();
76 0 ▶ while (my $result = $finder->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $finder->next) 0 0 0
77 0 ▶ die $result->as_string if $result->is_fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_fatal 0 0 0
78 0 ▶ warn($result->as_string), next if $result->is_warning;
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_warning 0 0 0
79 0 for my $pref (@{ $result->prefs }) {
80 0 ▶ if ($pref->matches(\%arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pref->matches(\%arg)) 0 0 0
81 0 foreach my $patch ($pref->data->{patches}->@*) {
82 0 push @patches, $patch;
83 }
84 }
85 }
86 }
87  
88 0 return map { File::Spec->catfile($self->{patches_dir}, $_) } @patches;
89 }
90  
91 0 ▶sub untar {
Subroutine untar: not called CC=2 SCAR=17.9
Pod undocumented
92 0 my $self = shift;
93 0 my $tarball = shift;
94 0 ▶ defined $tarball or die "Need to pass tarball parameter";
Branch: 1 branch
BranchTrueFalseTotal
defined $tarball or die "Need to pass tarball parameter" 0 0 0
95  
96 0 my $dir = dirname($tarball);
97 0 my $tar = Archive::Tar->new($tarball, 1);
98  
99 0 chdir($dir);
100 0 $tar->extract();
101 0 my $filename = basename($tarball);
102 0 $filename =~ s/\.tar\.gz$//;
103 0 chdir($filename);
104 }
105  
106 0 ▶sub tar {
Subroutine tar: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my $self = shift;
108 0 my $tarball = shift;
109 0 ▶ defined $tarball or die "Need to pass tarball parameter";
Branch: 1 branch
BranchTrueFalseTotal
defined $tarball or die "Need to pass tarball parameter" 0 0 0
110  
111 0 my $dir = basename(getcwd());
112 0 chdir(File::Spec->updir);
113  
114 0 my $tarfile = basename($tarball);
115  
116 0 my $tar = Archive::Tar->new;
117 0 my @files;
118  
119 find(
120 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
121 0 push @files, $File::Find::name;
122 },
123 0 $dir
124 );
125 0 $tar->add_files(@files);
126  
127 0 $tar->write($tarfile, 1);
128 }
129  
130 0 ▶sub patch {
Subroutine patch: not called CC=6 SCAR=37.4
Pod undocumented
131 0 my $self = shift;
132 0 my $tarball = shift;
133 0 ▶ defined $tarball or die "Need to pass tarball parameter";
Branch: 1 branch
BranchTrueFalseTotal
defined $tarball or die "Need to pass tarball parameter" 0 0 0
134  
135  
136 0 my @patches = $self->patches($tarball);
137 0 ▶ if (scalar @patches > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @patches > 0) { } 0 0 0
138 0 $self->untar($tarball);
139 0 for my $patch (@patches) {
140 0 ▶ next if ! -f $patch;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $patch 0 0 0
141 0 print 'patching ', $tarball, ' with ', $patch, "\n";
142 0 ▶ system('cat ' . $patch . ' | patch --quiet --force -p1') and die 'failed';
Branch: 1 branch
BranchTrueFalseTotal
system 'cat ' . $patch . ' | patch --quiet --force -p1' and die "failed" 0 0 0
143 0 $self->tar($tarball);
144 0 chdir(File::Spec->updir);
145 }
146 } else {
147 0 print "Nothing to patch\n";
148 }
149  
150 }
151  
152 1 1; # End of CPAN::Tarball::Patch
↑ Summary