test-wrap-argv.pl 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#!/usr/bin/perl
#
# Copyright (C) 2015 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.  If not, see
# <http://www.gnu.org/licenses/>.
#
# This script is intended to be passed a list of .args files, used
# to store command line ARGV for the test suites. It will reformat
# them such that there is at most one '-param value' on each line
# of the file. Parameter values that are longer than 80 chars will
# also be split.
#


foreach my $file (@ARGV) {
    &rewrap($file);
}

sub rewrap {
    my $file = shift;

    # Read the original file
    open FILE, "<", $file or die "cannot read $file: $!";
    my @lines;
    while (<FILE>) {
        # If there is a trailing '\' then kill the new line
        if (/\\$/) {
            chomp;
            $_ =~ s/\\$//;
        }

        push @lines, $_;
    }

    # Skip empty files
    return unless @lines;

    # Kill the last new line in the file
    chomp @lines[$#lines];
    close FILE;

    # Reconstruct the master data by joining all lines
    # and then split again based on the real desired
    # newlines
    @lines = split /\n/, join('', @lines);

    # Now each @lines represents a single command, we
    # can process them
    foreach my $line (@lines) {
62 63 64 65
        &rewrap_line ($line);
    }

}
66

67 68 69
sub rewrap_line {
    my $line = shift;
    my @bits = split / /, join('', $line);
70

71 72 73 74 75
    # @bits contains env vars, then the command line
    # and then the arguments
    my @env;
    my $cmd;
    my @args;
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if ($bits[0] !~ /=/) {
        $cmd = shift @bits;
    }

    foreach my $bit (@bits) {
        # If no command is defined yet, we must still
        # have env vars
        if (!defined $cmd) {
            # Look for leading / to indicate command name
            if ($bit =~ m,^/,) {
                $cmd = $bit;
            } else {
                push @env, $bit;
            }
        } else {
            # If there's a leading '-' then this is a new
            # parameter, otherwise its a value for the prev
            # parameter.
            if ($bit =~ m,^-,) {
                push @args, $bit;
97
            } else {
98
                $args[$#args] .= " " . $bit;
99 100
            }
        }
101
    }
102

103 104 105 106
    # Print env + command first
    print join(" \\\n", @env, $cmd), " \\\n";
    # We might have to split line argument values...
    for (my $i = 0; $i <= $#args; $i++) {
107
        &rewrap_arg($args[$i]);
108 109 110 111 112

        if ($i != $#args) {
            print " \\\n";
        } else {
            print "\n";
113 114 115
        }
    }
}
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

sub rewrap_arg {
    my $arg = shift;

    while (length($arg) > 80) {
        my $split = rindex $arg, ",", 80;
        if ($split == -1) {
            $split = rindex $arg, ":", 80;
        }
        if ($split == -1) {
            $split = rindex $arg, " ", 80;
        }
        if ($split == -1) {
            warn "cannot find nice place to split '$arg' below 80 chars\n";
            $split = 79;
        }
        $split++;

        my $head = substr $arg, 0, $split;
        $arg = substr $arg, $split;

        print $head, "\\\n";
    }
    print $arg;
}