/
main.go
224 lines (204 loc) · 5.34 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"go/importer"
"io"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"runtime/debug"
"strings"
"syscall"
"github.com/golang/glog"
"github.com/yunabe/lgo/cmd/lgo-internal/liner"
"github.com/yunabe/lgo/cmd/runner"
"github.com/yunabe/lgo/converter"
"github.com/yunabe/lgo/core"
"golang.org/x/sys/unix"
)
var (
subcomandFlag = flag.String("subcommand", "", "lgo subcommand")
sessIDFlag = flag.String("sess_id", "", "lgo session id")
connectionFile = flag.String("connection_file", "", "jupyter kernel connection file path. This flag is used with kernel subcommand")
)
type printer struct{}
func (*printer) Println(args ...interface{}) {
for _, arg := range args {
fmt.Println(arg)
}
}
func createRunContext(parent context.Context, sigint <-chan os.Signal) (ctx context.Context, cancel func()) {
ctx, cancel = context.WithCancel(parent)
go func() {
select {
case <-sigint:
cancel()
case <-ctx.Done():
}
}()
return
}
func exitProcess() {
// Programs should call Flush before exiting to guarantee all log output is written.
// https://godoc.org/github.com/golang/glog
glog.Flush()
os.Exit(0)
}
func createProcessContext(withSigint bool) context.Context {
// Use SIGUSR1 to notify the death of the parent process.
unix.Prctl(unix.PR_SET_PDEATHSIG, uintptr(syscall.SIGUSR1), 0, 0, 0)
sigch := make(chan os.Signal)
ctx, cancel := context.WithCancel(context.Background())
signals := []os.Signal{syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGUSR1}
if withSigint {
signals = append(signals, syscall.SIGINT)
}
signal.Notify(sigch, signals...)
go func() {
sig := <-sigch
if sig == syscall.SIGUSR1 {
glog.Info("The parent process died. Cancelling the internal process")
} else {
glog.Infof("Received a signal (%s). Cancelling the internal process", sig)
}
cancel()
}()
return ctx
}
func fromFiles(ctx context.Context, rn *runner.LgoRunner) {
for _, path := range flag.Args() {
src, err := ioutil.ReadFile(path)
if err != nil {
glog.Errorf("Failed to read %s: %v", path, err)
return
}
if err = rn.Run(core.LgoContext{Context: ctx}, string(src)); err != nil {
glog.Error(err)
return
}
}
}
func fromStdin(ctx context.Context, rn *runner.LgoRunner) {
ln := liner.NewLiner()
ln.SetCompleter(func(lines []string) []string {
if len(lines) == 0 {
return nil
}
src := strings.Join(lines, "\n")
last := lines[len(lines)-1]
matches, start, end := rn.Complete(ctx, src, len(src))
if len(matches) == 0 {
return nil
}
start = start - (len(src) - len(last))
end = end - (len(src) - len(last))
if start < 0 || start > len(src) || end < 0 || end > len(src) {
return nil
}
for i, m := range matches {
matches[i] = last[:start] + m + last[end:]
}
return matches
})
sigint := make(chan os.Signal)
signal.Notify(sigint, syscall.SIGINT)
loop:
for {
select {
case <-ctx.Done():
break loop
default:
}
src, err := ln.Next()
if err != nil {
if err != io.EOF {
glog.Errorf("liner returned non-EOF error unexpectedly: %v", err)
}
return
}
runCtx, cancel := context.WithCancel(ctx)
go func() {
select {
case <-sigint:
cancel()
case <-runCtx.Done():
}
}()
func() {
defer func() {
cancel()
p := recover()
if p != nil {
// The return value of debug.Stack() ends with \n.
fmt.Fprintf(os.Stderr, "panic: %v\n\n%s", p, debug.Stack())
}
}()
if err := rn.Run(core.LgoContext{Context: runCtx}, src); err != nil {
glog.Error(err)
}
}()
}
}
func installPkgArchive(pkgDir string, paths []string) error {
return exec.Command("go", append([]string{"install", "-linkshared", "-pkgdir", pkgDir}, paths...)...).Run()
}
type packageArchiveInstaller struct{ pkgDir string }
func (in *packageArchiveInstaller) Install(pkgs []string) error {
return installPkgArchive(in.pkgDir, pkgs)
}
func main() {
flag.Parse()
if *sessIDFlag == "" {
glog.Fatal("--sess_id is not set")
}
var sessID runner.SessionID
if err := sessID.Unmarshal(*sessIDFlag); err != nil {
glog.Fatalf("--sess_id=%s is invalid: %v", *sessIDFlag, err)
}
if *subcomandFlag == "" {
glog.Fatal("--subcomand is not set")
}
lgopath := os.Getenv("LGOPATH")
if lgopath == "" {
glog.Fatal("LGOPATH is empty")
}
lgopath, err := filepath.Abs(lgopath)
if err != nil {
glog.Fatalf("Failed to get the absolute path of LGOPATH: %v", err)
}
core.RegisterLgoPrinter(&printer{})
pkgDir := path.Join(lgopath, "pkg")
// Fom go1.10, go install does not install .a files into GOPATH.
// We need to read package information from .a files installed in LGOPATH instead.
converter.SetLGOImporter(importer.For("gc", func(path string) (io.ReadCloser, error) {
abs := filepath.Join(lgopath, "pkg", path+".a")
if _, err := os.Stat(abs); os.IsNotExist(err) {
installPkgArchive(pkgDir, []string{path})
}
return os.Open(abs)
}))
converter.SetPackageArchiveInstaller(&packageArchiveInstaller{
pkgDir: pkgDir,
})
if *subcomandFlag == "kernel" {
kernelMain(lgopath, &sessID)
exitProcess()
}
rn := runner.NewLgoRunner(lgopath, &sessID)
useFiles := len(flag.Args()) > 0
ctx := createProcessContext(useFiles)
if len(flag.Args()) > 0 {
fromFiles(ctx, rn)
} else {
fromStdin(ctx, rn)
}
// clean-up
glog.Infof("Clean the session: %s", sessID.Marshal())
runner.CleanSession(lgopath, &sessID)
exitProcess()
}