File Coverage

lib/Sys/Path.pm
Criterion Covered Total %
statement 70 86 81.4
branch 13 18 72.2
condition 5 11 45.4
subroutine 16 17 94.1
pod 4 4 100.0
total 108 136 79.4


line stmt bran cond sub pod time code
1             package Sys::Path;
2              
3 1     1   126459 use warnings;
  1         5  
  1         50  
4 1     1   6 use strict;
  1         2  
  1         37  
5              
6             our $VERSION = '0.16';
7              
8 1     1   6 use File::Spec;
  1         2  
  1         21  
9 1     1   4 use Text::Diff 'diff';
  1         2  
  1         43  
10 1     1   338 use JSON::Util;
  1         23931  
  1         32  
11 1     1   8 use Digest::MD5 qw(md5_hex);
  1         2  
  1         47  
12 1     1   6 use List::MoreUtils 'any', 'none';
  1         2  
  1         7  
13 1     1   759 use Carp 'croak', 'confess';
  1         2  
  1         42  
14 1     1   5 use Cwd 'cwd';
  1         1  
  1         34  
15              
16 1     1   11 use base 'Sys::Path::SPc';
  1         2  
  1         304  
17              
18             sub find_distribution_root {
19 4     4 1 2949 my $self = shift;
20 4         12 my $module_name = shift;
21            
22 4 50       17 croak 'pass module_name as argument'
23             if not $module_name;
24            
25 4         15 my $module_filename = $module_name.'.pm';
26 4         25 $module_filename =~ s{::}{/}g;
27             eval 'use '.$module_name
28 1 100   1   299 unless $INC{$module_filename};
  0         0  
  0         0  
  4         122  
29            
30 4         13 my @path;
31 4 100       14 if ($INC{$module_filename}) {
32 3         71 $module_filename = File::Spec->rel2abs($INC{$module_filename});
33            
34 3         34 @path = File::Spec->splitdir($module_filename);
35 3         17 my @package_names = split('::',$module_name);
36 3         19 @path = splice(@path,0,-1-@package_names);
37             }
38             else {
39 1         4456 @path = File::Spec->splitdir(cwd);
40             }
41            
42 4   100     230 while (
      66        
43             (not -f File::Spec->catdir(@path, 'MANIFEST'))
44             and (not -f File::Spec->catdir(@path, 'Build.PL'))
45             and (not -f File::Spec->catdir(@path, 'Makefile.PL'))
46             ) {
47 0         0 pop @path;
48 0 0       0 confess 'failed to find distribution root'
49             if not @path;
50             }
51 4         3581 return File::Spec->catdir(@path);
52             }
53              
54             sub prompt_cfg_file_changed {
55 2     2 1 5986 my $self = shift;
56 2         10 my $src_file = shift;
57 2         8 my $dst_file = shift;
58 2         6 my $prompt_function = shift;
59              
60 2         6 my $answer = '';
61 2     12   33 while (none { $answer eq $_ } qw(Y I N O) ) {
  12         37  
62 2         85 print qq{
63             Installing new version of config file $dst_file ...
64              
65             Configuration file `$dst_file'
66             ==> Modified (by you or by a script) since installation.
67             ==> Package distributor has shipped an updated version.
68             What would you like to do about it ? Your options are:
69             Y or I : install the package maintainer's version
70             N or O : keep your currently-installed version
71             D : show the differences between the versions
72             Z : background this process to examine the situation
73             The default action is to keep your current version.
74             };
75            
76 2         17 $answer = uc $prompt_function->('*** '.$dst_file.' (Y/I/N/O/D/Z) ?', 'N');
77 2 50       36 if ($answer eq 'D') {
    50          
78 0         0 print "\n\n";
79 0         0 print diff($src_file, $dst_file, { STYLE => 'Unified' });
80 0         0 print "\n";
81             }
82             elsif ($answer eq 'Z') {
83 0         0 print "Type `exit' when you're done.\n";
84 0         0 system('bash');
85             }
86             }
87              
88 2 100   3   20 return 1 if any { $answer eq $_ } qw(Y I);
  3         15  
89 1         7 return 0;
90             }
91              
92             sub changed_since_install {
93 0     0 1 0 my $self = shift;
94 0         0 my $dest_file = shift;
95 0   0     0 my $file = shift || $dest_file;
96              
97 0         0 my %files_checksums = $self->install_checksums;
98 0         0 my $checksum = md5_hex(IO::Any->slurp([$file]));
99 0   0     0 $files_checksums{$dest_file} ||= '';
100 0         0 return $files_checksums{$dest_file} ne $checksum;
101             }
102              
103             sub install_checksums {
104 3     3 1 558 my $self = shift;
105 3         11 my @args = @_;
106 3         19 my $checksums_filename = File::Spec->catfile(
107             Sys::Path::SPc->sharedstatedir,
108             'syspath',
109             'install-checksums.json'
110             );
111              
112 3 100       16 if (@args) {
113 1         26 print 'Updating ', $checksums_filename, "\n";
114 1         9 my %conffiles_md5 = (
115             $self->install_checksums,
116             @args,
117             );
118 1         264 JSON::Util->encode(\%conffiles_md5, [ $checksums_filename ]);
119 1         1663 return %conffiles_md5;
120             }
121            
122             # create empty json file if non available
123 2 100       835 JSON::Util->encode({}, [ $checksums_filename ])
124             if not -f $checksums_filename;
125            
126 2         738 return %{JSON::Util->decode([ $checksums_filename ])};
  2         24  
127             }
128              
129              
130             1;
131              
132              
133             __END__