williamr/sbs_findstr.pl
changeset 15 a5c7bdb47359
parent 7 705136d2022f
equal deleted inserted replaced
14:f57f08cb4e1d 15:a5c7bdb47359
    13 #
    13 #
    14 # Description:
    14 # Description:
    15 # Filter an SBSv2 log to keep only recipes which match a specified RE
    15 # Filter an SBSv2 log to keep only recipes which match a specified RE
    16 
    16 
    17 use strict;
    17 use strict;
       
    18 use Getopt::Long;
       
    19 
       
    20 my $sort_recipes = 0;
       
    21 GetOptions(
       
    22   "s|sort" => \$sort_recipes,   # sort output by <recipe> line
       
    23   );
    18 
    24 
    19 my $expression = shift @ARGV;
    25 my $expression = shift @ARGV;
    20 my $line;
    26 my $line;
    21 my $skipping = 1;
    27 my $skipping = 1;
    22 my $current_target = "";
    28 my $current_target = "";
       
    29 my @buffer = ();
       
    30 my %recipes;
    23 
    31 
    24 @ARGV = map {glob} @ARGV;
    32 @ARGV = map {glob} @ARGV;
    25 
    33 
       
    34 sub save_buffer
       
    35   {
       
    36   return if (scalar @buffer == 0);
       
    37   if ($sort_recipes)
       
    38     {
       
    39     my $recipe = shift @buffer;
       
    40     $recipes{$recipe} = join("",@buffer);
       
    41     }
       
    42   else
       
    43     {
       
    44     print @buffer;
       
    45     }
       
    46   @buffer = ();
       
    47   }
       
    48   
    26 while ($line =<>)
    49 while ($line =<>)
    27   {
    50   {
    28   if (substr($line,0,9) eq "</recipe>")
    51   if (substr($line,0,9) eq "</recipe>")
    29     {
    52     {
    30     print $line if ($skipping == 0);  
    53     push @buffer, $line if ($skipping == 0);  
    31     $skipping = 1;    # set this to 0 to get the "between recipes" stuff
    54     $skipping = 1;    # set this to 0 to get the "between recipes" stuff
    32     next;
    55     next;
    33     }
    56     }
    34   if (substr($line,0,8) eq "<recipe ")
    57   if (substr($line,0,8) eq "<recipe ")
    35     {
    58     {
       
    59     save_buffer();
    36     if ($line =~ /$expression/io)
    60     if ($line =~ /$expression/io)
    37       {
    61       {
    38       $skipping = 0;
    62       $skipping = 0;
    39       $current_target = "";
    63       $current_target = "";
    40       if ($line =~ /(target='[^']+') /)
    64       if ($line =~ /(target='[^']+') /)
    50   next if ($skipping == 1);  
    74   next if ($skipping == 1);  
    51   if (substr($line,0,8) eq "<status ")
    75   if (substr($line,0,8) eq "<status ")
    52     {
    76     {
    53     substr($line,-3) = "$current_target />\n";
    77     substr($line,-3) = "$current_target />\n";
    54     }
    78     }
    55   print $line;
    79   push @buffer, $line;
    56   }
    80   }
       
    81 
       
    82 save_buffer();
       
    83 
       
    84 if ($sort_recipes)
       
    85   {
       
    86   foreach my $recipe (sort keys %recipes)
       
    87     {
       
    88     print $recipe, $recipes{$recipe};
       
    89     }
       
    90   }