blib/lib/File/Create/Layout.pm

filter: Statementstmt 88.6% 140 / 158 Branchbran 71.1% 74 / 104 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 80.0% 8 / 10 Podpod 100% 3 / 3 totaltotal 79.1% 228 / 288 CC 59 SCAR 35.5
CC
59
Cov
80%
create_files_using_layout
49.7
_parse_layout
33.1
check_layout
6.0
CRAP 34.8
↑ Summary
1 package File::Create::Layout;
2  
3 3 our $DATE = '2019-04-16'; # DATE
4 3 our $VERSION = '0.060'; # VERSION
5  
6 3 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 require Exporter;
14 3 our @ISA = qw(Exporter);
15 3 our @EXPORT_OK = qw(
16 create_files_using_layout
17 );
18  
19 3 our %SPEC;
20  
21 3 my %arg_layout = (
22 layout => {
23 summary => 'Layout',
24 description => <<'_',
25  
26 See the module documentation for the format/specification of layout.
27  
28 _
29 schema => 'str*',
30 req => 1,
31 pos => 0,
32 },
33 );
34  
35 7 ▶sub _decode_json {
Subroutine _decode_json: called CC=1 SCAR=0.0
36 7 state $json = do {
37 1 require JSON::MaybeXS;
38 1 JSON::MaybeXS->new->allow_nonref;
39 };
40 7 $json->decode(shift);
41 }
42  
43 30 ▶sub _parse_layout {
Subroutine _parse_layout: called CC=33 SCAR=35.0
44 30 my $layout = shift;
45  
46 30 my @indents;
47 my @res;
48 30 my $linum = 0;
49 30 my $prev_is_dir = 0;
50 30 for my $line (split /^/, $layout) {
51 73 chomp $line;
52 73 my $orig_line = $line;
53  
54 73 $linum++;
55 73 ▶ next if $line =~ /\A\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*\z/ 3 70 73
56 70 ▶ next if $line =~ /\A\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\s*#/ 2 68 70
57  
58 68 $line =~ s/\A(\s*)//;
59 68 my $cur_indent = $1;
60 68 ▶ die "(layout):$linum: Tabs are not allowed: $orig_line"
Branch: 1 branch
BranchTrueFalseTotal
if $cur_indent =~ /\t/ 0 68 68
61 if $cur_indent =~ /\t/;
62  
63 68 $cur_indent = length($cur_indent);
64  
65 68 ▶ if (!@indents) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@indents) 28 40 68
66 28 push @indents, $cur_indent;
67 }
68  
69 68 ▶ if ($cur_indent > $indents[-1]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cur_indent > $indents[-1]) { } 13 55 68
elsif ($cur_indent < $indents[-1]) { } 8 47 55
70 # indent is deeper than previous spec-line, we require that the
71 # previous spec-line is directory
72 13 ▶ die "(layout):$linum: More indented than previous spec-line, but ".
Branch: 1 branch
BranchTrueFalseTotal
unless $prev_is_dir 1 12 13
73 "previous spec-line is not a directory: $orig_line"
74 unless $prev_is_dir;
75 12 push @indents, $cur_indent;
76 } elsif ($cur_indent < $indents[-1]) {
77 # indent is shallower than previous spec-line, find previous level
78 8 my $found = 0;
79 8 for my $i (reverse 0..$#indents) {
80 17 ▶ if ($cur_indent == $indents[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cur_indent == $indents[$i]) 6 11 17
81 6 $found++;
82 6 splice @indents, $i+1;
83 6 last;
84 }
85 }
86 8 ▶ die "(layout):$linum: Invalid indent, must return to one of ".
Branch: 1 branch
BranchTrueFalseTotal
unless $found 2 6 8
87 "previous levels' indent: $orig_line" unless $found;
88 }
89  
90 # parse filename
91 65 my $name;
92 65 ▶ if ($line =~ /\A"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A"/) { } 5 60 65
93 5 ▶ $line =~ s#\A(".*?(?<!\\)(?:\\\\)*")##
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/\A(".*?(?<!\\)(?:\\\\)*")// or die "(layout):${linum}: Invalid quoted filename: $orig_line" 3 2 5
94 or die "(layout):$linum: Invalid quoted filename: $orig_line";
95 3 $name = $1;
96 3 eval { $name = _decode_json($name) };
97 3 ▶ die "(layout):$linum: Invalid JSON string in filename: $@: $1"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
98 if $@;
99 } else {
100 60 $line =~ s!\A([^\s\(/]*)!!;
101 60 $name = $1;
102 }
103 63 ▶ die "(layout):$linum: Filename cannot be empty: $orig_line"
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 1 62 63
104 unless length($name);
105 62 ▶ die "(layout):$linum: Filename cannot contain slashes: $orig_line"
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ m[/] 1 61 62
106 if $name =~ m!/!;
107 61 ▶ die "(layout):$linum: Filename cannot be . or ..: $orig_line"
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A\.\.?\z/ 2 59 61
108 if $name =~ m!\A\.\.?\z!;
109  
110 59 my $is_dir;
111 59 ▶ if ($line =~ s!\A/!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s[\A/][]) { } 15 44 59
112 15 $is_dir = 1;
113 } else {
114 44 $is_dir = 0;
115 }
116  
117 59 my ($orig_perm, $perm, $user, $group);
118 59 ▶ if ($line =~ /\A\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\(/) 6 53 59
119 6 ▶ $line =~ s/\A\((?:([^,]*),([^,]*),)?([0-7]{3,4})\)//
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/\A\((?:([^,]*),([^,]*),)?([0-7]{3,4})\)// or die "(layout):${linum}: Invalid syntax in permission/owner: $orig_line" 2 4 6
120 or die "(layout):$linum: Invalid syntax in permission/owner: $orig_line";
121 2 $user = $1;
122 2 $group = $2;
123 2 $orig_perm = $3;
124 2 $perm = oct($3);
125 }
126  
127 55 my $sym_target;
128 55 ▶ if ($line =~ s/\s+->\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\s+->\s*//) 5 50 55
129 5 ▶ die "(layout):$linum: Symlink cannot be a directory: $orig_line"
Branch: 1 branch
BranchTrueFalseTotal
if $is_dir 1 4 5
130 if $is_dir;
131 # parse symlink target
132 4 ▶ if ($line =~ /\A"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A"/) { } 3 1 4
133 3 ▶ $line =~ s#\A(".*?(?<!\\)(?:\\\\)*")##
Branch: 1 branch
BranchTrueFalseTotal
$line =~ s/\A(".*?(?<!\\)(?:\\\\)*")// or die "(layout):${linum}: Invalid quoted symlink target: $orig_line" 2 1 3
134 or die "(layout):$linum: Invalid quoted symlink target: $orig_line";
135 2 $sym_target = $1;
136 2 eval { $sym_target = _decode_json($sym_target) };
137 2 ▶ die "(layout):$linum: Invalid JSON string in symlink target: $@: $1"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
138 if $@;
139 } else {
140 1 $line =~ s!\A([^\s]*)!!;
141 1 $sym_target = $1;
142 }
143 3 ▶ die "(layout):$linum: Symlink target cannot be empty: $orig_line"
Branch: 1 branch
BranchTrueFalseTotal
unless length $sym_target 1 2 3
144 unless length($sym_target);
145 }
146  
147 52 my $extras;
148 52 ▶ if ($line =~ s/\s+(\S.*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/\s+(\S.*)//) 2 50 52
149 2 $extras = $1;
150 2 eval { $extras = _decode_json("{$extras}") };
151 2 ▶ die "(layout):$linum: Invalid unquoted JSON hash in extras: $@: $extras"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
152 if $@;
153 2 ▶ if (defined $extras->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extras->{"content"}) 2 0 2
154 2 ▶ die "(layout):$linum: Directory must not have 'content': $@: $orig_line"
Branch: 1 branch
BranchTrueFalseTotal
if $is_dir 1 1 2
155 if $is_dir;
156 }
157 }
158  
159 push @res, {
160 name => $name,
161 is_dir => $is_dir,
162 is_symlink => defined($sym_target) ? 1:0,
163 (symlink_target => $sym_target) x !!(defined $sym_target),
164 level => $#indents >= 0 ? $#indents : 0,
165 _linum => $linum,
166 perm => $perm,
167 perm_octal => $orig_perm,
168 user => $user,
169 group => $group,
170 51 ▶ (content => $extras->{content}) x !!(defined $extras->{content}),
Branch: 2 branches
BranchTrueFalseTotal
defined $sym_target ? : 2 49 51
$#indents >= 0 ? : 51 0 51
171 };
172  
173 51 $prev_is_dir = $is_dir;
174 }
175  
176 13 \@res;
177 }
178  
179 $SPEC{create_files_using_layout} = {
180 3 v => 1.1,
181 summary => 'Create files/directories according to a layout',
182 description => <<'_',
183  
184 This routine can be used to quickly create several files/directories according
185 to a layout which you specify. The layout uses a few simple rules and common
186 conventions usually found in Linux/Unix environment.
187  
188 You can use this routine e.g. in a test script.
189  
190 _
191 args => {
192 %arg_layout,
193 prefix => {
194 summary => 'Root directory to create the files/directories in',
195 description => <<'_',
196  
197 Directory must already exist.
198  
199 If unspecified, will simply create starting from current directory.
200  
201 _
202 schema => 'str*',
203 },
204 },
205 };
206 1 ▶sub create_files_using_layout {
Subroutine create_files_using_layout: called CC=25 SCAR=39.1
Pod documented
207 1 require File::chown;
208  
209 1 my %args = @_;
210  
211 1 my $parse_res;
212 1 eval { $parse_res = _parse_layout($args{layout}) };
213 1 ▶ return [400, "Syntax error in layout: $@"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
214  
215 1 my $prefix = $args{prefix};
216 1 ▶ local $CWD = $prefix // $CWD;
Condition33%1/3
$prefix // $File::Create::Layout::CWD
l!l&&r!l&&!r
100
MC/DC0%0/2
$prefix // $File::Create::Layout::CWD
$prefix $File::Create::Layout::CWD
Truth table33%1/3
A // B
A= $prefixB= $File::Create::Layout::CWD
ABresult
1X1
011
000
217 1 ▶ $prefix //= ".";
Condition50%1/2
$prefix //= "."
l!l
10
MC/DC0%0/1
$prefix //= "."
$prefix
Truth table50%1/2
A
A= $prefix
Aresult
11
00
218  
219 1 my $prev_level;
220 my @dirs;
221 1 for my $e (@$parse_res) {
222 33 my $p = $prefix . join("", map {"/$_"} @dirs);
223  
224 33 ▶ if (defined $prev_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_level) 32 1 33
225 32 ▶ if ($e->{level} > $prev_level) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->{'level'} > $prev_level) { } 8 24 32
elsif ($e->{'level'} < $prev_level) { } 5 19 24
226 8 log_trace("chdir %s ...", $dirs[-1]);
227 8 eval { $CWD = $dirs[-1] };
228 8 ▶ return [500, "Can't chdir to $p/$e->{name}: $! (cwd=$CWD)"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
229 } elsif ($e->{level} < $prev_level) {
230 5 my $dir = join("/", (("..") x ($prev_level - $e->{level})));
231 5 splice @dirs, $e->{level};
232 5 $p = $prefix . join("", map {"/$_"} @dirs);
233 5 log_trace("chdir back %s ...", $dir);
234 5 eval { $CWD = $dir };
235 5 ▶ return [500, "Can't chdir back to $dir: $! (cwd=$CWD)"]
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
236 if $@;
237 }
238 }
239  
240 log_trace("Creating %s/%s%s ...",
241 33 ▶ $p, $e->{name}, $e->{is_dir} ? "/":"");
Branch: 1 branch
BranchTrueFalseTotal
$e->{'is_dir'} ? : 8 25 33
242 33 ▶ if ($e->{is_dir}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->{'is_dir'}) { } 8 25 33
elsif ($e->{'is_symlink'}) { } 0 25 25
243 8 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
do { if (defined $e->{"perm"}) { mkdir $e->{"name"}, $e->{"perm"}; } else { mkdir $e->{"name"}; } } or return [500, qq[Can't create directory $p/$e->{"name"}: $!]] 8 0 8
244 8 ▶ if (defined $e->{perm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e->{'perm'}) { } 0 8 8
245 0 mkdir($e->{name}, $e->{perm});
246 } else {
247 8 mkdir($e->{name});
248 }
249 } or return [500, "Can't create directory $p/$e->{name}: $!"];
250 8 $dirs[$e->{level}] = $e->{name};
251 } elsif ($e->{is_symlink}) {
252 symlink($e->{symlink_target}, $e->{name})
253 0 ▶ or return [500, "Can't create symlink $p/$e->{name} -> ".
Branch: 1 branch
BranchTrueFalseTotal
symlink $e->{"symlink_target"}, $e->{"name"} or return [500, qq[Can't create symlink $p/$e->{"name"} -> ] . qq[$e->{"symlink_target"}: $!]] 0 0 0
254 "$e->{symlink_target}: $!"];
255 } else {
256 open my($fh), ">", $e->{name}
257 25 ▶ or return [500, "Can't create file $p/$e->{name}: $!"];
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $e->{"name"} or return [500, qq[Can't create file $p/$e->{"name"}: $!]] 25 0 25
258 25 ▶ if (defined $e->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e->{"content"}) 0 25 25
259 print $fh $e->{content}
260 0 ▶ or return [500, "Can't write content to file ".
Branch: 1 branch
BranchTrueFalseTotal
print $fh $e->{"content"} or return [500, q[Can't write content to file ] . qq[$p/$e->{"name"}: $!]] 0 0 0
261 "$p/$e->{name}: $!"];
262 }
263 25 ▶ if (defined $e->{perm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e->{"perm"}) 0 25 25
264 chmod($e->{perm}, $e->{name})
265 0 ▶ or return [500, "Can't chmod file $p/$e->{name}: $!"];
Branch: 1 branch
BranchTrueFalseTotal
chmod $e->{"perm"}, $e->{"name"} or return [500, qq[Can't chmod file $p/$e->{"name"}: $!]] 0 0 0
266 }
267 }
268  
269 33 ▶ if (defined($e->{user}) || defined($e->{group})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e->{"user"} or defined $e->{"group"}) 0 33 33
Condition33%1/3
defined $e->{"user"} or defined $e->{"group"}
l!l&&r!l&&!r
0033
MC/DC0%0/2
defined $e->{"user"} or defined $e->{"group"}
defined $e->{"user"} defined $e->{"group"}
Truth table33%1/3
A or B
A= defined $e->{"user"}B= defined $e->{"group"}
ABresult
1X1
011
000
270 0 my %opts;
271 0 ▶ $opts{deref} = 0 if $e->{is_symlink};
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"is_symlink"} 0 0 0
272 File::chown::chown(\%opts, $e->{user}, $e->{group}, $e->{name})
273 0 ▶ or return [500, "Can't chown file $p/$e->{name}: $!"];
Branch: 1 branch
BranchTrueFalseTotal
File::chown::chown(\%opts, $e->{"user"}, $e->{"group"}, $e->{"name"}) or return [500, qq[Can't chown file $p/$e->{"name"}: $!]] 0 0 0
274 }
275  
276 33 $prev_level = $e->{level};
277 }
278  
279 1 [200, "OK"];
280 }
281  
282 $SPEC{check_layout} = {
283 3 v => 1.1,
284 summary => 'Check whether layout has syntax errors',
285 args => {
286 %arg_layout,
287 },
288 };
289 0 ▶sub check_layout {
Subroutine check_layout: not called CC=2 SCAR=17.9
Pod documented
290 0 my %args = @_;
291  
292 0 eval { _parse_layout($args{layout}) };
293 0 my $err = $@;
294 0 ▶ [200, "OK", $err ? 0:1, {'func.error' => $err}];
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 0 0
295 }
296  
297 $SPEC{parse_layout} = {
298 3 v => 1.1,
299 summary => 'Parse layout string into a data structure '.
300 'suitable for processing',
301 args => {
302 %arg_layout,
303 },
304 };
305 0 ▶sub parse_layout {
Subroutine parse_layout: not called CC=2 SCAR=17.9
Pod documented
306 0 my %args = @_;
307  
308 0 my $res;
309 0 eval { $res = _parse_layout($args{layout}) };
310 0 ▶ return [400, "Layout has error(s): $@"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
311 0 [200, "OK", $res];
312 }
313  
314 3 1;
315 # ABSTRACT: Quickly create files/directories according to a layout
316  
317 __END__
↑ Summary