DiskUsage probe tests
[scpubgit/System-Introspector.git] / lib / System / Introspector / LibDirs / Perl.pm
CommitLineData
f356dcb0 1package System::Introspector::LibDirs::Perl;
2use Moo;
3use Module::Metadata;
4use Digest::SHA;
5
6has root => (
7 is => 'ro',
8 default => sub { '/' },
9);
10
11sub gather {
12 my ($self) = @_;
13 my $pipe = $self->_open_locate_libdirs_pipe;
14 my %libdir;
15 while (defined( my $line = <$pipe> )) {
16 chomp $line;
17 $libdir{ $line } = $self->_gather_libdir_info($line);
18 }
19 return \%libdir;
20}
21
22sub _gather_libdir_info {
23 my ($self, $libdir) = @_;
24 my %module;
25 my $pipe = $self->_open_locate_pm_pipe($libdir);
26 while (defined( my $line = <$pipe> )) {
27 chomp $line;
28 my $metadata = Module::Metadata->new_from_file($line);
29 next unless $metadata->name;
30 my $sha = Digest::SHA->new(256);
31 $sha->addfile($line);
32 push @{ $module{ $metadata->name } //= [] }, {
33 file => $line,
34 version => $metadata->version,
35 size => scalar(-s $line),
36 sha256_hex => $sha->hexdigest,
37 };
38 }
39 return \%module;
40}
41
42sub _open_locate_pm_pipe {
43 my ($self, $libdir) = @_;
44 my $command = sprintf
45 q{find %s -name '*.pm'},
46 $libdir;
47 open my $pipe, '-|', $command
48 or die "Unable to open pipe to '$command': $!\n";
49 return $pipe;
50}
51
52sub _open_locate_libdirs_pipe {
53 my ($self) = @_;
54 my $root = $self->root;
55 $root .= '/'
56 unless $root =~ m{/$};
57 my $command = sprintf
58 q{locate --regex '^%s.*lib/perl5$'},
59 $root;
60 open my $pipe, '-|', $command
61 or die "Unable to open pipe to '$command': $!\n";
62 return $pipe;
63}
64
651;