File Coverage

blib/lib/Dir/Split.pm
Criterion Covered Total %
statement 140 189 74.0
branch 13 42 30.9
condition 5 15 33.3
subroutine 29 32 90.6
pod 4 4 100.0
total 191 282 67.7


line stmt bran cond sub pod time code
1             package Dir::Split;
2              
3 3     3   109861 use strict;
  3         20  
  3         98  
4 3     3   18 use warnings;
  3         6  
  3         101  
5 3     3   562 use boolean qw(true false);
  3         3669  
  3         15  
6              
7 3     3   273 use Carp qw(croak);
  3         7  
  3         190  
8 3     3   27 use File::Basename ();
  3         6  
  3         125  
9 3     3   1752 use File::Copy ();
  3         7703  
  3         85  
10 3     3   25 use File::Find ();
  3         12  
  3         43  
11 3     3   13 use File::Path ();
  3         8  
  3         38  
12 3     3   13 use File::Spec ();
  3         6  
  3         57  
13 3     3   1968 use Params::Validate ':all';
  3         33940  
  3         9485  
14              
15             our $VERSION = '0.80_02';
16              
17             validation_options(
18             on_fail => sub
19             {
20             my ($error) = @_;
21             chomp $error;
22             croak $error;
23             },
24             stack_skip => 2,
25             );
26              
27             my %num_presets = (
28             verbose => false,
29             override => false,
30             sort => 'asc',
31             limit => 5,
32             prefix => 'sub',
33             separator => '-',
34             continue => false,
35             length => 5,
36             );
37             my %char_presets = (
38             verbose => false,
39             override => false,
40             prefix => 'sub',
41             separator => '-',
42             case => 'upper',
43             length => 1,
44             );
45              
46             sub new
47             {
48 2     2 1 7148 my $class = shift;
49              
50 2   33     52 my $self = bless {}, ref($class) || $class;
51              
52 2         33 $self->_init(@_);
53              
54 2         22 return $self;
55             }
56              
57             sub _init
58             {
59 2     2   5 my $self = shift;
60 2         12 my %opts = @_;
61              
62             validate(@_, {
63             source => {
64             type => SCALAR,
65             callbacks => {
66 2     2   45 'directory exists' => sub { -d $_[0] }
67             },
68             },
69             target => {
70             type => SCALAR,
71             callbacks => {
72 2     2   48 'directory exists' => sub { -d $_[0] }
73             },
74             },
75 2         101 });
76              
77 2         46 foreach my $opt (qw(source target)) {
78 4         38 $self->{ucfirst $opt} = $opts{$opt};
79             }
80             }
81              
82             sub split_num
83             {
84 1     1 1 7 my $self = shift;
85              
86 1         5 $self->_validate_num(@_);
87              
88 1         105 $self->_init_mode(@_, \%num_presets);
89              
90 1         5 my ($dirs, $files) = $self->_gather_files;
91 1         32 $self->_sort_files($files);
92              
93 1         7 my $suffix = $self->_get_num_suffix;
94 1         6 $self->_move_num($files, $suffix);
95             }
96              
97             sub _validate_num
98             {
99 1     1   3 my $self = shift;
100              
101 1         5 validate(@_, {
102             verbose => {
103             type => BOOLEAN,
104             optional => true,
105             },
106             override => {
107             type => BOOLEAN,
108             optional => true,
109             },
110             sort => {
111             type => SCALAR,
112             optional => true,
113             regex => qr!^(?:asc|desc)$!,
114             },
115             limit => {
116             type => SCALAR,
117             optional => true,
118             regex => qr!^\d+$!,
119             },
120             prefix => {
121             type => SCALAR,
122             optional => true,
123             regex => qr!^\S+$!,
124             },
125             separator => {
126             type => SCALAR,
127             optional => true,
128             regex => qr!^\S+$!,
129             },
130             continue => {
131             type => BOOLEAN,
132             optional => true,
133             },
134             length => {
135             type => SCALAR,
136             optional => true,
137             regex => qr!^\d+$!,
138             },
139             });
140             }
141              
142             sub split_char
143             {
144 1     1 1 6 my $self = shift;
145              
146 1         5 $self->_validate_char(@_);
147              
148 1         101 $self->_init_mode(@_, \%char_presets);
149              
150 1         5 my ($dirs, $files) = $self->_gather_files;
151              
152 1         3 my %suffixes;
153 1         20 $self->_get_char_suffixes($files, \%suffixes);
154 1         405 $self->_move_char(\%suffixes);
155             }
156              
157             sub _validate_char
158             {
159 1     1   2 my $self = shift;
160              
161 1         16 validate(@_, {
162             verbose => {
163             type => BOOLEAN,
164             optional => true,
165             },
166             override => {
167             type => BOOLEAN,
168             optional => true,
169             },
170             prefix => {
171             type => SCALAR,
172             optional => true,
173             regex => qr!^\S+$!,
174             },
175             separator => {
176             type => SCALAR,
177             optional => true,
178             regex => qr!^\S+$!,
179             },
180             case => {
181             type => SCALAR,
182             optional => true,
183             regex => qr!^(?:lower|upper)$!,
184             },
185             length => {
186             type => SCALAR,
187             optional => true,
188             regex => qr!^\d+$!,
189             },
190             });
191             }
192              
193             sub _init_mode
194             {
195 2     2   5 my $self = shift;
196 2         5 my $presets = pop;
197 2         7 my %opts = @_;
198              
199 2         10 delete @$self{qw(exists failure track)};
200              
201 2         28 foreach my $opt (keys %num_presets, keys %char_presets) {
202 28         51 delete $self->{ucfirst $opt};
203             }
204 2         12 foreach my $opt (keys %$presets) {
205 14         44 $self->{ucfirst $opt} = $presets->{$opt};
206             }
207 2         8 foreach my $opt (keys %opts) {
208 0         0 $self->{ucfirst $opt} = $opts{$opt};
209             }
210              
211 2         9 $self->{track}{target}{dirs} = 0;
212 2         7 $self->{track}{target}{files} = 0;
213             }
214              
215             sub _gather_files
216             {
217 2     2   4 my $self = shift;
218              
219 2         5 my (@dirs, @files);
220              
221             File::Find::find({
222             wanted => sub {
223 22 100   22   287 push @dirs, $File::Find::name if -d $_;
224 22 100       1229 push @files, $File::Find::name if -f $_;
225             },
226 2         323 }, $self->{Source});
227              
228 2         18 shift @dirs; # remove top-level directory
229              
230 2         10 $self->{track}{source}{dirs} = scalar @dirs;
231 2         6 $self->{track}{source}{files} = scalar @files;
232              
233 2         12 return (\@dirs, \@files);
234             }
235              
236             sub _sort_files
237             {
238 1     1   3 my $self = shift;
239 1         2 my ($files) = @_;
240              
241 1         7 my %sort = (
242             asc => 'lc File::Basename::basename($a) cmp lc File::Basename::basename($b)',
243             desc => 'lc File::Basename::basename($b) cmp lc File::Basename::basename($a)',
244             );
245              
246 1         2 my $cmp = $sort{$self->{Sort}};
247              
248 1         14 @$files = sort { eval $cmp } @$files;
  12         747  
249             }
250              
251             sub _get_num_suffix
252             {
253 1     1   2 my $self = shift;
254              
255 1 50       6 if ($self->{Continue}) {
256 0         0 my @dirs;
257 0         0 $self->_read_dir($self->{Target}, \@dirs);
258              
259             # Leave files behind as we need to evaluate names of subdirs.
260 0         0 @dirs = grep { -d File::Spec->catfile($self->{Target}, $_) } @dirs;
  0         0  
261              
262 0         0 my $continue = 0;
263              
264 0         0 foreach my $dir (@dirs) {
265 0 0       0 if ($dir =~ /^(.+?)\Q$self->{Separator}\E([0-9]+)$/) {
266 0         0 my ($prefix, $suffix) = ($1, $2);
267 0 0 0     0 if ($prefix eq $self->{Prefix}
      0        
268             && length $suffix == $self->{Length}
269             && $suffix > $continue
270             ) {
271 0         0 $continue = $suffix;
272             }
273             }
274             }
275 0         0 return sprintf "%0.$self->{Length}d", ++$continue;
276             }
277             else {
278 1         18 return sprintf "%0.$self->{Length}d", 1;
279             }
280             }
281              
282             sub _get_char_suffixes
283             {
284 1     1   2 my $self = shift;
285 1         4 my ($files, $suffixes) = @_;
286              
287             my %alter = (
288 0     0   0 lower => sub { lc $_[0] },
289 7     7   20 upper => sub { uc $_[0] },
290 1         12 );
291              
292 1         4 foreach my $file (@$files) {
293 7         12 my $suffix = do {
294 7         178 local $_ = File::Basename::fileparse($file, qr/(?<=\S)\.[^.]*/); # returns filename
295 7         29 s/\s//g;
296 7         19 $_ = substr($_, 0, $self->{Length});
297 7         20 $alter{$self->{Case}}->($_);
298             };
299 7         12 push @{$suffixes->{$suffix}}, $file;
  7         34  
300             }
301             }
302              
303             sub _move_num
304             {
305 1     1   2 my $self = shift;
306 1         4 my ($files, $suffix) = @_;
307              
308 1         6 while (@$files) {
309 2         8 my $target_path = $self->_make_path($suffix);
310 2         5 my $copied = 0;
311 2         3 my %seen;
312 2   100     18 while ($copied < $self->{Limit} && @$files) {
313 7         15 my $file = shift @$files;
314 7         292 my $basename = File::Basename::basename($file);
315 7 50       24 if ($seen{$basename}) {
316 0         0 $self->_copy($file, $self->_make_path($suffix, $seen{$basename}));
317             }
318             else {
319 7         16 $self->_copy($file, $target_path);
320             }
321 7         31 $seen{$basename}++;
322 7         36 $copied++;
323             }
324 2         14 $suffix++;
325             }
326             }
327              
328             sub _move_char
329             {
330 1     1   2 my $self = shift;
331 1         3 my ($suffixes) = @_;
332              
333 1         8 foreach my $suffix (sort keys %$suffixes) {
334 7         21 my $target_path = $self->_make_path($suffix);
335 7         12 my %seen;
336 7         13 while (my $file = shift @{$suffixes->{$suffix}}) {
  14         67  
337 7         223 my $basename = File::Basename::basename($file);
338 7 50       27 if ($seen{$basename}) {
339 0         0 $self->_copy($file, $self->_make_path($suffix, $seen{$basename}));
340             }
341             else {
342 7         22 $self->_copy($file, $target_path);
343             }
344 7         26 $seen{$basename}++;
345             }
346             }
347             }
348              
349             sub _make_path
350             {
351 9     9   29 my $self = shift;
352 9         24 my ($suffix, $seen) = @_;
353              
354 9 50       140 my $target_path = File::Spec->catfile($self->{Target}, "$self->{Prefix}$self->{Separator}$suffix", defined $seen ? $seen : ());
355              
356 9 50       244 if (-e $target_path) {
357 0 0       0 croak "Target path `$target_path' is not a directory" unless -d $target_path;
358 0         0 return $target_path;
359             }
360              
361 9 50       695 if (File::Path::mkpath($target_path, $self->{Verbose})) {
362 9         752 $self->{track}{target}{dirs}++;
363             }
364             else {
365 0         0 croak "Target directory `$target_path' cannot be created: $!";
366             }
367              
368 9         32 return $target_path;
369             }
370              
371             sub _copy
372             {
373 14     14   36 my $self = shift;
374 14         35 my ($file, $target_path) = @_;
375              
376 14         21 my $source_file = $file;
377 14         404 my $target_file = File::Spec->catfile($target_path, File::Basename::basename($file));
378              
379 14 50 33     340 if (-e $target_file && !$self->{Override}) {
380 0         0 push @{$self->{exists}}, $target_file;
  0         0  
381 0         0 return;
382             }
383              
384 14 50       87 if (File::Copy::copy($source_file, $target_file)) {
385 14 50       4274 print "copy $source_file -> $target_file\n" if $self->{Verbose};
386 14         152 $self->{track}{target}{files}++;
387             }
388             else {
389 0           push @{$self->{failure}{copy}}, $target_file;
  0            
390             }
391             }
392              
393             sub _read_dir
394             {
395 0     0     my $self = shift;
396 0           my ($dir, $files) = @_;
397              
398 0 0         opendir(my $dh, $dir) or croak "Cannot open directory `$dir': $!";
399 0           @$files = grep !/^\.\.?$/, readdir($dh);
400 0 0         closedir($dh) or croak "Cannot close directory `$dir': $!";
401             }
402              
403             sub print_summary
404             {
405 0     0 1   my $self = shift;
406              
407 0 0         return unless exists $self->{track};
408              
409 0           my %track = %{$self->{track}};
  0            
410              
411             my @output = (
412             [ 'dirs', $track{source}{dirs}, $track{target}{dirs} ],
413 0           [ 'files', $track{source}{files}, $track{target}{files} ],
414             );
415              
416             format STDOUT_TOP =
417             Type Source Target
418             ==========================
419             .
420 0           foreach my $line (@output) {
421             format STDOUT =
422             @<<<<< @<<<<< @<<<<<
423             @$line
424             .
425 0           write;
426             }
427 0           print "\n";
428              
429 0 0         if (@{$self->{exists} || []}) {
  0 0          
430 0           print <<'EOT';
431             Existing files
432             ==============
433             EOT
434 0           foreach my $file (@{$self->{exists}}) {
  0            
435 0           print $file, "\n";
436             }
437 0           print "\n";
438             }
439 0 0         if (@{$self->{failure}{copy} || []}) {
  0 0          
440 0           print <<'EOT';
441             Copy failures
442             =============
443             EOT
444 0           foreach my $file (@{$self->{failure}{copy}}) {
  0            
445 0           print $file, "\n";
446             }
447             }
448             }
449              
450             1;
451             __END__