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

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

Issue 2937583003: Refactor genjson processing code. (Closed)
Patch Set: Created 3 years, 6 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 | « no previous file | 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 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "errors" 8 "errors"
9 "fmt" 9 "fmt"
10 "os" 10 "os"
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 prefix = "" 127 prefix = ""
128 } 128 }
129 start := time.Now() 129 start := time.Now()
130 client, err := c.createAuthClient() 130 client, err := c.createAuthClient()
131 if err != nil { 131 if err != nil {
132 return err 132 return err
133 } 133 }
134 ctx := c.defaultFlags.MakeLoggingContext(os.Stderr) 134 ctx := c.defaultFlags.MakeLoggingContext(os.Stderr)
135 arch := archiver.New(ctx, isolatedclient.New(nil, client, c.isolatedFlag s.ServerURL, c.isolatedFlags.Namespace, nil, nil), out) 135 arch := archiver.New(ctx, isolatedclient.New(nil, client, c.isolatedFlag s.ServerURL, c.isolatedFlags.Namespace, nil, nil), out)
136 CancelOnCtrlC(arch) 136 CancelOnCtrlC(arch)
137 » type tmp struct { 137
138 » type namedItem struct {
138 *archiver.Item 139 *archiver.Item
139 name string 140 name string
140 } 141 }
141 » items := make(chan *tmp, len(args)) 142 » items := make(chan *namedItem, len(args))
142 var wg sync.WaitGroup 143 var wg sync.WaitGroup
143 for _, arg := range args { 144 for _, arg := range args {
144 wg.Add(1) 145 wg.Add(1)
145 » » go func(genJsonPath string) { 146 » » go func(genJSONPath string) {
146 defer wg.Done() 147 defer wg.Done()
147 » » » data := &struct { 148 » » » if opts, err := processGenJSON(genJSONPath); err != nil {
148 » » » » Args []string
149 » » » » Dir string
150 » » » » Version int
151 » » » }{}
152 » » » if err := common.ReadJSONFile(genJsonPath, data); err != nil {
153 arch.Cancel(err) 149 arch.Cancel(err)
154 » » » » return 150 » » » } else {
151 » » » » items <- &namedItem{
152 » » » » » isolate.Archive(arch, opts),
153 » » » » » strippedIsolatedName(opts.Isolated),
154 » » » » }
155 } 155 }
156 if data.Version != isolate.IsolatedGenJSONVersion {
157 arch.Cancel(fmt.Errorf("invalid version %d in %s ", data.Version, genJsonPath))
158 return
159 }
160 if !common.IsDirectory(data.Dir) {
161 arch.Cancel(fmt.Errorf("invalid dir %s in %s", d ata.Dir, genJsonPath))
162 return
163 }
164 opts, err := parseArchiveCMD(data.Args, data.Dir)
165 if err != nil {
166 arch.Cancel(fmt.Errorf("invalid archive command in %s: %s", genJsonPath, err))
167 return
168 }
169 name := filepath.Base(opts.Isolated)
170 // Strip the extension if there is one.
171 if dotIndex := strings.LastIndex(name, "."); dotIndex != -1 {
172 name = name[0:dotIndex]
173 }
174 items <- &tmp{isolate.Archive(arch, opts), name}
175 }(arg) 156 }(arg)
176 } 157 }
177 go func() { 158 go func() {
178 wg.Wait() 159 wg.Wait()
179 close(items) 160 close(items)
180 }() 161 }()
181 162
182 data := map[string]isolated.HexDigest{} 163 data := map[string]isolated.HexDigest{}
183 for item := range items { 164 for item := range items {
184 item.WaitForHashed() 165 item.WaitForHashed()
(...skipping 12 matching lines...) Expand all
197 } 178 }
198 if !c.defaultFlags.Quiet { 179 if !c.defaultFlags.Quiet {
199 stats := arch.Stats() 180 stats := arch.Stats()
200 fmt.Fprintf(os.Stderr, "Hits : %5d (%s)\n", stats.TotalHits() , stats.TotalBytesHits()) 181 fmt.Fprintf(os.Stderr, "Hits : %5d (%s)\n", stats.TotalHits() , stats.TotalBytesHits())
201 fmt.Fprintf(os.Stderr, "Misses : %5d (%s)\n", stats.TotalMisses (), stats.TotalBytesPushed()) 182 fmt.Fprintf(os.Stderr, "Misses : %5d (%s)\n", stats.TotalMisses (), stats.TotalBytesPushed())
202 fmt.Fprintf(os.Stderr, "Duration: %s\n", units.Round(duration, t ime.Millisecond)) 183 fmt.Fprintf(os.Stderr, "Duration: %s\n", units.Round(duration, t ime.Millisecond))
203 } 184 }
204 return err 185 return err
205 } 186 }
206 187
188 // processGenJSON validates a genJSON file and returns the contents.
189 func processGenJSON(genJSONPath string) (*isolate.ArchiveOptions, error) {
190 data := &struct {
191 Args []string
192 Dir string
193 Version int
194 }{}
195 if err := common.ReadJSONFile(genJSONPath, data); err != nil {
196 return nil, err
197 }
198 if data.Version != isolate.IsolatedGenJSONVersion {
199 return nil, fmt.Errorf("invalid version %d in %s", data.Version, genJSONPath)
200 }
201 if !common.IsDirectory(data.Dir) {
202 return nil, fmt.Errorf("invalid dir %s in %s", data.Dir, genJSON Path)
203 }
204 opts, err := parseArchiveCMD(data.Args, data.Dir)
205 if err != nil {
206 return nil, fmt.Errorf("invalid archive command in %s: %s", genJ SONPath, err)
207 }
208 return opts, nil
209 }
210
211 // strippedIsolatedName returns the base name of an isolated path, with the exte nsion (if any) removed.
212 func strippedIsolatedName(isolated string) string {
213 name := filepath.Base(isolated)
214 // Strip the extension if there is one.
215 if dotIndex := strings.LastIndex(name, "."); dotIndex != -1 {
216 return name[0:dotIndex]
217 }
218 return name
219 }
220
207 func (c *batchArchiveRun) Run(a subcommands.Application, args []string, _ subcom mands.Env) int { 221 func (c *batchArchiveRun) Run(a subcommands.Application, args []string, _ subcom mands.Env) int {
208 if err := c.Parse(a, args); err != nil { 222 if err := c.Parse(a, args); err != nil {
209 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 223 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
210 return 1 224 return 1
211 } 225 }
212 cl, err := c.defaultFlags.StartTracing() 226 cl, err := c.defaultFlags.StartTracing()
213 if err != nil { 227 if err != nil {
214 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 228 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
215 return 1 229 return 1
216 } 230 }
217 defer cl.Close() 231 defer cl.Close()
218 if err := c.main(a, args); err != nil { 232 if err := c.main(a, args); err != nil {
219 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) 233 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
220 return 1 234 return 1
221 } 235 }
222 return 0 236 return 0
223 } 237 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698