Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "encoding/json" | 8 "encoding/json" |
| 9 "errors" | 9 "errors" |
| 10 "fmt" | 10 "fmt" |
| 11 "io/ioutil" | 11 "io/ioutil" |
| 12 "log" | 12 "log" |
| 13 "os" | 13 "os" |
| 14 "path/filepath" | 14 "path/filepath" |
| 15 | 15 |
| 16 humanize "github.com/dustin/go-humanize" | |
| 16 "github.com/luci/luci-go/client/isolate" | 17 "github.com/luci/luci-go/client/isolate" |
| 17 "github.com/luci/luci-go/common/isolated" | 18 "github.com/luci/luci-go/common/isolated" |
| 18 "github.com/maruel/subcommands" | 19 "github.com/maruel/subcommands" |
| 19 ) | 20 ) |
| 20 | 21 |
| 22 const ( | |
| 23 // archiveThreshold is the size (in bytes) used to determine whether to add | |
|
mcgreevy
2016/11/22 23:23:47
I've added some nitpicky comments about the wordin
djd-OOO-Apr2017
2016/11/23 00:36:34
In future stages of this change we have more thres
mcgreevy
2016/11/23 00:44:13
OK, let's stick with the consts, no func, and see
djd-OOO-Apr2017
2016/11/23 00:46:29
(I had done – see ps2)
| |
| 24 // files to an tar archive before uploading. Files smaller than this siz e will | |
|
mcgreevy
2016/11/22 23:23:47
s/an tar/a tar/
djd-OOO-Apr2017
2016/11/23 00:36:34
Done.
| |
| 25 // be combined into archives before | |
|
mcgreevy
2016/11/22 23:23:46
s/before uploaded directly to/before being uploade
djd-OOO-Apr2017
2016/11/23 00:36:34
Done.
| |
| 26 // uploaded directly to the server. | |
| 27 archiveThreshold = 100e3 // 100kB | |
|
mcgreevy
2016/11/22 23:23:47
I'd expect this to be 100 * 1024 ...
djd-OOO-Apr2017
2016/11/23 00:36:34
Without invoking religious wars (https://en.wikipe
mcgreevy
2016/11/23 00:44:13
I don't care that much.
| |
| 28 ) | |
| 29 | |
| 21 var cmdExpArchive = &subcommands.Command{ | 30 var cmdExpArchive = &subcommands.Command{ |
| 22 UsageLine: "exparchive <options>", | 31 UsageLine: "exparchive <options>", |
| 23 ShortDesc: "EXPERIMENTAL creates a .isolated file and uploads the tree t o an isolate server.", | 32 ShortDesc: "EXPERIMENTAL creates a .isolated file and uploads the tree t o an isolate server.", |
| 24 LongDesc: "All the files listed in the .isolated file are put in the is olate server cache. Small files are combined together in a tar archive before up loading.", | 33 LongDesc: "All the files listed in the .isolated file are put in the is olate server cache. Small files are combined together in a tar archive before up loading.", |
| 25 CommandRun: func() subcommands.CommandRun { | 34 CommandRun: func() subcommands.CommandRun { |
| 26 c := expArchiveRun{} | 35 c := expArchiveRun{} |
| 27 c.commonServerFlags.Init() | 36 c.commonServerFlags.Init() |
| 28 c.isolateFlags.Init(&c.Flags) | 37 c.isolateFlags.Init(&c.Flags) |
| 29 return &c | 38 return &c |
| 30 }, | 39 }, |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 45 } | 54 } |
| 46 if err := c.isolateFlags.Parse(cwd, RequireIsolatedFile); err != nil { | 55 if err := c.isolateFlags.Parse(cwd, RequireIsolatedFile); err != nil { |
| 47 return err | 56 return err |
| 48 } | 57 } |
| 49 if len(args) != 0 { | 58 if len(args) != 0 { |
| 50 return errors.New("position arguments not expected") | 59 return errors.New("position arguments not expected") |
| 51 } | 60 } |
| 52 return nil | 61 return nil |
| 53 } | 62 } |
| 54 | 63 |
| 64 // Item represents a file or symlink referenced by an isolate file. | |
| 65 type Item struct { | |
| 66 Path string | |
| 67 RelPath string | |
| 68 Size int64 | |
| 69 Mode os.FileMode | |
| 70 } | |
| 71 | |
| 55 func (c *expArchiveRun) main(a subcommands.Application, args []string) error { | 72 func (c *expArchiveRun) main(a subcommands.Application, args []string) error { |
| 56 // Parse the incoming isolate file. | 73 // Parse the incoming isolate file. |
| 57 deps, rootDir, isol, err := isolate.ProcessIsolate(&c.ArchiveOptions) | 74 deps, rootDir, isol, err := isolate.ProcessIsolate(&c.ArchiveOptions) |
| 58 if err != nil { | 75 if err != nil { |
| 59 return fmt.Errorf("failed to process isolate: %v", err) | 76 return fmt.Errorf("failed to process isolate: %v", err) |
| 60 } | 77 } |
| 61 log.Printf("Isolate referenced %d deps", len(deps)) | 78 log.Printf("Isolate referenced %d deps", len(deps)) |
| 62 | 79 |
| 63 » // TODO(djd): actually do something with the isolated. | 80 » // Walk each of the deps, partioning the results into symlinks and files categorised by size. |
| 64 » _ = rootDir | 81 » var links, smallFiles, largeFiles []*Item |
| 82 » var smallSize, largeSize int64 // Cumulative size of small/large files. | |
| 83 » for _, dep := range deps { | |
| 84 » » // Try to walk dep. If dep is a file (or symlink), the inner fun ction is called exactly once. | |
| 85 » » err := filepath.Walk(filepath.Clean(dep), func(path string, info os.FileInfo, err error) error { | |
| 86 » » » if err != nil { | |
| 87 » » » » return err | |
| 88 » » » } | |
| 89 » » » if info.IsDir() { | |
| 90 » » » » return nil | |
| 91 » » » } | |
| 92 | |
| 93 » » » relPath, err := filepath.Rel(rootDir, path) | |
| 94 » » » if err != nil { | |
| 95 » » » » return err | |
| 96 » » » } | |
| 97 | |
| 98 » » » item := &Item{ | |
| 99 » » » » Path: path, | |
| 100 » » » » RelPath: relPath, | |
| 101 » » » » Mode: info.Mode(), | |
| 102 » » » » Size: info.Size(), | |
| 103 » » » } | |
| 104 | |
| 105 » » » switch { | |
| 106 » » » case item.Mode&os.ModeSymlink == os.ModeSymlink: | |
| 107 » » » » links = append(links, item) | |
| 108 » » » case item.Size < archiveThreshold: | |
| 109 » » » » smallFiles = append(smallFiles, item) | |
| 110 » » » » smallSize += item.Size | |
| 111 » » » default: | |
| 112 » » » » largeFiles = append(largeFiles, item) | |
| 113 » » » » largeSize += item.Size | |
| 114 » » » } | |
| 115 » » » return nil | |
| 116 » » }) | |
| 117 » » if err != nil { | |
| 118 » » » return err | |
| 119 » » } | |
| 120 » } | |
| 121 | |
| 122 » log.Printf("Isolate expanded to %d files (total size %s) and %d symlinks ", len(smallFiles)+len(largeFiles), humanize.Bytes(uint64(smallSize+largeSize)), len(links)) | |
| 123 » log.Printf("\t%d files (%s) to be isolated individually", len(largeFiles ), humanize.Bytes(uint64(largeSize))) | |
| 124 » log.Printf("\t%d files (%s) to be isolated in archives", len(smallFiles) , humanize.Bytes(uint64(smallSize))) | |
| 125 | |
| 126 » // TODO(djd): actually do something with the each of links, smallFiles a nd largeFiles. | |
| 65 | 127 |
| 66 // Marshal the isolated file into JSON. | 128 // Marshal the isolated file into JSON. |
| 67 isolJSON, err := json.Marshal(isol) | 129 isolJSON, err := json.Marshal(isol) |
| 68 if err != nil { | 130 if err != nil { |
| 69 return err | 131 return err |
| 70 } | 132 } |
| 71 // TODO(djd): actually check/upload the isolated. | 133 // TODO(djd): actually check/upload the isolated. |
| 72 | 134 |
| 73 // Write the isolated file, and emit its digest to stdout. | 135 // Write the isolated file, and emit its digest to stdout. |
| 74 if err := ioutil.WriteFile(c.ArchiveOptions.Isolated, isolJSON, 0644); e rr != nil { | 136 if err := ioutil.WriteFile(c.ArchiveOptions.Isolated, isolJSON, 0644); e rr != nil { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 88 if len(c.ArchiveOptions.Blacklist) != 0 { | 150 if len(c.ArchiveOptions.Blacklist) != 0 { |
| 89 fmt.Fprintf(a.GetErr(), "%s: blacklist is not supported\n", a.Ge tName()) | 151 fmt.Fprintf(a.GetErr(), "%s: blacklist is not supported\n", a.Ge tName()) |
| 90 return 1 | 152 return 1 |
| 91 } | 153 } |
| 92 if err := c.main(a, args); err != nil { | 154 if err := c.main(a, args); err != nil { |
| 93 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) | 155 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) |
| 94 return 1 | 156 return 1 |
| 95 } | 157 } |
| 96 return 0 | 158 return 0 |
| 97 } | 159 } |
| OLD | NEW |