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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use Getopt::Long qw(:config posix_default no_ignore_case bundling pass_through);
use List::Util qw(shuffle);
my $usage = <<__EOUSAGE__;
######################################################################################
#
# Required:
#
# --cmds <string> cmds file
#
# --max_batch_size <int> maximum batch size
#
# Optional:
#
# --shuffle shuffle the commands in random order before batching
#
#######################################################################################
__EOUSAGE__
;
my $help_flag;
my $cmds_file;
my $max_batch_size;
my $shuffle_flag = 0;
&GetOptions ( 'h' => \$help_flag,
'cmds=s' => \$cmds_file,
'max_batch_size=i' => \$max_batch_size,
'shuffle' => \$shuffle_flag);
if ($help_flag) {
die $usage;
}
unless ($cmds_file && $max_batch_size) {
die $usage;
}
main: {
my @cmds = `cat $cmds_file`;
chomp @cmds;
if ($shuffle_flag) {
@cmds = shuffle(@cmds);
}
my $num_cmds = scalar(@cmds);
my $cmds_per_batch = int($num_cmds / $max_batch_size);
if ($cmds_per_batch < 1) {
$cmds_per_batch = 1;
}
while (@cmds) {
my @batch;
for (1..$cmds_per_batch) {
my $cmd = shift @cmds;
if ($cmd) {
push (@batch, $cmd);
}
}
my $batched_cmds = join(" && ", @batch);
print "$batched_cmds\n";
}
exit(0);
}
|