blib/script/filedetails.pl untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 85 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 => q(Create a directory for metadata, and write details to file self->check_dir/meta/file.meta Defaults to yes.)
22 );
23  
24 has 'print_stdout' => (
25 is => 'rw',
26 isa => 'Bool',
27 required => 0,
28 default => 1,
29 documentation => q(Print metadata to STDOUT. Default is yes.),
30 );
31  
32 has 'line_count' => (
33 is => 'rw',
34 isa => 'Bool',
35 required => 0,
36 default => 1,
37 documentation => 'q(Get a line count per file)',
38 predicate => 'has_line_count',
39 );
40  
41 package My::App::Run;
42 use My::App::Options;
43 use File::Details;
44 use File::Find::Rule;
45 use File::Spec;
46 use Time::localtime;
47 use File::stat;
48 use File::Path qw(make_path remove_tree);
49 use File::Basename;
50 use Number::Bytes::Human qw(format_bytes parse_bytes);
51  
52 my $self = My::App::Options->new_with_options();
53  
54 my @files = File::Find::Rule->file()->in( $self->check_dir);
55  
56 if($self->print_dir){
57 make_path($self->check_dir."/meta");
58 }
59 my $human = Number::Bytes::Human->new(bs => 1024, round_style => 'round', precision => 2);
60  
61 print "|| File || MD5 || Size (bytes) || Size (human) || File Creation Time || Last access time || Last modify time ||\n";
62  
63 foreach my $file (@files) {
64 $file = File::Spec->rel2abs($file);
65 my $details = File::Details->new( $file );
66  
67 my($hash, $size, $ctime, $actime, $mtime) = ($details->hash, $details->size, ctime(stat($file)->ctime), ctime(stat($file)->atime), ctime(stat($file)->mtime));
68  
69 my $hsize = $human->format($size);
70  
71 my $info =<<EOF;
72 | $basename | $hash | $size | $hsize | $ctime | $actime | $mtime |
73 EOF
74  
75 if($self->has_line_count){
76 my $line_count;
77 if($file =~ m/\.gz$/){
78 $line_count = `zcat $file |wc -l`
79 }
80 else{
81 $line_count = `wc -l < $file`;
82 }
83 $info .=<<EOF;
84 $line_count|
85 EOF
86 $info .="\n";
87  
88 }
89  
90 $DB::single=2;
91 if($self->print_stdout){
92 print $info;
93 }
94 $DB::single=2;
95  
96 if($self->print_dir){
97 my $basename = fileparse($file);
98 open(my $out, ">".$self->check_dir."/meta/$basename.meta");
99 print $out $info;
100 }
101 }
102  
103