Install/Util.pm

filter: Statementstmt 46.4% 52 / 112 Branchbran 19.0% 8 / 42 Conditioncond 42.8% 6 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 63.6% 7 / 11 Podpod 0.0% 0 / 4 totaltotal 38.7% 74 / 191 CC 35 SCAR 39.3
CC
35
Cov
39%
cmd_sync_bin_deps
132.0
_check_mtimes
72.0
cmd_check_dependencies
30.0
CRAP 50.7
« Install.pm ↑ Summary Install/Deps.pm »
1 package
2 XS::Install::Util;
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use XS::Install::Payload;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Fcntl qw(:flock); # import LOCK_* constants
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub linearize_dependent {
Subroutine linearize_dependent: called CC=7 SCAR=19.5
Pod undocumented
9 1 my $modules = shift;
10 1 my %modules = map { $_ => 1 } @$modules;
11 # make list of all dependent
12 1 my %dependent;
13 1 for my $module (@$modules) {
14 8 ▶ my $info = XS::Install::Payload::binary_module_info($module) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = XS::Install::Payload::binary_module_info($module) or next 8 0 8
15 8 ▶ my $dependent = $info->{BIN_DEPENDENT} || [];
Condition50%1/2
$info->{'BIN_DEPENDENT'} || []
l!l
80
MC/DC0%0/1
$info->{'BIN_DEPENDENT'} || []
$info->{'BIN_DEPENDENT'}
Truth table50%1/2
A
A= $info->{'BIN_DEPENDENT'}
Aresult
11
00
16 8 for my $d_module (@$dependent) {
17 8 ▶ next unless $modules{$d_module};
Branch: 1 branch
BranchTrueFalseTotal
unless $modules{$d_module} 1 7 8
18 7 push @{ $dependent{$module} }, $d_module;
19 }
20 }
21  
22 1 ▶ my $get_score; $get_score = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
23 18 my $module = shift;
24 18 my $score = 1; # initial value for myself
25 18 ▶ my $dependent = $dependent{$module} || [];
Condition100%2/2
$dependent{$module} || []
l!l
810
MC/DC100%1/1
$dependent{$module} || []
$dependent{$module}
Truth table100%2/2
A
A= $dependent{$module}
Aresult
11
00
26 18 for my $d_module (@$dependent) {
27 10 $score += $get_score->($d_module);
28 }
29 18 return $score;
30 1 };
31 1 my %scores = map { $_ => $get_score->($_) } @$modules;
32 my @ordered_modules = sort {
33 1 ▶ $scores{$a} <=> $scores{$b}
Condition66%2/3
$scores{$a} <=> $scores{$b} || $a cmp $b
l!l&&r!l&&!r
1040
MC/DC0%0/2
$scores{$a} <=> $scores{$b} || $a cmp $b
$scores{$a} <=> $scores{$b} $a cmp $b
Truth table66%2/3
A || B
A= $scores{$a} <=> $scores{$b}B= $a cmp $b
ABresult
1X1
011
000
34 || $a cmp $b
35 } @$modules;
36  
37 1 return \@ordered_modules;
38 }
39  
40 0 ▶sub cmd_sync_bin_deps {
Subroutine cmd_sync_bin_deps: not called CC=11 SCAR=48.8
Pod undocumented
41 0 my $myself = shift @ARGV;
42 0 my @modules = @ARGV;
43 0 foreach my $module (sort @modules) {
44 0 my $file = XS::Install::Payload::binary_module_info_file($module);
45 0 my $lock_file = "$file.lock";
46 0 my $fh_lock;
47 0 ▶ open $fh_lock, '>', $lock_file or warn "Cannot open $lock_file for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh_lock, ">", $lock_file or warn "Cannot open $lock_file for writing: $!\n" 0 0 0
48 0 ▶ if ($fh_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh_lock) 0 0 0
49 0 my $ok = eval { flock($fh_lock, LOCK_EX); 1 };
50 0 ▶ warn "Cannot lock $lock_file: $! ($@)\n" unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
51 }
52  
53 0 ▶ my $info = XS::Install::Payload::binary_module_info($module) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $info = XS::Install::Payload::binary_module_info($module) or next 0 0 0
54 0 ▶ my $dependent = $info->{BIN_DEPENDENT} || [];
Condition0%0/2
$info->{'BIN_DEPENDENT'} || []
l!l
00
MC/DC0%0/1
$info->{'BIN_DEPENDENT'} || []
$info->{'BIN_DEPENDENT'}
Truth table0%0/2
A
A= $info->{'BIN_DEPENDENT'}
Aresult
11
00
55 0 my %tmp = map {$_ => 1} grep {$_ ne $module} @$dependent;
56 0 $tmp{$myself} = 1;
57 0 $info->{BIN_DEPENDENT} = linearize_dependent([keys %tmp]);
58 0 ▶ delete $info->{BIN_DEPENDENT} unless @{$info->{BIN_DEPENDENT}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$info->{"BIN_DEPENDENT"};} 0 0 0
59 0 my $ok = eval { module_info_write($file, $info); 1 };
60 0 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
61 0 warn("Reverse dependency write failed: $@");
62 }
63 0 ▶ if ($fh_lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh_lock) 0 0 0
64 # possible errors are ignored, as we can do nothing
65 0 ▶ flock($fh_lock, LOCK_UN) && unlink($lock_file);
Branch: 1 branch
BranchTrueFalseTotal
flock $fh_lock, 8 and unlink $lock_file 0 0 0
66 }
67 }
68 }
69  
70 0 ▶sub cmd_check_dependencies {
Subroutine cmd_check_dependencies: not called CC=5 SCAR=34.0
Pod undocumented
71 0 require XS::Install::Deps;
72  
73 0 my $objext = shift @ARGV;
74  
75 0 my (@inc, @cfiles, @xsfiles);
76 0 my $curlist = \@cfiles;
77 0 foreach my $arg (@ARGV) {
78 0 ▶ if ($arg =~ s/^-I//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ s/^-I//) { } 0 0 0
elsif ($arg eq '-xs') { } 0 0 0
79 0 push @inc, $arg;
80 }
81 elsif ($arg eq '-xs') {
82 0 $curlist = \@xsfiles;
83 }
84 else {
85 0 push @$curlist, $arg;
86 }
87 }
88  
89 my @touch_list = (
90 _check_mtimes(
91 XS::Install::Deps::find_header_deps({
92 files => \@cfiles,
93 headers => ['./'],
94 inc => \@inc,
95 }),
96 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 my $ofile = shift;
98 0 $ofile =~ s/\.[^.]+$//;
99 0 $ofile .= $objext;
100 0 return $ofile;
101 },
102 0 ),
103 _check_mtimes(XS::Install::Deps::find_xsi_deps(\@xsfiles))
104 );
105  
106 0 ▶ if (@touch_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@touch_list) 0 0 0
107 0 my $now = time();
108 0 utime($now, $now, @touch_list);
109 }
110 }
111  
112 0 ▶sub _check_mtimes {
Subroutine _check_mtimes: not called CC=8 SCAR=42.8
113 0 my ($deps, $reference_file_sub) = @_;
114 0 my %mtimes;
115 my @touch_list;
116 0 foreach my $file (keys %$deps) {
117 0 ▶ my $list = $deps->{$file} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $list = $deps->{$file} or next 0 0 0
118 0 ▶ my $reference_file = $reference_file_sub ? $reference_file_sub->($file) : $file;
Branch: 1 branch
BranchTrueFalseTotal
$reference_file_sub ? : 0 0 0
119 0 ▶ my $reference_time = (stat($reference_file))[9] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $reference_time = (stat $reference_file)[9] or next 0 0 0
120 0 foreach my $depfile (@$list) {
121 0 ▶ my $mtime = $mtimes{$depfile} ||= (stat($depfile))[9];
Condition0%0/3
$mtimes{$depfile} ||= (stat $depfile)[9]
l!l&&r!l&&!r
000
MC/DC0%0/2
$mtimes{$depfile} ||= (stat $depfile)[9]
$mtimes{$depfile} (stat $depfile)[9]
Truth table0%0/3
A ||= B
A= $mtimes{$depfile}B= (stat $depfile)[9]
ABresult
1X1
011
000
122 0 ▶ next if $mtime <= $reference_time;
Branch: 1 branch
BranchTrueFalseTotal
if $mtime <= $reference_time 0 0 0
123 #warn "for file $file dependency $depfile changed";
124 0 push @touch_list, $file;
125 0 last;
126 }
127 }
128  
129 0 return @touch_list;
130 }
131  
132 4 ▶sub module_info_write {
Subroutine module_info_write: called CC=6 SCAR=18.8
Pod undocumented
133 4 my ($file, $info) = @_;
134 4 require Data::Dumper;
135 4 local $Data::Dumper::Terse = 1;
136 4 local $Data::Dumper::Indent = 0;
137 4 my $content = Data::Dumper::Dumper($info);
138 4 my $restore_mode;
139 4 ▶ if (-e $file) { # make sure we have permissions to write, because perl installs files with 444 perms
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 2 2 4
140 2 my $mode = (stat $file)[2];
141 2 ▶ unless ($mode & 0200) { # if not, temporary enable write permissions
Branch: 1 branch
BranchTrueFalseTotal
unless ($mode & 128) 0 2 2
142 0 $restore_mode = $mode;
143 0 $mode |= 0200;
144 0 chmod $mode, $file;
145 }
146 }
147  
148 4 my $temp_file = "$file.$$";
149 4 ▶ open my $fh, '>', $temp_file or die "Cannot open $temp_file for writing: $!, binary data could not be written\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $temp_file or die "Cannot open $temp_file for writing: $!, binary data could not be written\n" 4 0 4
150 4 print $fh $content;
151 4 close $fh;
152 4 ▶ rename $temp_file, $file || die("Cannot rename $temp_file to $file\n");
Condition50%1/2
$file || die("Cannot rename $temp_file to $file\n")
l!l
40
MC/DC0%0/1
$file || die("Cannot rename $temp_file to $file\n")
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
153  
154 4 ▶ chmod $restore_mode, $file if $restore_mode; # restore old perms if we changed it
Branch: 1 branch
BranchTrueFalseTotal
if $restore_mode 0 4 4
155 }
156  
157 11 1;
« Install.pm ↑ Summary Install/Deps.pm »