| 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 |
| 24 // files to a tar archive before uploading. Files smaller than this size
will |
| 25 // be combined into archives before being uploaded to the server. |
| 26 archiveThreshold = 100e3 // 100kB |
| 27 ) |
| 28 |
| 21 var cmdExpArchive = &subcommands.Command{ | 29 var cmdExpArchive = &subcommands.Command{ |
| 22 UsageLine: "exparchive <options>", | 30 UsageLine: "exparchive <options>", |
| 23 ShortDesc: "EXPERIMENTAL parses a .isolate file to create a .isolated fi
le, and uploads it and all referenced files to an isolate server", | 31 ShortDesc: "EXPERIMENTAL parses a .isolate file to create a .isolated fi
le, and uploads it and all referenced files to 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.", | 32 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 { | 33 CommandRun: func() subcommands.CommandRun { |
| 26 c := &expArchiveRun{} | 34 c := &expArchiveRun{} |
| 27 c.commonServerFlags.Init() | 35 c.commonServerFlags.Init() |
| 28 c.isolateFlags.Init(&c.Flags) | 36 c.isolateFlags.Init(&c.Flags) |
| 29 return c | 37 return c |
| 30 }, | 38 }, |
| 31 } | 39 } |
| 32 | 40 |
| 33 // expArchiveRun contains the logic for the experimental archive subcommand. | 41 // expArchiveRun contains the logic for the experimental archive subcommand. |
| 34 // It implements subcommand.CommandRun | 42 // It implements subcommand.CommandRun |
| 35 type expArchiveRun struct { | 43 type expArchiveRun struct { |
| 36 commonServerFlags // Provides the GetFlags method. | 44 commonServerFlags // Provides the GetFlags method. |
| 37 isolateFlags isolateFlags | 45 isolateFlags isolateFlags |
| 38 } | 46 } |
| 39 | 47 |
| 48 // Item represents a file or symlink referenced by an isolate file. |
| 49 type Item struct { |
| 50 Path string |
| 51 RelPath string |
| 52 Size int64 |
| 53 Mode os.FileMode |
| 54 } |
| 55 |
| 40 // main contains the core logic for experimental archive. | 56 // main contains the core logic for experimental archive. |
| 41 func (c *expArchiveRun) main() error { | 57 func (c *expArchiveRun) main() error { |
| 42 archiveOpts := &c.isolateFlags.ArchiveOptions | 58 archiveOpts := &c.isolateFlags.ArchiveOptions |
| 43 // Parse the incoming isolate file. | 59 // Parse the incoming isolate file. |
| 44 deps, rootDir, isol, err := isolate.ProcessIsolate(archiveOpts) | 60 deps, rootDir, isol, err := isolate.ProcessIsolate(archiveOpts) |
| 45 if err != nil { | 61 if err != nil { |
| 46 return fmt.Errorf("failed to process isolate: %v", err) | 62 return fmt.Errorf("failed to process isolate: %v", err) |
| 47 } | 63 } |
| 48 log.Printf("Isolate referenced %d deps", len(deps)) | 64 log.Printf("Isolate referenced %d deps", len(deps)) |
| 49 | 65 |
| 50 » // TODO(djd): actually do something with the isolated. | 66 » // Walk each of the deps, partioning the results into symlinks and files
categorised by size. |
| 51 » _ = rootDir | 67 » var links, archiveFiles, indivFiles []*Item |
| 68 » var archiveSize, indivSize int64 // Cumulative size of archived/individu
al files. |
| 69 » for _, dep := range deps { |
| 70 » » // Try to walk dep. If dep is a file (or symlink), the inner fun
ction is called exactly once. |
| 71 » » err := filepath.Walk(filepath.Clean(dep), func(path string, info
os.FileInfo, err error) error { |
| 72 » » » if err != nil { |
| 73 » » » » return err |
| 74 » » » } |
| 75 » » » if info.IsDir() { |
| 76 » » » » return nil |
| 77 » » » } |
| 78 |
| 79 » » » relPath, err := filepath.Rel(rootDir, path) |
| 80 » » » if err != nil { |
| 81 » » » » return err |
| 82 » » » } |
| 83 |
| 84 » » » item := &Item{ |
| 85 » » » » Path: path, |
| 86 » » » » RelPath: relPath, |
| 87 » » » » Mode: info.Mode(), |
| 88 » » » » Size: info.Size(), |
| 89 » » » } |
| 90 |
| 91 » » » switch { |
| 92 » » » case item.Mode&os.ModeSymlink == os.ModeSymlink: |
| 93 » » » » links = append(links, item) |
| 94 » » » case item.Size < archiveThreshold: |
| 95 » » » » archiveFiles = append(archiveFiles, item) |
| 96 » » » » archiveSize += item.Size |
| 97 » » » default: |
| 98 » » » » indivFiles = append(indivFiles, item) |
| 99 » » » » indivSize += item.Size |
| 100 » » » } |
| 101 » » » return nil |
| 102 » » }) |
| 103 » » if err != nil { |
| 104 » » » return err |
| 105 » » } |
| 106 » } |
| 107 |
| 108 » log.Printf("Isolate expanded to %d files (total size %s) and %d symlinks
", len(archiveFiles)+len(indivFiles), humanize.Bytes(uint64(archiveSize+indivSiz
e)), len(links)) |
| 109 » log.Printf("\t%d files (%s) to be isolated individually", len(indivFiles
), humanize.Bytes(uint64(indivSize))) |
| 110 » log.Printf("\t%d files (%s) to be isolated in archives", len(archiveFile
s), humanize.Bytes(uint64(archiveSize))) |
| 111 |
| 112 » // TODO(djd): actually do something with the each of links, archiveFiles
and indivFiles. |
| 52 | 113 |
| 53 // Marshal the isolated file into JSON. | 114 // Marshal the isolated file into JSON. |
| 54 isolJSON, err := json.Marshal(isol) | 115 isolJSON, err := json.Marshal(isol) |
| 55 if err != nil { | 116 if err != nil { |
| 56 return err | 117 return err |
| 57 } | 118 } |
| 58 // TODO(djd): actually check/upload the isolated. | 119 // TODO(djd): actually check/upload the isolated. |
| 59 | 120 |
| 60 // Write the isolated file, and emit its digest to stdout. | 121 // Write the isolated file, and emit its digest to stdout. |
| 61 if err := ioutil.WriteFile(archiveOpts.Isolated, isolJSON, 0644); err !=
nil { | 122 if err := ioutil.WriteFile(archiveOpts.Isolated, isolJSON, 0644); err !=
nil { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 92 if len(c.isolateFlags.ArchiveOptions.Blacklist) != 0 { | 153 if len(c.isolateFlags.ArchiveOptions.Blacklist) != 0 { |
| 93 fmt.Fprintf(a.GetErr(), "%s: blacklist is not supported\n", a.Ge
tName()) | 154 fmt.Fprintf(a.GetErr(), "%s: blacklist is not supported\n", a.Ge
tName()) |
| 94 return 1 | 155 return 1 |
| 95 } | 156 } |
| 96 if err := c.main(); err != nil { | 157 if err := c.main(); err != nil { |
| 97 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) | 158 fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err) |
| 98 return 1 | 159 return 1 |
| 99 } | 160 } |
| 100 return 0 | 161 return 0 |
| 101 } | 162 } |
| OLD | NEW |