blib/script/filedetails.pl untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 87 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  
60 my $human = Number::Bytes::Human->new(bs => 1024, round_style => 'round', precision => 2);
61  
62 print "# Directory\n\n";
63 print "## ".$self->check_dir."\n\n";
64  
65 print "|| File || MD5 || Size (bytes) || Size (human) || File Creation Time || Last access time || Last modify time ||\n";
66  
67 foreach my $file (@files) {
68 $file = File::Spec->rel2abs($file);
69 my $details = File::Details->new( $file );
70  
71 my($hash, $size, $ctime, $actime, $mtime) = ($details->hash, $details->size, ctime(stat($file)->ctime), ctime(stat($file)->atime), ctime(stat($file)->mtime));
72  
73 my $hsize = $human->format($size);
74  
75 my $info =<<EOF;
76 | $basename | $hash | $size | $hsize | $ctime | $actime | $mtime |
77 EOF
78  
79 if($self->has_line_count){
80 my $line_count;
81 if($file =~ m/\.gz$/){
82 $line_count = `zcat $file |wc -l`
83 }
84 else{
85 $line_count = `wc -l < $file`;
86 }
87 $info .=<<EOF;
88 $line_count|
89 EOF
90 $info .="\n";
91  
92 }
93  
94 $DB::single=2;
95 if($self->print_stdout){
96 print $info;
97 }
98 $DB::single=2;
99  
100 if($self->print_dir){
101 my $basename = fileparse($file);
102 open(my $out, ">".$self->check_dir."/meta/$basename.meta");
103 print $out $info;
104 }
105 }
106  
107