| 1 | package Fedora::RPM::Spec::License; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use Class::Utils qw(set_params); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use English; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use License::SPDX; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use List::Util qw(none); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Parse::RecDescent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 7 | my $GRAMMAR1 = <<'END'; | |||||||||||||||||||||||||||||||
| 15 | start: expression | ||||||||||||||||||||||||||||||||
| 16 | expression: and_expr 'or' expression { | ||||||||||||||||||||||||||||||||
| 17 | [$item[1], '||', $item[3]], | ||||||||||||||||||||||||||||||||
| 18 | } | and_expr | ||||||||||||||||||||||||||||||||
| 19 | and_expr: brack_expression 'and' and_expr { | ||||||||||||||||||||||||||||||||
| 20 | [$item[1], '&&', $item[3]], | ||||||||||||||||||||||||||||||||
| 21 | } | brack_expression | ||||||||||||||||||||||||||||||||
| 22 | brack_expression: '(' expression ')' { | ||||||||||||||||||||||||||||||||
| 23 | $item[2]; | ||||||||||||||||||||||||||||||||
| 24 | } | identifier | ||||||||||||||||||||||||||||||||
| 25 | identifier: /([\w\s\.\+]+?)(?=(?:\s*and|\s*or|\(|\)|$))/ { | ||||||||||||||||||||||||||||||||
| 26 | $item[1]; | ||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | END | ||||||||||||||||||||||||||||||||
| 29 | 7 | my $GRAMMAR2 = <<'END'; | |||||||||||||||||||||||||||||||
| 30 | start: expression | ||||||||||||||||||||||||||||||||
| 31 | expression: and_expr 'OR' expression { | ||||||||||||||||||||||||||||||||
| 32 | [$item[1], '||', $item[3]], | ||||||||||||||||||||||||||||||||
| 33 | } | and_expr | ||||||||||||||||||||||||||||||||
| 34 | and_expr: brack_expression 'AND' and_expr { | ||||||||||||||||||||||||||||||||
| 35 | [$item[1], '&&', $item[3]], | ||||||||||||||||||||||||||||||||
| 36 | } | brack_expression | ||||||||||||||||||||||||||||||||
| 37 | brack_expression: '(' expression ')' { | ||||||||||||||||||||||||||||||||
| 38 | $item[2]; | ||||||||||||||||||||||||||||||||
| 39 | } | identifier | ||||||||||||||||||||||||||||||||
| 40 | identifier: /[\w\-\.]+/ { | ||||||||||||||||||||||||||||||||
| 41 | if (! License::SPDX->new->check_license($item[1])) { | ||||||||||||||||||||||||||||||||
| 42 | die "License '$item[1]' isn't SPDX license.\n"; | ||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | $item[1]; | ||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | END | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 7 | our $VERSION = 0.02; | |||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | sub new { | ||||||||||||||||||||||||||||||||
| 51 | 20 | ▶ | my ($class, @params) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | # Create object. | ||||||||||||||||||||||||||||||||
| 54 | 20 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | # Process parameters. | ||||||||||||||||||||||||||||||||
| 57 | 20 | set_params($self, @params); | |||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | 20 | $self->{'spdx'} = License::SPDX->new; | |||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | 20 | $self->{'parser1'} = Parse::RecDescent->new($GRAMMAR1); | |||||||||||||||||||||||||||||||
| 62 | 20 | $self->{'parser2'} = Parse::RecDescent->new($GRAMMAR2); | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 20 | return $self; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | sub format { | ||||||||||||||||||||||||||||||||
| 68 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
format: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | 9 | ▶ | if (! $self->{'result'}->{'status'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 71 | 2 | err 'No Fedora license string processed.'; | |||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 7 | return $self->{'result'}->{'format'}; | |||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | sub licenses { | ||||||||||||||||||||||||||||||||
| 78 | 7 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
licenses: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 7 | ▶ | if (! $self->{'result'}->{'status'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 81 | 1 | err 'No Fedora license string processed.'; | |||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 6 | return sort @{$self->{'result'}->{'licenses'}}; | |||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | sub parse { | ||||||||||||||||||||||||||||||||
| 88 | 17 | ▶ | my ($self, $fedora_license_string) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 17 | $self->_init; | |||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||
| 92 | 17 | $self->{'result'}->{'input'} = $fedora_license_string; | |||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | 17 | ▶ | if ($fedora_license_string =~ m/AND/ms | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$fedora_license_string =~ /AND/ms or $fedora_license_string =~ /OR/ms
MC/DC50%1/2
$fedora_license_string =~ /AND/ms or $fedora_license_string =~ /OR/ms
$fedora_license_string =~ /AND/ms
$fedora_license_string =~ /OR/ms
Truth table66%2/3
A or B
A= $fedora_license_string =~ /AND/msB= $fedora_license_string =~ /OR/ms
| |||||||||||||||||||||||||||||||||
| 95 | || $fedora_license_string =~ m/OR/ms) { | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 8 | $self->{'result'}->{'format'} = 2; | |||||||||||||||||||||||||||||||
| 98 | 8 | $self->_process_format_2($fedora_license_string); | |||||||||||||||||||||||||||||||
| 99 | ▶ | } elsif ($fedora_license_string =~ m/and/ms | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$fedora_license_string =~ /and/ms or $fedora_license_string =~ /or/ms
MC/DC100%2/2
$fedora_license_string =~ /and/ms or $fedora_license_string =~ /or/ms
$fedora_license_string =~ /and/ms
$fedora_license_string =~ /or/ms
Truth table100%3/3
A or B
A= $fedora_license_string =~ /and/msB= $fedora_license_string =~ /or/ms
| |||||||||||||||||||||||||||||||||
| 100 | || $fedora_license_string =~ m/or/ms) { | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 4 | $self->{'result'}->{'format'} = 1; | |||||||||||||||||||||||||||||||
| 103 | 4 | $self->_process_format_1($fedora_license_string); | |||||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||||||
| 105 | 5 | ▶ | if ($self->{'spdx'}->check_license($fedora_license_string)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 106 | 4 | $self->{'result'}->{'format'} = 2; | |||||||||||||||||||||||||||||||
| 107 | 4 | $self->_process_format_2($fedora_license_string); | |||||||||||||||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||||||||||||||
| 109 | 1 | $self->{'result'}->{'format'} = 1; | |||||||||||||||||||||||||||||||
| 110 | 1 | $self->_process_format_1($fedora_license_string); | |||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | 15 | $self->{'result'}->{'status'} = 1; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 15 | $self->_unique_licenses($self->{'result'}->{'res'}); | |||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 15 | return; | |||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | sub reset { | ||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | 1 | $self->_init; | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 1 | return; | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | sub _init { | ||||||||||||||||||||||||||||||||
| 129 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||
| 131 | 18 | $self->{'result'} = { | |||||||||||||||||||||||||||||||
| 132 | 'format' => undef, | ||||||||||||||||||||||||||||||||
| 133 | 'input' => undef, | ||||||||||||||||||||||||||||||||
| 134 | 'licenses' => [], | ||||||||||||||||||||||||||||||||
| 135 | 'status' => 0, | ||||||||||||||||||||||||||||||||
| 136 | 'res' => undef, | ||||||||||||||||||||||||||||||||
| 137 | }; | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 18 | return; | |||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | sub _process_format_1 { | ||||||||||||||||||||||||||||||||
| 143 | 5 | ▶ | my ($self, $fedora_license_string) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_process_format_1: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | 5 | $self->{'result'}->{'res'} = $self->{'parser1'}->start($fedora_license_string); | |||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 5 | return; | |||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | sub _process_format_2 { | ||||||||||||||||||||||||||||||||
| 151 | 12 | ▶ | my ($self, $fedora_license_string) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_process_format_2: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 12 | eval { | |||||||||||||||||||||||||||||||
| 154 | 12 | $self->{'result'}->{'res'} = $self->{'parser2'}->start($fedora_license_string); | |||||||||||||||||||||||||||||||
| 155 | }; | ||||||||||||||||||||||||||||||||
| 156 | 12 | ▶ | if ($EVAL_ERROR) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 157 | 2 | err $EVAL_ERROR; | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 10 | return; | |||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | sub _unique_licenses { | ||||||||||||||||||||||||||||||||
| 164 | 63 | ▶ | my ($self, $value) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_unique_licenses: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 63 | ▶ | if (ref $value eq '') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 167 | 47 | ▶ | if ($value ne '||' && $value ne '&&') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$value ne "||" and $value ne "&&"
MC/DC100%2/2
$value ne "||" and $value ne "&&"
$value ne "||"
$value ne "&&"
Truth table100%3/3
A and B
A= $value ne "||"B= $value ne "&&"
| |||||||||||||||||||||||||||||||||
| 168 | 31 | push @{$self->{'result'}->{'licenses'}}, $value; | |||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||
| 170 | } elsif (ref $value eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
| 171 | 16 | foreach my $item (@{$value}) { | |||||||||||||||||||||||||||||||
| 172 | 48 | $self->_unique_licenses($item); | |||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 63 | return; | |||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 7 | 1; | |||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||||||