| 1 | #!/usr/local/bin/perl -w | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | # -*- mode: cperl; coding: latin-2 -*- | ||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||||
| 5 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2006,2007,2008,2009,2015 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||
| 8 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||||
| 9 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||
| 11 | # Mail: slaven@rezic.de | ||||||||||||||||||||||||||||||||||||||||||||
| 12 | # WWW: http://www.rezic.de/eserte/ | ||||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 4 | $VERSION = '1.13'; | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use Kwalify; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use Getopt::Long; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | my $schema_file; | |||||||||||||||||||||||||||||||||||||||||||
| 23 | my $parse_mod; | ||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | my $silent; | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | my $show_version; | |||||||||||||||||||||||||||||||||||||||||||
| 26 | GetOptions("f=s" => \$schema_file, | ||||||||||||||||||||||||||||||||||||||||||||
| 27 | "m|module=s" => \$parse_mod, | ||||||||||||||||||||||||||||||||||||||||||||
| 28 | "s|silent" => \$silent, | ||||||||||||||||||||||||||||||||||||||||||||
| 29 | "v|version" => \$show_version, | ||||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | "h|help" => sub { print usage(); exit 0 }, | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 31 | ) | ||||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | or die usage(); | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | ▶ | if ($show_version) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | version(); | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | if (!defined $schema_file) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | die usage("-f option is mandatory"); | |||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | my $data_file = shift @ARGV; | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | if (!defined $data_file) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | die usage("datafile is mandatory"); | |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | my(@schema) = read_file($schema_file); | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (@schema != 1) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | print "<$schema_file> does not contain exactly one schema, cannot handle this."; | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $schema = $schema[0]; | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | my(@data) = read_file($data_file); | |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $errors = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $document_index = 0; | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | for my $data (@data) { | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $document_label = $data_file . '#' . $document_index; | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | eval { Kwalify::validate($schema, $data) }; | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | print "$document_label: INVALID\n$@\n"; | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $errors++; | |||||||||||||||||||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if (!$silent) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | print "$document_label: valid.\n"; | |||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $document_index++; | |||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | exit $errors; | |||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | sub read_file { | ||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | my $file = shift; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_file: called CC=22 SCAR=50.1
| |||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | my @try_order; | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | if (defined $parse_mod) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | @try_order = ($parse_mod); | |||||||||||||||||||||||||||||||||||||||||||
| 80 | } elsif ($file =~ m{\.json$}i) { | ||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | @try_order = ('JSON::XS', 'JSON', 'YAML::Syck', 'YAML', 'YAML::XS'); | |||||||||||||||||||||||||||||||||||||||||||
| 82 | } else { # yaml or don't know | ||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | @try_order = ('YAML::Syck', 'YAML', 'YAML::XS', 'JSON::XS', 'JSON'); | |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | my @errors; | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | for my $try (@try_order) { | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 5 | ▶ | if ($try eq 'YAML::Syck' && eval { require YAML::Syck; 1 }) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition66%2/3
$try eq "YAML::Syck" and eval {
do {
require YAML::Syck;
1
}
}
MC/DC0%0/2
$try eq "YAML::Syck" and eval {
do {
require YAML::Syck;
1
}
}
$try eq "YAML::Syck"
eval {
do {
require YAML::Syck;
1
}
}
Truth table66%2/3
A and B
A= $try eq "YAML::Syck"B= eval {
do {
require YAML::Syck;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my @data = eval { YAML::Syck::LoadFile($file) }; | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return @data if !$@; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | push @errors, $@; | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | } elsif ($try eq 'YAML::XS' && eval { require YAML::XS; 1 }) { | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$try eq "YAML::XS" and eval {
do {
require YAML::XS;
1
}
}
MC/DC0%0/2
$try eq "YAML::XS" and eval {
do {
require YAML::XS;
1
}
}
$try eq "YAML::XS"
eval {
do {
require YAML::XS;
1
}
}
Truth table66%2/3
A and B
A= $try eq "YAML::XS"B= eval {
do {
require YAML::XS;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my @data = eval { YAML::XS::LoadFile($file) }; | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | return @data if !$@; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | push @errors, $@; | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | } elsif ($try eq 'YAML' && eval { require YAML; 1 }) { | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$try eq "YAML" and eval {
do {
require YAML;
1
}
}
MC/DC0%0/2
$try eq "YAML" and eval {
do {
require YAML;
1
}
}
$try eq "YAML"
eval {
do {
require YAML;
1
}
}
Truth table66%2/3
A and B
A= $try eq "YAML"B= eval {
do {
require YAML;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my @data = eval { YAML::LoadFile($file) }; | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return @data if !$@; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | push @errors, $@; | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | } elsif ($try eq 'JSON::XS' && eval { require JSON::XS; 1 }) { | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$try eq "JSON::XS" and eval {
do {
require JSON::XS;
1
}
}
MC/DC0%0/2
$try eq "JSON::XS" and eval {
do {
require JSON::XS;
1
}
}
$try eq "JSON::XS"
eval {
do {
require JSON::XS;
1
}
}
Truth table66%2/3
A and B
A= $try eq "JSON::XS"B= eval {
do {
require JSON::XS;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my @data = eval { JSON::XS::decode_json(slurp_file($file)) }; | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | return @data if !$@; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | push @errors, $@; | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | } elsif ($try eq 'JSON' && eval { require JSON; 1 }) { | ||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$try eq "JSON" and eval {
do {
require JSON;
1
}
}
MC/DC0%0/2
$try eq "JSON" and eval {
do {
require JSON;
1
}
}
$try eq "JSON"
eval {
do {
require JSON;
1
}
}
Truth table66%2/3
A and B
A= $try eq "JSON"B= eval {
do {
require JSON;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | my $data = eval { | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $json = slurp_file($file); | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (defined &JSON::from_json) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | JSON::from_json($json, {utf8 => 1}); | |||||||||||||||||||||||||||||||||||||||||||
| 109 | } else { # old style | ||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | JSON::jsonToObj($json); | |||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 112 | }; | ||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return ($data) if $data && !$@; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data and not $@
MC/DC0%0/2
$data and not $@
$data
not $@
Truth table0%0/3
A and not(B)
A= $dataB= not $@
| |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | push @errors, $@; | |||||||||||||||||||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | push @errors, "Unsupported module $try"; | |||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | if (!@errors) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | die "Cannot parse <$file>. Try to install a YAML and/or JSON parsing module first.\n"; | |||||||||||||||||||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | die "Cannot parse <$file>. Cumulated errors:\n" . join("\n", @errors) . "\n"; | |||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | sub slurp_file { | ||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my $file = shift; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slurp_file: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | open FH, "< $file" | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 129 | or die "Can't open <$file>: $!"; | ||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | local $/ = undef; | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my $json = <FH>; | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | close FH; | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $json; | |||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||
| 136 | sub usage { | ||||||||||||||||||||||||||||||||||||||||||||
| 137 | 3 | ▶ | my($msg) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 3 | ▶ | if (defined $msg) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | $msg .= "\n"; | |||||||||||||||||||||||||||||||||||||||||||
| 140 | } else { | ||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | $msg = ""; | |||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 143 | 3 | <<EOF; | |||||||||||||||||||||||||||||||||||||||||||
| 144 | ${msg}usage: $0 [-v] [-s] [-m parse-mod] -f schema.yml data.yml | ||||||||||||||||||||||||||||||||||||||||||||
| 145 | $0 -f schema.json data.json | ||||||||||||||||||||||||||||||||||||||||||||
| 146 | EOF | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | sub version { | ||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | print <<EOF; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 151 | pkwalify $VERSION | ||||||||||||||||||||||||||||||||||||||||||||
| 152 | Kwalify.pm $Kwalify::VERSION | ||||||||||||||||||||||||||||||||||||||||||||
| 153 | perl $] | ||||||||||||||||||||||||||||||||||||||||||||
| 154 | EOF | ||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||