File Coverage

blib/script/pkwalify
Criterion Covered Total %
statement 67 104 64.4
branch 26 46 56.5
condition 10 18 55.5
subroutine 6 9 66.6
pod n/a
total 109 177 61.5


line stmt bran cond sub pod time code
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 12     12   68160 use strict;
  12         22  
  12         750  
16 12     12   67 use vars qw($VERSION);
  12         21  
  12         979  
17 12         2316134 $VERSION = '1.13';
18              
19 12     12   6767 use Kwalify;
  12         42  
  12         844  
20 12     12   9838 use Getopt::Long;
  12         176936  
  12         109  
21              
22 12         85 my $schema_file;
23             my $parse_mod;
24 12         0 my $silent;
25 12         0 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     0   0 "h|help" => sub { print usage(); exit 0 },
  0         0  
31             )
32 12 100       205 or die usage();
33            
34 11 50       15740 if ($show_version) {
35 0         0 version();
36 0         0 exit;
37             }
38              
39 11 100       48 if (!defined $schema_file) {
40 1         7 die usage("-f option is mandatory");
41             }
42              
43 10         33 my $data_file = shift @ARGV;
44 10 100       116 if (!defined $data_file) {
45 1         6 die usage("datafile is mandatory");
46             }
47              
48 9         91 my(@schema) = read_file($schema_file);
49 8 100       46 if (@schema != 1) {
50 1         9 print "<$schema_file> does not contain exactly one schema, cannot handle this.";
51 1         0 exit 1;
52             }
53 7         22 my $schema = $schema[0];
54 7         27 my(@data) = read_file($data_file);
55              
56 7         22 my $errors = 0;
57 7         18 my $document_index = 0;
58 7         24 for my $data (@data) {
59 10         39 my $document_label = $data_file . '#' . $document_index;
60 10         26 eval { Kwalify::validate($schema, $data) };
  10         53  
61 10 100       74 if ($@) {
62 4         28 print "$document_label: INVALID\n$@\n";
63 4         8 $errors++;
64             } else {
65 6 100       23 if (!$silent) {
66 1         8 print "$document_label: valid.\n";
67             }
68             }
69 10         32 $document_index++;
70             }
71              
72 7         0 exit $errors;
73              
74             sub read_file {
75 16     16   48 my $file = shift;
76              
77 16         38 my @try_order;
78 16 50       172 if (defined $parse_mod) {
    50          
79 0         0 @try_order = ($parse_mod);
80             } elsif ($file =~ m{\.json$}i) {
81 0         0 @try_order = ('JSON::XS', 'JSON', 'YAML::Syck', 'YAML', 'YAML::XS');
82             } else { # yaml or don't know
83 16         93 @try_order = ('YAML::Syck', 'YAML', 'YAML::XS', 'JSON::XS', 'JSON');
84             }
85              
86 16         30 my @errors;
87 16         45 for my $try (@try_order) {
88 50 50 66     317 if ($try eq 'YAML::Syck' && eval { require YAML::Syck; 1 }) {
  16 100 66     2852  
  0 50 66     0  
    50 66        
    50 66        
89 0         0 my @data = eval { YAML::Syck::LoadFile($file) };
  0         0  
90 0 0       0 return @data if !$@;
91 0         0 push @errors, $@;
92 16         5554 } elsif ($try eq 'YAML::XS' && eval { require YAML::XS; 1 }) {
  16         39210  
93 16         37 my @data = eval { YAML::XS::LoadFile($file) };
  16         67  
94 16 100       5984 return @data if !$@;
95 1         6 push @errors, $@;
96 16         3455 } elsif ($try eq 'YAML' && eval { require YAML; 1 }) {
  0         0  
97 0         0 my @data = eval { YAML::LoadFile($file) };
  0         0  
98 0 0       0 return @data if !$@;
99 0         0 push @errors, $@;
100 1         146 } elsif ($try eq 'JSON::XS' && eval { require JSON::XS; 1 }) {
  0         0  
101 0         0 my @data = eval { JSON::XS::decode_json(slurp_file($file)) };
  0         0  
102 0 0       0 return @data if !$@;
103 0         0 push @errors, $@;
104 1         119 } elsif ($try eq 'JSON' && eval { require JSON; 1 }) {
  0         0  
105 0         0 my $data = eval {
106 0         0 my $json = slurp_file($file);
107 0 0       0 if (defined &JSON::from_json) {
108 0         0 JSON::from_json($json, {utf8 => 1});
109             } else { # old style
110 0         0 JSON::jsonToObj($json);
111             }
112             };
113 0 0 0     0 return ($data) if $data && !$@;
114 0         0 push @errors, $@;
115             } else {
116 34         176 push @errors, "Unsupported module $try";
117             }
118             }
119 1 50       4 if (!@errors) {
120 0         0 die "Cannot parse <$file>. Try to install a YAML and/or JSON parsing module first.\n";
121             } else {
122 1         0 die "Cannot parse <$file>. Cumulated errors:\n" . join("\n", @errors) . "\n";
123             }
124             }
125              
126             sub slurp_file {
127 0     0   0 my $file = shift;
128 0 0       0 open FH, "< $file"
129             or die "Can't open <$file>: $!";
130 0         0 local $/ = undef;
131 0         0 my $json = ;
132 0         0 close FH;
133 0         0 $json;
134             }
135              
136             sub usage {
137 3     3   1373 my($msg) = @_;
138 3 100       13 if (defined $msg) {
139 2         7 $msg .= "\n";
140             } else {
141 1         3 $msg = "";
142             }
143 3           <
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     0     print <
151             pkwalify $VERSION
152             Kwalify.pm $Kwalify::VERSION
153             perl $]
154             EOF
155             }
156              
157             __END__