blib/lib/File/RdistByRsync.pm untested

filter: Statementstmt 0.0% 0 / 436 Branchbran 0.0% 0 / 138 Conditioncond 0.0% 0 / 116 MC/DCmcdc 0.0% 0 / 74 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 795 CC 70 SCAR 60.8
CC
70
Cov
0%
do_rsync
1056.0
convert_pats_to_globs
240.0
parse_rdist
90.0
CRAP 436.5
1  
2 package File::RdistByRsync;
3  
4 use Parse::RecDescent;
5 use Getopt::Declare;
6 use File::Find;
7 use File::Slurp;
8 require Exporter;
9  
10 @ISA = Exporter;
11 @EXPORT = qw(parse_rdist rdist rsync);
12  
13 our($VERSION);
14 $VERSION = 0.3;
15  
16 use strict;
17  
18 my $rsync_command = 'rsync';
19 my $rsh_command = 'rsh';
20  
21 my $debug = 1;
22  
23 my $parser;
24 my $vars;
25 my $distributes,
26 my $timestamptests;
27  
28 #
29 # This module has to deal with a few problems:
30 #
31 # 1. Parsing the rdist distfile
32 #
33 # 2. Expanding the rdist variables
34 #
35 # 3. Parsing the rdist command line
36 #
37 # 4. Converting except_pat regular expressions into
38 # globs or lists of files
39 #
40 # 5. Invoking rdist
41 #
42 # 6. Handling post-change commands (rdist 'special')
43 #
44  
45  
46 #
47 # Parse an rdist distfile
48 #
49  
50 sub parse_rdist
51 {
52 my ($d, %opts) = @_;
53  
54 define_parser() unless $parser;
55  
56 my $lines = ($d =~ tr/\n/\n/);
57  
58 ($vars, $distributes) = @{$parser->distfile(\$d)};
59  
60 if ($opts{DEFINES}) {
61 for my $k (keys %{$opts{DEFINES}}) {
62 $vars->{$k} = $opts{DEFINES}{$k};
63 }
64 }
65  
66 my $rem = $d;
67 $rem =~ s,[\s\n\r]+,,;
68 if ($rem) {
69 my $remaining = ($d =~ tr/\n/\n/);
70 my $stopped = $lines - $remaining + 2;
71 die "Could not parse distfile, stopped at line $stopped\n";
72 }
73  
74 for my $d (@$distributes) {
75 $d->{HOSTS} = [ expand_list(@{$d->{HOSTS}}) ];
76 $d->{FILES} = [ expand_list(@{$d->{FILES}}) ];
77 $d->{EXCEPT} = [ expand_list(@{$d->{EXCEPT}}) ];
78 $d->{RSYNC_OPTION} = [ expand_list(@{$d->{RSYNC_OPTION}}) ];
79 for my $s (@{$d->{SPECIAL}}) {
80 $s->{FILES} = [ expand_list(@{$s->{FILES}}) ];
81 $s->{COMMAND} = expand_val($s->{COMMAND});
82 }
83 if ($d->{INSTALL}) {
84 if (@{$d->{INSTALL}}) {
85 for my $i (@{$d->{INSTALL}}) {
86 $i->{DESTINATION} = expand_val($i->{DESTINATION});
87 }
88 } else {
89 $d->{INSTALL} = [
90 {
91 DESTINATION => '',
92 FLAGS => {},
93  
94 },
95 ];
96 }
97 } else {
98 $d->{TSFILE} = expand_val($d->{TSFILE});
99 }
100 }
101  
102 if ($opts{TARGETS} && @{$opts{TARGETS}}) {
103 my @found_label;
104 my @t = @{$opts{TARGETS}};
105 my %t;
106 @t{@t} = @t;
107 for my $i (@$distributes) {
108 push(@found_label, $i)
109 if $i->{LABEL} && $t{$i->{LABEL}};
110 }
111 if (@found_label) {
112 $distributes = \@found_label;
113 } else {
114 # now we need to muck through the distributes.. yuck
115 for my $d (@$distributes) {
116 $d->{FILES} = [ grep $t{$_}, map { glob } @{$d->{FILES}} ];
117 }
118 }
119 }
120  
121 use Data::Dumper;
122 #print Dumper($distributes);
123 return @$distributes;
124 }
125  
126 #
127 # Variable expansion in a rdist list context. A variable
128 # can expand to multiple items
129 #
130  
131 sub expand_list
132 {
133 my (@v) = @_;
134 my @r;
135 for my $val (@v) {
136 if ($val =~ m/^\$\{([^{}]+)\}$/ || $val =~ /^\$(\w)$/) {
137 if (defined $vars->{$1}) {
138 push(@r, expand_list(@{$vars->{$1}}));
139 } else {
140 warn "no variable $1 defined!"
141 }
142 next;
143 }
144 push(@r, expand_val($val));
145 }
146 return $r[0] unless wantarray;
147 return @r;
148 }
149  
150  
151 #
152 # Expand any variables inside a string. Variable expansion
153 # can make the string grow, but cannot add elements to
154 # any surrounding list (if there is one)
155 #
156  
157 sub expand_val
158 {
159 my ($val) = @_;
160 #
161 # \ is an escape character in rdist distfiles. It can
162 # escape $ and itself. Presumably, other things too.
163 # I'm not sure what the right behavior is.
164 #
165 my $nv = '';
166 pos($val) = 0;
167 while (pos($val) < length($val)) {
168 (($val =~ /\G([^\\\$]*)/gc) || ($val =~ /\G\\(.)/gc))
169 && do {
170 $nv .= $1;
171 next;
172 };
173 (($val =~ /\G\$(\w)/) || ($val =~ /\G\$\{(.+?)\}/))
174 && do {
175 $nv .= expand_var($1);
176 next;
177 };
178 die "illegal variable expansion in $val";
179 }
180 return $nv;
181 }
182  
183 #
184 # This is used for variables that occur inside strings
185 # rather than all by themselves in a list.
186 #
187  
188 sub expand_var
189 {
190 my ($var) = @_;
191 if (defined $vars->{$var}) {
192 my $r;
193 for my $val (@{$vars->{$var}}) {
194 $r .= expand_val($val);
195 }
196 return $r;
197 } else {
198 warn "no variable $var defined!";
199 if (length($var) eq 1) {
200 return "\$$var";
201 } else {
202 return "\${$var}";
203 }
204 }
205 }
206  
207 my %host;
208  
209 sub rdist
210 {
211 my (@argv) = @_;
212  
213 local(@ARGV) = @argv;
214  
215 our(@hosts);
216 our(%definitions);
217 our(@rsync_options);
218 our(@targets);
219 our($cflag);
220  
221 local(@hosts);
222 local(%definitions);
223 local(@rsync_options);
224 local(@targets);
225 local($cflag);
226  
227 my $rdist_cmdline_grammar = <<'END';
228
229 -f <distfile:if> Use the specified distfile
230  
231 -c <files>... [<user>@]<host>:[<path>] Interpret the rest of the aguments as a mini distfile
232 {
233 our($cflag);
234 $cflag = {
235 HOSTS => [ $user
236 ? "$user\@$host"
237 : $host ],
238 FILES => [ @files ],
239 INSTALL => [
240 {
241 DESTINATION => $path,
242 FLAGS => {},
243 },
244 ],
245 };
246 }
247  
248 -P <rshcmd> Use rshcmd instead of rsh for remote access
249  
250 -d <var>=<value> Set the variable to the value
251 [repeatable]
252 {
253 our(%definitions);
254 $definitions{$var} = $value;
255 }
256  
257 -h Follow symbolic links
258  
259 -i Ignore unresolved links
260  
261 -m <host> Limit which meachines are updated
262 [repeatable]
263 {
264 our(@hosts);
265 push(@hosts, $host);
266 }
267
268 -n Print the commands w/o executing them
269  
270 -q Quite mode.
271  
272 -R Remove extraneous files.
273  
274 -v Verify only
275  
276 -w Whole mode. The file name is appended to the dest.
277  
278 -y Younger mode. Only update older files.
279  
280 -D Debug mode
281  
282 -r <rsync_option> Pass option through to rsync command line
283 [repeatable]
284 {
285 our(@rsync_options);
286 push(@rsync_options,
287 $rsync_option);
288 }
289  
290 --<rsync_option> Pass long option through to rsync command line
291 [repeatable]
292 {
293 our(@rsync_options);
294 push(@rsync_options,
295 "--$rsync_option");
296 }
297  
298 <names>... Labels or files to update
299 {
300
301 our(@targets);
302 push(@targets, @names);
303 }
304  
305 END
306  
307 my $args = new Getopt::Declare($rdist_cmdline_grammar);
308 #Dumper(\$args);
309 die "argparse probelm" unless $args;
310  
311 $debug = $args->{'-D'};
312  
313 my $distfile;
314  
315 if ($args->{'-c'}) {
316 return ($args, $cflag);
317 } elsif ($args->{'-f'} eq '-') {
318 $distfile = join('', <STDIN>);
319 } elsif ($args->{'-f'}) {
320 $distfile = read_file($args->{'-f'});
321 } elsif (-e 'distfile') {
322 $distfile = read_file('distfile');
323 } elsif (-e 'Distfile') {
324 $distfile = read_file('Distfile');
325 } else {
326 die "no distfile found or specified";
327 }
328  
329 my @distributes = parse_rdist($distfile, 'DEFINES' => \%definitions,
330 'TARGETS' => \@targets);
331  
332 return ($args, {
333 HOSTS => \@hosts,
334 DEFINES => \%definitions,
335 RSYNCOPT=> \@rsync_options,
336 TARGETS => \@targets,
337 CFLAG => $cflag,
338 }, @distributes);
339 }
340  
341 sub rsync
342 {
343 my (@argv) = @_;
344  
345 my ($args, $extras, @distributes) = rdist(@argv);
346  
347 expand_globs(@distributes);
348  
349 convert_pats_to_globs(@distributes);
350  
351 my @hosts = @{$extras->{HOSTS}};
352 my %hosts;
353 @hosts{@hosts} = @hosts;
354  
355 for my $d (@distributes) {
356 next unless @{$d->{FILES}};
357 #print Dumper($d);
358 #print "OKF=@{$d->{FILES}}\n";
359 for my $h (@{$d->{HOSTS}}) {
360 next if @hosts && ! $hosts{$h};
361 for my $i (@{$d->{INSTALL}}) {
362 if ($i->{DESTINATION} && $i->{DESTINATION} !~ /:$/) {
363 do_rsync($args, $d, $h, $i, $i->{DESTINATION}, undef, $extras);
364 } else {
365 do_rsync($args, $d, $h, $i, "$i->{DESTINATION}/", qr|^/|, $extras);
366 do_rsync($args, $d, $h, $i, "$i->{DESTINATION}", qr|^[^/]|, $extras);
367 }
368 }
369 }
370 }
371  
372 use Data::Dumper;
373 #print Dumper($vars);
374 #print Dumper(@distributes);
375 }
376  
377 sub do_rsync
378 {
379 my ($args, $d, $host, $i, $dest, $filefilter, $extras) = @_;
380  
381 my @ra;
382 my @unlink;
383  
384 return unless @{$d->{FILES}};
385 #print "OF=@{$d->{FILES}}\n";
386  
387 my (@files) = $filefilter
388 ? grep(/$filefilter/, @{$d->{FILES}})
389 : @{$d->{FILES}};
390 #print "FILES=@files\n";
391
392 return unless @files;
393  
394 if ($d->{EXCEPT}) {
395 my $tmpfile = get_tmpfile();
396  
397 my @e;
398  
399 my @excepts = @{$d->{NEW_EXCEPT}};
400 while (my($type, $p) = splice(@excepts, 0, 2)) {
401 if ($type eq 'EXCEPT') {
402 push(@e,"- $p");
403 } elsif ($type eq 'EXCEPT_GLOB') {
404 push(@e,"- $p");
405 } elsif ($type eq 'RSYNC_INCLUDE') {
406 push(@e,"+ $p");
407 } elsif ($type eq 'RSYNC_EXCLUDE') {
408 push(@e,"- $p");
409 } else {
410 die "unknown except type: '$type' (with '$p')";
411 }
412 }
413 if (@e > 200) {
414 write_file($tmpfile, join("\n", @e) . "\n");
415 push(@ra, "--exclude-from=$tmpfile");
416 push(@unlink, $tmpfile);
417 } else {
418 for my $e (@e) {
419 push(@ra, "--exclude=$e");
420 }
421 }
422 }
423  
424 my @notify = @{$d->{NOTIFY}};
425 my @special = @{$d->{SPECIAL}};
426  
427 push(@ra, "--perms");
428 push(@ra, "--owner");
429 push(@ra, "--group");
430 push(@ra, "--devices");
431 push(@ra, "--times");
432  
433 if ($args->{'-R'} || $i->{FLAGS}{R}) {
434 push(@ra, "--delete");
435 push(@ra, "--delete-after");
436 }
437  
438 # push(@ra, "--force");
439 push(@ra, "--hard-links"); # slow, but the alternative is bad
440 push(@ra, "--recursive");
441  
442 if ($args->{'-w'} || $i->{FLAGS}{w} || $dest eq '/' || $dest eq '') {
443 if (@files == 1 && $files[0] !~ /[\]\[\*\?\{\}]/) {
444 $dest .= "/" unless $dest =~ m,/$,;
445 my $f = $files[0];
446 $f =~ s,^/,,;
447 $dest .= $f;
448 } else {
449 push(@ra, "--relative");
450 }
451 }
452  
453 if ($args->{'-v'}) {
454 push(@ra, "--dry-run");
455 push(@ra, "--verbose")
456 unless $args->{'-q'};
457 }
458  
459 if ($args->{'-P'}) {
460 push(@ra, "--rsh=$args->{-P}");
461 }
462  
463 if ($args->{'-h'} || $i->{FLAGS}{h}) {
464 push(@ra, "--copy-links");
465 } else {
466 push(@ra, "--links");
467 }
468  
469 if ($args->{'-y'} || $i->{FLAGS}{y}) {
470 push(@ra, "--update");
471 }
472  
473 if ($args->{'-q'} && ! @notify && ! @special) {
474 push(@ra, "--quiet");
475 } else {
476 push(@ra, "--verbose");
477 }
478  
479 push(@ra, @{$d->{RSYNC_OPTION}});
480  
481 push(@ra, @{$extras->{RSYNCOPT}});
482  
483 push(@ra, @files);
484  
485 if ($dest =~ /:/) {
486 push(@ra, $dest);
487 } else {
488 push(@ra, "$host:$dest");
489 }
490  
491 my $output = get_tmpfile();
492 push(@unlink, $output);
493  
494 if ($args->{'-n'}) {
495 print "+ $rsync_command @ra\n";
496 } elsif (@notify || @special) {
497 for my $ra (@ra) {
498 $ra =~ s/'/'"'"'/g;
499 $ra = "'$ra'" unless $ra =~ /^\w+$/;
500 }
501 my $tee = "| tee $output";
502  
503 $tee = "> $output"
504 if $args->{'-q'};
505  
506 system("set -x; ($rsync_command @ra 2>&1) | tee $output");
507  
508 my %special;
509 for my $s (@special) {
510 for my $f (@{$s->{FILES}}) {
511 $special{$f} = []
512 unless $special{$f};
513 push(@{$special{$f}}, $s->{COMMAND});
514 }
515 }
516  
517 my $o = read_file($output);
518 for(;;) {
519 $o =~ /\G(.*)[\n\r]*/gcm || last;
520 my $line = $1;
521 if ($line =~ /^\S+$/) {
522 next unless $special{$line};
523 for my $c (@{$special{$line}}) {
524 my $rsh = $args->{'-P'} || $rsh_command;
525  
526 #
527 # Proper quoting through three layers of shell
528 # is a right proper pain.
529 #
530  
531 my $cc = $c;
532 my $ccsr = q{'"'"'"'"'"'"'"'"'};
533 my %lr = (
534 "'" => $ccsr,
535 '"' => q{"'"'"},
536 );
537 $cc =~ s/'/$ccsr/g;
538 $line =~ s/(['"])/$lr{$1}/g;
539 my $x = '';
540 $x = '-x' if $debug;
541 my $cmd = qq{$rsh $host 'sh -a $x -c '"'"'FILE="$line"; $cc'"'"};
542 print "+ $cmd\n"
543 if $debug;
544 next if $args->{'-v'};
545 system($cmd);
546 }
547 }
548 }
549 } else {
550 print "+ $rsync_command @ra\n";
551 system($rsync_command, @ra);
552 }
553  
554 for my $u (@unlink) {
555 unlink($u)
556 unless $debug;
557 }
558 }
559  
560 my $tfcount;
561 sub get_tmpfile
562 {
563 $tfcount++;
564 return "$ENV{HOME}/#rdist2rsync.$$.$tfcount";
565 }
566  
567 #
568 # 'except_pat' doesn't translate to rdist very well because except_pat
569 # is specified with regular expressions but rsync uses globbing for
570 # excludes.
571 #
572 sub convert_pats_to_globs
573 {
574 my (@distribute) = @_;
575 for my $d (@distribute) {
576 my @new;
577 my @excepts = @{$d->{EXCEPT}};
578 my $has_regex;
579 my @re;
580 while (my($type, $p) = splice(@excepts, 0, 2)) {
581 if ($type ne 'EXCEPT_PAT') {
582 push(@new, $type, $p);
583 next;
584 }
585  
586 my $glob_okay = 1;
587 my $glob = '';
588 my $plevel = 0;
589 my $last_okay = 1;
590 my $lp;
591
592 pos($p) = 0;
593 if ($p =~ /\G\^/gc) {
594 # beginning is anchored
595 } elsif ($p =~ /\G\//gc) {
596 # beginning may be a fullpath
597 $glob = '/';
598 } else {
599 $glob = '*';
600 }
601 while (pos($p) < length($p)) {
602 $p =~ /\G\\(.)/gc && do {
603 $glob .= '?';
604 next;
605 };
606 $p =~ /\G\.\*/gc && do {
607 $glob .= '*';
608 next;
609 };
610 $p =~ /\G\.\+/gc && do {
611 $glob .= '?*';
612 next;
613 };
614 $p =~ /\G\./gc && do {
615 $glob .= '?';
616 next;
617 };
618 $p =~ /\G\((?!\?)/gc && do {
619 $plevel++;
620 $glob .= '{'; # }
621 next;
622 };
623 $p =~ /\G\(\?/gc && do {
624 warn "regex not standard (see re_format(7)): $p";
625 $glob_okay = 0;
626 next;
627 };
628 $p =~ /\G\)/gc && do {
629 if ($plevel) {
630 $plevel--;
631 # {
632 $glob .= "}";
633 } else {
634 warn "unbalanced parens in regex: $p";
635 $glob .= ')';
636 }
637 next;
638 };
639 $p =~ /\G\|/gc && do {
640 $glob .= ',' if $plevel;
641 next;
642 };
643 $p =~ /\G,/gc && do {
644 $glob .= "[,]" if $plevel;
645 next;
646 };
647 $p =~ /\G[+*?]/gc && do {
648 $glob_okay = 0;
649 next;
650 };
651 $p =~ /\G(?<=.)\^/gc && do {
652 $glob_okay = 0;
653 next;
654 };
655 $p =~ /\G\$(?=.)/gc && do {
656 $glob_okay = 0;
657 next;
658 };
659 $p =~ /\G\$\z/gc && do {
660 last;
661 };
662 $p =~ /\G\{\d+(?:,\d+)?\}/gc && do {
663 $glob_okay = 0;
664 next;
665 };
666 $p =~ /\G\{/gc && do {
667 # }
668 $glob .= "[{]"; # }
669 next;
670 };
671 $p =~ /\G(\[\^.+?\])/gc && do {
672  
673 $glob .= $1;
674 next;
675 };
676 $p =~ /\G(\[(.+?)\])/gc && do {
677 $glob .= $1;
678 next;
679 };
680 $p =~ /\G([^]\$\\*+?{}().]+)/gc && do {
681 $glob .= $1;
682 next;
683 };
684 my $before = substr($p, 0, pos($p));
685 my $after = substr($p, pos($p), length($p)-pos($p));
686 die "could not parse regex: '$before<PROBLEM HERE>$after'";
687 } continue {
688 if ($last_okay != $glob_okay) {
689 my $before = substr($p, 0, $lp);
690 my $during = substr($p, $lp, pos($p)-$lp);
691 my $after = substr($p, pos($p), length($p)-pos($p));
692 print STDERR "TRANSITION TO NOT-OKAY: '$before<OKAY>$during<NOT_OKAY>$after\n"
693 if $debug;
694 $last_okay = $glob_okay;
695 }
696 $lp = pos($p);
697 }
698
699 if ($glob_okay) {
700 if ($p =~ /\$\z/) {
701 # ending is anchored
702 } else {
703 $glob .= '*';
704 }
705 $glob =~ s/\*\*+/*/g;
706 push(@new, 'EXCEPT_GLOB', $glob);
707 } else {
708 push(@new, 'EXCEPT_REGEX', $p);
709 push(@re, qr/$p/);
710 $has_regex = 1;
711 }
712 }
713  
714 my (@matches) = ([]) x scalar(@re);
715  
716 my $matchfunc = sub {
717 for (my $i = 0; $i < @re; $i++) {
718 next unless $File::Find::name =~ /$re[$i]/;
719 push(@{$matches[$i]}, $File::Find::name);
720 }
721 # it would be nice to short-circuit the find if we
722 # can, but that's a bit of work. I'll leave that
723 # as an exercise for the reader...
724 };
725  
726 if (@re) {
727 find($matchfunc, @{$d->{FILES}});
728 my @nn;
729 while (my($type, $p) = splice(@new, 0, 2)) {
730 if ($type eq 'EXCEPT_REGEX') {
731 my $match = shift(@matches);
732 for my $erf (@$match) {
733 push(@nn, 'EXCEPT', $erf);
734 print "replacing except_pat '$p' with '$erf'\n"
735 if $debug;
736 }
737 } else {
738 push(@nn, $type, $p);
739 }
740 }
741 @new = @nn;
742 }
743  
744 $d->{NEW_EXCEPT} = \@new;
745 }
746 }
747  
748 #
749 # We need to expand all the FILES lists that may include
750 # globs into actual lists of files.
751 #
752 sub expand_globs
753 {
754 my (@distribute) = @_;
755  
756 for my $d (@distribute) {
757 $d->{FILES} = [ map { glob } @{$d->{FILES}} ];
758 for my $s (@{$d->{SPECIAL}}) {
759 $s->{FILES} = [ map { glob } @{$s->{FILES}} ];
760 }
761 }
762 }
763  
764 sub define_parser
765 {
766 $::RD_HINT = 1
767 if $debug;
768  
769 my $grammar = <<'END';
770  
771 {
772 # startup actions...
773 my %vars;
774 my @distributes;
775 }
776  
777 distfile: component(s)
778 { [\%vars, \@distributes ] }
779  
780 component: definition | distribution | timestamptest | comment
781  
782 comment: /#[^\n]*/
783 { '' }
784  
785 definition: identifier '=' <commit> name_list
786 {
787 $vars{$item{identifier}} = $item{name_list};
788 0;
789 }
790  
791 variable: '${' identifier '}'
792 { join('', @item[1..$#item]) }
793  
794 identifier: /[a-z]\w*/i
795  
796 name_list: name
797 { [ $item{name} ] }
798 name_list: '(' <commit> name(s?) ')'
799 { $item{name} }
800  
801 name: comment
802 | variable
803 | hostname
804 | filename
805 | email
806  
807 email: user '@' hostname
808 { $item{user} . '@' . $item{hostname}; }
809  
810 filename: m{[^;()\s]+}
811  
812 hostname: m{[a-z](?:[\.\w]*\w)?}
813  
814 user: m{[\w.]+}
815  
816 timestamptest: label(?) source_list '::' <commit> time_stamp_file command_list
817 {
818 my @l = @{$item{label}};
819 push(@distributes, {
820 LABEL => $l[0],
821 FILES => $item{source_list},
822 TSFILE => $item{time_stamp_file},
823 %{$item{command_list}},
824 });
825 }
826  
827 distribution: label(?) source_list '->' destination_list command_list
828 {
829 my @l = @{$item{label}};
830 push(@distributes, {
831 LABEL => $l[0],
832 FILES => $item{source_list},
833 HOSTS => $item{destination_list},
834 %{$item{command_list}},
835 });
836 }
837  
838 label: identifier ':'
839 { $item{identifier} }
840  
841 source_list: source
842 { [ $item{source} ] }
843 source_list: '(' <commit> source(s?) ')'
844 { $item{source} }
845  
846 source: comment
847 | variable
848 | filename
849  
850 destination_list: destination
851 { [ $item{destination} ] }
852 destination_list: '(' <commit> destination(s?) ')'
853 { $item{destination} }
854  
855 destination: comment
856 | variable
857 | hostname
858  
859 command_list: command(s?)
860 {
861 $return = {
862 INSTALL => [],
863 NOTIFY => [],
864 EXCEPT => [],
865 SPECIAL => [],
866 RSYNC_OPTION => [],
867 };
868 for my $c (@{$item{command}}) {
869 next unless $c;
870 my $cmd = shift @$c;
871 if ($cmd eq 'INSTALL') {
872 my $dest = pop(@$c);
873 my %flags;
874 for my $os (@$c) {
875 for my $of (split('', $os)) {
876 $flags{$of} = 1;
877 }
878 }
879 push(@{$return->{$cmd}}, {
880 DESTINATION => $dest,
881 FLAGS => \%flags,
882 });
883 } elsif ($cmd eq 'SPECIAL') {
884 my $command = pop(@$c);
885 push(@{$return->{$cmd}}, {
886 COMMAND => $command,
887 FILES => $c,
888 });
889 } elsif ($cmd eq 'EXCEPT'
890 || $cmd eq 'EXCEPT_PAT'
891 || $cmd eq 'RSYNC_INCLUDE'
892 || $cmd eq 'RSYNC_EXCLUDE')
893 {
894 for my $arg (@$c) {
895 push(@{$return->{EXCEPT}}, $cmd, $arg);
896 }
897 } elsif ($cmd eq 'NOTIFY') {
898 push(@{$return->{$cmd}}, @$c);
899 } elsif ($cmd eq 'RSYNC_OPTION') {
900 push(@{$return->{$cmd}}, @$c);
901 } else {
902 die;
903 }
904 }
905 }
906  
907 command: comment
908 { 0 }
909 command: 'notify' <commit> name_list ';'
910 { [ 'NOTIFY', @{$item{name_list}} ] }
911 command: /except(?!_)/ <commit> name_list ';'
912 { [ 'EXCEPT', @{$item{name_list}} ] }
913 command: 'except_pat' <commit> pattern_list ';'
914 { [ 'EXCEPT_PAT', @{$item{pattern_list}} ] }
915 command: 'special' <commit> name_list string ';'
916 { [ 'SPECIAL', @{$item{name_list}}, $item{string} ] }
917 command: 'install' <commit> option(s?) opt_dest_name(?) ';'
918 { [ 'INSTALL', @{$item{option}}, join('',@{$item{opt_dest_name}}) ] }
919 command: 'rsync_include' <commit> pattern_list ';'
920 { [ 'RSYNC_INCLUDE', @{$item{pattern_list}} ] }
921 command: 'rsync_exclude' <commit> pattern_list ';'
922 { [ 'RSYNC_EXCLUDE', @{$item{pattern_list}} ] }
923 command: 'rsync_option' <commit> rsync_option_list ';'
924 { [ 'RSYNC_OPTION', @{$item{pattern_list}} ] }
925  
926 option: '-' <commit> option_flags
927  
928 option_flags: m{[Rhivwyb]+}
929  
930 opt_dest_name: filename
931 | hostname ':' <commit> filename
932 | user '@' <commit> hostname ':' filename
933  
934 rsync_option_list: rsync_option
935 { [ $item{rsync_option} ] }
936 rsync_option_list: '(' rsync_option(s) ')'
937 { $item{rsync_option} }
938  
939 rsync_option: string
940  
941 rsync_option: /[^;"()]/
942  
943 pattern_list: pattern
944 { [ $item{pattern} ] }
945 pattern_list: '(' pattern(s) ')'
946 { $item{pattern} }
947  
948 pattern: comment
949 | filename
950 | variable
951  
952 string: '"' <skip: ''> string_middle <skip: ''> '"'
953 { $item{string_middle} }
954  
955 string_middle: /(\\.|[^\\"])*/s
956  
957 time_stamp_file: m{\S+}
958 END
959
960 if ($debug) {
961 $parser = Precompile Parse::RecDescent ($grammar, 'ParseRdistParser');
962 require ParseRdistParser;
963 $parser = ParseRdistParser->new();
964 } else {
965 $parser = new Parse::RecDescent $grammar
966 }
967 }
968  
969 1;