| 1 | package OPM::Maker::Command::filetest; | ||||||||||
| 2 | 22 | $OPM::Maker::Command::filetest::VERSION = '1.17'; | |||||||||
| 3 | # ABSTRACT: check if filelist in .sopm includes the files on your disk | ||||||||||
| 4 | |||||||||||
| 5 | 22 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 22 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 22 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 22 | ▶ | use File::Find::Rule; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 22 | ▶ | use Path::Class (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 22 | ▶ | use Text::Gitignore qw(match_gitignore); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 22 | ▶ | use XML::LibXML; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 22 | ▶ | use OPM::Maker -command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 22 | ▶ | use OPM::Maker::Utils qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | reformat_size | ||||||||||
| 17 | check_args_sopm | ||||||||||
| 18 | 22 | ); | |||||||||
| 19 | |||||||||||
| 20 | 1 | ▶ | sub abstract { | ||||||||
|
Subroutine
abstract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 1 | return "Check if filelist in .sopm includes the files on your disk"; | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 1 | ▶ | sub usage_desc { | ||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 1 | return "opmbuild filetest <path_to_sopm>"; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 16 | ▶ | sub validate_args { | ||||||||
|
Subroutine
validate_args: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 29 | 16 | my ($self, $opt, $args) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 16 | my $sopm = check_args_sopm( $args ); | |||||||||
| 32 | |||||||||||
| 33 | 16 | ▶ | $self->usage_error( 'need path to .sopm' ) if | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | !$sopm; | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 12 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||
| 38 | 12 | my ($self, $opt, $args) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 12 | my $file = check_args_sopm( $args ); | |||||||||
| 41 | |||||||||||
| 42 | 11 | my %opts; | |||||||||
| 43 | 11 | ▶ | if ( !$ENV{OPM_UNSECURE} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 11 | %opts = ( | |||||||||
| 45 | no_network => 1, | ||||||||||
| 46 | expand_entities => 0, | ||||||||||
| 47 | ); | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 11 | my $size = -s $file; | |||||||||
| 51 | |||||||||||
| 52 | # if file is big, but not "too big" | ||||||||||
| 53 | 11 | my $max_size = 31_457_280; | |||||||||
| 54 | 11 | ▶ | if ( $ENV{OPM_MAX_SIZE} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 7 | $max_size = reformat_size( $ENV{OPM_MAX_SIZE} ); | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 11 | ▶ | if ( $size > $max_size ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 1 | croak "$file too big (max size: $max_size bytes)"; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 10 | ▶ | if ( $size > 10_000_000 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | $opts{huge} = 1; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 10 | my $parser = XML::LibXML->new( %opts ); | |||||||||
| 67 | 10 | my $tree = $parser->parse_file( $file ); | |||||||||
| 68 | |||||||||||
| 69 | 10 | my $sopm_path = Path::Class::File->new( $file ); | |||||||||
| 70 | 10 | my $path = $sopm_path->dir; | |||||||||
| 71 | |||||||||||
| 72 | 10 | my $path_str = $path->stringify; | |||||||||
| 73 | 10 | my $hidden_files = File::Find::Rule->file->name(".*"); | |||||||||
| 74 | 10 | my @files_in_fs = File::Find::Rule->file | |||||||||
| 75 | ->not( $hidden_files ) | ||||||||||
| 76 | ->in ( $path_str ); | ||||||||||
| 77 | |||||||||||
| 78 | 31 | my %fs = map{ $_ =~ s{\A\Q$path_str\E/?}{}; $_ => 1 } | |||||||||
| 79 | 10 | grep{ $_ !~ /\.git|CVS|svn/ }@files_in_fs; | |||||||||
| 80 | |||||||||||
| 81 | 10 | delete $fs{ $sopm_path->basename }; | |||||||||
| 82 | |||||||||||
| 83 | 10 | my $ignore_file = Path::Class::File->new( | |||||||||
| 84 | $path->stringify, | ||||||||||
| 85 | '.opmbuild_filetest_ignore', | ||||||||||
| 86 | ); | ||||||||||
| 87 | |||||||||||
| 88 | 10 | my $root_elem = $tree->getDocumentElement; | |||||||||
| 89 | |||||||||||
| 90 | # retrieve file information | ||||||||||
| 91 | 10 | my @files = $root_elem->findnodes( 'Filelist/File' ); | |||||||||
| 92 | |||||||||||
| 93 | 10 | my @not_found; | |||||||||
| 94 | |||||||||||
| 95 | FILE: | ||||||||||
| 96 | 10 | for my $file ( @files ) { | |||||||||
| 97 | 19 | my $name = $file->findvalue( '@Location' ); | |||||||||
| 98 | |||||||||||
| 99 | 19 | ▶ | push @not_found, $name if !delete $fs{$name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 10 | ▶ | if ( @not_found ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | print "Files listed in .sopm but not found on disk:\n", | ||||||||||
| 104 | 1 | map{ " - $_\n" }@not_found; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 10 | my @patterns; | |||||||||
| 108 | 10 | eval { | |||||||||
| 109 | 10 | @patterns = $ignore_file->slurp( | |||||||||
| 110 | chomp => 1, | ||||||||||
| 111 | iomode => '<:encoding(utf-8)', | ||||||||||
| 112 | ); | ||||||||||
| 113 | }; | ||||||||||
| 114 | |||||||||||
| 115 | 10 | ▶ | if ( @patterns ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 1 | my @ignore = match_gitignore( | |||||||||
| 117 | [ @patterns ], | ||||||||||
| 118 | keys %fs, | ||||||||||
| 119 | ); | ||||||||||
| 120 | |||||||||||
| 121 | 1 | ▶ | if ( @ignore ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 1 | delete @fs{@ignore}; | |||||||||
| 123 | } | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 10 | ▶ | if ( %fs ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 127 | print "Files found on disk but not listed in .sopm:\n", | ||||||||||
| 128 | 1 | map{ " - $_\n" }sort keys %fs; | |||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 22 | 1; | |||||||||
| 133 | |||||||||||
| 134 | __END__ | ||||||||||