| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RPM::Spec::License; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 81932 | use strict; | 
|  | 2 |  |  |  |  | 11 |  | 
|  | 2 |  |  |  |  | 58 |  | 
| 4 | 2 |  |  | 2 |  | 12 | use warnings; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 91 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 1009 | use English; | 
|  | 2 |  |  |  |  | 7700 |  | 
|  | 2 |  |  |  |  | 17 |  | 
| 7 | 2 |  |  | 2 |  | 3576 | use Error::Pure qw(err); | 
|  | 2 |  |  |  |  | 15448 |  | 
|  | 2 |  |  |  |  | 40 |  | 
| 8 | 2 |  |  | 2 |  | 1201 | use File::Find::Rule; | 
|  | 2 |  |  |  |  | 18378 |  | 
|  | 2 |  |  |  |  | 20 |  | 
| 9 | 2 |  |  | 2 |  | 4258 | use Getopt::Std; | 
|  | 2 |  |  |  |  | 109 |  | 
|  | 2 |  |  |  |  | 135 |  | 
| 10 | 2 |  |  | 2 |  | 14 | use List::Util qw(none); | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 250 |  | 
| 11 | 2 |  |  | 2 |  | 985 | use Parse::RPM::Spec; | 
|  | 2 |  |  |  |  | 1286167 |  | 
|  | 2 |  |  |  |  | 1698 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = 0.02; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | $| = 1; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | # Constructor. | 
| 18 |  |  |  |  |  |  | sub new { | 
| 19 | 0 |  |  | 0 | 1 |  | my ($class, @params) = @_; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # Create object. | 
| 22 | 0 |  |  |  |  |  | my $self = bless {}, $class; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | # Object. | 
| 25 | 0 |  |  |  |  |  | return $self; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # Run. | 
| 29 |  |  |  |  |  |  | sub run { | 
| 30 | 0 |  |  | 0 | 1 |  | my $self = shift; | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # Process arguments. | 
| 33 | 0 |  |  |  |  |  | $self->{'_opts'} = { | 
| 34 |  |  |  |  |  |  | 'f' => 0, | 
| 35 |  |  |  |  |  |  | 'g' => '*', | 
| 36 |  |  |  |  |  |  | 'h' => 0, | 
| 37 |  |  |  |  |  |  | 's' => 0, | 
| 38 |  |  |  |  |  |  | 'u' => 0, | 
| 39 |  |  |  |  |  |  | }; | 
| 40 | 0 | 0 | 0 |  |  |  | if (! getopts('fg:hsu', $self->{'_opts'}) || $self->{'_opts'}->{'h'}) { | 
| 41 | 0 |  |  |  |  |  | print STDERR "Usage: $0 [-f] [-g file_glog] [-h] [-s] [-u] [--version] [file_or_dir]\n"; | 
| 42 | 0 |  |  |  |  |  | print STDERR "\t-f\t\tPrint spec file name.\n"; | 
| 43 | 0 |  |  |  |  |  | print STDERR "\t-g file_glob\tFile glob (default is * = *.spec).\n"; | 
| 44 | 0 |  |  |  |  |  | print STDERR "\t-h\t\tPrint help.\n"; | 
| 45 | 0 |  |  |  |  |  | print STDERR "\t-s\t\tSkip errors.\n"; | 
| 46 | 0 |  |  |  |  |  | print STDERR "\t-u\t\tPrint unique only.\n"; | 
| 47 | 0 |  |  |  |  |  | print STDERR "\t--version\tPrint version.\n"; | 
| 48 | 0 |  |  |  |  |  | print STDERR "\tfile_or_dir\tFile or directory to check license in spec ". | 
| 49 |  |  |  |  |  |  | "files (default is actual directory)\n"; | 
| 50 | 0 |  |  |  |  |  | return 1; | 
| 51 |  |  |  |  |  |  | } | 
| 52 | 0 |  | 0 |  |  |  | $self->{'_file_or_dir'} = shift @ARGV || '.'; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  |  |  |  |  | $self->{'_license_printed'} = []; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 | 0 |  |  |  |  | if (-f $self->{'_file_or_dir'}) { | 
|  |  | 0 |  |  |  |  |  | 
| 57 | 0 |  |  |  |  |  | $self->_process_spec_file($self->{'_file_or_dir'}); | 
| 58 |  |  |  |  |  |  | } elsif (-d $self->{'_file_or_dir'}) { | 
| 59 | 0 |  |  |  |  |  | foreach my $spec_file (File::Find::Rule->file | 
| 60 |  |  |  |  |  |  | ->name($self->{'_opts'}->{'g'}.'.spec')->in($self->{'_file_or_dir'})) { | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  |  | $self->_process_spec_file($spec_file); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  | } else { | 
| 65 |  |  |  |  |  |  | err 'Cannot process file or dir.', | 
| 66 | 0 |  |  |  |  |  | 'Input', $self->{'_file_or_dir'}, | 
| 67 |  |  |  |  |  |  | ; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  |  | return 0; | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub _process_spec_file { | 
| 74 | 0 |  |  | 0 |  |  | my ($self, $spec_file) = @_; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | my $rpm_spec = eval { | 
| 77 | 0 |  |  |  |  |  | Parse::RPM::Spec->new({'file' => $spec_file}); | 
| 78 |  |  |  |  |  |  | }; | 
| 79 | 0 | 0 |  |  |  |  | if ($EVAL_ERROR) { | 
| 80 | 0 | 0 |  |  |  |  | if (! $self->{'_opts'}->{'s'}) { | 
| 81 | 0 |  |  |  |  |  | err "Failing of '$spec_file'.", | 
| 82 |  |  |  |  |  |  | 'Error', $EVAL_ERROR, | 
| 83 |  |  |  |  |  |  | ; | 
| 84 |  |  |  |  |  |  | } else { | 
| 85 | 0 |  |  |  |  |  | print STDERR "Skip spec file '$spec_file' (error).\n"; | 
| 86 | 0 |  |  |  |  |  | return; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  | } | 
| 89 | 0 | 0 |  |  |  |  | if (defined $rpm_spec->license) { | 
| 90 | 0 | 0 | 0 |  |  |  | if (! $self->{'_opts'}->{'u'} | 
|  |  |  | 0 |  |  |  |  | 
| 91 |  |  |  |  |  |  | || ($self->{'_opts'}->{'u'} | 
| 92 | 0 |  |  | 0 |  |  | && (none { $rpm_spec->license eq $_ } @{$self->{'_license_printed'}}))) { | 
|  | 0 |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 | 0 |  |  |  |  | if ($self->{'_opts'}->{'f'}) { | 
| 95 | 0 |  |  |  |  |  | print $spec_file.': '; | 
| 96 |  |  |  |  |  |  | } | 
| 97 | 0 |  |  |  |  |  | print $rpm_spec->license."\n"; | 
| 98 | 0 | 0 |  |  |  |  | if ($self->{'_opts'}->{'u'}) { | 
| 99 | 0 |  |  |  |  |  | push @{$self->{'_license_printed'}}, $rpm_spec->license; | 
|  | 0 |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  | } else { | 
| 103 | 0 |  |  |  |  |  | print STDERR "Skip spec file '$spec_file' (no license).\n"; | 
| 104 | 0 |  |  |  |  |  | return; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 0 |  |  |  |  |  | return; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | 1; | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | __END__ | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =pod | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =encoding utf8 | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head1 NAME | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | App::RPM::Spec::License - Base class for rpm-spec-license tool. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | use App::RPM::Spec::License; | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | my $app = App::RPM::Spec::License->new; | 
| 128 |  |  |  |  |  |  | my $exit_code = $app->run; | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head1 METHODS | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | =head2 C<new> | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | my $app = App::RPM::Spec::License->new; | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | Constructor. | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | Returns instance of object. | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head2 C<run> | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | my $exit_code = $app->run; | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | Run. | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | Returns 1 for error, 0 for success. | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =head1 EXAMPLE1 | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =for comment filename=print_licenses.pl | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | use strict; | 
| 153 |  |  |  |  |  |  | use warnings; | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | use App::RPM::Spec::License; | 
| 156 |  |  |  |  |  |  | use File::Temp; | 
| 157 |  |  |  |  |  |  | use File::Spec::Functions qw(catfile); | 
| 158 |  |  |  |  |  |  | use IO::Barf qw(barf); | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | # Temp dir. | 
| 161 |  |  |  |  |  |  | my $temp_dir = File::Temp->newdir; | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | barf(catfile($temp_dir, 'ex1.spec'), <<'END'); | 
| 164 |  |  |  |  |  |  | License: BSD | 
| 165 |  |  |  |  |  |  | END | 
| 166 |  |  |  |  |  |  | barf(catfile($temp_dir, 'ex2.spec'), <<'END'); | 
| 167 |  |  |  |  |  |  | License: MIT | 
| 168 |  |  |  |  |  |  | END | 
| 169 |  |  |  |  |  |  | barf(catfile($temp_dir, 'ex3.spec'), <<'END'); | 
| 170 |  |  |  |  |  |  | License: MIT | 
| 171 |  |  |  |  |  |  | END | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | # Arguments. | 
| 174 |  |  |  |  |  |  | @ARGV = ( | 
| 175 |  |  |  |  |  |  | $temp_dir, | 
| 176 |  |  |  |  |  |  | ); | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | # Run. | 
| 179 |  |  |  |  |  |  | exit App::RPM::Spec::License->new->run; | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | # Output: | 
| 182 |  |  |  |  |  |  | # BSD | 
| 183 |  |  |  |  |  |  | # MIT | 
| 184 |  |  |  |  |  |  | # MIT | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head1 EXAMPLE2 | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | =for comment filename=print_unique_licenses.pl | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | use strict; | 
| 191 |  |  |  |  |  |  | use warnings; | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | use App::RPM::Spec::License; | 
| 194 |  |  |  |  |  |  | use File::Temp; | 
| 195 |  |  |  |  |  |  | use File::Spec::Functions qw(catfile); | 
| 196 |  |  |  |  |  |  | use IO::Barf qw(barf); | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | # Temp dir. | 
| 199 |  |  |  |  |  |  | my $temp_dir = File::Temp->newdir; | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | barf(catfile($temp_dir, 'ex1.spec'), <<'END'); | 
| 202 |  |  |  |  |  |  | License: BSD | 
| 203 |  |  |  |  |  |  | END | 
| 204 |  |  |  |  |  |  | barf(catfile($temp_dir, 'ex2.spec'), <<'END'); | 
| 205 |  |  |  |  |  |  | License: MIT | 
| 206 |  |  |  |  |  |  | END | 
| 207 |  |  |  |  |  |  | barf(catfile($temp_dir, 'ex3.spec'), <<'END'); | 
| 208 |  |  |  |  |  |  | License: MIT | 
| 209 |  |  |  |  |  |  | END | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | # Arguments. | 
| 212 |  |  |  |  |  |  | @ARGV = ( | 
| 213 |  |  |  |  |  |  | '-u', | 
| 214 |  |  |  |  |  |  | $temp_dir, | 
| 215 |  |  |  |  |  |  | ); | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | # Run. | 
| 218 |  |  |  |  |  |  | exit App::RPM::Spec::License->new->run; | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | # Output: | 
| 221 |  |  |  |  |  |  | # BSD | 
| 222 |  |  |  |  |  |  | # MIT | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | =head1 DEPENDENCIES | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | L<English>, | 
| 227 |  |  |  |  |  |  | L<Error::Pure>, | 
| 228 |  |  |  |  |  |  | L<File::Find::Rule>, | 
| 229 |  |  |  |  |  |  | L<Getopt::Std>. | 
| 230 |  |  |  |  |  |  | L<List::Util>, | 
| 231 |  |  |  |  |  |  | L<Parse::RPM::Spec>, | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =head1 REPOSITORY | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | L<https://github.com/michal-josef-spacek/App-RPM-Spec-License> | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | =head1 AUTHOR | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | Michal Josef Špaček L<mailto:skim@cpan.org> | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | L<http://skim.cz> | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | © 2023 Michal Josef Špaček | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | BSD 2-Clause License | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | =head1 VERSION | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | 0.02 | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | =cut |