| 1 | package App::Toolforge::MixNMatch; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Error::Pure qw(err); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Getopt::Std; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use IO::Barf qw(barf); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use LWP::Simple qw(get); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Perl6::Slurp qw(slurp); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Toolforge::MixNMatch::Diff; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Toolforge::MixNMatch::Print::Catalog; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Toolforge::MixNMatch::Struct::Catalog; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use Unicode::UTF8 qw(encode_utf8); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # Constants | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | Readonly::Scalar our $URI_BASE => 'https://mix-n-match.toolforge.org/'; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | Readonly::Scalar our $URI_CATALOG_DETAIL => $URI_BASE.'api.php?query=catalog_details&catalog=%s'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | our $VERSION = 0.06; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | # Constructor. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | my ($class, @params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # Create object. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # Object. | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub _catalog_json_file_to_obj { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_catalog_json_file_to_obj: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | my $json_file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | my $json = slurp($json_file); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $struct_hr = decode_json($json); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $obj = Toolforge::MixNMatch::Struct::Catalog::struct2obj($struct_hr->{'data'}); | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $obj; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _command_diff { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_command_diff: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | my ($json_file1, $json_file2, $print_options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (! defined $json_file1 || ! -r $json_file1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $json_file1 and -r $json_file1
MC/DC0%0/2
defined $json_file1 and -r $json_file1
defined $json_file1
-r $json_file1
Truth table0%0/3
A and B
A= defined $json_file1B= -r $json_file1
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return (1, "Doesn't exist JSON file #1 for diff."); | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if (! defined $json_file2 || ! -r $json_file2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $json_file2 and -r $json_file2
MC/DC0%0/2
defined $json_file2 and -r $json_file2
defined $json_file2
-r $json_file2
Truth table0%0/3
A and B
A= defined $json_file2B= -r $json_file2
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | return (1, "Doesn't exist JSON file #2 for diff."); | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $opts_hr = _process_print_options($print_options); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my $obj1 = _catalog_json_file_to_obj($json_file1); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my $obj2 = _catalog_json_file_to_obj($json_file2); | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $diff = Toolforge::MixNMatch::Diff::diff($obj1, $obj2); | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $ret = Toolforge::MixNMatch::Print::Catalog::print($diff, $opts_hr); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | print encode_utf8($ret)."\n"; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | return (0, undef); | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub _command_download { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_command_download: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my ($catalog_id, $output_file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (! defined $catalog_id || $catalog_id !~ m/^\d+$/ms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $catalog_id and $catalog_id =~ /^\d+$/ms
MC/DC0%0/2
defined $catalog_id and $catalog_id =~ /^\d+$/ms
defined $catalog_id
$catalog_id =~ /^\d+$/ms
Truth table0%0/3
A and B
A= defined $catalog_idB= $catalog_id =~ /^\d+$/ms
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | return (1, 'Missing or bad catalog ID.'); | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (! defined $output_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $output_file = $catalog_id.'.json'; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $json = _download_catalog_detail($catalog_id); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | barf($output_file, $json); | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | print "Catalog with '$catalog_id' ID was saved to '$output_file'.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return (0, undef); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _command_print { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_command_print: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my ($json_file_or_catalog_id, $print_options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $json; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | if (-r $json_file_or_catalog_id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $json = slurp($json_file_or_catalog_id); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } elsif ($json_file_or_catalog_id =~ m/^\d+$/ms) { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $json = _download_catalog_detail($json_file_or_catalog_id); | |||||||||||||||||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | return (1, "Doesn't exist JSON file or catalog ID for print."); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $opts_hr = _process_print_options($print_options); | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $struct_hr = decode_json($json); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | my $obj = Toolforge::MixNMatch::Struct::Catalog::struct2obj($struct_hr->{'data'}); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my $ret = Toolforge::MixNMatch::Print::Catalog::print($obj, $opts_hr); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | print encode_utf8($ret)."\n"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | return (0, undef); | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _download_catalog_detail { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_download_catalog_detail: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $catalog_id = shift; | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | my $uri = sprintf $URI_CATALOG_DETAIL, $catalog_id; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | my $json = get($uri); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if (! defined $json) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | err "Cannot download '$uri'."; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return $json; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub _process_print_options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_print_options: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my $print_options = shift; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if (! defined $print_options) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my $opts_hr = {}; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | foreach my $print_option (split m/,/, $print_options) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | $opts_hr->{$print_option} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return $opts_hr; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # Run script. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=8 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | # Process arguments. | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $self->{'_opts'} = { | |||||||||||||||||||||||||||||||||||||||||
| 141 | 'h' => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 142 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | ▶ | if (! getopts('h', $self->{'_opts'}) || $self->{'_opts'}->{'h'} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not &getopts("h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1
not &getopts("h", $self->{"_opts"}) or $self->{"_opts"}{"h"}
MC/DC0%0/3
not &getopts("h", $self->{"_opts"}) or $self->{"_opts"}{"h"} or @ARGV < 1
not &getopts("h", $self->{"_opts"})
$self->{"_opts"}{"h"}
@ARGV < 1
Truth table25%1/4
not(A) or B or C
A= not &getopts("h", $self->{"_opts"})B= $self->{"_opts"}{"h"}C= @ARGV < 1
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | || @ARGV < 1) { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | print STDERR "Usage: $0 [-h] [--version] [command] [command_args ..]\n"; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | print STDERR "\t-h\t\tPrint help.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | print STDERR "\t--version\tPrint version.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | print STDERR "\tcommand\t\tCommand (diff, download, print).\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | print STDERR "\tcommand 'diff' arguments:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | print STDERR "\t\tjson_file1 - JSON file #1\n"; | |||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | print STDERR "\t\tjson_file2 - JSON file #2\n"; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | print STDERR "\t\t[print_options] - Print options (type, count, year_months, users)\n"; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | print STDERR "\tcommand 'download' arguments:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | print STDERR "\t\tcatalog_id - Catalog ID\n"; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | print STDERR "\t\t[output_file] - Output file (default is catalog_id.json)\n"; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | print STDERR "\tcommand 'print' arguments:\n"; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | print STDERR "\t\tjson_file or catalog_id - Catalog ID or JSON file\n"; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | print STDERR "\t\t[print_options] - Print options (type, count, year_months, users)\n"; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $command = shift @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | my @command_args = @ARGV; | |||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | my ($return, $error); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if ($command eq 'diff') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ($return, $error) = _command_diff(@command_args); | |||||||||||||||||||||||||||||||||||||||||
| 168 | } elsif ($command eq 'download') { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ($return, $error) = _command_download(@command_args); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } elsif ($command eq 'print') { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ($return, $error) = _command_print(@command_args); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | print STDERR "Command '$command' doesn't supported.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if ($return == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | print STDERR $error."\n"; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | __END__ | ||||||||||||||||||||||||||||||||||||||||||