2011-03-02 20:41:09 -07:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-03-08 03:54:46 -07:00
|
|
|
"bytes"
|
2011-11-08 16:43:02 -07:00
|
|
|
"encoding/xml"
|
2010-09-12 18:46:17 -06:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2011-12-04 22:44:10 -07:00
|
|
|
"path/filepath"
|
2010-09-12 18:46:17 -06:00
|
|
|
"regexp"
|
2011-05-29 19:20:46 -06:00
|
|
|
"runtime"
|
2010-09-12 18:46:17 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2011-03-02 20:41:09 -07:00
|
|
|
codeProject = "go"
|
|
|
|
codePyScript = "misc/dashboard/googlecode_upload.py"
|
2012-03-05 08:16:11 -07:00
|
|
|
hgUrl = "https://code.google.com/p/go/"
|
2011-03-02 20:41:09 -07:00
|
|
|
mkdirPerm = 0750
|
2011-11-30 10:01:46 -07:00
|
|
|
waitInterval = 30 * time.Second // time to wait before checking for new revs
|
|
|
|
pkgBuildInterval = 24 * time.Hour // rebuild packages every 24 hours
|
2010-09-12 18:46:17 -06:00
|
|
|
)
|
|
|
|
|
2011-03-02 20:41:09 -07:00
|
|
|
// These variables are copied from the gobuilder's environment
|
|
|
|
// to the envv of its subprocesses.
|
|
|
|
var extraEnv = []string{
|
2012-01-10 21:44:20 -07:00
|
|
|
"GOARM",
|
2011-03-02 20:41:09 -07:00
|
|
|
"GOHOSTARCH",
|
2012-01-10 21:44:20 -07:00
|
|
|
"GOHOSTOS",
|
2011-03-02 20:41:09 -07:00
|
|
|
"PATH",
|
2012-01-10 21:44:20 -07:00
|
|
|
"TMPDIR",
|
2011-03-02 20:41:09 -07:00
|
|
|
}
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
type Builder struct {
|
|
|
|
name string
|
|
|
|
goos, goarch string
|
|
|
|
key string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2012-09-13 20:53:30 -06:00
|
|
|
buildroot = flag.String("buildroot", defaultBuildRoot(), "Directory under which to build")
|
2011-05-19 18:05:35 -06:00
|
|
|
commitFlag = flag.Bool("commit", false, "upload information about new commits")
|
2011-12-20 23:12:16 -07:00
|
|
|
dashboard = flag.String("dashboard", "build.golang.org", "Go Dashboard Host")
|
2010-09-21 04:32:36 -06:00
|
|
|
buildRelease = flag.Bool("release", false, "Build and upload binary release archives")
|
|
|
|
buildRevision = flag.String("rev", "", "Build specified revision and exit")
|
2012-02-08 20:52:01 -07:00
|
|
|
buildCmd = flag.String("cmd", filepath.Join(".", allCmd), "Build command (specify relative to go/src/)")
|
2012-02-12 20:32:45 -07:00
|
|
|
failAll = flag.Bool("fail", false, "fail all builds")
|
2011-04-26 18:12:10 -06:00
|
|
|
parallel = flag.Bool("parallel", false, "Build multiple targets in parallel")
|
2012-09-17 11:41:47 -06:00
|
|
|
buildTimeout = flag.Duration("buildTimeout", 60*time.Minute, "Maximum time to wait for builds and tests")
|
2012-09-17 12:05:43 -06:00
|
|
|
cmdTimeout = flag.Duration("cmdTimeout", 5*time.Minute, "Maximum time to wait for an external command")
|
2011-03-02 20:41:09 -07:00
|
|
|
verbose = flag.Bool("v", false, "verbose")
|
2010-09-12 18:46:17 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2011-07-01 22:02:42 -06:00
|
|
|
goroot string
|
|
|
|
binaryTagRe = regexp.MustCompile(`^(release\.r|weekly\.)[0-9\-.]+`)
|
|
|
|
releaseRe = regexp.MustCompile(`^release\.r[0-9\-.]+`)
|
2012-02-08 20:52:01 -07:00
|
|
|
allCmd = "all" + suffix
|
|
|
|
cleanCmd = "clean" + suffix
|
|
|
|
suffix = defaultSuffix()
|
2010-09-12 18:46:17 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Usage = func() {
|
|
|
|
fmt.Fprintf(os.Stderr, "usage: %s goos-goarch...\n", os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
flag.Parse()
|
2011-05-12 09:21:34 -06:00
|
|
|
if len(flag.Args()) == 0 && !*commitFlag {
|
2010-09-12 18:46:17 -06:00
|
|
|
flag.Usage()
|
|
|
|
}
|
2012-02-08 20:52:01 -07:00
|
|
|
goroot = filepath.Join(*buildroot, "goroot")
|
2010-09-12 18:46:17 -06:00
|
|
|
builders := make([]*Builder, len(flag.Args()))
|
|
|
|
for i, builder := range flag.Args() {
|
|
|
|
b, err := NewBuilder(builder)
|
|
|
|
if err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal(err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
builders[i] = b
|
|
|
|
}
|
2012-02-12 20:32:45 -07:00
|
|
|
|
2012-02-09 21:46:48 -07:00
|
|
|
if *failAll {
|
|
|
|
failMode(builders)
|
|
|
|
return
|
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
|
2012-02-22 13:39:53 -07:00
|
|
|
// set up work environment, use existing enviroment if possible
|
|
|
|
if hgRepoExists(goroot) {
|
|
|
|
log.Print("Found old workspace, will use it")
|
|
|
|
} else {
|
|
|
|
if err := os.RemoveAll(*buildroot); err != nil {
|
|
|
|
log.Fatalf("Error removing build root (%s): %s", *buildroot, err)
|
|
|
|
}
|
|
|
|
if err := os.Mkdir(*buildroot, mkdirPerm); err != nil {
|
|
|
|
log.Fatalf("Error making build root (%s): %s", *buildroot, err)
|
|
|
|
}
|
|
|
|
if err := hgClone(hgUrl, goroot); err != nil {
|
|
|
|
log.Fatal("Error cloning repository:", err)
|
|
|
|
}
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2011-05-19 18:05:35 -06:00
|
|
|
|
2011-05-12 09:21:34 -06:00
|
|
|
if *commitFlag {
|
|
|
|
if len(flag.Args()) == 0 {
|
|
|
|
commitWatcher()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go commitWatcher()
|
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
|
2010-09-21 04:32:36 -06:00
|
|
|
// if specified, build revision and return
|
|
|
|
if *buildRevision != "" {
|
2011-12-04 22:44:10 -07:00
|
|
|
hash, err := fullHash(goroot, *buildRevision)
|
2010-09-21 04:32:36 -06:00
|
|
|
if err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatal("Error finding revision: ", err)
|
2010-09-21 04:32:36 -06:00
|
|
|
}
|
|
|
|
for _, b := range builders {
|
2011-05-12 09:21:34 -06:00
|
|
|
if err := b.buildHash(hash); err != nil {
|
2010-10-20 17:46:10 -06:00
|
|
|
log.Println(err)
|
2010-09-21 04:32:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
|
|
|
|
// go continuous build mode (default)
|
2010-09-12 18:46:17 -06:00
|
|
|
// check for new commits and build them
|
|
|
|
for {
|
|
|
|
built := false
|
2011-11-30 10:01:46 -07:00
|
|
|
t := time.Now()
|
2011-04-26 18:12:10 -06:00
|
|
|
if *parallel {
|
|
|
|
done := make(chan bool)
|
|
|
|
for _, b := range builders {
|
|
|
|
go func(b *Builder) {
|
|
|
|
done <- b.build()
|
|
|
|
}(b)
|
|
|
|
}
|
|
|
|
for _ = range builders {
|
|
|
|
built = <-done || built
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, b := range builders {
|
|
|
|
built = b.build() || built
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
// sleep if there was nothing to build
|
2010-09-12 18:46:17 -06:00
|
|
|
if !built {
|
2011-05-12 09:21:34 -06:00
|
|
|
time.Sleep(waitInterval)
|
|
|
|
}
|
|
|
|
// sleep if we're looping too fast.
|
2011-11-30 10:01:46 -07:00
|
|
|
dt := time.Now().Sub(t)
|
|
|
|
if dt < waitInterval {
|
|
|
|
time.Sleep(waitInterval - dt)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-09 21:46:48 -07:00
|
|
|
// go continuous fail mode
|
|
|
|
// check for new commits and FAIL them
|
|
|
|
func failMode(builders []*Builder) {
|
|
|
|
for {
|
|
|
|
built := false
|
|
|
|
for _, b := range builders {
|
|
|
|
built = b.failBuild() || built
|
|
|
|
}
|
|
|
|
// stop if there was nothing to fail
|
|
|
|
if !built {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func NewBuilder(builder string) (*Builder, error) {
|
2010-09-12 18:46:17 -06:00
|
|
|
b := &Builder{name: builder}
|
|
|
|
|
|
|
|
// get goos/goarch from builder string
|
2011-06-27 17:43:14 -06:00
|
|
|
s := strings.SplitN(builder, "-", 3)
|
2011-04-14 19:56:56 -06:00
|
|
|
if len(s) >= 2 {
|
2010-09-12 18:46:17 -06:00
|
|
|
b.goos, b.goarch = s[0], s[1]
|
|
|
|
} else {
|
2010-09-30 22:14:18 -06:00
|
|
|
return nil, fmt.Errorf("unsupported builder form: %s", builder)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// read keys from keyfile
|
2012-02-08 20:52:01 -07:00
|
|
|
fn := ""
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
fn = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
|
|
|
|
} else {
|
|
|
|
fn = os.Getenv("HOME")
|
|
|
|
}
|
|
|
|
fn = filepath.Join(fn, ".gobuildkey")
|
2010-09-29 19:59:36 -06:00
|
|
|
if s := fn + "-" + b.name; isFile(s) { // builder-specific file
|
2010-09-12 18:46:17 -06:00
|
|
|
fn = s
|
|
|
|
}
|
|
|
|
c, err := ioutil.ReadFile(fn)
|
|
|
|
if err != nil {
|
2010-09-30 22:14:18 -06:00
|
|
|
return nil, fmt.Errorf("readKeys %s (%s): %s", b.name, fn, err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2012-03-08 03:54:46 -07:00
|
|
|
b.key = string(bytes.TrimSpace(bytes.SplitN(c, []byte("\n"), 2)[0]))
|
2010-09-12 18:46:17 -06:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// build checks for a new commit for this builder
|
2012-10-30 14:38:01 -06:00
|
|
|
// and builds it if one is found.
|
2010-09-12 18:46:17 -06:00
|
|
|
// It returns true if a build was attempted.
|
|
|
|
func (b *Builder) build() bool {
|
2011-12-19 21:30:11 -07:00
|
|
|
hash, err := b.todo("build-go-commit", "", "")
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
2010-10-20 17:46:10 -06:00
|
|
|
log.Println(err)
|
2010-09-12 18:46:17 -06:00
|
|
|
return false
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
if hash == "" {
|
2010-09-12 18:46:17 -06:00
|
|
|
return false
|
|
|
|
}
|
2011-05-13 10:14:31 -06:00
|
|
|
// Look for hash locally before running hg pull.
|
2011-12-04 22:44:10 -07:00
|
|
|
if _, err := fullHash(goroot, hash[:12]); err != nil {
|
2011-05-13 10:14:31 -06:00
|
|
|
// Don't have hash, so run hg pull.
|
2012-09-17 11:41:47 -06:00
|
|
|
if err := run(*cmdTimeout, nil, goroot, hgCmd("pull")...); err != nil {
|
2011-05-13 10:14:31 -06:00
|
|
|
log.Println("hg pull failed:", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
err = b.buildHash(hash)
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
2010-10-20 17:46:10 -06:00
|
|
|
log.Println(err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2012-01-29 20:53:48 -07:00
|
|
|
func (b *Builder) buildHash(hash string) error {
|
2011-05-12 09:21:34 -06:00
|
|
|
log.Println(b.name, "building", hash)
|
2010-10-20 22:33:31 -06:00
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
// create place in which to do work
|
2012-02-08 20:52:01 -07:00
|
|
|
workpath := filepath.Join(*buildroot, b.name+"-"+hash[:12])
|
2012-01-29 20:53:48 -07:00
|
|
|
if err := os.Mkdir(workpath, mkdirPerm); err != nil {
|
|
|
|
return err
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
defer os.RemoveAll(workpath)
|
2010-09-12 18:46:17 -06:00
|
|
|
|
2010-09-21 04:32:36 -06:00
|
|
|
// clone repo
|
2012-09-17 11:41:47 -06:00
|
|
|
if err := run(*cmdTimeout, nil, workpath, hgCmd("clone", goroot, "go")...); err != nil {
|
2012-01-29 20:53:48 -07:00
|
|
|
return err
|
2010-09-21 04:32:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// update to specified revision
|
2012-09-17 11:41:47 -06:00
|
|
|
if err := run(*cmdTimeout, nil, filepath.Join(workpath, "go"), hgCmd("update", hash)...); err != nil {
|
2012-01-29 20:53:48 -07:00
|
|
|
return err
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
2012-02-08 20:52:01 -07:00
|
|
|
srcDir := filepath.Join(workpath, "go", "src")
|
2010-09-12 18:46:17 -06:00
|
|
|
|
2010-09-21 04:32:36 -06:00
|
|
|
// build
|
2012-02-08 20:52:01 -07:00
|
|
|
logfile := filepath.Join(workpath, "build.log")
|
|
|
|
cmd := *buildCmd
|
|
|
|
if !filepath.IsAbs(cmd) {
|
|
|
|
cmd = filepath.Join(srcDir, cmd)
|
|
|
|
}
|
2011-12-20 21:43:12 -07:00
|
|
|
startTime := time.Now()
|
2012-09-17 11:41:47 -06:00
|
|
|
buildLog, status, err := runLog(*buildTimeout, b.envv(), logfile, srcDir, cmd)
|
2011-12-20 21:43:12 -07:00
|
|
|
runTime := time.Now().Sub(startTime)
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
2011-06-06 06:17:28 -06:00
|
|
|
return fmt.Errorf("%s: %s", *buildCmd, err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
if status != 0 {
|
|
|
|
// record failure
|
2011-12-20 21:43:12 -07:00
|
|
|
return b.recordResult(false, "", hash, "", buildLog, runTime)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// record success
|
2011-12-20 21:43:12 -07:00
|
|
|
if err = b.recordResult(true, "", hash, "", "", runTime); err != nil {
|
2010-09-30 22:14:18 -06:00
|
|
|
return fmt.Errorf("recordResult: %s", err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
2012-01-29 18:02:14 -07:00
|
|
|
// build Go sub-repositories
|
|
|
|
b.buildSubrepos(filepath.Join(workpath, "go"), hash)
|
2011-12-07 16:31:06 -07:00
|
|
|
|
2012-01-29 20:53:48 -07:00
|
|
|
return nil
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
2012-02-09 21:46:48 -07:00
|
|
|
// failBuild checks for a new commit for this builder
|
2012-10-30 14:38:01 -06:00
|
|
|
// and fails it if one is found.
|
2012-02-09 21:46:48 -07:00
|
|
|
// It returns true if a build was "attempted".
|
|
|
|
func (b *Builder) failBuild() bool {
|
|
|
|
hash, err := b.todo("build-go-commit", "", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if hash == "" {
|
|
|
|
return false
|
|
|
|
}
|
2012-02-12 20:32:45 -07:00
|
|
|
|
2012-02-09 21:46:48 -07:00
|
|
|
log.Printf("fail %s %s\n", b.name, hash)
|
|
|
|
|
2012-02-12 20:32:45 -07:00
|
|
|
if err := b.recordResult(false, "", hash, "", "auto-fail mode run by "+os.Getenv("USER"), 0); err != nil {
|
2012-02-09 21:46:48 -07:00
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2012-01-29 18:02:14 -07:00
|
|
|
func (b *Builder) buildSubrepos(goRoot, goHash string) {
|
|
|
|
for _, pkg := range dashboardPackages("subrepo") {
|
2011-12-07 16:31:06 -07:00
|
|
|
// get the latest todo for this package
|
2011-12-19 21:30:11 -07:00
|
|
|
hash, err := b.todo("build-package", pkg, goHash)
|
2011-12-07 16:31:06 -07:00
|
|
|
if err != nil {
|
2012-01-29 18:02:14 -07:00
|
|
|
log.Printf("buildSubrepos %s: %v", pkg, err)
|
2011-12-07 16:31:06 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if hash == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2012-01-29 18:02:14 -07:00
|
|
|
// build the package
|
2011-12-07 16:31:06 -07:00
|
|
|
if *verbose {
|
2012-01-29 18:02:14 -07:00
|
|
|
log.Printf("buildSubrepos %s: building %q", pkg, hash)
|
2011-12-07 16:31:06 -07:00
|
|
|
}
|
2012-01-29 18:02:14 -07:00
|
|
|
buildLog, err := b.buildSubrepo(goRoot, pkg, hash)
|
2011-12-07 16:31:06 -07:00
|
|
|
if err != nil {
|
2012-01-29 18:02:14 -07:00
|
|
|
if buildLog == "" {
|
|
|
|
buildLog = err.Error()
|
|
|
|
}
|
|
|
|
log.Printf("buildSubrepos %s: %v", pkg, err)
|
2011-12-07 16:31:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// record the result
|
2012-01-29 18:02:14 -07:00
|
|
|
err = b.recordResult(err == nil, pkg, hash, goHash, buildLog, 0)
|
2011-12-07 16:31:06 -07:00
|
|
|
if err != nil {
|
2012-01-29 18:02:14 -07:00
|
|
|
log.Printf("buildSubrepos %s: %v", pkg, err)
|
2011-12-07 16:31:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 18:02:14 -07:00
|
|
|
// buildSubrepo fetches the given package, updates it to the specified hash,
|
2012-03-05 08:16:11 -07:00
|
|
|
// and runs 'go test -short pkg/...'. It returns the build log and any error.
|
2012-01-29 18:02:14 -07:00
|
|
|
func (b *Builder) buildSubrepo(goRoot, pkg, hash string) (string, error) {
|
|
|
|
goBin := filepath.Join(goRoot, "bin")
|
|
|
|
goTool := filepath.Join(goBin, "go")
|
2011-12-07 16:31:06 -07:00
|
|
|
env := append(b.envv(), "GOROOT="+goRoot)
|
|
|
|
|
2012-01-29 18:02:14 -07:00
|
|
|
// add goBin to PATH
|
|
|
|
for i, e := range env {
|
|
|
|
const p = "PATH="
|
|
|
|
if !strings.HasPrefix(e, p) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
env[i] = p + goBin + string(os.PathListSeparator) + e[len(p):]
|
|
|
|
}
|
|
|
|
|
2011-12-07 16:31:06 -07:00
|
|
|
// fetch package and dependencies
|
2012-09-17 11:41:47 -06:00
|
|
|
log, status, err := runLog(*cmdTimeout, env, "", goRoot, goTool, "get", "-d", pkg)
|
2012-01-29 18:02:14 -07:00
|
|
|
if err == nil && status != 0 {
|
|
|
|
err = fmt.Errorf("go exited with status %d", status)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2012-09-17 12:21:02 -06:00
|
|
|
// 'go get -d' will fail for a subrepo because its top-level
|
|
|
|
// directory does not contain a go package. No matter, just
|
|
|
|
// check whether an hg directory exists and proceed.
|
|
|
|
hgDir := filepath.Join(goRoot, "src/pkg", pkg, ".hg")
|
|
|
|
if fi, e := os.Stat(hgDir); e != nil || !fi.IsDir() {
|
|
|
|
return log, err
|
|
|
|
}
|
2011-12-07 16:31:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// hg update to the specified hash
|
|
|
|
pkgPath := filepath.Join(goRoot, "src/pkg", pkg)
|
2012-09-17 11:41:47 -06:00
|
|
|
if err := run(*cmdTimeout, nil, pkgPath, hgCmd("update", hash)...); err != nil {
|
2011-12-07 16:31:06 -07:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2012-01-29 18:02:14 -07:00
|
|
|
// test the package
|
2012-09-17 11:41:47 -06:00
|
|
|
log, status, err = runLog(*buildTimeout, env, "", goRoot, goTool, "test", "-short", pkg+"/...")
|
2012-01-29 18:02:14 -07:00
|
|
|
if err == nil && status != 0 {
|
|
|
|
err = fmt.Errorf("go exited with status %d", status)
|
|
|
|
}
|
2011-12-07 16:31:06 -07:00
|
|
|
return log, err
|
|
|
|
}
|
|
|
|
|
2011-03-02 20:41:09 -07:00
|
|
|
// envv returns an environment for build/bench execution
|
|
|
|
func (b *Builder) envv() []string {
|
2011-05-29 19:20:46 -06:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return b.envvWindows()
|
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
e := []string{
|
|
|
|
"GOOS=" + b.goos,
|
|
|
|
"GOARCH=" + b.goarch,
|
|
|
|
"GOROOT_FINAL=/usr/local/go",
|
|
|
|
}
|
|
|
|
for _, k := range extraEnv {
|
2012-02-18 22:18:13 -07:00
|
|
|
if s, ok := getenvOk(k); ok {
|
2011-06-20 20:26:38 -06:00
|
|
|
e = append(e, k+"="+s)
|
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2011-05-29 19:20:46 -06:00
|
|
|
// windows version of envv
|
|
|
|
func (b *Builder) envvWindows() []string {
|
|
|
|
start := map[string]string{
|
|
|
|
"GOOS": b.goos,
|
|
|
|
"GOARCH": b.goarch,
|
2012-02-13 23:23:25 -07:00
|
|
|
"GOROOT_FINAL": `c:\go`,
|
2012-02-15 13:25:37 -07:00
|
|
|
"GOBUILDEXIT": "1", // exit all.bat with completion status.
|
2011-05-29 19:20:46 -06:00
|
|
|
}
|
|
|
|
for _, name := range extraEnv {
|
2012-02-18 22:18:13 -07:00
|
|
|
if s, ok := getenvOk(name); ok {
|
2011-06-20 20:26:38 -06:00
|
|
|
start[name] = s
|
|
|
|
}
|
2011-05-29 19:20:46 -06:00
|
|
|
}
|
|
|
|
skip := map[string]bool{
|
|
|
|
"GOBIN": true,
|
|
|
|
"GOROOT": true,
|
|
|
|
"INCLUDE": true,
|
|
|
|
"LIB": true,
|
|
|
|
}
|
|
|
|
var e []string
|
|
|
|
for name, v := range start {
|
|
|
|
e = append(e, name+"="+v)
|
|
|
|
skip[name] = true
|
|
|
|
}
|
|
|
|
for _, kv := range os.Environ() {
|
2011-06-27 17:43:14 -06:00
|
|
|
s := strings.SplitN(kv, "=", 2)
|
2011-05-29 19:20:46 -06:00
|
|
|
name := strings.ToUpper(s[0])
|
|
|
|
switch {
|
|
|
|
case name == "":
|
|
|
|
// variables, like "=C:=C:\", just copy them
|
|
|
|
e = append(e, kv)
|
|
|
|
case !skip[name]:
|
|
|
|
e = append(e, kv)
|
|
|
|
skip[name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
func isDirectory(name string) bool {
|
|
|
|
s, err := os.Stat(name)
|
2011-11-30 10:04:16 -07:00
|
|
|
return err == nil && s.IsDir()
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func isFile(name string) bool {
|
|
|
|
s, err := os.Stat(name)
|
2011-11-30 10:04:16 -07:00
|
|
|
return err == nil && !s.IsDir()
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
|
|
|
|
// commitWatcher polls hg for new commits and tells the dashboard about them.
|
|
|
|
func commitWatcher() {
|
|
|
|
// Create builder just to get master key.
|
|
|
|
b, err := NewBuilder("mercurial-commit")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2011-12-04 22:44:10 -07:00
|
|
|
key := b.key
|
|
|
|
|
2011-05-12 09:21:34 -06:00
|
|
|
for {
|
|
|
|
if *verbose {
|
|
|
|
log.Printf("poll...")
|
|
|
|
}
|
2012-01-29 18:02:14 -07:00
|
|
|
// Main Go repository.
|
2011-12-04 22:44:10 -07:00
|
|
|
commitPoll(key, "")
|
2012-01-29 18:02:14 -07:00
|
|
|
// Go sub-repositories.
|
|
|
|
for _, pkg := range dashboardPackages("subrepo") {
|
2011-12-04 22:44:10 -07:00
|
|
|
commitPoll(key, pkg)
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("sleep...")
|
|
|
|
}
|
|
|
|
time.Sleep(60e9)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-04 22:44:10 -07:00
|
|
|
func hgClone(url, path string) error {
|
2012-09-17 11:41:47 -06:00
|
|
|
return run(*cmdTimeout, nil, *buildroot, hgCmd("clone", url, path)...)
|
2011-12-04 22:44:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func hgRepoExists(path string) bool {
|
|
|
|
fi, err := os.Stat(filepath.Join(path, ".hg"))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return fi.IsDir()
|
|
|
|
}
|
|
|
|
|
2011-05-12 09:21:34 -06:00
|
|
|
// HgLog represents a single Mercurial revision.
|
|
|
|
type HgLog struct {
|
2011-05-19 18:05:35 -06:00
|
|
|
Hash string
|
2011-05-12 09:21:34 -06:00
|
|
|
Author string
|
2011-05-19 18:05:35 -06:00
|
|
|
Date string
|
|
|
|
Desc string
|
2011-05-12 09:21:34 -06:00
|
|
|
Parent string
|
2011-05-19 18:05:35 -06:00
|
|
|
|
2011-05-12 09:21:34 -06:00
|
|
|
// Internal metadata
|
|
|
|
added bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// logByHash is a cache of all Mercurial revisions we know about,
|
|
|
|
// indexed by full hash.
|
|
|
|
var logByHash = map[string]*HgLog{}
|
|
|
|
|
|
|
|
// xmlLogTemplate is a template to pass to Mercurial to make
|
|
|
|
// hg log print the log in valid XML for parsing with xml.Unmarshal.
|
|
|
|
const xmlLogTemplate = `
|
2012-01-23 09:50:39 -07:00
|
|
|
<Log>
|
|
|
|
<Hash>{node|escape}</Hash>
|
|
|
|
<Parent>{parent|escape}</Parent>
|
|
|
|
<Author>{author|escape}</Author>
|
|
|
|
<Date>{date|rfc3339date}</Date>
|
|
|
|
<Desc>{desc|escape}</Desc>
|
|
|
|
</Log>
|
2011-05-12 09:21:34 -06:00
|
|
|
`
|
|
|
|
|
|
|
|
// commitPoll pulls any new revisions from the hg server
|
|
|
|
// and tells the server about them.
|
2011-12-04 22:44:10 -07:00
|
|
|
func commitPoll(key, pkg string) {
|
|
|
|
pkgRoot := goroot
|
|
|
|
|
|
|
|
if pkg != "" {
|
2012-02-08 20:52:01 -07:00
|
|
|
pkgRoot = filepath.Join(*buildroot, pkg)
|
2011-12-04 22:44:10 -07:00
|
|
|
if !hgRepoExists(pkgRoot) {
|
|
|
|
if err := hgClone(repoURL(pkg), pkgRoot); err != nil {
|
|
|
|
log.Printf("%s: hg clone failed: %v", pkg, err)
|
|
|
|
if err := os.RemoveAll(pkgRoot); err != nil {
|
|
|
|
log.Printf("%s: %v", pkg, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-17 11:41:47 -06:00
|
|
|
if err := run(*cmdTimeout, nil, pkgRoot, hgCmd("pull")...); err != nil {
|
2011-05-12 09:21:34 -06:00
|
|
|
log.Printf("hg pull: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2011-05-19 18:05:35 -06:00
|
|
|
|
2011-08-23 20:39:39 -06:00
|
|
|
const N = 50 // how many revisions to grab
|
2011-05-12 09:21:34 -06:00
|
|
|
|
2012-09-17 11:41:47 -06:00
|
|
|
data, _, err := runLog(*cmdTimeout, nil, "", pkgRoot, hgCmd("log",
|
2011-05-12 09:21:34 -06:00
|
|
|
"--encoding=utf-8",
|
2011-05-19 18:05:35 -06:00
|
|
|
"--limit="+strconv.Itoa(N),
|
2012-04-02 04:46:24 -06:00
|
|
|
"--template="+xmlLogTemplate)...,
|
2011-05-12 09:21:34 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("hg log: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2011-05-19 18:05:35 -06:00
|
|
|
|
2011-05-12 09:21:34 -06:00
|
|
|
var logStruct struct {
|
|
|
|
Log []HgLog
|
|
|
|
}
|
2012-01-23 20:10:32 -07:00
|
|
|
err = xml.Unmarshal([]byte("<Top>"+data+"</Top>"), &logStruct)
|
2011-05-12 09:21:34 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("unmarshal hg log: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logs := logStruct.Log
|
|
|
|
|
|
|
|
// Pass 1. Fill in parents and add new log entries to logsByHash.
|
|
|
|
// Empty parent means take parent from next log entry.
|
2011-05-30 02:02:59 -06:00
|
|
|
// Non-empty parent has form 1234:hashhashhash; we want full hash.
|
2011-05-12 09:21:34 -06:00
|
|
|
for i := range logs {
|
|
|
|
l := &logs[i]
|
|
|
|
if l.Parent == "" && i+1 < len(logs) {
|
|
|
|
l.Parent = logs[i+1].Hash
|
|
|
|
} else if l.Parent != "" {
|
2011-12-04 22:44:10 -07:00
|
|
|
l.Parent, _ = fullHash(pkgRoot, l.Parent)
|
2011-05-12 09:21:34 -06:00
|
|
|
}
|
2011-12-04 22:44:10 -07:00
|
|
|
if *verbose {
|
|
|
|
log.Printf("hg log %s: %s < %s\n", pkg, l.Hash, l.Parent)
|
2011-05-12 09:21:34 -06:00
|
|
|
}
|
|
|
|
if logByHash[l.Hash] == nil {
|
|
|
|
// Make copy to avoid pinning entire slice when only one entry is new.
|
|
|
|
t := *l
|
|
|
|
logByHash[t.Hash] = &t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range logs {
|
|
|
|
l := &logs[i]
|
2011-12-04 22:44:10 -07:00
|
|
|
addCommit(pkg, l.Hash, key)
|
2011-05-12 09:21:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addCommit adds the commit with the named hash to the dashboard.
|
|
|
|
// key is the secret key for authentication to the dashboard.
|
|
|
|
// It avoids duplicate effort.
|
2011-12-04 22:44:10 -07:00
|
|
|
func addCommit(pkg, hash, key string) bool {
|
2011-05-12 09:21:34 -06:00
|
|
|
l := logByHash[hash]
|
|
|
|
if l == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if l.added {
|
|
|
|
return true
|
|
|
|
}
|
2011-05-19 18:05:35 -06:00
|
|
|
|
2011-05-12 09:21:34 -06:00
|
|
|
// Check for already added, perhaps in an earlier run.
|
2011-12-04 22:44:10 -07:00
|
|
|
if dashboardCommit(pkg, hash) {
|
2011-05-12 09:21:34 -06:00
|
|
|
log.Printf("%s already on dashboard\n", hash)
|
|
|
|
// Record that this hash is on the dashboard,
|
|
|
|
// as must be all its parents.
|
|
|
|
for l != nil {
|
|
|
|
l.added = true
|
|
|
|
l = logByHash[l.Parent]
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create parent first, to maintain some semblance of order.
|
2011-12-04 22:44:10 -07:00
|
|
|
if l.Parent != "" {
|
|
|
|
if !addCommit(pkg, l.Parent, key) {
|
|
|
|
return false
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create commit.
|
2011-12-18 22:57:03 -07:00
|
|
|
if err := postCommit(key, pkg, l); err != nil {
|
|
|
|
log.Printf("failed to add %s to dashboard: %v", key, err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2011-05-12 09:21:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// fullHash returns the full hash for the given Mercurial revision.
|
2012-01-29 20:53:48 -07:00
|
|
|
func fullHash(root, rev string) (string, error) {
|
2012-09-17 11:41:47 -06:00
|
|
|
s, _, err := runLog(*cmdTimeout, nil, "", root,
|
2012-04-02 04:46:24 -06:00
|
|
|
hgCmd("log",
|
|
|
|
"--encoding=utf-8",
|
|
|
|
"--rev="+rev,
|
|
|
|
"--limit=1",
|
|
|
|
"--template={node}")...,
|
2011-05-12 09:21:34 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
2012-01-29 20:53:48 -07:00
|
|
|
return "", nil
|
2011-05-12 09:21:34 -06:00
|
|
|
}
|
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
if s == "" {
|
|
|
|
return "", fmt.Errorf("cannot find revision")
|
|
|
|
}
|
2011-07-01 22:02:42 -06:00
|
|
|
if len(s) != 40 {
|
2011-05-12 09:21:34 -06:00
|
|
|
return "", fmt.Errorf("hg returned invalid hash " + s)
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2011-12-04 22:44:10 -07:00
|
|
|
var repoRe = regexp.MustCompile(`^code\.google\.com/p/([a-z0-9\-]+(\.[a-z0-9\-]+)?)(/[a-z0-9A-Z_.\-/]+)?$`)
|
|
|
|
|
|
|
|
// repoURL returns the repository URL for the supplied import path.
|
|
|
|
func repoURL(importPath string) string {
|
|
|
|
m := repoRe.FindStringSubmatch(importPath)
|
|
|
|
if len(m) < 2 {
|
|
|
|
log.Printf("repoURL: couldn't decipher %q", importPath)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return "https://code.google.com/p/" + m[1]
|
|
|
|
}
|
2012-02-08 20:52:01 -07:00
|
|
|
|
|
|
|
// defaultSuffix returns file extension used for command files in
|
|
|
|
// current os environment.
|
|
|
|
func defaultSuffix() string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return ".bat"
|
|
|
|
}
|
|
|
|
return ".bash"
|
|
|
|
}
|
2012-02-18 22:18:13 -07:00
|
|
|
|
2012-09-13 20:53:30 -06:00
|
|
|
// defaultBuildRoot returns default buildroot directory.
|
|
|
|
func defaultBuildRoot() string {
|
|
|
|
var d string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// will use c:\, otherwise absolute paths become too long
|
|
|
|
// during builder run, see http://golang.org/issue/3358.
|
|
|
|
d = `c:\`
|
|
|
|
} else {
|
|
|
|
d = os.TempDir()
|
|
|
|
}
|
|
|
|
return filepath.Join(d, "gobuilder")
|
|
|
|
}
|
|
|
|
|
2012-02-18 22:18:13 -07:00
|
|
|
func getenvOk(k string) (v string, ok bool) {
|
|
|
|
v = os.Getenv(k)
|
|
|
|
if v != "" {
|
|
|
|
return v, true
|
|
|
|
}
|
|
|
|
keq := k + "="
|
|
|
|
for _, kv := range os.Environ() {
|
|
|
|
if kv == keq {
|
|
|
|
return "", true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
2012-04-02 04:46:24 -06:00
|
|
|
|
|
|
|
func hgCmd(args ...string) []string {
|
|
|
|
return append([]string{"hg", "--config", "extensions.codereview=!"}, args...)
|
|
|
|
}
|