Medley/File.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Medley/Roles/Attributes/File.pm ↑ Summary Medley.pm »
1 package Util::Medley::File;
2 $Util::Medley::File::VERSION = '0.047';
3 4 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Kavorka '-all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use File::LibMagic;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use File::Path qw(make_path remove_tree);
11 use File::Touch;
12 use File::Slurp;
13 use File::Which;
14 use Try::Tiny;
15 use Path::Iterator::Rule;
16  
17 with 'Util::Medley::Roles::Attributes::Logger';
18 with 'Util::Medley::Roles::Attributes::String';
19 with 'Util::Medley::Roles::Attributes::Spawn';
20  
21 =head1 NAME
22  
23 Util::Medley::File - utility file methods
24  
25 =head1 VERSION
26  
27 version 0.047
28  
29 =cut
30  
31 =head1 SYNOPSIS
32  
33 my $util = Util::Medley::File->new;
34  
35 my $basename = $util->basename($path);
36 my $dirname = $util->dirname($path);
37 my $newpath = $util->trimSuffix($path);
38  
39 my ($dir, $utilname, $suffix) = $util->parsePath($path);
40  
41 $util->cp($src, $dest);
42 $util->mv($src, $dest);
43 $util->chmod($path);
44 $util->mkdir($path);
45 $util->rmdir($path);
46 $util->unlink($path);
47  
48 my $prev_dir = $util->chdir($path);
49 my $type = $util->fileType($path);
50 my @found = $util->find($path);
51 my $cwd = $util->getcwd;
52  
53 =cut
54  
55 ########################################################
56  
57 =head1 DESCRIPTION
58  
59 Provides frequently used file operation methods. Many of these
60 are pass-through to another module. Others offer variations on
61 the originals. All methods output debug logging statements when enabled.
62 Any errors are bubbled up with Carp::confess(). Use eval as appropriate.
63  
64 =cut
65  
66 ########################################################
67  
68 =head1 METHODS
69  
70 =head2 basename
71  
72 Pass-through to File::Path::basename().
73  
74 =over
75  
76 =item usage:
77  
78 $basename = $util->basename($path);
79  
80 $basename = $util->basename(path => $path);
81  
82 =item args:
83  
84 =over
85  
86 =item path [Str]
87  
88 The file path.
89  
90 =back
91  
92 =back
93  
94 =cut
95  
96 multi method basename (Str :$path!) {
97
98 return $self->basename($path);
99 }
100  
101 multi method basename (Str $path) {
102  
103 $self->Logger->debug("basename($path)");
104 return File::Basename::basename($path);
105 }
106  
107 =head2 chdir
108  
109 Pass-through to CORE::chdir(), but differs in that it returns the original dir.
110  
111 =over
112  
113 =item usage:
114  
115 $previous_dir = $util->chdir($path);
116  
117 $previous_dir = $util->chdir(path => $path);
118
119 =item args:
120  
121 =over
122  
123 =item $path [Str]
124  
125 Destination directory.
126  
127 =back
128  
129 =back
130  
131 =cut
132  
133 multi method chdir (Str :$dir!) {
134  
135 return $self->chdir($dir);
136 }
137  
138 multi method chdir (Str $dir) {
139  
140 $self->Logger->debug("chdir($dir)");
141 my $orig_dir = $self->getcwd;
142 CORE::chdir($dir) or confess "failed to chdir to $dir: $!";
143  
144 return $orig_dir;
145 }
146  
147 =head2 chmod
148  
149 Pass-through to CORE::chmod().
150  
151 =over
152  
153 =item usage:
154  
155 $util->chmod(0755, $path);
156  
157 $util->chmod(perm => 0755, path => $path);
158
159 =item args:
160  
161 =over
162  
163 =item perm [Str]
164  
165 Numeric mode.
166  
167 =item file [Str]
168  
169 Location of the file to update.
170  
171 =back
172  
173 =back
174  
175 =cut
176  
177 multi method chmod (Str :$perm!, Str :$file!) {
178  
179 return $self->chmod($perm, $file);
180 }
181  
182 multi method chmod (Str $perm, Str $file) {
183  
184 $self->Logger->debug("chmod($perm, $file");
185 CORE::chmod( $perm, $file );
186 }
187  
188 =head2 cp
189  
190 Pass-through to File::Copy::copy().
191  
192 =over
193  
194 =item usage:
195  
196 $util->cp($src, $dest);
197  
198 $util->cp(src => $src, dest => $dest);
199
200 =item args:
201  
202 =over
203  
204 =item src [Str]
205  
206 Source file.
207  
208 =item dest [Str]
209  
210 Destination file.
211  
212 =back
213  
214 =back
215  
216 =cut
217  
218 multi method cp (Str :$src!, Str :$dest!) {
219  
220 return $self->cp($src, $dest);
221 }
222  
223 multi method cp (Str $src, Str $dest) {
224  
225 $self->Logger->debug("cp $src, $dest");
226 return File::Copy::copy( $src, $dest );
227 }
228  
229 =head2 dirname
230  
231 Pass-through to File::Path::dirname().
232  
233 =over
234  
235 =item usage:
236  
237 $dir = $util->dirname($path);
238
239 $dir = $util->dirname(path => $path);
240
241 =item args:
242  
243 =over
244  
245 =item path [Str]
246  
247 The file path.
248  
249 =back
250  
251 =back
252  
253 =cut
254  
255 multi method dirname (Str :$path!) {
256  
257 return $self->dirname($path);
258 }
259  
260 multi method dirname (Str $path) {
261  
262 $self->Logger->debug("dirname($path)");
263 return File::Basename::dirname($path);
264 }
265  
266 =head2 fileType
267  
268 Get the filetype of a file.
269  
270 =over
271  
272 =item usage:
273  
274 $type = $util->fileType($path);
275
276 $type = $util->fileType(path => $path);
277
278 =item args:
279  
280 =over
281  
282 =item path [Str]
283  
284 Path of the file you wish to interrogate.
285  
286 =back
287  
288 =back
289  
290 =cut
291  
292 multi method fileType (Str :$path!) {
293  
294 return $self->fileType($path);
295 }
296  
297 multi method fileType (Str $path) {
298  
299 if ( $self->String->is_blank($path) ) {
300 confess "path is empty";
301 }
302  
303 if ( !-f $path ) {
304 confess "$path does not exist";
305 }
306  
307 my $info;
308 my $magic = File::LibMagic->new();
309 try {
310 # apparently dies on error?
311 $info = $magic->info_from_filename($path);
312 }
313 catch {
314 return "unknown type for $path: $_";
315 };
316  
317 return $info->{description};
318 }
319  
320 =head2 find
321  
322 Pass-through to Path::Iterator::Rule. Returns a list of all files and
323 directories. Note this does NOT return the dir passed in.
324  
325 =over
326  
327 =item usage:
328  
329 @files = $util->find($dir);
330
331 @files = $util->find( dir => $dir,
332 [minDepth => $minDepth],
333 [maxDepth => $maxDepth] );
334  
335 =item args:
336  
337 =over
338  
339 =item dir [Str]
340  
341 The directory path you wish to search.
342  
343 =item minDepth [Int]
344  
345 Minimum directory depth to traverse. Not availble for positional based method.
346  
347 =item maxDepth [Int]
348  
349 Maximum directory depth to traverse. Not availble for positional based method.
350  
351 =back
352  
353 =back
354  
355 =cut
356  
357 multi method find (Str :$dir!,
358 Int :$minDepth,
359 Int :$maxDepth) {
360
361 if ( !-d $dir ) {
362 confess "dir $dir does not exist";
363 }
364  
365 my $rule = Path::Iterator::Rule->new;
366 $rule->min_depth($minDepth) if defined $minDepth;
367 $rule->max_depth($maxDepth) if defined $maxDepth;
368
369 my @paths;
370 my $next = $rule->iter($dir);
371
372 while ( defined( my $path = $next->() ) ) {
373
374 next if $path eq $dir; # don't return self
375 push @paths, $path;
376 }
377  
378 return @paths;
379 }
380  
381 multi method find (Str $dir) {
382  
383 return $self->find(dir => $dir);
384 }
385  
386  
387 =head2 findFiles
388  
389 Returns a list of all files under a given directory. Just a
390 convenience wrapper around find.
391  
392 =over
393  
394 =item usage:
395  
396 @files = $util->findFiles($dir);
397
398 @files = $util->findFiles( dir => $dir,
399 [minDepth => $minDepth],
400 [maxDepth => $maxDepth],
401 [extension => $extension] );
402  
403 =item args:
404  
405 =over
406  
407 =item dir [Str]
408  
409 The directory path you wish to search.
410  
411 =item minDepth [Int]
412  
413 Minimum directory depth to traverse. Not availble for positional based method.
414  
415 =item maxDepth [Int]
416  
417 Maximum number of directeries (in terms of depth) to traverse. Not availble
418 for positional based method.
419  
420 =item extension [Str]
421  
422 Only return files with the given extension.
423  
424 =back
425  
426 =back
427  
428 =cut
429  
430 multi method findFiles (Str :$dir!,
431 Int :$minDepth,
432 Int :$maxDepth,
433 Str :$extension) {
434  
435 # remove leading dot from extension if provided
436 $extension =~ s/^\.// if $extension;
437
438 my %a;
439 $a{dir} = $dir;
440 $a{minDepth} = $minDepth if defined $minDepth;
441 $a{maxDepth} = $maxDepth if defined $maxDepth;
442 my @paths = $self->find(%a);
443
444 my @files;
445
446 foreach my $path (@paths) {
447 next if -d $path;
448
449 if ($extension) {
450 if ($path =~ /\.$extension$/) {
451 push @files, $path;
452 }
453 }
454 else {
455 push @files, $path;
456 }
457 }
458
459 return @files;
460 }
461  
462 multi method findFiles (Str $dir) {
463
464 return $self->findFiles(dir => $dir);
465 }
466  
467  
468 =head2 findDirs
469  
470 Returns a list of all directories under a given directory. Just a
471 convenience wrapper around find.
472  
473 =over
474  
475 =item usage:
476  
477 @dirs = $util->findDirs($dir);
478
479 @dirs = $util->findDirs( dir => $dir,
480 [minDepth => $minDepth],
481 [maxDepth => $maxDepth] );
482  
483 =item args:
484  
485 =over
486  
487 =item dir [Str]
488  
489 The directory path you wish to search.
490  
491 =item minDepth [Int]
492  
493 Minimum directory depth to traverse. Not availble for positional based method.
494  
495 =item maxDepth [Int]
496  
497 Maximum number of directeries (in terms of depth) to traverse. Not availble
498 for positional based method.
499  
500 =back
501  
502 =back
503  
504 =cut
505  
506 multi method findDirs (Str :$dir!,
507 Int :$minDepth,
508 Int :$maxDepth) {
509  
510 my %a;
511 $a{dir} = $dir;
512 $a{minDepth} = $minDepth if defined $minDepth;
513 $a{maxDepth} = $maxDepth if defined $maxDepth;
514 my @paths = $self->find(%a);
515
516 my @dirs;
517
518 foreach my $path (@paths) {
519 next if !-d $path;
520 push @dirs, $path;
521 }
522
523 return @dirs;
524 }
525  
526 multi method findDirs (Str $dir) {
527  
528 return $self->findDirs(dir => $dir);
529 }
530  
531  
532 =head2 getcwd
533  
534 Pass-through to Cwd::getcwd().
535  
536 =over
537  
538 =item usage:
539  
540 $cwd = $util->getcwd;
541  
542 =back
543  
544 =cut
545  
546 method getcwd {
547  
548 my $cwd = Cwd::getcwd();
549 $self->Logger->debug("cwd: $cwd");
550 return $cwd;
551 }
552  
553 =head2 mkdir
554  
555 Pass-through to File::Path::make_path().
556  
557 =over
558  
559 =item usage:
560  
561 $util->mkdir($path, [$perm]);
562
563 $util->mkdir(path => $path, [perm => $perm]);
564  
565 =item args:
566  
567 =over
568  
569 =item path [Str]
570  
571 The directory path.
572  
573 =item perm [Str]
574  
575 Numeric mode.
576  
577 =back
578  
579 =back
580  
581 =cut
582  
583 multi method mkdir (Str :$path!, Str :$perm) {
584  
585 return $self->mkdir(@_);
586 }
587  
588 multi method mkdir (Str $path, Str $perm?) {
589  
590 my @param = ($path);
591 push @param, { mode => $perm } if defined $perm;
592 $self->Logger->debug(sprintf("mkpath(%s)", join(', ', @param)));
593 make_path(@param);
594 }
595  
596 =head2 mv
597  
598 Pass-through to File::Copy::move().
599  
600 =over
601  
602 =item usage:
603  
604 $util->mv($src, $dest);
605  
606 $util->mv(src => $src, dest => $dest);
607
608 =item args:
609  
610 =over
611  
612 =item src [Str]
613  
614 The source path.
615  
616 =item dest [Str]
617  
618 The destination path.
619  
620 =back
621  
622 =back
623  
624 =cut
625  
626 multi method mv (Str :$src!, Str :$dest!) {
627  
628 return $self->mv($src, $dest);
629 }
630  
631 multi method mv (Str $src, Str $dest) {
632  
633 $self->Logger->debug("mv($src, $dest)");
634 my $rc = File::Copy::move( $src, $dest );
635 if (!$rc) {
636 confess "mv($src, $dest) failed: $!";
637 }
638 }
639  
640 =head2 parsePath
641  
642 Parse a file path into directory, filename, and extension. This is a
643 pass-through to File::Basename::fileparse, but it additional trims the '.'
644 from the extension and extraneous trailing /'s in the dir.
645  
646 =over
647  
648 =item usage:
649  
650 ($dir, $name, $ext) = $util->parsePath($path);
651  
652 ($dir, $name, $ext) = $util->parsePath(path => $path);
653  
654 =item args:
655  
656 =over
657  
658 =item path [Str]
659  
660 The file path for which you wish to parse.
661  
662 =back
663  
664 =back
665  
666 =cut
667  
668 multi method parsePath (Str :$path!) {
669  
670 return $self->parsePath($path);
671 }
672  
673 multi method parsePath (Str $path) {
674  
675 my ( $utilname, $dir, $suffix ) =
676 File::Basename::fileparse( $path, qr/\..*$/ );
677  
678 if ($dir ne './') {
679 $dir =~ s/\/$//g; # remove trailing slashes
680 }
681
682 if ($suffix) {
683 $suffix =~ s/^\.//g;
684 }
685  
686 return ( $dir, $utilname, $suffix );
687 }
688  
689 =head2 read
690  
691 Just a pass-through to File::Slurp::read_file().
692  
693 =over
694  
695 =item usage:
696  
697 $contents = $util->read($file, [0|1]);
698 @contents = $util->read($file, [0|1]);
699
700 $contents = $util->read(path => $file, trim => [0|1]);
701 @contents = $util->read(path => $file, trim => [0|1]);
702  
703 =item args:
704  
705 =over
706  
707 =item path [Str]
708  
709 File to read.
710  
711 =item trim [Bool]
712  
713 Trim newlines. Default 0.
714  
715 =back
716  
717 =back
718  
719 =cut
720  
721 multi method read (Str :$path,
722 Bool :$trim = 0) {
723  
724 if (wantarray) {
725 my @in;
726 foreach my $line (File::Slurp::read_file($path) ){
727 chomp $line if $trim;
728 push @in, $line;
729 }
730
731 return @in;
732 }
733 else {
734 my $in = File::Slurp::read_file($path);
735 chomp $in if $trim;
736 return $in;
737 }
738 }
739  
740 multi method read (Str $path,
741 Bool $trim = 0) {
742  
743 return $self->read(path => $path, trim => $trim);
744 }
745  
746  
747 =head2 rmdir
748  
749 Delete a directory and any contents. Pass-through to File::Path::remove_tree().
750  
751 =over
752  
753 =item usage:
754  
755 $util->rmdir($dir);
756  
757 $util->rmdir(dir => $dir);
758
759 =item args:
760  
761 =over
762  
763 =item dir [Str]
764  
765 Directory to remove.
766  
767 =back
768  
769 =back
770  
771 =cut
772  
773 multi method rmdir (Str :$dir!) {
774  
775 return $self->rmdir($dir);
776 }
777  
778 multi method rmdir (Str $dir) {
779  
780 if ( -d $dir ) {
781 $self->Logger->debug("rmdir $dir");
782 remove_tree($dir);
783 }
784 }
785  
786 =head2 slurp (deprecated)
787  
788 Just a pass-through to File::Slurp::read_file().
789  
790 =over
791  
792 =item usage:
793  
794 $contents = $util->slurp($file, [0|1]);
795 @contents = $util->slurp($file, [0|1]);
796
797 $contents = $util->slurp(path => $file, trim => [0|1]);
798 @contents = $util->slurp(path => $file, trim => [0|1]);
799  
800 =item args:
801  
802 =over
803  
804 =item path [Str]
805  
806 File to slurp.
807  
808 =item trim [Bool]
809  
810 Trim newlines. Default 0.
811  
812 =back
813  
814 =back
815  
816 =cut
817  
818 multi method slurp (Str :$path,
819 Bool :$trim = 0) {
820  
821 $self->Logger->deprecated('slurp', 'read');
822
823 if (wantarray) {
824 my @in;
825 foreach my $line (File::Slurp::read_file($path) ){
826 chomp $line if $trim;
827 push @in, $line;
828 }
829
830 return @in;
831 }
832 else {
833 my $in = File::Slurp::read_file($path);
834 chomp $in if $trim;
835 return $in;
836 }
837 }
838  
839 multi method slurp (Str $path,
840 Bool $trim = 0) {
841  
842 return $self->slurp(path => $path, trim => $trim);
843 }
844  
845  
846 =head2 touch
847  
848 Just a pass-through to File::Touch.
849  
850 =over
851  
852 =item usage:
853  
854 $util->touch($file);
855  
856 $util->touch(path => $file);
857  
858 =item args:
859  
860 =over
861  
862 =item path [Str]
863  
864 File or directory to touch.
865  
866 =back
867  
868 =back
869  
870 =cut
871  
872 multi method touch (Str :$path) {
873  
874 my $t = File::Touch->new;
875 return $t->touch($path);
876 }
877  
878 multi method touch (Str $path) {
879  
880 return $self->touch(path => $path);
881 }
882  
883  
884 =head2 trimExt
885  
886 Trim the file extension from a filename.
887  
888 =over
889  
890 =item usage:
891  
892 $filename_no_ext = $util->trimExt($filename);
893  
894 $filename_no_ext = $util->trimExt(name => $filename);
895  
896 =item args:
897  
898 =over
899  
900 =item name [Str]
901  
902 The filename for which you want to remove the extension.
903  
904 =back
905  
906 =back
907  
908 =cut
909  
910 multi method trimExt (Str :$name!) {
911  
912 return $self->trimExt($name);
913 }
914  
915 multi method trimExt (Str $name) {
916  
917 $name =~ s/\..*$//g;
918
919 return $name;
920 }
921  
922 =head2 unlink
923  
924 Pass-through to built-in unlink().
925  
926 =over
927  
928 =item usage:
929  
930 $util->unlink($path);
931  
932 $util->unlink(path => $path);
933
934 =item args:
935  
936 =over
937  
938 =item path [Str]
939  
940 Path of the file you wish to delete.
941  
942 =back
943  
944 =back
945  
946 =cut
947  
948 multi method unlink (Str :$path!) {
949  
950 return $self->unlink($path);
951 }
952  
953 multi method unlink (Str $path) {
954  
955 if ( -f $path ) {
956 $self->Logger->debug("unlink $path");
957 unlink($path) or confess "failed to unlink $path: $!";
958 }
959 }
960  
961 =head2 which
962  
963 Wrapper around File::Which::which()
964  
965 =over
966  
967 =item usage:
968  
969 $path = $util->which($exe);
970 @path = $util->which($exe);
971
972 $path = $util->which(exe => $exe);
973 @path = $util->which(exe => $exe);
974
975 =item args:
976  
977 =over
978  
979 =item exe [Str]
980  
981 Name of the executable you are searching for.
982  
983 =back
984  
985 =back
986  
987 =cut
988  
989 multi method which (Str :$exe) {
990  
991 return File::Which::which($exe);
992 }
993  
994 multi method which (Str $exe) {
995  
996 return $self->which(exe => $exe);
997 }
998  
999 =head2 write
1000  
1001 Just a pass-through to File::Slurp::write_file().
1002  
1003 =over
1004  
1005 =item usage:
1006  
1007 $util->write($file, $content, [ {opts} ]);
1008
1009 $util->write(path => $file, content => $content, [ opts => {} ]);
1010  
1011 =item args:
1012  
1013 =over
1014  
1015 =item path [Str]
1016  
1017 File to write.
1018  
1019 =item content [Str|ArrayRef]
1020  
1021 File content.
1022  
1023 =item opts [HashRef]
1024  
1025 Additional opts to pass to write_file.
1026  
1027 =back
1028  
1029 =back
1030  
1031 =cut
1032  
1033 multi method write (Str :$path!,
1034 Str|ArrayRef :$content!,
1035 HashRef :$opts) {
1036  
1037 my @a;
1038 push @a, $path;
1039 push @a, $opts if keys %$opts;
1040 push @a, $content;
1041
1042 File::Slurp::write_file(@a);
1043 }
1044  
1045 multi method write (Str $path!,
1046 Str|ArrayRef $content!,
1047 HashRef $opts?) {
1048  
1049 my %a;
1050 $a{path} = $path;
1051 $a{content} = $content;
1052 $a{opts} = $opts if $opts;
1053
1054 return $self->write(%a);
1055 }
1056  
1057  
1058 ######################################################################
1059  
1060 1;
« Medley/Roles/Attributes/File.pm ↑ Summary Medley.pm »