Syntax.pm

filter: Statementstmt 83.2% 273 / 328 Branchbran 68.7% 158 / 230 Conditioncond 64.1% 50 / 78 MC/DCmcdc 44.9% 22 / 49 Subroutinesub 88.8% 24 / 27 Podpod 100% 12 / 12 totaltotal 74.4% 539 / 724 CC 156 SCAR 34.4
CC
156
Cov
76%
_loadmap_csv_old
90.0
_group_tokens
64.9
loadmap_csv
46.5
CRAP 31.2
↑ Summary license_syntax.pl »
1 package License::Syntax;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 License::Syntax - Coding and Decoding of License strings using SPDX and SUSE syntax.
14  
15 =head1 VERSION
16  
17 Version 0.13
18  
19 =cut
20  
21 6 our $VERSION = '0.13';
22  
23  
24 =head1 SYNOPSIS
25  
26 This implements the SUSE License Syntax.
27  
28 use License::Syntax;
29  
30 my $obj = new License::Syntax licensemap => 'licenselist.csv;as';
31 my $obj = new License::Syntax map => { 'GNU General Public License V2.0' => 'GPLv2' };
32 my $obj = new License::Syntax 'pathname.sqlite;table(alias,name)';
33 $obj->loadmap_csv($filename_csv);
34 $obj->loadmap_sqlite($filename_sqlite, $table_name, $alias_col, $name_col);
35 $obj->savemap_csv($filename_csv, scalar(localtime));
36 $obj->savemap_sqlite($filename_sqlite, $table_name, $alias_col, $name_col, 'TRUNCATE');
37 $obj->add_alias($alias, $canonical_name);
38 $name = $obj->canonical_name($alias, $disambiguate);
39 $tree = $obj->tokenize('GPLv2 & Apache 1.1; LGPLv2.1 | BSD4c<<ex(UCB); Any Noncommercial', $disambiguate);
40 $name = $obj->format_tokens($tree);
41  
42  
43 =head1 FUNCTIONS
44  
45 =head2 new
46  
47 License::Syntax is an object oriented module.
48 When constructing new License::Syntax objects, you can provide a mapping table for
49 license names. The table is used for recognizing alternate alias names for the
50 licenses (left hand side) and also defines the canonical short names of the licenses
51 (right hand side).
52 The mapping table is consulted twice, before and after decoding the syntax.
53 (Thus non-terminal mappings may actually be followed.)
54  
55 The mapping table can be provided either
56  
57 =over 2
58  
59 =item * as a CSV files of two columns. Column seperator is a comma (,)
60  
61 =item * as a hash, or
62  
63 =item * as table in an sqlite database using the given columns as left hand side and right hand side respectivly.
64  
65 =back
66  
67 As an alternative to specifying a mapping with new(), or additionally, mappings
68 can also be provided via loadmap_sqlite(), loadmap_csv(), or add_alias()
69 methods. Earlier mappings take precedence over later mappings.
70  
71  
72 =cut
73  
74 sub new
75 6 ▶{
Subroutine new: called CC=8 SCAR=21.9
Pod documented
76 6 my $self = shift;
77 6 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
78 6 ▶ if (1 == scalar @_ and !ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @_ and not ref $_[0]) { } 2 4 6
Condition100%3/3
1 == scalar @_ and not ref $_[0]
!ll&&!rl&&r
222
MC/DC100%2/2
1 == scalar @_ and not ref $_[0]
1 == scalar @_ not ref $_[0]
Truth table100%3/3
A and not(B)
A= 1 == scalar @_B= not ref $_[0]
ABresult
0X0
100
111
79 {
80 2 $self = { new => { licensemap => $_[0] } };
81 }
82 else
83 {
84 4 ▶ $self = { new => { (ref $_[0] eq 'HASH') ? %{$_[0]} : @_ } };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 2 2 4
85 }
86 6 $self = bless $self, $class;
87  
88 6 $self->set_rejects('REJECT');
89  
90 # use Data::Dumper;
91 # carp Dumper $self, $class, \@_;
92 6 ▶ if ($self->{new}{map})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"new"}{"map"}) 0 6 6
93 {
94 0 for my $k (%{$self->{new}{map}})
95 {
96 0 $self->add_alias($k, $self->{new}{map}{$k});
97 }
98 0 delete $self->{new}{map};
99 }
100 $self->_loadmap($self->{new}{licensemap})
101 6 ▶ if $self->{new}{licensemap};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"new"}{"licensemap"} 3 3 6
102 6 return $self;
103 }
104  
105 # dispatch into either loadmap_sqlite or loadmap_csv.
106 sub _loadmap
107 3 ▶{
Subroutine _loadmap: called CC=6 SCAR=18.2
108 3 my ($s,$f) = @_;
109  
110 # "filename.csv"
111 # "filename.csv;garbage"
112 3 ▶ my $suf = $1 if $f =~ s{;([\w,;\#\(\)]+)$}{};
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ s/;([\w,;\#\(\)]+)$// 2 1 3
113 3 ▶ return $s->loadmap_csv($f, $suf) if $f =~ m{\.csv$}i;
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /\.csv$/i 1 2 3
114  
115 2 ▶ croak "$f: needs either .csv or .sqlite suffix\n" unless $f =~ m{\.sql(ite)?$}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /\.sql(ite)?$/i 0 2 2
116  
117 # "filename.sqlite;table"
118 # "filename.sqlite;table(alias,name)"
119 2 ▶ my ($table,$left,$right) = ($1,'alias','name') if $suf =~ m{^(\w+)};
Branch: 1 branch
BranchTrueFalseTotal
if $suf =~ /^(\w+)/ 2 0 2
120 2 ▶ ($left,$right) = ($1,$2) if $suf =~ m{\((\w+)\W(\w+)};
Branch: 1 branch
BranchTrueFalseTotal
if $suf =~ /\((\w+)\W(\w+)/ 2 0 2
121 2 return $s->loadmap_sqlite($f, $table, $left, $right);
122 }
123  
124 ## returns a two column array with the minimum representation of a license map.
125 sub _saveable_map
126 4 ▶{
Subroutine _saveable_map: called CC=5 SCAR=16.2
127 4 my ($s) = @_;
128 4 my %identity;
129 my %done;
130  
131 4 my @r;
132 4 for my $k (sort keys %{$s->{licensemap}{ex}})
133 {
134 452 my $v = $s->{licensemap}{ex}{$k};
135 452 ▶ if ($v eq $k)
Branch: 1 branch
BranchTrueFalseTotal
if ($v eq $k) { } 172 280 452
136 {
137 172 $identity{$v}++;
138 }
139 else
140 {
141 280 push @r, [$k,$v];
142 280 $done{$v}++;
143 }
144 }
145  
146 4 for my $k (keys %identity)
147 {
148 172 ▶ next if $done{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $done{$k} 172 0 172
149 0 push @r, ['',$k];
150 }
151  
152 4 return \@r;
153 }
154  
155 =head2 canonical_name
156  
157 $name = $obj->canonical_name($alias);
158 is equivalent to
159 $name = $obj->format_tokens($obj->tokenize($alias));
160
161 =cut
162  
163 sub canonical_name
164 2 ▶{
Subroutine canonical_name: called CC=1 SCAR=0.0
Pod documented
165 2 my ($s, $name) = @_;
166 2 return $s->format_tokens($s->tokenize($name));
167 }
168  
169 =head2 savemap_csv
170  
171 $obj->savemap_csv('filename.csv', scalar(localtime));
172  
173 Writes the current mapping table as a comma seperated file.
174  
175 =cut
176  
177 sub savemap_csv
178 2 ▶{
Subroutine savemap_csv: called CC=6 SCAR=18.6
Pod documented
179 2 my ($s, $f, $header_suffix) = @_;
180 2 ▶ open O, ">", $f or croak "$f: write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
open O, ">", $f or croak("${f}: write failed: $!") 2 0 2
181 2 print O qq{# "Alias name","Canonical Name" -- saved by License::Syntax $VERSION};
182 2 ▶ $header_suffix = '' unless defined $header_suffix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header_suffix 2 0 2
183 2 ▶ $header_suffix .= "\n" unless $header_suffix =~ m{\n$}s;
Branch: 1 branch
BranchTrueFalseTotal
unless $header_suffix =~ /\n$/s 2 0 2
184 2 print O $header_suffix;
185  
186 2 my $list = $s->_saveable_map();
187 2 for my $r (@$list)
188 {
189 140 print O qq{"$r->[0]","$r->[1]"\n};
190 }
191 2 ▶ close O or croak "$f: write failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
close O or croak("${f}: write failed: $!") 2 0 2
192 }
193  
194 =head2 set_rejects
195  
196 $obj->set_rejects('REJECT', ...);
197  
198 define the license names to be rejected. Per default,
199 exactly one name 'REJECT' is rejected.
200  
201 =cut
202  
203 sub set_rejects
204 6 ▶{
Subroutine set_rejects: called CC=1 SCAR=0.0
Pod documented
205 6 my ($s, @r) = @_;
206  
207 # store as a hash for faster test.
208 6 $s->{REJECT} = { map { $_ => 1 } @r };
209 6 return $s;
210 }
211  
212 =head2 add_alias
213  
214 $obj->add_alias($alias,$name);
215 $obj->add_alias(undef,$name);
216 $obj->add_alias('',$name);
217  
218 adds $name (and optionally $alias) to the objects licensemap.
219 Both, lower case and exact mappings are maintained.
220 (add_url is used in loadmap_csv)
221  
222 add_alias() takes care to extend to the right. That is, if it's right hand side
223 parameter is already known to be an alias, the new alias is added pointing to the old alias's canonical name (rahter than to the old alias that the caller provided).
224  
225 CAVEAT:
226 add_alias() does not maintain full tranitivity, as it does not extend to the left.
227 If its left hand side is already known to be a canonical name, a warning is
228 issued, but the situation cannot be corrected, as this would require rewriting
229 existing entries. This is non-obvious, as mappings are applied more than once
230 during format_tokens(), so indirect mappings involving non-terminal names, may
231 or may not work. A two step mapping currently works reliably, though.
232  
233 add_alias() does nothing, if it would directly redo an existing mapping.
234  
235 See also new() for more details about mappings.
236  
237 =cut
238  
239 =head2 add_url
240  
241 $obj->add_url($urls, $name);
242  
243 Add one or multiple URLs to the canonical license name. URLs can be seperated by comma or whitespace.
244 May be called multiple times for the same name, and fills an array of urls.
245 (add_url is used in loadmap_csv)
246  
247 =cut
248  
249 =head2 set_compat_class
250 $obj->set_compat_class($cc, $name);
251  
252 Specify the compatibility class, for a canonical license name.
253 compatibility classes are numerical. These classes allow to derive certain compatibility issues
254 amongst liceses. Some classes are always incompatible (even amongst themselves), other
255 classes are always comaptible, and for some other classses, compatibility is uncertain.
256 The exact semantics are to be defined. (set_compat_class is used in loadmap_csv).
257  
258 =cut
259  
260 sub set_compat_class
261 0 ▶{
Subroutine set_compat_class: not called CC=2 SCAR=17.9
Pod documented
262 0 my ($s, $cc, $canonical_name) = @_;
263 0 $cc += 0;
264 0 ▶ croak "compatibility class should be numeric and > 0\n" unless $cc;
Branch: 1 branch
BranchTrueFalseTotal
unless $cc 0 0 0
265 0 $s->{licensemap}{cc}{$canonical_name} = $cc;
266 }
267  
268 sub add_url
269 0 ▶{
Subroutine add_url: not called CC=1 SCAR=6.9
Pod documented
270 0 my ($s, $url, $canonical_name) = @_;
271 0 my @url = split(/[,\s]+/, $url);
272 0 push @{$s->{licensemap}{url}{$canonical_name}}, @url;
273 }
274  
275 sub add_alias
276 284 ▶{
Subroutine add_alias: called CC=16 SCAR=29.2
Pod documented
277 284 my ($s, $from, $to) = @_;
278  
279 284 ▶ $from = '' if defined($from) and $from eq $to; # not an alias.
Branch: 1 branch
BranchTrueFalseTotal
if defined $from and $from eq $to 0 284 284
Condition33%1/3
defined $from and $from eq $to
!ll&&!rl&&r
02840
MC/DC0%0/2
defined $from and $from eq $to
defined $from $from eq $to
Truth table33%1/3
A and B
A= defined $fromB= $from eq $to
ABresult
0X0
100
111
280  
281 # normalize whitespace:
282 284 $to =~ s{\s+}{ }g;
283 284 ▶ $from =~ s{\s+}{ }g if defined $from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from 284 0 284
284  
285 284 ▶ if (defined(my $nn = $s->{licensemap}{ex}{$to}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $nn = $s->{"licensemap"}{"ex"}{$to})) 109 175 284
286 {
287 # do right extend
288 # simple loopdetection first:
289 109 ▶ croak "cyclic alias '$from' -> '$to' with already known canonical name '$nn'\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $from and $from eq $nn 0 109 109
Condition33%1/3
defined $from and $from eq $nn
!ll&&!rl&&r
01090
MC/DC0%0/2
defined $from and $from eq $nn
defined $from $from eq $nn
Truth table33%1/3
A and B
A= defined $fromB= $from eq $nn
ABresult
0X0
100
111
290 if defined($from) && $from eq $nn;
291  
292 # now extend:
293 109 ▶ carp "add_alias: '$from' -> '$to' extended to '$nn'\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 109 109
294 109 $to = $nn;
295 }
296  
297 284 ▶ if (defined $from and $from ne '')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from and $from ne "") 284 0 284
Condition33%1/3
defined $from and $from ne ""
!ll&&!rl&&r
00284
MC/DC0%0/2
defined $from and $from ne ""
defined $from $from ne ""
Truth table33%1/3
A and B
A= defined $fromB= $from ne ""
ABresult
0X0
100
111
298 {
299 284 my $aa;
300 284 ▶ if (defined($aa = $s->{licensemap}{ex}{$from}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined($aa = $s->{"licensemap"}{"ex"}{$from})) 2 282 284
301 {
302 2 ▶ if ($aa eq $from)
Branch: 1 branch
BranchTrueFalseTotal
if ($aa eq $from) { } 1 1 2
303 {
304 # this alias is a right hand side.
305 # We recognize this, because all right hand sides map to itself.
306 1 my $msg = "mapping error: '$from' is now both alias and canonical name. Try to load '$from' -> '$to' earlier.";
307 1 ▶ carp "$msg\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 1 1
308 1 push @{$s->{diagnostics}}, $msg;
309 }
310 else
311 {
312 # this is a chane attempt to an existing mapping.
313 # silently ignored.
314 1 ▶ carp "mapping ignored: '$from' => '$to', it already maps to '$aa'\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 1 0 1
315 1 return $s;
316 }
317 }
318 283 $s->{licensemap}{ex}{$from} = $to;
319 283 $s->{licensemap}{lc}{lc $from} = $to;
320 283 ▶ if (scalar(my @a = _tokenize_linear($from)) > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(my(@a) = _tokenize_linear($from)) > 1) 72 211 283
321 {
322 72 $s->{licensemap}{tok}{lc $a[0]}{$from} = [ @a ];
323 }
324 }
325 283 $s->{licensemap}{ex}{$to} = $to;
326 283 $s->{licensemap}{lc}{lc $to} = $to;
327 283 ▶ if (scalar(my @a = _tokenize_linear($to)) > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(my(@a) = _tokenize_linear($to)) > 1) 68 215 283
328 {
329 68 $s->{licensemap}{tok}{lc $a[0]}{$to} = [ @a ];
330 }
331 283 return $s;
332 }
333  
334 =head2 savemap_sqlite
335  
336 $obj->savemap_sqlite('filename.sqlite', 'lic_map', 'alias', 'shortname', $trunc_flag);
337  
338  
339 # sqlite3 filename.sqlite
340 sqlite> select * from lic_map
341 alias | shortname
342 ------|----------
343 ...
344  
345 If $trunc_flag is true and the table previously exists, the table is truncated before it is written to;
346 otherwise new contents merges over old contents, if any.
347  
348 =cut
349  
350 sub savemap_sqlite
351 1 ▶{
Subroutine savemap_sqlite: called CC=5 SCAR=16.4
Pod documented
352 1 my ($s, $f, $t, $a, $n, $trunc_flag) = @_;
353  
354 1 ▶ my $dbh = DBI->connect("dbi:SQLite:dbname=$f","","") or carp "DBI-connect($f) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect("dbi:SQLite:dbname=$f", "", "") or carp("DBI-connect($f) failed: $!") 1 0 1
355  
356 1 ▶ $dbh->do("PRAGMA default_synchronous = OFF") if $s->{new}{nofsync};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"new"}{"nofsync"} 1 0 1
357 1 $dbh->do("CREATE TABLE IF NOT EXISTS $t ( $a TEXT, $n TEXT )");
358 1 ▶ $dbh->do("DELETE FROM $t") if $trunc_flag;
Branch: 1 branch
BranchTrueFalseTotal
if $trunc_flag 1 0 1
359  
360 1 my $list = $s->_saveable_map();
361 1 for my $r (@$list)
362 {
363 70 $dbh->do("INSERT OR REPLACE INTO $t ($a,$n) VALUES(?,?)", {}, $r->[0], $r->[1]);
364 }
365 1 $dbh->disconnect();
366 1 return $s;
367 }
368  
369 =head2 loadmap_sqlite
370  
371 See also new() for more details about mappings.
372  
373 =cut
374  
375 sub loadmap_sqlite
376 2 ▶{
Subroutine loadmap_sqlite: called CC=3 SCAR=11.0
Pod documented
377 2 my ($s, $f, $t, $a, $n) = @_;
378  
379 2 ▶ my $dbh = DBI->connect("dbi:SQLite:dbname=$f","","") or carp "DBI-connect($f) failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect("dbi:SQLite:dbname=$f", "", "") or carp("DBI-connect($f) failed: $!") 2 0 2
380 2 my $list = $dbh->selectall_arrayref("SELECT $a,$n FROM $t");
381 2 for my $r (@$list)
382 {
383 140 $s->add_alias($r->[0], $r->[1]);
384 }
385 2 $dbh->disconnect();
386 2 return $s;
387 }
388  
389 =head2 tokenize
390  
391 $tree_arr = $obj->tokenize($complex_license_expr);
392 $tree_arr = $obj->tokenize($complex_license_expr, 1);
393  
394 Returns an array reference containing tokens and sub-arrays,
395 describing how the $complex_license_expr is parsed.
396 If a second parameter disambiguate is provided and is true,
397 extra parenthesis are inserted to unambiguiusly show how the
398 complex expression is interpreted.
399 If names have been loaded with add_alias, before calling tokenize,
400 all names and aliases are recognized as one token. E.g. "GPL 2.0 or later"
401 would be split as ["GPL 2.0", "or", "later"] otherwise.
402 No name mapping is performed here.
403  
404  
405 =cut
406  
407 sub _tokenize_linear
408 572 ▶{
Subroutine _tokenize_linear: called CC=3 SCAR=11.0
409 572 my ($text) = @_;
410 572 $text =~ s{\s+}{ }g; # normalize whitespace
411 572 my @a = ($text =~ m{\s*(.*?)?\s*(;|\||&|\bor\b|\band\b|<<|\(|\)|$)}gi);
412  
413 ## the above regexp often returns ['somthing', '', '', '']
414 ## remove the empty trailers.
415 572 ▶ while ((scalar @a) and ($a[-1] eq ''))
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @a and $a[-1] eq "") 1598 572 2170
Condition66%2/3
scalar @a and $a[-1] eq ""
!ll&&!rl&&r
05721598
MC/DC50%1/2
scalar @a and $a[-1] eq ""
scalar @a $a[-1] eq ""
Truth table66%2/3
A and B
A= scalar @aB= $a[-1] eq ""
ABresult
0X0
100
111
416 {
417 1598 pop @a;
418 }
419 572 return @a;
420 }
421  
422 sub tokenize
423 6 ▶{
Subroutine tokenize: called CC=12 SCAR=25.1
Pod documented
424 6 my ($s, $text, $disambiguate) = @_;
425  
426 6 ▶ $text = "REJECT(?undefined($text)?)" unless $text =~ m{\w\w};
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\w\w/ 0 6 6
427  
428 #### accept a comma instead of a semicolon, unless there are semicolons.
429 ## Not done, we have to digest this: "The PHP License, version 3.01"
430 ## $text =~ s{,}{;} unless $text =~ m{;};
431  
432 ## tokenize the expression by cutting at all operators and parenthesis.
433 ## we cut before and after such operators and parenthesis, so that we
434 ## do not lose anything by cutting.
435  
436 6 my @a = _tokenize_linear($text);
437 6 my $i = 0;
438 6 ▶ for (; $i <= $#a; $i++) # this may shorten while we walk along.
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#a) 92 6 98
439 {
440 92 ▶ if (my $m = $s->{licensemap}{tok}{lc $a[$i]})
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $s->{"licensemap"}{"tok"}{lc $a[$i]}) 14 78 92
441 {
442 14 for my $k (keys %$m)
443 {
444 62 my $match = 1;
445 62 for my $j (1..$#{$m->{$k}})
446 {
447 84 ▶ if ($a[$i+$j] ne $m->{$k}[$j])
Branch: 1 branch
BranchTrueFalseTotal
if ($a[$i + $j] ne $m->{$k}[$j]) 62 22 84
448 {
449 62 $match = 0;
450 62 last;
451 }
452 }
453  
454 62 ▶ if ($match)
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 0 62 62
455 {
456 # Undo tokenization:
457 # Replace tokenized version with original license name
458 0 splice @a, $i, (scalar @{$m->{$k}}), $k;
459 0 last;
460 }
461 }
462 }
463 92 ▶ $a[$i] = 'and' if $a[$i] eq '&';
Branch: 1 branch
BranchTrueFalseTotal
if $a[$i] eq "&" 6 86 92
464 92 ▶ $a[$i] = 'or' if $a[$i] eq '|';
Branch: 1 branch
BranchTrueFalseTotal
if $a[$i] eq "|" 4 88 92
465 }
466  
467 ## before we group tokens, we pull back license names that contain or.
468
469 6 ▶ $s->{disambiguate}++ if $disambiguate;
Branch: 1 branch
BranchTrueFalseTotal
if $disambiguate 2 4 6
470 6 my $r = [ $s->_group_tokens(0, @a) ];
471 6 ▶ $s->{disambiguate}-- if $disambiguate;
Branch: 1 branch
BranchTrueFalseTotal
if $disambiguate 2 4 6
472 6 return $r;
473 }
474  
475 sub _group_tokens
476 10 ▶{
Subroutine _group_tokens: called CC=39 SCAR=41.7
477 10 my ($s, $l, @a) = @_;
478 10 ▶ $s->{debug} ||= 0; # manually enable debugging here, in new().
Condition50%1/2
$s->{"debug"} ||= 0
l!l
010
MC/DC0%0/1
$s->{"debug"} ||= 0
$s->{"debug"}
Truth table50%1/2
A
A= $s->{"debug"}
Aresult
11
00
479  
480 10 push @a, ''; # helps flushing $arr
481  
482 10 my @r;
483 10 my $arr = [];
484 10 my $in_word = 0;
485 10 my $in_parens = 0;
486  
487 10 for my $a (@a)
488 {
489 132 ▶ $in_parens++ if ($a =~ m{\(});
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /\(/ 14 118 132
490 132 ▶ $in_parens-- if ($a =~ m{\)}) and $in_parens;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /\)/ and $in_parens 14 118 132
Condition66%2/3
$a =~ /\)/ and $in_parens
!ll&&!rl&&r
118014
MC/DC50%1/2
$a =~ /\)/ and $in_parens
$a =~ /\)/ $in_parens
Truth table66%2/3
A and B
A= $a =~ /\)/B= $in_parens
ABresult
0X0
100
111
491 132 ▶ $in_word++ if ($a =~ m{\w}) and !$in_parens;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /\w/ and not $in_parens 34 98 132
Condition100%3/3
$a =~ /\w/ and not $in_parens
!ll&&!rl&&r
663234
MC/DC100%2/2
$a =~ /\w/ and not $in_parens
$a =~ /\w/ not $in_parens
Truth table100%3/3
A and not(B)
A= $a =~ /\w/B= not $in_parens
ABresult
0X0
100
111
492  
493 132 ▶ carp "$l: a='$a' in_parens=$in_parens in_word=$in_word\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 132 132
494  
495 ## operators, but not parenthesis
496 ## must include the empty string here!
497 132 ▶ if ($a =~ m{^(;|\||&|\bor\b|\band\b|<<|)$}i and !$in_parens)
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /^(;|\||&|\bor\b|\band\b|<<|)$/i and not $in_parens) 36 96 132
Condition100%3/3
$a =~ /^(;|\||&|\bor\b|\band\b|<<|)$/i and not $in_parens
!ll&&!rl&&r
781836
MC/DC100%2/2
$a =~ /^(;|\||&|\bor\b|\band\b|<<|)$/i and not $in_parens
$a =~ /^(;|\||&|\bor\b|\band\b|<<|)$/i not $in_parens
Truth table100%3/3
A and not(B)
A= $a =~ /^(;|\||&|\bor\b|\band\b|<<|)$/iB= not $in_parens
ABresult
0X0
100
111
498 {
499 36 ▶ carp "$l: emit [@$arr]\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 36 36
500 36 ▶ if (scalar @$arr)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$arr) 30 6 36
501 {
502 30 ▶ if ($in_word)
Branch: 1 branch
BranchTrueFalseTotal
if ($in_word) { } 26 4 30
503 {
504 # put whitespace around some operators, so that it looks nicer.
505 # ; only has a trailing whitespace, << has no whitespaces.
506 # KEEP IN SYNC with format_tokens()
507 26 ▶ map { $_ = " $1 " if /^(;|\||&|\bor\b|\band\b)$/; s{^ ; $}{; } } @$arr;
Branch: 1 branch
BranchTrueFalseTotal
if /^(;|\||&|\bor\b|\band\b)$/ 4 48 52
508 26 push @r, join '', @$arr;
509 }
510 else
511 {
512 ## must be an expression in parenthesis
513 4 ▶ unless ($arr->[0] eq '(' and $arr->[-1] eq ')')
Branch: 1 branch
BranchTrueFalseTotal
unless ($arr->[0] eq "(" and $arr->[-1] eq ")") 0 4 4
Condition33%1/3
$arr->[0] eq "(" and $arr->[-1] eq ")"
!ll&&!rl&&r
004
MC/DC0%0/2
$arr->[0] eq "(" and $arr->[-1] eq ")"
$arr->[0] eq "(" $arr->[-1] eq ")"
Truth table33%1/3
A and B
A= $arr->[0] eq "("B= $arr->[-1] eq ")"
ABresult
0X0
100
111
514 {
515 0 my $msg = "parse error: not in_word, and not in parens: a='$a' [@$arr]";
516 0 push @{$s->{diagnostics}}, $msg;
517 0 ▶ carp "$msg\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 0 0
518 }
519 4 ▶ shift @$arr if $arr->[0] eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $arr->[0] eq "(" 4 0 4
520 4 ▶ pop @$arr if $arr->[-1] eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $arr->[-1] eq ")" 4 0 4
521 4 ▶ carp "$l: recursion into [@$arr]\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 4 4
522 4 push @r, [ $s->_group_tokens($l+1, @$arr) ];
523 }
524 }
525 36 $arr = [];
526 36 $in_word = 0
527 }
528 132 ▶ if ($in_word or $in_parens or $a eq ')')
Branch: 1 branch
BranchTrueFalseTotal
if ($in_word or $in_parens or $a eq ')') { } 96 36 132
Condition100%6/6
$in_word or $in_parens or $a eq ")"
l!l&&r!l&&!r
92436
$in_word or $in_parens
l!l&&r!l&&!r
524040
MC/DC100%3/3
$in_word or $in_parens or $a eq ")"
$in_word $in_parens $a eq ")"
Truth table100%4/4
A or B or C
A= $in_wordB= $in_parensC= $a eq ")"
ABCresult
1XX1
01X1
0011
0000
529 {
530 96 ▶ carp "$l: add '$a' to [@$arr]\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 96 96
531 96 ▶ if ($a eq ')' and !$in_parens and !@$arr)
Branch: 1 branch
BranchTrueFalseTotal
if ($a eq ")" and not $in_parens and not @$arr) 0 96 96
Condition83%5/6
$a eq ")" and not $in_parens and not @$arr
!ll&&!rl&&r
86100
$a eq ")" and not $in_parens
!ll&&!rl&&r
82410
MC/DC0%0/3
$a eq ")" and not $in_parens and not @$arr
$a eq ")" not $in_parens not @$arr
Truth table75%3/4
A and not(B) and not(C)
A= $a eq ")"B= not $in_parensC= not @$arr
ABCresult
0XX0
10X0
1100
1111
532 {
533 0 my $msg = "parse error: bogus '$a'";
534 0 push @{$s->{diagnostics}}, $msg;
535 0 ▶ carp "$msg\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 0 0
536 }
537 96 ▶ push @$arr, $a if length $a;
Branch: 1 branch
BranchTrueFalseTotal
if length $a 90 6 96
538 }
539 else
540 {
541 36 ▶ carp "$l: emit '$a'\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 36 36
542 36 ▶ push @r, $a if length $a;
Branch: 1 branch
BranchTrueFalseTotal
if length $a 20 16 36
543 }
544 }
545  
546 10 ▶ if ($in_parens)
Branch: 1 branch
BranchTrueFalseTotal
if ($in_parens) 0 10 10
547 {
548 0 my $msg = "parse error: missing closing ')'";
549 0 push @{$s->{diagnostics}}, $msg;
550 0 ▶ carp "$msg\n" if $s->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"debug"} 0 0 0
551 0 return $s->_group_tokens($l, @a, ')')
552 }
553  
554 10 ▶ if ($s->{disambiguate})
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"disambiguate"}) 4 6 10
555 {
556 ## the ordering here defines operator precedence.
557 4 for my $op ('<<', '&', 'and', '|', 'or', ';')
558 {
559 24 @r = _disambiguate($op, @r);
560 }
561 }
562  
563 ## we remove extra parens unconditionally.
564 10 ▶ while (scalar(@r) == 1 and ref $r[0] eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @r == 1 and ref $r[0] eq "ARRAY") 4 10 14
Condition100%3/3
scalar @r == 1 and ref $r[0] eq "ARRAY"
!ll&&!rl&&r
824
MC/DC100%2/2
scalar @r == 1 and ref $r[0] eq "ARRAY"
scalar @r == 1 ref $r[0] eq "ARRAY"
Truth table100%3/3
A and B
A= scalar @r == 1B= ref $r[0] eq "ARRAY"
ABresult
0X0
100
111
565 {
566 4 ▶ if ($s->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"debug"}) 0 4 4
567 {
568 6 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
569 0 warn "removing extra parens from" . Dumper(\@r). Dumper $r[0];
570 }
571 4 @r = @{$r[0]};
572 }
573 10 return @r;
574 }
575  
576 ## find stretches of indentical operators $tok
577 ## and replace them by one sub-array each, containing the same.
578 sub _disambiguate
579 24 ▶{
Subroutine _disambiguate: called CC=6 SCAR=17.9
580 24 my ($tok, @a) = @_;
581
582 24 my $i;
583 24 ▶ for ($i = 1; $i <= $#a; $i+= 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#a) 36 24 60
584 {
585 36 ▶ if (defined($a[$i]) && $a[$i] eq $tok)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a[$i] and $a[$i] eq $tok) 8 28 36
Condition66%2/3
defined $a[$i] and $a[$i] eq $tok
!ll&&!rl&&r
0288
MC/DC50%1/2
defined $a[$i] and $a[$i] eq $tok
defined $a[$i] $a[$i] eq $tok
Truth table66%2/3
A and B
A= defined $a[$i]B= $a[$i] eq $tok
ABresult
0X0
100
111
586 {
587 8 my $e = $i;
588 8 ▶ while (defined($a[$e+2]) && $a[$e+2] eq $tok) { $e += 2; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a[$e + 2] and $a[$e + 2] eq $tok) 2 8 10
Condition100%3/3
defined $a[$e + 2] and $a[$e + 2] eq $tok
!ll&&!rl&&r
442
MC/DC100%2/2
defined $a[$e + 2] and $a[$e + 2] eq $tok
defined $a[$e + 2] $a[$e + 2] eq $tok
Truth table100%3/3
A and B
A= defined $a[$e + 2]B= $a[$e + 2] eq $tok
ABresult
0X0
100
111
589 8 splice @a, $i-1, $e-$i+3, [ @a[$i-1 .. $e+1] ];
590 # assert: $a[$i+2] cannot be $tok now.
591 }
592 }
593 24 return @a;
594 }
595  
596 =head2 format_tokens
597  
598 reverse operation of tokenize()
599  
600 =cut
601  
602 sub _map_license_name
603 26 ▶{
Subroutine _map_license_name: called CC=19 SCAR=29.9
604 26 my ($s, $name, $prev_op) = @_;
605 26 my $parens;
606  
607 26 my $origname = $name;
608  
609 # used as a flag, so that we know if we mapped the name at least once.
610 26 my $mapped;
611 # we try the mapping three times:
612 # first including any parenthesis
613 # second: after splitting parethetical description
614 # third: after shaping into possibly conforming syntax
615  
616 {
617 26 my $new = $s->{licensemap}{ex}{$name};
618 26 ▶ $new = $s->{licensemap}{lc}{lc $name} unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 12 14 26
619 26 ▶ $mapped = $name = $new if defined $new;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new 16 10 26
620 }
621  
622 26 ▶ ($name,$parens) = ($1,$3) if $name =~ m{^\s*(.*?)\s*(\((.*)\))?\s*$};
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\s*(.*?)\s*(\((.*)\))?\s*$/ 26 0 26
623 26 ▶ $origname = $name unless $name eq 'REJECT';
Branch: 1 branch
BranchTrueFalseTotal
unless $name eq "REJECT" 26 0 26
624  
625 ## allow for underscores in these name, to make vim users happy.
626 26 $name =~ s{(PERMISSIVE|NON|COPYLEFT)[-_]OSI[-_]COMPLIANT}{$1-OSI-COMPLIANT}g;
627  
628  
629 {
630 26 my $new = $s->{licensemap}{ex}{$name};
631 26 ▶ $new = $s->{licensemap}{lc}{lc $name} unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 8 18 26
632 26 ▶ $mapped = $name = $new if defined $new;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new 18 8 26
633 }
634  
635 26 if (1)
636 {
637 ## policy: version numbers are appended with '-', not with 'v', ' V'
638 ## version numbers do not end in '.0'
639 # LGPLv2.1 -> LGPL-2.1
640 # Apache V2.0 -> Apache-2.0
641 ###
642 26 ▶ $name =~ s{(\w+)(-v|-V| V| v|v| )(\d[\.\d]*\+?|\d+\.\d[-~\w]*\+?)\s*$}{$1-$3} unless $s->{licensemap}{lc}{lc $name};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"licensemap"}{"lc"}{lc $name} 8 18 26
643 26 ▶ $name =~ s{(\d)\.0$}{$1} unless $s->{licensemap}{lc}{lc $name};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"licensemap"}{"lc"}{lc $name} 8 18 26
644 }
645 else # this is old policy, we do it the other way round now.
646 {
647 ## policy: version numbers are appended with a lower case v,
648 ## if the name is all caps and without white space.
649 ## otherwise append with space and capital V.
650  
651 ## LGPL-2.1 -> LGPL v2.1 -> LGPLv2.1
652 ## LGPL-V2.1 -> LGPL v2.1 -> LGPLv2.1
653 ## GPL-2+ -> GPL v2+ -> GPLv2+
654 ## Apache-2.0 -> Apache v2.0 -> Apache V2.0
655 ## do this only, if it really looks like a version number.
656 ## e.g. XXX-3 XXX-3.0~alpha BUT NOT vision-3d or BSD-4clause
657 $name =~ s{(\w+)-[vV]?(\d[\.\d]*\+?|\d+\.\d[-~\w]*\+?)\s*$}{$1 v$2};
658  
659  
660 ## LGPL v2.1 -> LGPLv2.1
661 ## LGPL 2.1+ -> LGPLv2.1+
662 ## PERMISSIVE -> PERMISSIVE
663 unless ($name =~ s{^([A-Z_\d\.-]+)\s*[vV ](\d\S*?)$}{$1v$2})
664 {
665 ## CC BY-SA v3.5 -> CC BY-SA V3.5
666 ## Apache v2.0 -> Apache V2.0
667 $name =~ s{^(.*\S)\s*[vV ](\d\S*?)$}{$1 V$2};
668 }
669 }
670  
671 ##
672 ## policy: modifiers are all lower case, licenses start upper case.
673 26 ▶ if (($prev_op||'') eq '<<')
Branch: 1 branch
BranchTrueFalseTotal
if (($prev_op || '') eq '<<') { } 4 22 26
Condition100%2/2
$prev_op || ''
l!l
1412
MC/DC100%1/1
$prev_op || ''
$prev_op
Truth table100%2/2
A
A= $prev_op
Aresult
11
00
674 {
675 4 $name = lc $name;
676 }
677 else
678 {
679  
680 {
681 22 my $new = $s->{licensemap}{ex}{$name};
682 22 ▶ $new = $s->{licensemap}{lc}{lc $name} unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 4 18 22
683 22 ▶ $mapped = $name = $new if defined $new;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new 18 4 22
684 }
685
686 22 ▶ unless (defined $mapped)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mapped) { } 18 4 22
687 {
688 # names not in the mapping table get an questionmark!
689 4 $name = ucfirst $origname;
690 4 ▶ $name = '?' . $name unless $name =~ m{^\?};
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\?/ 4 0 4
691 4 ▶ $name = $name . '?' unless $name =~ m{\?$};
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\?$/ 4 0 4
692 4 push @{$s->{diagnostics}}, "unknown name: '$origname'";
693 }
694 else
695 {
696 18 $name = ucfirst $name;
697 }
698 }
699 26 ▶ if ($name eq 'REJECT' and $origname !~ m{REJECT})
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "REJECT" and not $origname =~ /REJECT/) 0 26 26
Condition33%1/3
$name eq "REJECT" and not $origname =~ /REJECT/
!ll&&!rl&&r
2600
MC/DC0%0/2
$name eq "REJECT" and not $origname =~ /REJECT/
$name eq "REJECT" not $origname =~ /REJECT/
Truth table33%1/3
A and not(B)
A= $name eq "REJECT"B= not $origname =~ /REJECT/
ABresult
0X0
100
111
700 {
701 0 $parens = "?$origname?";
702 0 push @{$s->{diagnostics}}, "rejected name: '$origname'";
703 }
704 26 ▶ $name .= "($parens)" if $parens;
Branch: 1 branch
BranchTrueFalseTotal
if $parens 8 18 26
705 26 return $name;
706 }
707  
708 sub format_tokens
709 14 ▶{
Subroutine format_tokens: called CC=6 SCAR=17.9
Pod documented
710 14 my ($s, $aa) = @_;
711 14 my @a;
712 14 for my $a (@$aa)
713 {
714 54 ▶ if (ref $a)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a) { } 8 46 54
715 {
716 8 push @a, '(', $s->format_tokens($a), ')';
717 }
718 else
719 {
720 46 ▶ if ($a =~ m{\w\w} and $a !~ m{^(and|or)$}i)
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /\w\w/ and not $a =~ /^(and|or)$/i) { } 26 20 46
Condition100%3/3
$a =~ /\w\w/ and not $a =~ /^(and|or)$/i
!ll&&!rl&&r
12826
MC/DC100%2/2
$a =~ /\w\w/ and not $a =~ /^(and|or)$/i
$a =~ /\w\w/ not $a =~ /^(and|or)$/i
Truth table100%3/3
A and not(B)
A= $a =~ /\w\w/B= not $a =~ /^(and|or)$/i
ABresult
0X0
100
111
721 {
722 26 ▶ $a = $s->_map_license_name($a, @a?$a[-1]:undef);
Branch: 1 branch
BranchTrueFalseTotal
@a ? : 14 12 26
723 }
724 else
725 {
726 # put whitespace around some operators, so that it looks nicer.
727 # ; only has a trailing whitespace, << has no whitespaces.
728 # KEEP IN SYNC with _group_tokens() if in_word
729 20 $a =~ s{^(;|\||&|or|and)$}{ $1 }i; $a =~ s{^ ; $}{; };
730 }
731 46 push @a, $a;
732 }
733 }
734 14 return join '', @a;
735 }
736  
737 =head2 loadmap_csv
738  
739 $obj->loadmap_csv('license_map.csv', 'as');
740 $obj->loadmap_csv('synopsis.csv', 'lauaas#c');
741  
742 Object method to
743 load (or merge) contents of a CVS table into the object.
744 This uses a trivial csv parser. Field seperator must be ;
745 linebreaks are record seperators, and the first line is ignored,
746 if it starts with '#'.
747 Fields can be surrounded by doublequotes, if a comma may be embedded.
748  
749 The second parameter is a field template, defining what the meaning of the fields is.
750 l Long name (none or once). This is a speaking name of the License: Example "Creative Commons Attribution 1.0"
751 a Alias name (any number). Any other name by which the license is known: Example: "CC-BY 1.0"
752 s Short name (once). The canonical (unique) short license identifiner: Example: "CC-BY-1"
753 u URL (any). Multiple URLs can also be written in one filed, seperated by whitespace.
754 # License classification number (none or once). (1..5)
755 c Comment (none or once)
756 The default template is "as", an alias, followed by the canonical short name.
757 Empty fields are ignored, as well as fields that contian only one '?'. Thus you
758 can use records like
759 "","Name"
760 to pass in a valid name without an alias.
761  
762 See also new() for more details about mappings.
763  
764 =cut
765 sub loadmap_csv
766 3 ▶{
Subroutine loadmap_csv: called CC=26 SCAR=38.4
Pod documented
767 3 my ($self,$file,$template) = @_;
768 3 ▶ $template ||= "as";
Condition100%2/2
$template ||= "as"
l!l
21
MC/DC100%1/1
$template ||= "as"
$template
Truth table100%2/2
A
A= $template
Aresult
11
00
769  
770 3 my @colum_types = split(//, $template);
771 3 my $canon_idx = undef;
772 3 for my $i (0..$#colum_types)
773 {
774 12 ▶ if ($colum_types[$i] eq 's')
Branch: 1 branch
BranchTrueFalseTotal
if ($colum_types[$i] eq "s") 3 9 12
775 {
776 3 ▶ die "multiple canonical short name columns in template '$template'\n" if defined $canon_idx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $canon_idx 0 3 3
777 3 $canon_idx = $i;
778 }
779 }
780 3 ▶ die "no canonical short name columns in template '$template'\n" unless defined $canon_idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $canon_idx 0 3 3
781  
782 3 ▶ open my $in, "<", $file or croak "open($file) failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $file or croak("open($file) failed: $!\n") 3 0 3
783 3 my %opts = ( binary => 1, sep_char => ',', empty_is_undef => 1, eol => $/ );
784 3 my $csv = Text::CSV->new(\%opts);
785 3 my $line_no = 0;
786 3 for (;;)
787 {
788 324 my $row = $csv->getline ($in);
789 324 ▶ last if $csv->eof();
Branch: 1 branch
BranchTrueFalseTotal
if $csv->eof 3 321 324
790 321 ▶ if (!$line_no++)
Branch: 1 branch
BranchTrueFalseTotal
unless ($line_no++) 3 318 321
791 {
792 # be forgiving, if we have errors while parsing the first line.
793 # it may be a comment, or the seperator chacracter may be wrong.
794 3 ▶ if (my @err = $csv->error_diag())
Branch: 1 branch
BranchTrueFalseTotal
if (my(@err) = $csv->error_diag) 3 0 3
795 {
796 3 my $line = $csv->error_input();
797 3 ▶ if ($err[0] == 2025)
Branch: 3 branches
BranchTrueFalseTotal
if ($err[0] == 2025) { } 0 3 3
elsif (defined $line and $line =~ s/^#\s*//) { } 2 1 3
elsif ($err[0]) { } 1 0 1
798 {
799 # no field seperator seen in the line
800 0 ▶ if ($opts{sep_char} eq ',')
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'sep_char'} eq ',') { } 0 0 0
801 {
802 0 $opts{sep_char} = ';';
803 0 POSIX::rewind $in;
804 0 $csv = Text::CSV->new(\%opts);
805 0 $line_no = 0;
806 0 next;
807 }
808 else
809 {
810 0 die "neither comma nor semicolon work as a field seperator.\n";
811 }
812 }
813 ▶ elsif (defined($line) and $line =~ s{^#\s*}{})
Condition66%2/3
defined $line and $line =~ s/^#\s*//
!ll&&!rl&&r
012
MC/DC50%1/2
defined $line and $line =~ s/^#\s*//
defined $line $line =~ s/^#\s*//
Truth table66%2/3
A and B
A= defined $lineB= $line =~ s/^#\s*//
ABresult
0X0
100
111
814 {
815 # can we do something with this header line now?
816 2 next;
817 }
818 elsif ($err[0])
819 {
820 # sometimes we come here, although there is no error.
821 # e.g. after restarting with changed sep_char,
822 1 print Dumper @err, $line;
823 1 next;
824 }
825 else
826 {
827 # "# heading","fields","...."
828 # "\160SPDX Full name","..."
829 # " SPDX Full name","..."
830 0 ▶ next if $row->[0] =~ m{^(\#|\W*SPDX)}; # heading
Branch: 1 branch
BranchTrueFalseTotal
if $row->[0] =~ /^(\#|\W*SPDX)/ 0 0 0
831 }
832 }
833 }
834  
835 318 ▶ next unless defined $row->[$canon_idx]; # a dummy entry?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->[$canon_idx] 178 140 318
836  
837 140 my $alias_count = 0;
838 140 for my $i (0..$#colum_types)
839 {
840 280 ▶ next unless defined $row->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->[$i] 0 280 280
841 280 ▶ next if $row->[$i] =~ m{^[\?\s-]+$};
Branch: 1 branch
BranchTrueFalseTotal
if $row->[$i] =~ /^[\?\s-]+$/ 0 280 280
842 280 ▶ if ($colum_types[$i] eq 'l' or
Branch: 3 branches
BranchTrueFalseTotal
if ($colum_types[$i] eq 'l' or $colum_types[$i] eq 'a') { } 140 140 280
elsif ($colum_types[$i] eq 'u') { } 0 140 140
elsif ($colum_types[$i] eq 'c') { } 0 140 140
Condition66%2/3
$colum_types[$i] eq "l" or $colum_types[$i] eq "a"
l!l&&r!l&&!r
0140140
MC/DC50%1/2
$colum_types[$i] eq "l" or $colum_types[$i] eq "a"
$colum_types[$i] eq "l" $colum_types[$i] eq "a"
Truth table66%2/3
A or B
A= $colum_types[$i] eq "l"B= $colum_types[$i] eq "a"
ABresult
1X1
011
000
843 $colum_types[$i] eq 'a')
844 {
845 140 $self->add_alias($row->[$i], $row->[$canon_idx]);
846 140 $alias_count++;
847 }
848 elsif($colum_types[$i] eq 'u')
849 {
850 0 $self->add_url($row->[$i], $row->[$canon_idx]);
851 }
852 elsif($colum_types[$i] eq 'c')
853 {
854 0 $self->set_compat_class($row->[$i], $row->[$canon_idx]);
855 }
856 }
857 140 ▶ unless ($alias_count)
Branch: 1 branch
BranchTrueFalseTotal
unless ($alias_count) 0 140 140
858 {
859 0 $self->add_alias(undef, $row->[$canon_idx]);
860 }
861 140 ▶ last if $csv->eof();
Branch: 1 branch
BranchTrueFalseTotal
if $csv->eof 0 140 140
862 }
863 3 return $self;
864 }
865  
866 sub _loadmap_csv_old
867 0 ▶{
Subroutine _loadmap_csv_old: not called CC=9 SCAR=45.0
868 0 my ($self,$file) = @_;
869  
870 0 ▶ open IN, "<", $file or croak "open($file) failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open IN, "<", $file or croak("open($file) failed: $!\n") 0 0 0
871 0 my $linecount = 0;
872 0 ▶ while (defined (my $line = <IN>))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline IN)) 0 0 0
873 {
874 0 chomp $line;
875 0 ▶ next if $line =~ m{^#} and !$linecount++;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ and not $linecount++ 0 0 0
Condition0%0/3
$line =~ /^#/ and not $linecount++
!ll&&!rl&&r
000
MC/DC0%0/2
$line =~ /^#/ and not $linecount++
$line =~ /^#/ not $linecount++
Truth table0%0/3
A and not(B)
A= $line =~ /^#/B= not $linecount++
ABresult
0X0
100
111
876 0 ▶ if (($line =~ m{^"([^"]*)",\s*"([^"]*)"}) or
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^"([^"]*)",\s*"([^"]*)"/ or $line =~ /^([^,"]*),\s*([^,]*)/) { } 0 0 0
elsif ($line =~ /^("",\s*)?"([^"]*)"\s*$/ or $line =~ /^(,)?([^,"]*)\s*$/) { } 0 0 0
Condition0%0/3
$line =~ /^"([^"]*)",\s*"([^"]*)"/ or $line =~ /^([^,"]*),\s*([^,]*)/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^"([^"]*)",\s*"([^"]*)"/ or $line =~ /^([^,"]*),\s*([^,]*)/
$line =~ /^"([^"]*)",\s*"([^"]*)"/ $line =~ /^([^,"]*),\s*([^,]*)/
Truth table0%0/3
A or B
A= $line =~ /^"([^"]*)",\s*"([^"]*)"/B= $line =~ /^([^,"]*),\s*([^,]*)/
ABresult
1X1
011
000
877 ($line =~ m{^([^,"]*),\s*([^,]*)}))
878 {
879 # actual mapping from old name to new name
880 0 $self->add_alias($1,$2);
881  
882 }
883 ▶ elsif ($line =~ m{^("",\s*)?"([^"]*)"\s*$} or
Condition0%0/3
$line =~ /^("",\s*)?"([^"]*)"\s*$/ or $line =~ /^(,)?([^,"]*)\s*$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^("",\s*)?"([^"]*)"\s*$/ or $line =~ /^(,)?([^,"]*)\s*$/
$line =~ /^("",\s*)?"([^"]*)"\s*$/ $line =~ /^(,)?([^,"]*)\s*$/
Truth table0%0/3
A or B
A= $line =~ /^("",\s*)?"([^"]*)"\s*$/B= $line =~ /^(,)?([^,"]*)\s*$/
ABresult
1X1
011
000
884 $line =~ m{^(,)?([^,"]*)\s*$})
885 {
886 # simple mentioning of good ones, needs no mapping
887 0 $self->add_alias(undef,$2);
888 }
889 else
890 {
891 0 die "$file:$linecount:\n\t$line\n not my csv syntax.";
892 }
893 }
894 0 return $self;
895 }
896  
897 =head1 AUTHOR
898  
899 Juergen Weigert, C<< <jw at suse.de> >>
900  
901 =head1 BUGS
902  
903 This module defines a different syntax than
904 http://rpmlint.zarb.org/cgi-bin/trac.cgi/browser/trunk/TagsCheck.py
905  
906 Please report any bugs or feature requests to C<bug-rpm-license at rt.cpan.org>, or through
907 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=License-Syntax>. I will be notified, and then you'll
908 automatically be notified of progress on your bug as I make changes.
909  
910  
911  
912  
913 =head1 SUPPORT
914  
915 You can find documentation for this module with the perldoc command.
916  
917 perldoc License::Syntax
918  
919  
920 You can also look for information at:
921  
922 =over 4
923  
924 =item * RT: CPAN's request tracker
925  
926 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=License-Syntax>
927  
928 =item * AnnoCPAN: Annotated CPAN documentation
929  
930 L<http://annocpan.org/dist/License-Syntax>
931  
932 =item * CPAN Ratings
933  
934 L<http://cpanratings.perl.org/d/License-Syntax>
935  
936 =item * Search CPAN
937  
938 L<http://search.cpan.org/dist/License-Syntax/>
939  
940 =back
941  
942  
943 =head1 ACKNOWLEDGEMENTS
944  
945  
946 =head1 COPYRIGHT & LICENSE
947  
948 Copyright 2009 Juergen Weigert.
949  
950 This program is free software; you can redistribute it and/or modify it
951 under the terms of either: the GNU General Public License as published
952 by the Free Software Foundation; or the Artistic License.
953  
954 See http://dev.perl.org/licenses/ for more information.
955  
956  
957 =cut
958  
959 6 1; # End of License::Syntax
↑ Summary license_syntax.pl »