-
Notifications
You must be signed in to change notification settings - Fork 1
/
libinteractive.go
166 lines (153 loc) · 3.83 KB
/
libinteractive.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
package gitserver
import (
"bytes"
"encoding/json"
"io"
"os/exec"
"github.com/omegaup/go-base/v3/logging"
"github.com/omegaup/quark/common"
"github.com/pkg/errors"
)
// InteractiveSettingsCompiler converts the .idl file contents and the module
// name + parent language pair into a common.InteractiveSettings object.
type InteractiveSettingsCompiler interface {
// GetInteractiveSettings converts the .idl file contents and the module name
// + parent language pair into a common.InteractiveSettings object.
GetInteractiveSettings(
idlFileContents io.Reader,
moduleName string,
parentLang string,
) (*common.InteractiveSettings, error)
}
// LibinteractiveCompiler is an implementation of
// InteractiveSettingsCompiler that uses the real libinteractive.jar to convert
// the .idl file.
type LibinteractiveCompiler struct {
// A way to optionally override the path of libinteractive.jar.
LibinteractiveJarPath string
Log logging.Logger
}
// GetInteractiveSettings calls libinteractive.jar to produce the
// common.InteractiveSettings.
func (c *LibinteractiveCompiler) GetInteractiveSettings(
contents io.Reader,
moduleName string,
parentLang string,
) (*common.InteractiveSettings, error) {
libinteractiveJarPath := "/usr/share/java/libinteractive.jar"
if c.LibinteractiveJarPath != "" {
libinteractiveJarPath = c.LibinteractiveJarPath
}
cmd := exec.Command(
"/usr/bin/java",
"-jar", libinteractiveJarPath,
"json",
"--module-name", moduleName,
"--parent-lang", parentLang,
"--omit-debug-targets",
)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, errors.Wrap(
err,
"failed to get stdin path",
)
}
stdinErrChan := make(chan error, 1)
go (func() {
defer stdin.Close()
if _, err := io.Copy(stdin, contents); err != nil {
c.Log.Error(
"Failed to write to libinteractive",
map[string]any{
"cmd": cmd,
"err": err,
},
)
stdinErrChan <- err
}
close(stdinErrChan)
})()
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, errors.Wrap(
err,
"failed to get stdout path",
)
}
settingsChan := make(chan *common.InteractiveSettings, 1)
go (func() {
var settings common.InteractiveSettings
if err := json.NewDecoder(stdout).Decode(&settings); err != nil {
c.Log.Error(
"Failed to read from libinteractive",
map[string]any{
"cmd": cmd,
"err": err,
},
)
settingsChan <- nil
} else {
settingsChan <- &settings
}
close(settingsChan)
})()
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, errors.Wrap(
err,
"failed to get stderr pipe",
)
}
stderrChan := make(chan *bytes.Buffer, 1)
go (func() {
var buffer bytes.Buffer
if _, err := io.Copy(&buffer, stderr); err != nil {
c.Log.Error(
"Failed to copy libinteractive stderr",
map[string]any{
"cmd": cmd,
"err": err,
},
)
}
stderrChan <- &buffer
close(stderrChan)
})()
if err := cmd.Run(); err != nil {
stderrError := errors.New((<-stderrChan).String())
c.Log.Error(
"Failed to run command",
map[string]any{
"cmd": cmd,
"err": err,
"stderr": stderrError,
},
)
return nil, errors.Wrap(
stderrError,
"libinteractive compile error",
)
}
if err, ok := <-stdinErrChan; ok {
return nil, errors.Wrap(
err,
"failed to send input to libinteractive",
)
}
return <-settingsChan, nil
}
// FakeInteractiveSettingsCompiler is an implementation of
// InteractiveSettingsCompiler that just returns pre-specified settings.
type FakeInteractiveSettingsCompiler struct {
Settings *common.InteractiveSettings
Err error
}
// GetInteractiveSettings returns the pre-specified settings.
func (c *FakeInteractiveSettingsCompiler) GetInteractiveSettings(
contents io.Reader,
moduleName string,
parentLang string,
) (*common.InteractiveSettings, error) {
return c.Settings, c.Err
}