-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlittlejohn.go
183 lines (154 loc) · 4.09 KB
/
littlejohn.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"sync"
cli "github.com/jawher/mow.cli"
)
func openCSV(filename string) (*csv.Reader, []string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, nil, err
}
reader := csv.NewReader(file)
raw_argnames, err := reader.Read()
if err != nil {
return nil, nil, err
}
// for the arguments, strip any extraneous spaces
argnames := make([]string, len(raw_argnames))
for idx, value := range raw_argnames {
argnames[idx] = strings.TrimSpace(value)
}
return reader, argnames, nil
}
func run_command(command string, fullarglist []string, output_channel chan string) {
cmd := &exec.Cmd{
Path: command,
Args: fullarglist,
Stderr: os.Stderr,
}
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Printf("Failed to get output pipe for %v: %v", fullarglist, err)
return
}
defer stdout.Close()
err = cmd.Start()
if err != nil {
log.Printf("Failed to run %v: %v", fullarglist, err)
return
}
buf := bufio.NewReader(stdout)
count := 0
for {
line, _, err := buf.ReadLine()
if err != nil {
if err != io.EOF {
log.Printf("Error reading process %v: %v", fullarglist, err)
}
break
}
result := fullarglist[0]
for _, arg := range fullarglist[1:] {
result = fmt.Sprintf("%v, %v", result, arg)
}
result = fmt.Sprintf("%v, %d, %v", result, count, string(line))
output_channel <- result
count += 1
}
err = cmd.Wait()
if err != nil {
log.Printf("Failed to wait %v: %v", fullarglist, err)
return
}
}
func cmd_main(command string, parallelism int, dryrun bool, csvfile string, fixedargs []string, outputfile string) error {
argreader, argnames, err := openCSV(csvfile)
if err != nil {
return fmt.Errorf("failed to open CSV file: %w", err)
}
var output io.Writer = os.Stdout
if outputfile != "" {
f, err := os.Create(outputfile)
if err != nil {
return fmt.Errorf("Failed to open output file: %w", err)
}
defer f.Close()
output = f
}
output_channel := make(chan string, 1)
go func() {
for {
res := <-output_channel
output.Write([]byte(res + "\n"))
}
}()
distribution := make(chan []string, parallelism)
wg := new(sync.WaitGroup)
for i := 0; i < parallelism; i += 1 {
wg.Add(1)
go func() {
for {
args := <-distribution
if len(args) == 0 {
break
}
if len(args) != len(argnames) {
log.Printf("Skipping run as %v and %v don't match", args, argnames)
continue
}
fullarglist := make([]string, 1, (len(args)*2)+len(fixedargs)+1)
fullarglist[0] = command
fullarglist = append(fullarglist, fixedargs...)
for argindex := 0; argindex < len(args); argindex += 1 {
fullarglist = append(fullarglist, argnames[argindex])
fullarglist = append(fullarglist, args[argindex])
}
if dryrun {
fmt.Printf("%s\n", strings.Join(fullarglist, " "))
continue
}
fmt.Printf("%s\n", strings.Join(fullarglist, " "))
run_command(command, fullarglist, output_channel)
}
wg.Done()
}()
}
// push data into the distribution channel - should be
// limited in pace based on how fast the workers drain the channel
for {
args, err := argreader.Read()
if err != nil {
break
}
distribution <- args
}
// kill the workers by sending a null for each one
for i := 0; i < parallelism; i += 1 {
distribution <- []string{}
}
wg.Wait()
return nil
}
func main() {
fmt.Printf("Assembling the merry tasks...\n")
app := cli.App("littlejohn", "Run the same command with argument management")
app.Spec = "[-jno] -c PROG -- [ARGS...]"
parallelism := app.IntOpt("j", 4, "Number of parallel copies to run")
dryrun := app.BoolOpt("n dry-run", false, "Just print out commands rather than run")
csv := app.StringOpt("c csv", "", "File with CSV arguments for program")
command := app.StringArg("PROG", "", "Program to run")
output := app.StringOpt("o outputfile", "", "File to write output to")
args := app.StringsArg("ARGS", nil, "Fixed arguments to all child programs")
app.Action = func() {
cmd_main(*command, *parallelism, *dryrun, *csv, *args, *output)
}
app.Run(os.Args)
}