| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | sub main() { |
|---|
| 7 | my $args = scalar(@ARGV); |
|---|
| 8 | if ($args<2) { |
|---|
| 9 | die ("Usage: xml_2_depends.pl XSLTPROC STYLESHEET XSLTPROCARGS\n". |
|---|
| 10 | " or xml_2_depends.pl CHECK RESULTOFFIRSTCALL"); |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | my $first = shift @ARGV; |
|---|
| 14 | |
|---|
| 15 | if ($first eq'CHECK') { |
|---|
| 16 | # second pass: |
|---|
| 17 | # * check for invalid dependencies |
|---|
| 18 | # * if found -> delete depending .xml |
|---|
| 19 | if ($args>2) { die "Too many arguments"; } |
|---|
| 20 | my $depends = shift(@ARGV); |
|---|
| 21 | my $deleted_xml = 0; |
|---|
| 22 | open(DEPENDS,'<'.$depends) || die "can't read '$depends' (Reason: $!)"; |
|---|
| 23 | while (my $line=<DEPENDS>) { |
|---|
| 24 | chomp($line); |
|---|
| 25 | |
|---|
| 26 | my @depends = split(/[ :]+/,$line); |
|---|
| 27 | my $count = scalar(@depends); |
|---|
| 28 | |
|---|
| 29 | my $xml = $depends[0]; |
|---|
| 30 | my $invalid = undef; |
|---|
| 31 | for (my $d=1; ($d<$count) and (not defined $invalid); ++$d) { |
|---|
| 32 | my $hlp = $depends[$d]; |
|---|
| 33 | if (not -f $hlp) { |
|---|
| 34 | $invalid = $hlp; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | if (defined $invalid) { |
|---|
| 38 | print STDERR "Invalid dependency '$invalid' found for '$xml' -> deleting .xml\n"; |
|---|
| 39 | unlink($xml) || die "Failed to unlink '$xml' (Reason: $!)"; |
|---|
| 40 | $deleted_xml++; |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | close(DEPENDS); |
|---|
| 44 | |
|---|
| 45 | if ($deleted_xml>0) { |
|---|
| 46 | print STDERR "Deleted $deleted_xml .xml-files that had invalid dependencies\n"; |
|---|
| 47 | print STDERR "Removing $depends\n"; |
|---|
| 48 | unlink($depends) || die "Failed to unlink '$depends' (Reason: $!)"; |
|---|
| 49 | die "Can't continue - please rerun (maybe caused by renaming a helpfile)"; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | else { |
|---|
| 53 | # first pass: generate dependencies from .xml |
|---|
| 54 | if ($args<3) { die "Missing arguments"; } |
|---|
| 55 | |
|---|
| 56 | my $XSLTPROC = $first; |
|---|
| 57 | my $STYLESHEET = shift @ARGV; |
|---|
| 58 | my $XSLTPROCARGS = join(' ',@ARGV); |
|---|
| 59 | |
|---|
| 60 | while (my $line=<STDIN>) { |
|---|
| 61 | chomp($line); |
|---|
| 62 | my @xml = split(/ /,$line); |
|---|
| 63 | foreach my $xml (@xml) { |
|---|
| 64 | my $cmd = $XSLTPROC.' --stringparam xml '.$xml.' '.$XSLTPROCARGS.' '.$STYLESHEET.' '.$xml; |
|---|
| 65 | system($cmd)==0 || die "Failed to execute '$cmd' (Reason: $?)"; |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | main(); |
|---|