| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package File::RandomGenerator; | 
| 2 |  |  |  |  |  |  | { | 
| 3 |  |  |  |  |  |  | $File::RandomGenerator::VERSION = '0.06'; | 
| 4 |  |  |  |  |  |  | } | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # ABSTRACT: Utility to generate a random dir tree with random files. | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 944 | use Modern::Perl; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 10 |  | 
| 10 | 1 |  |  | 1 |  | 1191 | use Moose; | 
|  | 1 |  |  |  |  | 966296 |  | 
|  | 1 |  |  |  |  | 9 |  | 
| 11 | 1 |  |  | 1 |  | 11483 | use namespace::autoclean; | 
|  | 1 |  |  |  |  | 1872 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 12 | 1 |  |  | 1 |  | 61 | use File::Path; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 68 |  | 
| 13 | 1 |  |  | 1 |  | 6 | use File::Temp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 99 |  | 
| 14 | 1 |  |  | 1 |  | 7 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 68 |  | 
| 15 | 1 |  |  | 1 |  | 1017 | use Smart::Args; | 
|  | 1 |  |  |  |  | 33708 |  | 
|  | 1 |  |  |  |  | 97 |  | 
| 16 | 1 |  |  | 1 |  | 14 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 52 |  | 
| 17 | 1 |  |  | 1 |  | 6 | use Cwd; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 68 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  | 1 |  | 7 | use constant DEPTH => 1; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 73 |  | 
| 20 | 1 |  |  | 1 |  | 5 | use constant WIDTH => 1; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 53 |  | 
| 21 | 1 |  |  | 1 |  | 5 | use constant FILE_CNT => 10; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 22 | 1 |  |  | 1 |  | 5 | use constant ROOT_DIR => '/tmp'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 23 | 1 |  |  | 1 |  | 5 | use constant UNLINK => 0; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 758 |  | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | has 'depth' => ( is      => 'rw', | 
| 28 |  |  |  |  |  |  | isa     => 'Int', | 
| 29 |  |  |  |  |  |  | default => DEPTH | 
| 30 |  |  |  |  |  |  | ); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | has 'num_files' => ( is      => 'rw', | 
| 34 |  |  |  |  |  |  | isa     => 'Int', | 
| 35 |  |  |  |  |  |  | default => FILE_CNT | 
| 36 |  |  |  |  |  |  | ); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | has 'root_dir' => ( is      => 'rw', | 
| 41 |  |  |  |  |  |  | isa     => 'Str', | 
| 42 |  |  |  |  |  |  | default => ROOT_DIR, | 
| 43 |  |  |  |  |  |  | ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | has '_template' => ( is      => 'rw', | 
| 46 |  |  |  |  |  |  | isa     => 'Str', | 
| 47 |  |  |  |  |  |  | default => 'frgXXXXXX' | 
| 48 |  |  |  |  |  |  | ); | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | has 'unlink' => ( is      => 'rw', | 
| 52 |  |  |  |  |  |  | isa     => 'Bool', | 
| 53 |  |  |  |  |  |  | default => UNLINK | 
| 54 |  |  |  |  |  |  | ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | has 'width' => ( is      => 'rw', | 
| 58 |  |  |  |  |  |  | isa     => 'Int', | 
| 59 |  |  |  |  |  |  | default => WIDTH | 
| 60 |  |  |  |  |  |  | ); | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # | 
| 64 |  |  |  |  |  |  | # private attributes | 
| 65 |  |  |  |  |  |  | # | 
| 66 |  |  |  |  |  |  | has '_file_temp_list' => ( is      => 'rw', | 
| 67 |  |  |  |  |  |  | isa     => 'ArrayRef[ File::Temp ]', | 
| 68 |  |  |  |  |  |  | default => sub { [] } | 
| 69 |  |  |  |  |  |  | ); | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub BUILD { | 
| 73 | 3 |  |  | 3 | 0 | 7 | my $self = shift; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 3 | 50 |  |  |  | 142 | if ( !-d $self->root_dir ) { | 
| 76 | 0 |  |  |  |  | 0 | mkpath $self->root_dir; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub generate { | 
| 82 | 2 |  |  | 2 | 1 | 12 | my $self = shift; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 2 |  |  |  |  | 15 | my $orig_dir = getcwd(); | 
| 85 | 2 |  |  |  |  | 88 | my $file_tmp = File::Temp->new( UNLINK => $self->unlink ); | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 2 |  |  |  |  | 1221 | my $list = $self->_file_temp_list; | 
| 88 | 2 |  |  |  |  | 4 | push @$list, $file_tmp; | 
| 89 | 2 |  |  |  |  | 107 | $self->_file_temp_list($list); | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 2 |  |  |  |  | 92 | my $cnt = $self->_gen_level( file_tmp   => $file_tmp, | 
| 92 |  |  |  |  |  |  | curr_depth => 1, | 
| 93 |  |  |  |  |  |  | want_num   => $self->num_files, | 
| 94 |  |  |  |  |  |  | want_width => $self->width, | 
| 95 |  |  |  |  |  |  | curr_dir   => $self->root_dir, | 
| 96 |  |  |  |  |  |  | ); | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 2 | 50 |  |  |  | 47 | chdir $orig_dir or confess "failed to chdir back to $orig_dir: $!"; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 2 |  |  |  |  | 22 | return $cnt; | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | sub _gen_level { | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 5 |  |  | 5 |  | 36 | args my $self      => __PACKAGE__, | 
| 106 |  |  |  |  |  |  | my $file_tmp   => 'File::Temp', | 
| 107 |  |  |  |  |  |  | my $curr_depth => 'Int', | 
| 108 |  |  |  |  |  |  | my $want_num   => 'Int', | 
| 109 |  |  |  |  |  |  | my $want_width => 'Int', | 
| 110 |  |  |  |  |  |  | my $curr_dir   => 'Str'; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 5 |  |  |  |  | 1657 | my $cnt = 0; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 5 |  |  |  |  | 19 | for ( my $i = 0; $i < $want_num; $i++ ) { | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 120 |  |  |  |  | 5842 | my ( $fh, $filename ) | 
| 117 |  |  |  |  |  |  | = $file_tmp->tempfile( $self->_template, | 
| 118 |  |  |  |  |  |  | DIR    => $curr_dir, | 
| 119 |  |  |  |  |  |  | UNLINK => 0 | 
| 120 |  |  |  |  |  |  | ); | 
| 121 | 120 |  |  |  |  | 45661 | close $fh; | 
| 122 | 120 |  |  |  |  | 566 | $cnt++; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 5 | 100 |  |  |  | 244 | if ( $curr_depth < $self->depth ) { | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 2 |  |  |  |  | 11 | for ( my $w = 0; $w < $want_width; $w++ ) { | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 3 |  |  |  |  | 50 | my $dir = $file_tmp->newdir( DIR => $curr_dir, CLEANUP => 0 ); | 
| 130 | 3 | 50 |  |  |  | 1338 | chdir $dir or confess "failed to chdir $dir: $!"; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 3 |  |  |  |  | 112 | $cnt+= $self->_gen_level( file_tmp   => $file_tmp, | 
| 133 |  |  |  |  |  |  | curr_depth => $curr_depth + 1, | 
| 134 |  |  |  |  |  |  | want_num   => $want_num * 2, | 
| 135 |  |  |  |  |  |  | want_width => $want_width * 2, | 
| 136 |  |  |  |  |  |  | curr_dir   => $dir->{DIRNAME}, | 
| 137 |  |  |  |  |  |  | ); | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 3 | 50 |  |  |  | 48 | chdir '..' or confess "failed to chdir: $!"; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  |  | 
| 143 | 5 |  |  |  |  | 71 | return $cnt; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | __PACKAGE__->meta->make_immutable; | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | 1; | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | __END__ | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =pod | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =encoding UTF-8 | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =head1 NAME | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | File::RandomGenerator - Utility to generate a random dir tree with random files. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =head1 VERSION | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | version 0.06 | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | my $frg = File::RandomGenerator->new; | 
| 167 |  |  |  |  |  |  | $frg->generate; | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | my $frg = File::RandomGenerator->new( | 
| 170 |  |  |  |  |  |  | depth     => 2, | 
| 171 |  |  |  |  |  |  | width     => 3, | 
| 172 |  |  |  |  |  |  | num_files => 2, | 
| 173 |  |  |  |  |  |  | root_dir  => '/tmp', | 
| 174 |  |  |  |  |  |  | unlink    => 1, | 
| 175 |  |  |  |  |  |  | ); | 
| 176 |  |  |  |  |  |  | $frg->generate; | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =head2 depth | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | Max directory depth.  Default is 1. | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =head2 num_files | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | Number of files to create in the root_dir.  This number is doubled at each depth level.  For example: | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | /tmp             (10 files) | 
| 189 |  |  |  |  |  |  | /tmp/dir_a       (20 files) | 
| 190 |  |  |  |  |  |  | /tmp/dir_a/dir_b (40 files) | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | Default is 10. | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =head2 root_dir | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | Directory to put temp files and dirs.  Default is /tmp. | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | =head2 unlink | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | Flag to indicate whether or not to unlink the files and directories after the object goes out of scope.  Default is 1. | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =head2 width | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | Number of subdirs to create at each depth level. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =head1 METHODS | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =head2 new( %attrs ) | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | Constructor, 'nuff said. | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =head2 generate() | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | Generate files and directories.  Returns the number of files created. | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | =head1 AUTHOR | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | John Gravatt <john@gravatt.org> | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | This software is copyright (c) 2013 by John Gravatt. | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 225 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | =cut |