-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_c_compiler.go
296 lines (248 loc) · 8.09 KB
/
go_c_compiler.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
// StudentDir represents the hw directory
type StudentDir struct {
Path string // master dir where hws are located
CppDir string // where the student cpp files are stored
BinDir string // where the binaries produced from the cpps are stored
HWs HWs
Next chan *HW // once built send over channel to "running" goroutine
Seq chan struct{}
Close chan struct{} // closed after no more hws are left to build and run
Source string // config var for changing behavior of dir walk
}
// HW has fields for accessing HW for building and running
type HW struct {
Name string // name of the binary
CppFile string // path for the CppFile relative to StudentDir.Path
BuildFile string // path for the BuildFile relative to StudentDir.Path
Build *exec.Cmd
Run *exec.Cmd
}
// HWs is a slice of HW
type HWs []*HW
// New inits the StudentDir
func New(inDir, bin, cppDir, source string, seq bool) *StudentDir {
sd := StudentDir{Path: inDir, BinDir: bin, CppDir: cppDir, Source: source}
sd.HWs = make([]*HW, 0)
sd.Next = make(chan *HW)
if seq {
sd.Seq = make(chan struct{}, 1)
sd.Seq <- struct{}{}
}
sd.Close = make(chan struct{})
return &sd
}
// WriteToHWChan writes HW chan
func (sd StudentDir) WriteToHWChan(hw *HW) {
sd.Next <- hw
}
// Exec finds, runs, and builds
func (sd *StudentDir) Exec(compiler, startStudent, q string, times int, args ...string) {
var err error
go func() { // build producer
err = filepath.Walk(sd.Path, func(currPath string, info os.FileInfo, err error) error {
switch sd.Source {
case "nyuclasses":
if currPath == sd.BinDir || currPath == sd.CppDir {
return filepath.SkipDir
}
foundDir := ""
if startStudent != "" && foundDir == "" { // begin here
dirs, err := ioutil.ReadDir(sd.Path)
if err != nil {
log.Fatalln(err)
}
for _, dir := range dirs {
if strings.Contains(dir.Name(), startStudent) {
foundDir = path.Join(sd.Path, dir.Name()) // find the dir name we need
return nil
}
}
log.Fatalln("NetID Not Found")
} else if startStudent != "" && currPath != foundDir {
if info, _ := os.Stat(currPath); currPath != sd.Path && info.IsDir() { // if currPath is not the root and is a dir
return filepath.SkipDir
}
return nil // a non dir file, continue searching (if we have a file in the root dir, we don't want to skip)
} else if startStudent != "" && currPath == foundDir { // located the student dir we need
startStudent = ""
} // executes the below once found
default:
if currPath == sd.BinDir || currPath == sd.CppDir {
return filepath.SkipDir
}
// general matcher
if matchedFile := strings.Contains(info.Name(), startStudent); startStudent != "" && !matchedFile {
return nil
} else if matchedFile {
startStudent = ""
}
}
if q != "" { // if question specified
if strings.Contains(info.Name(), q) && strings.Contains(info.Name(), "_") && path.Ext(info.Name()) == ".cpp" {
hw := &HW{CppFile: currPath, Name: info.Name()}
sd.HWs = append(sd.HWs, hw)
hw.BuildIt(compiler, *sd, args...)
sd.WriteToHWChan(hw)
return filepath.SkipDir
}
return nil // not the right file
} else if path.Ext(info.Name()) == ".cpp" {
hw := &HW{CppFile: currPath, Name: info.Name()}
sd.HWs = append(sd.HWs, hw)
hw.BuildIt(compiler, *sd, args...)
sd.WriteToHWChan(hw)
return filepath.SkipDir
}
return nil
})
if err != nil {
log.Fatalln(err)
}
close(sd.Close)
}()
for true { // build consumer
select {
case hw := <-sd.Next: // consume builds
fmt.Println("\n" + (*hw).Name)
for i := 0; i < times; i++ {
hw.RunIt(*sd)
}
if sd.Seq != nil {
sd.Seq <- struct{}{}
}
fmt.Println()
case <-sd.Close: // nothing left to consume
fmt.Println("\nDONE")
return
}
}
}
// CopyCpp copies the cpp file to the cppdir
func CopyCpp(hw *HW, cppDir string) {
src, err := os.Open(hw.CppFile)
if err != nil {
log.Fatalln(err)
}
defer src.Close()
dst, err := os.Create(path.Join(cppDir, hw.Name))
if err != nil {
log.Fatalln(err)
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
log.Fatalln(err)
}
}
// BuildIt builds the cpp file located in the hw directory
func (hw *HW) BuildIt(compiler string, sd StudentDir, args ...string) {
hw.BuildFile = path.Join(sd.BinDir, strings.TrimSuffix(hw.Name, ".cpp"))
allArgs := []string{hw.CppFile, "-o", hw.BuildFile}
allArgs = append(allArgs, args...)
if sd.Seq != nil {
select {
case <-sd.Seq:
}
}
hw.Build = exec.Command(compiler, allArgs...)
hw.Build.Dir = sd.Path
if err := hw.Build.Run(); err != nil {
fmt.Printf("Error Building %s; Err:%s\n", hw.Name, err) // TODO: save failed to compile files in a list
}
go CopyCpp(hw, sd.CppDir) // run in goroutine to prevent i/o delay
}
// ProcReadForwarder is used to forward read bytes to the intended reader unless it is a kill command such as 'q', which kills the underlying process
type ProcReadForwarder struct {
KillChar byte
Proc **os.Process // a ptr to the field in exec.Command which points to a Process; since the field gets populated after creating PF
In io.Reader // the reading source we forward to
}
/// Read allows ProcReadForwarder to implement the Reader interface
// it is used to scan for a escape character which kills the running process without killing the go process
// all other scanned bytes are forwarded to the internal reader as normal
func (pf ProcReadForwarder) Read(p []byte) (n int, err error) {
if p[0] == pf.KillChar && p[1] == 10 { // if the bytes read == 'q'. 10 is the null character
return 0, (*pf.Proc).Kill()
}
return pf.In.Read(p) // forward to the internal Reader
}
// RunIt runs the binary in bin that was created by build it
func (hw *HW) RunIt(sd StudentDir, args ...string) {
hw.Run = exec.Command(hw.BuildFile, args...)
hw.Run.Dir = hw.Build.Dir
pf := ProcReadForwarder{'q', &hw.Run.Process, os.Stdin}
hw.Run.Stdout = os.Stdout
hw.Run.Stderr = os.Stderr
hw.Run.Stdin = pf
if err := hw.Run.Run(); err != nil {
fmt.Printf("\nError Running %s; Err:%s\n", hw.Name, err)
}
}
// CreateBinDir creates the binary directory in the folder where the hws are located if it isnt already there
func CreateBinDir(inDir string) string {
var err error
if info, _ := os.Stat(inDir); !info.IsDir() {
err = os.Mkdir(inDir, 0666)
if err != nil {
log.Fatalln(err)
}
}
bin := path.Join(inDir, "bin")
if err = os.Mkdir(bin, 0666); err != nil && err.(*os.PathError).Err.Error() != "file exists" {
log.Fatalln(err)
}
return bin
}
// CreateCppDir creates the binary directory in the folder where the hws are located if it isnt already there
func CreateCppDir(inDir string) string {
var err error
cppDir := path.Join(inDir, "cpps")
if err = os.Mkdir(cppDir, 0666); err != nil && err.(*os.PathError).Err.Error() != "file exists" {
log.Fatalln(err)
}
return cppDir
}
// GetNewLine checks for newline from StdIn
func GetNewLine() {
bio := bufio.NewReader(os.Stdin)
if _, err := bio.ReadString('\n'); err != nil {
log.Fatalln(err)
}
}
func main() {
var (
inDir = flag.String("inDir", "", "Dir with the student files")
compiler = flag.String("compiler", "g++", "Compiler Command")
q = flag.String("q", "", "Question number in the form of \"q<n>\"")
student = flag.String("student", "", "Student to begin iteration with")
source = flag.String("source", "gradescope", "Student files download source")
times = flag.Int("times", 1, "Times to execute each program")
seq = flag.Bool("seq", false, "Whether to build only after finished running previous program")
// args = flag.String("")
)
flag.Parse()
for _, required := range []*string{inDir} {
if *required == "" {
fmt.Println("Error: A Required Var Must Be set")
}
}
bin := CreateBinDir(*inDir)
cppDir := CreateCppDir(*inDir)
sd := New(*inDir, bin, cppDir, *source, *seq)
sd.Exec(*compiler, *student, *q, *times, "-std=c++11")
//
GetNewLine() // press return once more to finish execution
}