-
Notifications
You must be signed in to change notification settings - Fork 7
/
output.go
273 lines (252 loc) · 6.07 KB
/
output.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
package cwl
import (
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/robertkrimen/otto"
"github.com/otiai10/jsonindent"
)
// Output represents and conbines "CommandOutputParameter" and "WorkflowOutputParameter"
// @see
// - http://www.commonwl.org/v1.0/CommandLineTool.html#CommandOutputParameter
// - http://www.commonwl.org/v1.0/Workflow.html#WorkflowOutputParameter
type Output struct {
ID string `json:"id"`
Label string `json:"label"`
Doc []string `json:"doc"`
Format string `json:"format"`
Binding *Binding `json:"outputBinding"`
Source []string `json:"outputSource"`
Types []Type `json:"type"`
SecondaryFiles []SecondaryFile
// Loaded Contents if Binding.LoadContents == true
Contents interface{} `json:"contents"`
}
// New constructs "Output" struct from interface.
func (o Output) New(i interface{}) Output {
dest := Output{}
switch x := i.(type) {
case map[string]interface{}:
for key, v := range x {
switch key {
case "id":
dest.ID = v.(string)
case "type":
dest.Types = Type{}.NewList(v)
case "outputBinding":
dest.Binding = Binding{}.New(v)
case "outputSource":
dest.Source = StringArrayable(v)
case "doc":
dest.Doc = StringArrayable(v)
case "format":
dest.Format = v.(string)
case "secondaryFiles":
dest.SecondaryFiles = SecondaryFile{}.NewList(v)
}
}
case string:
dest.Types = Type{}.NewList(x)
}
return dest
}
// Outputs represents "outputs" field in "CWL".
type Outputs []Output
// New constructs "Outputs" struct.
func (outs Outputs) New(i interface{}) Outputs {
dest := Outputs{}
switch x := i.(type) {
case []interface{}:
for _, v := range x {
dest = append(dest, Output{}.New(v))
}
case map[string]interface{}:
for key, v := range x {
output := Output{}.New(v)
output.ID = key
dest = append(dest, output)
}
}
return dest
}
// Len for sorting
func (outs Outputs) Len() int {
return len(outs)
}
// Less for sorting
func (outs Outputs) Less(i, j int) bool {
prev, next := outs[i].Binding, outs[j].Binding
switch [2]bool{prev == nil, next == nil} {
case [2]bool{true, true}:
return false
case [2]bool{false, true}:
return prev.Position < 0
case [2]bool{true, false}:
return next.Position > 0
default:
return prev.Position <= next.Position
}
}
// Swap for sorting
func (outs Outputs) Swap(i, j int) {
outs[i], outs[j] = outs[j], outs[i]
}
// LoadContents ...
func (outs Outputs) LoadContents(srcdir string) (*otto.Otto, error) {
self := []map[string]interface{}{}
for _, o := range outs {
if o.Binding == nil {
continue
}
if !o.Binding.LoadContents {
continue
}
f, err := os.Open(filepath.Join(srcdir, o.Binding.Glob[0]))
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
self = append(self, map[string]interface{}{
"contents": string(b),
})
}
// No contents to load
if len(self) == 0 {
return nil, nil
}
vm := otto.New()
if err := vm.Set("self", self); err != nil {
return nil, err
}
return vm, nil
}
// Dump ...
func (outs Outputs) Dump(vm *otto.Otto, dir string, stdout, stderr string, w io.Writer) error {
dest := map[string]interface{}{}
for _, o := range outs {
if o.Binding != nil && o.Binding.Eval != nil && vm != nil {
js, err := o.Binding.Eval.ToJavaScriptString()
if err != nil {
return err
}
v, err := vm.Run(js)
if err != nil {
return err
}
switch {
case v.IsNumber():
dest[o.ID], err = v.ToInteger()
if err != nil {
return err
}
case v.IsString():
dest[o.ID], err = v.ToString()
if err != nil {
return err
}
default:
// TODO: more type switch
}
// TODO: do we need integrate all the outputs?
}
if err := o.DumpFileMeta(dest, dir, stdout, stderr, w); err != nil {
return err
}
}
return jsonindent.NewEncoder(w).Encode(dest)
}
// DumpFileMeta ...
func (o Output) DumpFileMeta(dest map[string]interface{}, dir string, stdout, stderr string, w io.Writer) error {
// This output should not be dumped
if o.Binding != nil && o.Binding.LoadContents {
return nil
}
switch o.Types[0].Type {
case "Directory":
dir := filepath.Clean(filepath.Clean(filepath.Join(dir, o.Binding.Glob[0])))
filelist, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
out := map[string]interface{}{
"class": "Directory",
}
listing := []map[string]interface{}{}
for _, info := range filelist {
// {{{ TODO: filter "inputs" files
if filepath.Ext(info.Name()) == ".tar" {
continue
}
// }}}
targetfilepath := filepath.Join(dir, info.Name())
metadata, err := getFileMetaData(targetfilepath)
if err != nil {
return err
}
listing = append(listing, metadata)
}
out["listing"] = listing
dest[o.ID] = out
case "File":
for _, glob := range o.Binding.Glob {
metadata, err := getFileMetaData(filepath.Join(dir, glob))
if err != nil {
return err
}
dest[o.ID] = metadata
}
case "stdout":
name := o.ID
if stdout != "" && name != stdout {
name = stdout
}
metadata, err := getFileMetaData(filepath.Join(dir, name))
if err != nil {
return err
}
dest[o.ID] = metadata
case "stderr":
name := o.ID
if stderr != "" && name != stderr {
name = stderr
}
metadata, err := getFileMetaData(filepath.Join(dir, name))
if err != nil {
return err
}
dest[o.ID] = metadata
default:
return nil // do nothing
}
return nil
}
// getFileMetaData
func getFileMetaData(targetfilepath string) (map[string]interface{}, error) {
targetfile, err := os.Open(targetfilepath)
if err != nil {
return nil, err
}
defer targetfile.Close()
info, err := os.Stat(targetfilepath)
if err != nil {
return nil, err
}
h := sha1.New()
if _, err := io.Copy(h, targetfile); err != nil {
return nil, err
}
return map[string]interface{}{
"checksum": fmt.Sprintf("sha1$%x", string(h.Sum(nil))),
"basename": filepath.Base(targetfilepath),
"location": fmt.Sprintf("file://%s", targetfilepath),
"path": targetfilepath,
"class": "File",
"size": info.Size(),
}, nil
}