« blib/lib/HPC/Runner/Command/stats/Logger/JSON/Long/TableOutput.pm
↑ Summary
blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler/Files.pm »
| 1 | package HPC::Runner::Command::archive; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use MooseX::App::Command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | with 'HPC::Runner::Command::Logger::Loggers'; | |||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Cwd; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Archive::Tar; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use File::Path qw(make_path remove_tree); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use File::Find::Rule; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use IO::Dir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use IPC::Cmd qw[can_run run]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | command_short_description 'Create an archive of results.'; | |||||||||
| 20 | 1 | command_long_description 'Create an archive of results. ' | |||||||||
| 21 | . 'Default is to add all files in your current working directory.' | ||||||||||
| 22 | . 'Include or exclude with --include_paths and --exclude_paths. ' | ||||||||||
| 23 | . 'This requires tar to be installed'; | ||||||||||
| 24 | |||||||||||
| 25 | =head2 Command Line Options | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | 1 | option 'include_paths' => ( | |||||||||
| 30 | is => 'rw', | ||||||||||
| 31 | isa => Paths, | ||||||||||
| 32 | required => 0, | ||||||||||
| 33 | coerce => 1, | ||||||||||
| 34 | documentation => 'Include files or directories', | ||||||||||
| 35 | cmd_aliases => ['ip'], | ||||||||||
| 36 | predicate => 'has_include_paths', | ||||||||||
| 37 | clearer => 'clear_include_paths', | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | 1 | option 'exclude_paths' => ( | |||||||||
| 41 | is => 'rw', | ||||||||||
| 42 | isa => Paths, | ||||||||||
| 43 | required => 0, | ||||||||||
| 44 | coerce => 1, | ||||||||||
| 45 | predicate => 'has_exclude_paths', | ||||||||||
| 46 | documentation => 'Files or directories to exclude', | ||||||||||
| 47 | cmd_aliases => ['ep'], | ||||||||||
| 48 | clearer => 'clear_exclude_paths', | ||||||||||
| 49 | ); | ||||||||||
| 50 | |||||||||||
| 51 | option 'archive' => ( | ||||||||||
| 52 | is => 'rw', | ||||||||||
| 53 | isa => Path, | ||||||||||
| 54 | coerce => 1, | ||||||||||
| 55 | required => 0, | ||||||||||
| 56 | default => sub { | ||||||||||
| 57 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 58 | 0 | my $dt = DateTime->now( time_zone => 'local' ); | |||||||||
| 59 | 0 | $dt = "$dt"; | |||||||||
| 60 | 0 | $dt =~ s/:/-/g; | |||||||||
| 61 | |||||||||||
| 62 | 0 | my $tar_path = File::Spec->catdir( 'archive-' . $dt . '.tar.gz' ); | |||||||||
| 63 | |||||||||||
| 64 | 0 | return path($tar_path); | |||||||||
| 65 | }, | ||||||||||
| 66 | 1 | ); | |||||||||
| 67 | |||||||||||
| 68 | sub execute { | ||||||||||
| 69 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
execute: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | my $files = $self->list_dirs; | |||||||||
| 72 | 0 | $files = $self->check_dirs_exist($files); | |||||||||
| 73 | |||||||||||
| 74 | 0 | $self->create_archive($files); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | sub create_archive { | ||||||||||
| 78 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
create_archive: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 79 | 0 | my $files = shift; | |||||||||
| 80 | |||||||||||
| 81 | 0 | make_path( $self->archive->parent ); | |||||||||
| 82 | |||||||||||
| 83 | 0 | my $buffer; | |||||||||
| 84 | my $cmd = | ||||||||||
| 85 | 0 | "tar -zcvf " . $self->archive->stringify . " " . join( ' ', @{$files} ); | |||||||||
| 86 | 0 | $self->screen_log->info( 'Cmd is: ' . $cmd ); | |||||||||
| 87 | 0 | ▶ | if ( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | scalar run( | ||||||||||
| 89 | command => $cmd, | ||||||||||
| 90 | verbose => 0, | ||||||||||
| 91 | buffer => \$buffer, | ||||||||||
| 92 | timeout => 20 | ||||||||||
| 93 | ) | ||||||||||
| 94 | ) | ||||||||||
| 95 | { | ||||||||||
| 96 | 0 | $self->screen_log->info( | |||||||||
| 97 | 'Archive ' . $self->archive->stringify . ' created successfully' ); | ||||||||||
| 98 | } | ||||||||||
| 99 | else { | ||||||||||
| 100 | 0 | $self->screen_log->info( 'Archive could not be created! ' . $buffer ); | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | my $tar = Archive::Tar->new; | |||||||||
| 104 | 0 | $tar->read( $self->archive ); | |||||||||
| 105 | |||||||||||
| 106 | 0 | return $tar; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | sub check_dirs_exist { | ||||||||||
| 110 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
check_dirs_exist: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 111 | 0 | my $files = shift; | |||||||||
| 112 | |||||||||||
| 113 | 0 | my @exists = (); | |||||||||
| 114 | |||||||||||
| 115 | 0 | foreach my $file ( @{$files} ) { | |||||||||
| 116 | 0 | ▶ | if ( $file->exists ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | push( @exists, $file ); | |||||||||
| 118 | } | ||||||||||
| 119 | else { | ||||||||||
| 120 | 0 | $self->screen_log->warn( | |||||||||
| 121 | 'Path ' . $file->stringify . ' does not exist. Excluding.' ); | ||||||||||
| 122 | } | ||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | if ( !scalar @exists ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | $self->screen_log->fatal('There are no paths to archive. Exiting.'); | |||||||||
| 127 | 0 | exit 1; | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 0 | return \@exists; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | sub list_dirs { | ||||||||||
| 134 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
list_dirs: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 135 | |||||||||||
| 136 | 0 | my @dirs = | |||||||||
| 137 | File::Find::Rule->extras( { follow => 1 } )->maxdepth(1)->directory() | ||||||||||
| 138 | ->in(getcwd); | ||||||||||
| 139 | |||||||||||
| 140 | 0 | my @files = | |||||||||
| 141 | File::Find::Rule->extras( { follow => 1 } )->maxdepth(1)->file() | ||||||||||
| 142 | ->in(getcwd); | ||||||||||
| 143 | |||||||||||
| 144 | # my @files = glob( File::Spec->catdir( getcwd(), "*" ) ); | ||||||||||
| 145 | 0 | my $cwd_files = {}; | |||||||||
| 146 | |||||||||||
| 147 | 0 | map { $cwd_files->{$_} = 1 } @files; | |||||||||
| 148 | 0 | map { $cwd_files->{$_} = 1 } @dirs; | |||||||||
| 149 | |||||||||||
| 150 | 0 | ▶ | if ( $self->has_include_paths ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 0 | map { my $str = $_->absolute->stringify; $cwd_files->{$str} = 1 } | |||||||||
| 152 | 0 | @{ $self->include_paths }; | |||||||||
| 153 | } | ||||||||||
| 154 | 0 | push( @{ $self->exclude_paths }, path('.git') ); | |||||||||
| 155 | 0 | push( @{ $self->exclude_paths }, path(getcwd()) ); | |||||||||
| 156 | 0 | map { my $str = $_->absolute->stringify; delete $cwd_files->{$str} } | |||||||||
| 157 | 0 | @{ $self->exclude_paths }; | |||||||||
| 158 | |||||||||||
| 159 | 0 | my @keys = keys %{$cwd_files}; | |||||||||
| 160 | 0 | @keys = sort(@keys); | |||||||||
| 161 | 0 | my @rel_files = map { path($_)->relative } @keys; | |||||||||
| 162 | |||||||||||
| 163 | 0 | return \@rel_files; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 1 | 1; | |||||||||