Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: client/cmd/isolate/upload_tracker.go

Issue 2985873002: Pass a Writer to Finalize for JSON dumping. (Closed)
Patch Set: Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « client/cmd/isolate/exp_archive.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The LUCI Authors. 1 // Copyright 2017 The LUCI Authors.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License. 4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 package main 15 package main
16 16
17 import ( 17 import (
18 "encoding/json" 18 "encoding/json"
19 "fmt" 19 "fmt"
20 "io"
20 "io/ioutil" 21 "io/ioutil"
21 "log" 22 "log"
22 "os" 23 "os"
23 "path/filepath" 24 "path/filepath"
24 "strings" 25 "strings"
25 26
26 humanize "github.com/dustin/go-humanize" 27 humanize "github.com/dustin/go-humanize"
27 "github.com/luci/luci-go/common/isolated" 28 "github.com/luci/luci-go/common/isolated"
28 "github.com/luci/luci-go/common/isolatedclient" 29 "github.com/luci/luci-go/common/isolatedclient"
29 ) 30 )
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 }) 139 })
139 } 140 }
140 return nil 141 return nil
141 } 142 }
142 143
143 // Finalize creates and uploads the isolate JSON at the isolatePath, and closes the checker and uploader. 144 // Finalize creates and uploads the isolate JSON at the isolatePath, and closes the checker and uploader.
144 // It returns the isolate digest. 145 // It returns the isolate digest.
145 // If dumpJSONPath is non-empty, the digest is also written to that path as 146 // If dumpJSONPath is non-empty, the digest is also written to that path as
146 // JSON (in the same format as batch_archive). 147 // JSON (in the same format as batch_archive).
147 // Finalize should only be called after UploadDeps. 148 // Finalize should only be called after UploadDeps.
148 func (ut *UploadTracker) Finalize(isolatedPath, dumpJSONPath string) (isolated.H exDigest, error) { 149 func (ut *UploadTracker) Finalize(isolatedPath string, dumpJSONWriter io.Writer) (isolated.HexDigest, error) {
149 // Marshal the isolated file into JSON, and create an Item to describe i t. 150 // Marshal the isolated file into JSON, and create an Item to describe i t.
150 isolJSON, err := json.Marshal(ut.isol) 151 isolJSON, err := json.Marshal(ut.isol)
151 if err != nil { 152 if err != nil {
152 return "", err 153 return "", err
153 } 154 }
154 isolItem := &Item{ 155 isolItem := &Item{
155 Path: isolatedPath, 156 Path: isolatedPath,
156 RelPath: filepath.Base(isolatedPath), 157 RelPath: filepath.Base(isolatedPath),
157 Digest: isolated.HashBytes(isolJSON), 158 Digest: isolated.HashBytes(isolJSON),
158 Size: int64(len(isolJSON)), 159 Size: int64(len(isolJSON)),
(...skipping 20 matching lines...) Expand all
179 return "", err 180 return "", err
180 } 181 }
181 182
182 // Write the isolated file, and emit its digest to stdout. 183 // Write the isolated file, and emit its digest to stdout.
183 if err := ioutil.WriteFile(isolatedPath, isolJSON, 0644); err != nil { 184 if err := ioutil.WriteFile(isolatedPath, isolJSON, 0644); err != nil {
184 return "", err 185 return "", err
185 } 186 }
186 187
187 fmt.Printf("%s\t%s\n", isolItem.Digest, filepath.Base(isolatedPath)) 188 fmt.Printf("%s\t%s\n", isolItem.Digest, filepath.Base(isolatedPath))
188 189
189 » if err := dumpJSON(isolatedPath, dumpJSONPath, isolItem); err != nil { 190 » if err := dumpJSON(isolatedPath, dumpJSONWriter, isolItem); err != nil {
190 return "", err 191 return "", err
191 } 192 }
192 193
193 return isolItem.Digest, nil 194 return isolItem.Digest, nil
194 } 195 }
195 196
196 func dumpJSON(isolatedPath, dumpJSONPath string, isolItem *Item) error { 197 func dumpJSON(isolatedPath string, dumpJSONWriter io.Writer, isolItem *Item) err or {
197 » if dumpJSONPath == "" {
198 » » return nil
199 » }
200 // The name is the base name of the isolated file, extension stripped. 198 // The name is the base name of the isolated file, extension stripped.
201 name := filepath.Base(isolatedPath) 199 name := filepath.Base(isolatedPath)
202 if i := strings.LastIndex(name, "."); i != -1 { 200 if i := strings.LastIndex(name, "."); i != -1 {
203 name = name[:i] 201 name = name[:i]
204 } 202 }
205 » j, err := json.Marshal(map[string]isolated.HexDigest{ 203
204 » enc := json.NewEncoder(dumpJSONWriter)
205 » return enc.Encode(map[string]isolated.HexDigest{
206 name: isolItem.Digest, 206 name: isolItem.Digest,
207 }) 207 })
208 if err != nil {
209 return err
210 }
211 if err := ioutil.WriteFile(dumpJSONPath, j, 0644); err != nil {
212 return err
213 }
214 return nil
215 } 208 }
OLDNEW
« no previous file with comments | « client/cmd/isolate/exp_archive.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698