| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package main | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "strings" | |
| 10 | |
| 11 "github.com/luci/luci-go/common/errors" | |
| 12 "github.com/luci/luci-go/deploytool/api/deploy" | |
| 13 "github.com/luci/luci-go/deploytool/managedfs" | |
| 14 ) | |
| 15 | |
| 16 func flattenVarToDir(v string) string { | |
| 17 return strings.Map(func(r rune) rune { | |
| 18 switch { | |
| 19 case (r >= 'a' && r <= 'z'), | |
| 20 (r >= 'A' && r <= 'Z'), | |
| 21 (r >= '0' && r <= '9'), | |
| 22 (r == '.'), (r == '_'): | |
| 23 return r | |
| 24 | |
| 25 default: | |
| 26 return '_' | |
| 27 } | |
| 28 }, v) | |
| 29 } | |
| 30 | |
| 31 // buildComponent runs the varuous build scripts defined by the specified | |
| 32 // deployment component. | |
| 33 // | |
| 34 // After running the build scripts, variable substitution will occur on "c" | |
| 35 // to update its directory. | |
| 36 func buildComponent(w *work, c *layoutDeploymentComponent, root *managedfs.Dir)
error { | |
| 37 if src := c.source(); !src.Source.RunScripts { | |
| 38 return errors.Reason("refusing to run scripts (run_scripts is fa
lse for %(source)q)"). | |
| 39 D("source", src.title).Err() | |
| 40 } | |
| 41 | |
| 42 // Create our build directories and map them to variables. | |
| 43 c.buildDirs = make(map[string]string, len(c.Build)) | |
| 44 dirs := make([]*managedfs.Dir, len(c.Build)) | |
| 45 for i, b := range c.Build { | |
| 46 if _, has := c.buildDirs[b.DirKey]; has { | |
| 47 return errors.Reason("duplicate build key [%(key)s]").D(
"key", b.DirKey).Err() | |
| 48 } | |
| 49 | |
| 50 dir, err := root.EnsureDirectory(fmt.Sprintf("%d_%s", i, flatten
VarToDir(b.DirKey))) | |
| 51 if err != nil { | |
| 52 return errors.Annotate(err).Reason("failed to create bui
ld directory").Err() | |
| 53 } | |
| 54 | |
| 55 dirs[i] = dir | |
| 56 c.buildDirs[b.DirKey] = dir.String() | |
| 57 } | |
| 58 | |
| 59 // Apply build directories. | |
| 60 if err := c.expandPaths(); err != nil { | |
| 61 return errors.Annotate(err).Reason("failed to expand component p
aths").Err() | |
| 62 } | |
| 63 | |
| 64 // Run all of our build scripts in parallel. | |
| 65 err := w.RunMulti(func(workC chan<- func() error) { | |
| 66 for i, b := range c.Build { | |
| 67 i, b := i, b | |
| 68 workC <- func() error { | |
| 69 return runComponentBuild(w, c, b, dirs[i]) | |
| 70 } | |
| 71 } | |
| 72 }) | |
| 73 if err != nil { | |
| 74 return errors.Annotate(err).Reason("failed to run component buil
ds").Err() | |
| 75 } | |
| 76 return nil | |
| 77 } | |
| 78 | |
| 79 func runComponentBuild(w *work, c *layoutDeploymentComponent, b *deploy.Componen
t_Build, root *managedfs.Dir) error { | |
| 80 switch t := b.Operation.(type) { | |
| 81 case *deploy.Component_Build_PythonScript_: | |
| 82 ps := t.PythonScript | |
| 83 | |
| 84 python, err := w.python() | |
| 85 if err != nil { | |
| 86 return err | |
| 87 } | |
| 88 | |
| 89 src := c.source() | |
| 90 scriptPath := src.pathTo(ps.Path, c.comp.Path) | |
| 91 buildDir := root.String() | |
| 92 | |
| 93 args := append(make([]string, 0, 2+len(ps.ExtraArgs)), | |
| 94 src.checkoutPath(), | |
| 95 buildDir) | |
| 96 args = append(args, ps.ExtraArgs...) | |
| 97 | |
| 98 if err := python.exec(scriptPath, args...).cwd(buildDir).check(w
); err != nil { | |
| 99 return errors.Annotate(err).Reason("failed to execute [%
(scriptPath)s]").D("scriptPath", scriptPath).Err() | |
| 100 } | |
| 101 return nil | |
| 102 | |
| 103 default: | |
| 104 return errors.Reason("unknown build type %(type)T").D("type", t)
.Err() | |
| 105 } | |
| 106 } | |
| OLD | NEW |