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