| 1 | package Perinci::CmdLine::Util; | |||||||||||||||||||||||||||||||||
| 2 | ||||||||||||||||||||||||||||||||||
| 3 | 1 | our $DATE = '2017-01-12'; # DATE | ||||||||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.15'; # VERSION | ||||||||||||||||||||||||||||||||
| 5 | ||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 9 | ||||||||||||||||||||||||||||||||||
| 10 | 1 | require Exporter; | ||||||||||||||||||||||||||||||||
| 11 | 1 | our @ISA = qw(Exporter); | ||||||||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw( | ||||||||||||||||||||||||||||||||
| 13 | detect_pericmd_script | |||||||||||||||||||||||||||||||||
| 14 | detect_perinci_cmdline_script | |||||||||||||||||||||||||||||||||
| 15 | ); | |||||||||||||||||||||||||||||||||
| 16 | ||||||||||||||||||||||||||||||||||
| 17 | 1 | our %SPEC; | ||||||||||||||||||||||||||||||||
| 18 | ||||||||||||||||||||||||||||||||||
| 19 | 1 | $SPEC{':package'} = { | ||||||||||||||||||||||||||||||||
| 20 | v => 1.1, | |||||||||||||||||||||||||||||||||
| 21 | summary => 'Utility routines related to Perinci::CmdLine', | |||||||||||||||||||||||||||||||||
| 22 | }; | |||||||||||||||||||||||||||||||||
| 23 | ||||||||||||||||||||||||||||||||||
| 24 | $SPEC{detect_pericmd_script} = { | |||||||||||||||||||||||||||||||||
| 25 | 1 | v => 1.1, | ||||||||||||||||||||||||||||||||
| 26 | summary => 'Detect whether a file is a Perinci::CmdLine-based CLI script', | |||||||||||||||||||||||||||||||||
| 27 | description => <<'_', | |||||||||||||||||||||||||||||||||
| 28 | ||||||||||||||||||||||||||||||||||
| 29 | The criteria are: | |||||||||||||||||||||||||||||||||
| 30 | ||||||||||||||||||||||||||||||||||
| 31 | * the file must exist and readable; | |||||||||||||||||||||||||||||||||
| 32 | ||||||||||||||||||||||||||||||||||
| 33 | * (optional, if `include_noexec` is false) file must have its executable mode | |||||||||||||||||||||||||||||||||
| 34 | bit set; | |||||||||||||||||||||||||||||||||
| 35 | ||||||||||||||||||||||||||||||||||
| 36 | * content must start with a shebang C<#!>; | |||||||||||||||||||||||||||||||||
| 37 | ||||||||||||||||||||||||||||||||||
| 38 | * either: must be perl script (shebang line contains 'perl') and must contain | |||||||||||||||||||||||||||||||||
| 39 | something like `use Perinci::CmdLine`; | |||||||||||||||||||||||||||||||||
| 40 | ||||||||||||||||||||||||||||||||||
| 41 | _ | |||||||||||||||||||||||||||||||||
| 42 | args_rels => { | |||||||||||||||||||||||||||||||||
| 43 | req_one => [qw/filename string/], | |||||||||||||||||||||||||||||||||
| 44 | }, | |||||||||||||||||||||||||||||||||
| 45 | args => { | |||||||||||||||||||||||||||||||||
| 46 | filename => { | |||||||||||||||||||||||||||||||||
| 47 | summary => 'Path to file to be checked', | |||||||||||||||||||||||||||||||||
| 48 | schema => 'str*', | |||||||||||||||||||||||||||||||||
| 49 | description => <<'_', | |||||||||||||||||||||||||||||||||
| 50 | ||||||||||||||||||||||||||||||||||
| 51 | Either `filename` or `string` must be specified. | |||||||||||||||||||||||||||||||||
| 52 | ||||||||||||||||||||||||||||||||||
| 53 | _ | |||||||||||||||||||||||||||||||||
| 54 | pos => 0, | |||||||||||||||||||||||||||||||||
| 55 | }, | |||||||||||||||||||||||||||||||||
| 56 | string => { | |||||||||||||||||||||||||||||||||
| 57 | summary => 'Path to file to be checked', | |||||||||||||||||||||||||||||||||
| 58 | schema => 'buf*', | |||||||||||||||||||||||||||||||||
| 59 | description => <<'_', | |||||||||||||||||||||||||||||||||
| 60 | ||||||||||||||||||||||||||||||||||
| 61 | Either `file` or `string` must be specified. | |||||||||||||||||||||||||||||||||
| 62 | ||||||||||||||||||||||||||||||||||
| 63 | _ | |||||||||||||||||||||||||||||||||
| 64 | }, | |||||||||||||||||||||||||||||||||
| 65 | include_noexec => { | |||||||||||||||||||||||||||||||||
| 66 | summary => 'Include scripts that do not have +x mode bit set', | |||||||||||||||||||||||||||||||||
| 67 | schema => 'bool*', | |||||||||||||||||||||||||||||||||
| 68 | default => 1, | |||||||||||||||||||||||||||||||||
| 69 | }, | |||||||||||||||||||||||||||||||||
| 70 | }, | |||||||||||||||||||||||||||||||||
| 71 | }; | |||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub detect_pericmd_script { | |||||||||||||||||||||||||||||||
|
Subroutine
detect_pericmd_script: not called CC=23 SCAR=63.1
Pod
documented
| ||||||||||||||||||||||||||||||||||
| 73 | 0 | my %args = @_; | ||||||||||||||||||||||||||||||||
| 74 | ||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | (defined($args{filename}) xor defined($args{string})) | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/4
defined $args{"filename"} xor defined $args{"string"}
MC/DC0%0/2
defined $args{"filename"} xor defined $args{"string"}
defined $args{"filename"}
defined $args{"string"}
Truth table0%0/4
A xor B
A= defined $args{"filename"}B= defined $args{"string"}
| ||||||||||||||||||||||||||||||||||
| 76 | or return [400, "Please specify either filename or string"]; | |||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $include_noexec = $args{include_noexec} // 1; | |||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{"include_noexec"} // 1
MC/DC0%0/1
$args{"include_noexec"} // 1
$args{"include_noexec"}
Truth table0%0/2
A
A= $args{"include_noexec"}
| ||||||||||||||||||||||||||||||||||
| 78 | ||||||||||||||||||||||||||||||||||
| 79 | 0 | my $yesno = 0; | ||||||||||||||||||||||||||||||||
| 80 | 0 | my $reason = ""; | ||||||||||||||||||||||||||||||||
| 81 | ||||||||||||||||||||||||||||||||||
| 82 | 0 | my $meta = {}; | ||||||||||||||||||||||||||||||||
| 83 | ||||||||||||||||||||||||||||||||||
| 84 | 0 | my $str = $args{string}; | ||||||||||||||||||||||||||||||||
| 85 | DETECT: | |||||||||||||||||||||||||||||||||
| 86 | { | |||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if (defined $args{filename}) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 88 | 0 | my $fn = $args{filename}; | ||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | unless (-f $fn) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 90 | 0 | $reason = "'$fn' is not a file"; | ||||||||||||||||||||||||||||||||
| 91 | 0 | last; | ||||||||||||||||||||||||||||||||
| 92 | }; | |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (!$include_noexec && !(-x _)) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$include_noexec or -x _
MC/DC0%0/2
$include_noexec or -x _
$include_noexec
-x _
Truth table0%0/3
A or B
A= $include_noexecB= -x _
| ||||||||||||||||||||||||||||||||||
| 94 | 0 | $reason = "'$fn' is not an executable"; | ||||||||||||||||||||||||||||||||
| 95 | 0 | last; | ||||||||||||||||||||||||||||||||
| 96 | } | |||||||||||||||||||||||||||||||||
| 97 | 0 | my $fh; | ||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | unless (open $fh, "<", $fn) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 99 | 0 | $reason = "Can't be read"; | ||||||||||||||||||||||||||||||||
| 100 | 0 | last; | ||||||||||||||||||||||||||||||||
| 101 | } | |||||||||||||||||||||||||||||||||
| 102 | # for efficiency, we read a bit only here | |||||||||||||||||||||||||||||||||
| 103 | 0 | read $fh, $str, 2; | ||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | unless ($str eq '#!') { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 105 | 0 | $reason = "Does not start with a shebang (#!) sequence"; | ||||||||||||||||||||||||||||||||
| 106 | 0 | last; | ||||||||||||||||||||||||||||||||
| 107 | } | |||||||||||||||||||||||||||||||||
| 108 | 0 | my $shebang = <$fh>; | ||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | unless ($shebang =~ /perl/) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 110 | 0 | $reason = "Does not have 'perl' in the shebang line"; | ||||||||||||||||||||||||||||||||
| 111 | 0 | last; | ||||||||||||||||||||||||||||||||
| 112 | } | |||||||||||||||||||||||||||||||||
| 113 | 0 | seek $fh, 0, 0; | ||||||||||||||||||||||||||||||||
| 114 | { | |||||||||||||||||||||||||||||||||
| 115 | 0 | local $/; | ||||||||||||||||||||||||||||||||
| 116 | 0 | $str = <$fh>; | ||||||||||||||||||||||||||||||||
| 117 | } | |||||||||||||||||||||||||||||||||
| 118 | } | |||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | unless ($str =~ /\A#!/) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 120 | 0 | $reason = "Does not start with a shebang (#!) sequence"; | ||||||||||||||||||||||||||||||||
| 121 | 0 | last; | ||||||||||||||||||||||||||||||||
| 122 | } | |||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | unless ($str =~ /\A#!.*perl/) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 124 | 0 | $reason = "Does not have 'perl' in the shebang line"; | ||||||||||||||||||||||||||||||||
| 125 | 0 | last; | ||||||||||||||||||||||||||||||||
| 126 | } | |||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if ($str =~ /^#\s*NO_PERINCI_CMDLINE_SCRIPT\s*$/m) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 128 | 0 | $reason = "Marked with # NO_PERINCI_CMDLINE_SCRIPT directive"; | ||||||||||||||||||||||||||||||||
| 129 | 0 | last; | ||||||||||||||||||||||||||||||||
| 130 | } | |||||||||||||||||||||||||||||||||
| 131 | ||||||||||||||||||||||||||||||||||
| 132 | # NOTE: the presence of \s* pattern after ^ causes massive slowdown of | |||||||||||||||||||||||||||||||||
| 133 | # the regex when we reach many thousands of lines, so we use split() | |||||||||||||||||||||||||||||||||
| 134 | ||||||||||||||||||||||||||||||||||
| 135 | #if ($str =~ /^\s*(use|require)\s+ | |||||||||||||||||||||||||||||||||
| 136 | # (Perinci::CmdLine(|::Any|::Lite|::Classic))/mx) { | |||||||||||||||||||||||||||||||||
| 137 | ||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if ($str =~ /^# PERICMD_INLINE_SCRIPT: (.+)/m) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 139 | 0 | $yesno = 1; | ||||||||||||||||||||||||||||||||
| 140 | 0 | $meta->{'func.module'} = 'Perinci::CmdLine::Inline'; | ||||||||||||||||||||||||||||||||
| 141 | 0 | $meta->{'func.is_inline'} = 1; | ||||||||||||||||||||||||||||||||
| 142 | ||||||||||||||||||||||||||||||||||
| 143 | 0 | my $pericmd_inline_attrs = $1; | ||||||||||||||||||||||||||||||||
| 144 | 0 | my ($pericmd_inline_version) = | ||||||||||||||||||||||||||||||||
| 145 | $str =~ /Perinci::CmdLine::Inline version ([0-9._]+)/; | |||||||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $meta->{'func.notes'} //= []; | |||||||||||||||||||||||||||||||
|
Condition0%0/2
$meta->{"func.notes"} //= []
MC/DC0%0/1
$meta->{"func.notes"} //= []
$meta->{"func.notes"}
Truth table0%0/2
A
A= $meta->{"func.notes"}
| ||||||||||||||||||||||||||||||||||
| 147 | 0 | $meta->{'func.pericmd_inline_version'} = $pericmd_inline_version; | ||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | if (!$pericmd_inline_version) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 149 | 0 | push @{ $meta->{'func.notes'} }, | ||||||||||||||||||||||||||||||||
| 150 | "Can't detect version of Perinci::CmdLine::Inline version"; | |||||||||||||||||||||||||||||||||
| 151 | } | |||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($pericmd_inline_version < 0.17) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 153 | 0 | push @{ $meta->{'func.notes'} }, join( | ||||||||||||||||||||||||||||||||
| 154 | "", | |||||||||||||||||||||||||||||||||
| 155 | "Won't parse # PERICMD_INLINE_SCRIPT attributes ", | |||||||||||||||||||||||||||||||||
| 156 | "because prior to Perinci::CmdLine::Inline 0.17, ", | |||||||||||||||||||||||||||||||||
| 157 | "the attributes are dumped as Perl instead of JSON ", | |||||||||||||||||||||||||||||||||
| 158 | "so it's unsafe to parse", | |||||||||||||||||||||||||||||||||
| 159 | ); | |||||||||||||||||||||||||||||||||
| 160 | } else { | |||||||||||||||||||||||||||||||||
| 161 | 0 | require JSON::MaybeXS; | ||||||||||||||||||||||||||||||||
| 162 | 0 | eval { $pericmd_inline_attrs = | ||||||||||||||||||||||||||||||||
| 163 | JSON::MaybeXS::decode_json($pericmd_inline_attrs) }; | |||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if ($@) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 165 | 0 | push @{ $meta->{'func.notes'} }, | ||||||||||||||||||||||||||||||||
| 166 | "Can't parse # PERICMD_INLINE_SCRIPT line: $@"; | |||||||||||||||||||||||||||||||||
| 167 | } else { | |||||||||||||||||||||||||||||||||
| 168 | 0 | $meta->{'func.pericmd_inline_attrs'} = | ||||||||||||||||||||||||||||||||
| 169 | $pericmd_inline_attrs; | |||||||||||||||||||||||||||||||||
| 170 | } | |||||||||||||||||||||||||||||||||
| 171 | } | |||||||||||||||||||||||||||||||||
| 172 | ||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if ($str =~ /^my \$_pci_metas = (.+);/m) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 174 | 0 | my $pericmd_inline_metas = $1; | ||||||||||||||||||||||||||||||||
| 175 | 0 | eval "\$pericmd_inline_metas = $1"; | ||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | if ($@) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 177 | 0 | push @{ $meta->{'func.notes'} }, | ||||||||||||||||||||||||||||||||
| 178 | "Can't parse 'my \$_pci_metas = ...' line: $@"; | |||||||||||||||||||||||||||||||||
| 179 | } else { | |||||||||||||||||||||||||||||||||
| 180 | 0 | $meta->{'func.pericmd_inline_metas'} = | ||||||||||||||||||||||||||||||||
| 181 | $pericmd_inline_metas; | |||||||||||||||||||||||||||||||||
| 182 | } | |||||||||||||||||||||||||||||||||
| 183 | } | |||||||||||||||||||||||||||||||||
| 184 | ||||||||||||||||||||||||||||||||||
| 185 | 0 | last DETECT; | ||||||||||||||||||||||||||||||||
| 186 | } | |||||||||||||||||||||||||||||||||
| 187 | ||||||||||||||||||||||||||||||||||
| 188 | 0 | for (split /^/, $str) { | ||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | if (/^\s*(use|require)\s+(Perinci::CmdLine(|::Any|::Lite|::Classic))\b/) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 190 | 0 | $yesno = 1; | ||||||||||||||||||||||||||||||||
| 191 | 0 | $meta->{'func.module'} = $2; | ||||||||||||||||||||||||||||||||
| 192 | 0 | last DETECT; | ||||||||||||||||||||||||||||||||
| 193 | } | |||||||||||||||||||||||||||||||||
| 194 | } | |||||||||||||||||||||||||||||||||
| 195 | ||||||||||||||||||||||||||||||||||
| 196 | 0 | $reason = "Can't find any statement requiring Perinci::CmdLine". | ||||||||||||||||||||||||||||||||
| 197 | " module family"; | |||||||||||||||||||||||||||||||||
| 198 | } # DETECT | |||||||||||||||||||||||||||||||||
| 199 | ||||||||||||||||||||||||||||||||||
| 200 | 0 | $meta->{'func.reason'} = $reason; | ||||||||||||||||||||||||||||||||
| 201 | ||||||||||||||||||||||||||||||||||
| 202 | 0 | [200, "OK", $yesno, $meta]; | ||||||||||||||||||||||||||||||||
| 203 | } | |||||||||||||||||||||||||||||||||
| 204 | ||||||||||||||||||||||||||||||||||
| 205 | { | |||||||||||||||||||||||||||||||||
| 206 | 1 | ▶ | no strict 'refs'; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 207 | # old name, deprecated | |||||||||||||||||||||||||||||||||
| 208 | 1 | *detect_perinci_cmdline_script = \&detect_pericmd_script; | ||||||||||||||||||||||||||||||||
| 209 | } | |||||||||||||||||||||||||||||||||
| 210 | ||||||||||||||||||||||||||||||||||
| 211 | 1 | 1; | ||||||||||||||||||||||||||||||||
| 212 | # ABSTRACT: Utility routines related to Perinci::CmdLine | |||||||||||||||||||||||||||||||||
| 213 | ||||||||||||||||||||||||||||||||||
| 214 | __END__ | |||||||||||||||||||||||||||||||||