script/filedetails.pl untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 88 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2 package My::App::Options;
3 use Moose;
4  
5 with 'MooseX::Getopt::Usage';
6 with 'MooseX::Getopt::Usage::Role::Man';
7 with 'MooseX::SimpleConfig';
8  
9 has 'check_dir' => (
10 is => 'rw',
11 isa => 'Str',
12 required => 1,
13 documentation => q(Directory to check for files.)
14 );
15  
16 has 'print_dir' => (
17 is => 'rw',
18 isa => 'Bool',
19 required => 0,
20 default => 0,
21 documentation =>
22 q(Create a directory for metadata, and write details to file self->check_dir/meta/file.meta Defaults to yes.)
23 );
24  
25 has 'print_stdout' => (
26 is => 'rw',
27 isa => 'Bool',
28 required => 0,
29 default => 1,
30 documentation => q(Print metadata to STDOUT. Default is yes.),
31 );
32  
33 has 'line_count' => (
34 is => 'rw',
35 isa => 'Bool',
36 required => 0,
37 default => 1,
38 documentation => 'q(Get a line count per file)',
39 predicate => 'has_line_count',
40 );
41  
42 package My::App::Run;
43 use My::App::Options;
44 use File::Details;
45 use File::Find::Rule;
46 use File::Spec;
47 use Time::localtime;
48 use File::stat;
49 use File::Path qw(make_path remove_tree);
50 use File::Basename;
51 use Number::Bytes::Human qw(format_bytes parse_bytes);
52  
53 my $self = My::App::Options->new_with_options();
54  
55 my @files = File::Find::Rule->file()->in( $self->check_dir );
56  
57 if ( $self->print_dir ) {
58 make_path( $self->check_dir . "/meta" );
59 }
60  
61 my $human = Number::Bytes::Human->new(
62 bs => 1024,
63 round_style => 'round',
64 precision => 2
65 );
66  
67 print "# Directory\n\n";
68 print "## " . $self->check_dir . "\n\n";
69  
70 print
71 "|| File || MD5 || Size (bytes) || Size (human) || File Creation Time || Last access time || Last modify time ||\n";
72  
73 foreach my $file (@files) {
74 $file = File::Spec->rel2abs($file);
75 my $basename = basename($file);
76 my $details = File::Details->new($file);
77  
78 my ( $hash, $size, $ctime, $actime, $mtime ) = (
79 $details->hash, $details->size,
80 ctime( stat($file)->ctime ),
81 ctime( stat($file)->atime ),
82 ctime( stat($file)->mtime )
83 );
84  
85 my $hsize = $human->format($size);
86  
87 my $info = <<EOF;
88 | $basename | $hash | $size | $hsize | $ctime | $actime | $mtime |
89 EOF
90  
91 if ( $self->has_line_count ) {
92 my $line_count;
93 if ( $file =~ m/\.gz$/ ) {
94 $line_count = `zcat $file |wc -l`;
95 }
96 else {
97 $line_count = `wc -l < $file`;
98 }
99 $info .= <<EOF;
100 $line_count|
101 EOF
102 $info .= "\n";
103  
104 }
105  
106 $DB::single = 2;
107 if ( $self->print_stdout ) {
108 print $info;
109 }
110 $DB::single = 2;
111  
112 if ( $self->print_dir ) {
113 my $basename = fileparse($file);
114 open( my $out, ">" . $self->check_dir . "/meta/$basename.meta" );
115 print $out $info;
116 }
117 }