OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package cipd | 5 package local |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "io" | 9 "io" |
10 "io/ioutil" | 10 "io/ioutil" |
11 "path/filepath" | 11 "path/filepath" |
12 "regexp" | 12 "regexp" |
13 "sort" | 13 "sort" |
14 | 14 |
15 "github.com/go-yaml/yaml" | 15 "github.com/go-yaml/yaml" |
| 16 |
| 17 "infra/tools/cipd/common" |
16 ) | 18 ) |
17 | 19 |
18 // PackageDef defines how exactly to build a package: what files to put into it, | 20 // PackageDef defines how exactly to build a package: what files to put into it, |
19 // how to name them, how to name the package itself, etc. It is loaded from | 21 // how to name them, how to name the package itself, etc. It is loaded from |
20 // *.yaml file. | 22 // *.yaml file. |
21 type PackageDef struct { | 23 type PackageDef struct { |
22 // Package defines a name of the package. | 24 // Package defines a name of the package. |
23 Package string | 25 Package string |
24 // Root defines where to search for files, relative to package file itse
lf. | 26 // Root defines where to search for files, relative to package file itse
lf. |
25 Root string | 27 Root string |
(...skipping 28 matching lines...) Expand all Loading... |
54 | 56 |
55 // Substitute variables in all strings. | 57 // Substitute variables in all strings. |
56 for _, str := range out.strings() { | 58 for _, str := range out.strings() { |
57 *str, err = subVars(*str, vars) | 59 *str, err = subVars(*str, vars) |
58 if err != nil { | 60 if err != nil { |
59 return | 61 return |
60 } | 62 } |
61 } | 63 } |
62 | 64 |
63 // Validate package name right away. | 65 // Validate package name right away. |
64 » err = ValidatePackageName(out.Package) | 66 » err = common.ValidatePackageName(out.Package) |
65 if err != nil { | 67 if err != nil { |
66 return | 68 return |
67 } | 69 } |
68 | 70 |
69 // Make sure "file" or "dir" are used, but not both. | 71 // Make sure "file" or "dir" are used, but not both. |
70 for i, chunk := range out.Data { | 72 for i, chunk := range out.Data { |
71 if chunk.File == "" && chunk.Dir == "" { | 73 if chunk.File == "" && chunk.Dir == "" { |
72 return out, fmt.Errorf("files entry #%d needs 'file' or
'dir' key", i) | 74 return out, fmt.Errorf("files entry #%d needs 'file' or
'dir' key", i) |
73 } | 75 } |
74 if chunk.File != "" && chunk.Dir != "" { | 76 if chunk.File != "" && chunk.Dir != "" { |
(...skipping 26 matching lines...) Expand all Loading... |
101 } | 103 } |
102 | 104 |
103 // Used to skip duplicates. | 105 // Used to skip duplicates. |
104 seen := map[string]File{} | 106 seen := map[string]File{} |
105 add := func(f File) { | 107 add := func(f File) { |
106 if seen[f.Name()] == nil { | 108 if seen[f.Name()] == nil { |
107 seen[f.Name()] = f | 109 seen[f.Name()] = f |
108 } | 110 } |
109 } | 111 } |
110 | 112 |
111 » log.Info("Enumerating files to zip...") | 113 » log.Infof("Enumerating files to zip...") |
112 for _, chunk := range def.Data { | 114 for _, chunk := range def.Data { |
113 // Individual file. | 115 // Individual file. |
114 if chunk.File != "" { | 116 if chunk.File != "" { |
115 file, err := WrapFile(makeAbs(chunk.File), root, nil) | 117 file, err := WrapFile(makeAbs(chunk.File), root, nil) |
116 if err != nil { | 118 if err != nil { |
117 return nil, err | 119 return nil, err |
118 } | 120 } |
119 add(file) | 121 add(file) |
120 continue | 122 continue |
121 } | 123 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 re, err := regexp.Compile(expr) | 186 re, err := regexp.Compile(expr) |
185 if err != nil { | 187 if err != nil { |
186 return nil, err | 188 return nil, err |
187 } | 189 } |
188 exps = append(exps, re) | 190 exps = append(exps, re) |
189 } | 191 } |
190 | 192 |
191 return func(abs string) bool { | 193 return func(abs string) bool { |
192 rel, err := filepath.Rel(startDir, abs) | 194 rel, err := filepath.Rel(startDir, abs) |
193 if err != nil { | 195 if err != nil { |
194 » » » log.Warnf("Unexpected error when evaluating %s: %s", abs
, err) | 196 » » » log.Warningf("Unexpected error when evaluating %s: %s",
abs, err) |
195 return true | 197 return true |
196 } | 198 } |
197 // Do not evaluate paths outside of startDir. | 199 // Do not evaluate paths outside of startDir. |
198 rel = filepath.ToSlash(rel) | 200 rel = filepath.ToSlash(rel) |
199 if rel[:3] == "../" { | 201 if rel[:3] == "../" { |
200 return false | 202 return false |
201 } | 203 } |
202 for _, exp := range exps { | 204 for _, exp := range exps { |
203 if exp.MatchString(rel) { | 205 if exp.MatchString(rel) { |
204 return true | 206 return true |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 badKeys = append(badKeys, key) | 254 badKeys = append(badKeys, key) |
253 return match | 255 return match |
254 } | 256 } |
255 return val | 257 return val |
256 }) | 258 }) |
257 if len(badKeys) != 0 { | 259 if len(badKeys) != 0 { |
258 return res, fmt.Errorf("Values for some variables are not provid
ed: %v", badKeys) | 260 return res, fmt.Errorf("Values for some variables are not provid
ed: %v", badKeys) |
259 } | 261 } |
260 return res, nil | 262 return res, nil |
261 } | 263 } |
OLD | NEW |