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 (
|
2011-11-08 16:43:02 -07:00
|
|
|
"encoding/xml"
|
2011-11-01 20:06:05 -06:00
|
|
|
"errors"
|
2010-09-12 18:46:17 -06:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"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"
|
|
|
|
hgUrl = "https://go.googlecode.com/hg/"
|
|
|
|
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{
|
|
|
|
"GOHOSTOS",
|
|
|
|
"GOHOSTARCH",
|
|
|
|
"PATH",
|
|
|
|
"DISABLE_NET_TESTS",
|
2011-04-14 20:35:19 -06:00
|
|
|
"MAKEFLAGS",
|
2011-03-02 20:41:09 -07:00
|
|
|
"GOARM",
|
|
|
|
}
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
type Builder struct {
|
|
|
|
name string
|
|
|
|
goos, goarch string
|
|
|
|
key string
|
|
|
|
codeUsername string
|
|
|
|
codePassword string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2010-12-05 16:34:39 -07:00
|
|
|
buildroot = flag.String("buildroot", path.Join(os.TempDir(), "gobuilder"), "Directory under which to build")
|
2011-05-19 18:05:35 -06:00
|
|
|
commitFlag = flag.Bool("commit", false, "upload information about new commits")
|
2010-09-12 18:46:17 -06:00
|
|
|
dashboard = flag.String("dashboard", "godashboard.appspot.com", "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")
|
2010-09-21 23:18:41 -06:00
|
|
|
buildCmd = flag.String("cmd", "./all.bash", "Build command (specify absolute or relative to go/src/)")
|
2011-03-02 20:41:09 -07:00
|
|
|
external = flag.Bool("external", false, "Build external packages")
|
2011-04-26 18:12:10 -06:00
|
|
|
parallel = flag.Bool("parallel", false, "Build multiple targets in parallel")
|
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\-.]+`)
|
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()
|
|
|
|
}
|
2010-12-05 16:34:39 -07:00
|
|
|
goroot = path.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
|
|
|
|
}
|
2011-03-02 20:41:09 -07:00
|
|
|
|
|
|
|
// set up work environment
|
2010-12-05 16:34:39 -07:00
|
|
|
if err := os.RemoveAll(*buildroot); err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatalf("Error removing build root (%s): %s", *buildroot, err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2010-12-05 16:34:39 -07:00
|
|
|
if err := os.Mkdir(*buildroot, mkdirPerm); err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatalf("Error making build root (%s): %s", *buildroot, err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2010-12-05 16:34:39 -07:00
|
|
|
if err := run(nil, *buildroot, "hg", "clone", hgUrl, goroot); err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
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-05-12 09:21:34 -06:00
|
|
|
hash, err := fullHash(*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
|
|
|
|
|
|
|
// external package build mode
|
|
|
|
if *external {
|
|
|
|
if len(builders) != 1 {
|
|
|
|
log.Fatal("only one goos-goarch should be specified with -external")
|
|
|
|
}
|
|
|
|
builders[0].buildExternal()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
fn := path.Join(os.Getenv("HOME"), ".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
|
|
|
}
|
2011-06-27 17:43:14 -06:00
|
|
|
v := strings.Split(string(c), "\n")
|
2010-09-12 18:46:17 -06:00
|
|
|
b.key = v[0]
|
|
|
|
if len(v) >= 3 {
|
|
|
|
b.codeUsername, b.codePassword = v[1], v[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2011-03-02 20:41:09 -07:00
|
|
|
// buildExternal downloads and builds external packages, and
|
|
|
|
// reports their build status to the dashboard.
|
|
|
|
// It will re-build all packages after pkgBuildInterval nanoseconds or
|
|
|
|
// a new release tag is found.
|
|
|
|
func (b *Builder) buildExternal() {
|
|
|
|
var prevTag string
|
2011-11-30 10:01:46 -07:00
|
|
|
var nextBuild time.Time
|
2011-03-02 20:41:09 -07:00
|
|
|
for {
|
|
|
|
time.Sleep(waitInterval)
|
|
|
|
err := run(nil, goroot, "hg", "pull", "-u")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("hg pull failed:", err)
|
|
|
|
continue
|
|
|
|
}
|
2011-07-01 22:02:42 -06:00
|
|
|
hash, tag, err := firstTag(releaseRe)
|
2011-03-02 20:41:09 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if *verbose {
|
|
|
|
log.Println("latest release:", tag)
|
|
|
|
}
|
|
|
|
// don't rebuild if there's no new release
|
|
|
|
// and it's been less than pkgBuildInterval
|
|
|
|
// nanoseconds since the last build.
|
2011-11-30 10:01:46 -07:00
|
|
|
if tag == prevTag && time.Now().Before(nextBuild) {
|
2011-03-02 20:41:09 -07:00
|
|
|
continue
|
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
// build will also build the packages
|
|
|
|
if err := b.buildHash(hash); err != nil {
|
2011-03-02 20:41:09 -07:00
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
prevTag = tag
|
2011-11-30 10:01:46 -07:00
|
|
|
nextBuild = time.Now().Add(pkgBuildInterval)
|
2011-03-02 20:41:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
// build checks for a new commit for this builder
|
|
|
|
// and builds it if one is found.
|
|
|
|
// It returns true if a build was attempted.
|
|
|
|
func (b *Builder) build() bool {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err != nil {
|
2010-10-20 17:46:10 -06:00
|
|
|
log.Println(b.name, "build:", err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
}()
|
2011-05-12 09:21:34 -06:00
|
|
|
hash, err := b.todo()
|
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-05-19 18:05:35 -06:00
|
|
|
|
2011-05-13 10:14:31 -06:00
|
|
|
if _, err := fullHash(hash[:12]); err != nil {
|
|
|
|
// Don't have hash, so run hg pull.
|
|
|
|
if err := run(nil, goroot, "hg", "pull"); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func (b *Builder) buildHash(hash string) (err error) {
|
2010-09-12 18:46:17 -06:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2011-05-12 09:21:34 -06:00
|
|
|
err = fmt.Errorf("%s build: %s: %s", b.name, hash, err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
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
|
2011-05-12 09:21:34 -06:00
|
|
|
workpath := path.Join(*buildroot, b.name+"-"+hash[:12])
|
2010-09-12 18:46:17 -06:00
|
|
|
err = os.Mkdir(workpath, mkdirPerm)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
|
err = run(nil, workpath, "hg", "clone", goroot, "go")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// update to specified revision
|
2010-09-29 19:59:36 -06:00
|
|
|
err = run(nil, path.Join(workpath, "go"),
|
2011-05-12 09:21:34 -06:00
|
|
|
"hg", "update", hash)
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
srcDir := path.Join(workpath, "go", "src")
|
|
|
|
|
2010-09-21 04:32:36 -06:00
|
|
|
// build
|
2010-10-20 22:33:31 -06:00
|
|
|
logfile := path.Join(workpath, "build.log")
|
2011-03-02 20:41:09 -07:00
|
|
|
buildLog, status, err := runLog(b.envv(), logfile, srcDir, *buildCmd)
|
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
|
|
|
|
|
|
|
// if we're in external mode, build all packages and return
|
|
|
|
if *external {
|
|
|
|
if status != 0 {
|
2011-11-01 20:06:05 -06:00
|
|
|
return errors.New("go build failed")
|
2011-03-02 20:41:09 -07:00
|
|
|
}
|
2011-05-12 09:21:34 -06:00
|
|
|
return b.buildPackages(workpath, hash)
|
2011-03-02 20:41:09 -07:00
|
|
|
}
|
|
|
|
|
2010-09-12 18:46:17 -06:00
|
|
|
if status != 0 {
|
|
|
|
// record failure
|
2011-05-12 09:21:34 -06:00
|
|
|
return b.recordResult(buildLog, hash)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// record success
|
2011-05-12 09:21:34 -06:00
|
|
|
if err = b.recordResult("", hash); err != nil {
|
2010-09-30 22:14:18 -06:00
|
|
|
return fmt.Errorf("recordResult: %s", err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// finish here if codeUsername and codePassword aren't set
|
|
|
|
if b.codeUsername == "" || b.codePassword == "" || !*buildRelease {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this is a release, create tgz and upload to google code
|
2011-07-01 22:02:42 -06:00
|
|
|
releaseHash, release, err := firstTag(binaryTagRe)
|
2011-05-12 09:21:34 -06:00
|
|
|
if hash == releaseHash {
|
2010-09-12 18:46:17 -06:00
|
|
|
// clean out build state
|
2011-03-02 20:41:09 -07:00
|
|
|
err = run(b.envv(), srcDir, "./clean.bash", "--nopkg")
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
2010-09-30 22:14:18 -06:00
|
|
|
return fmt.Errorf("clean.bash: %s", err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
// upload binary release
|
2011-01-03 19:16:38 -07:00
|
|
|
fn := fmt.Sprintf("go.%s.%s-%s.tar.gz", release, b.goos, b.goarch)
|
2010-09-21 04:32:36 -06:00
|
|
|
err = run(nil, workpath, "tar", "czf", fn, "go")
|
2010-09-12 18:46:17 -06:00
|
|
|
if err != nil {
|
2010-09-30 22:14:18 -06:00
|
|
|
return fmt.Errorf("tar: %s", err)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2010-10-20 22:33:31 -06:00
|
|
|
err = run(nil, workpath, path.Join(goroot, codePyScript),
|
2010-09-21 04:32:36 -06:00
|
|
|
"-s", release,
|
|
|
|
"-p", codeProject,
|
|
|
|
"-u", b.codeUsername,
|
|
|
|
"-w", b.codePassword,
|
|
|
|
"-l", fmt.Sprintf("%s,%s", b.goos, b.goarch),
|
|
|
|
fn)
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
2010-09-21 04:32:36 -06:00
|
|
|
|
|
|
|
return
|
2010-09-12 18:46:17 -06:00
|
|
|
}
|
|
|
|
|
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 {
|
2011-06-20 20:26:38 -06:00
|
|
|
s, err := os.Getenverror(k)
|
|
|
|
if err == nil {
|
|
|
|
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,
|
|
|
|
"GOROOT_FINAL": "/c/go",
|
2011-06-20 20:26:38 -06:00
|
|
|
// TODO(brainman): remove once we find make that does not hang.
|
|
|
|
"MAKEFLAGS": "-j1",
|
2011-05-29 19:20:46 -06:00
|
|
|
}
|
|
|
|
for _, name := range extraEnv {
|
2011-06-20 20:26:38 -06:00
|
|
|
s, err := os.Getenverror(name)
|
|
|
|
if err == nil {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
if *verbose {
|
|
|
|
log.Printf("poll...")
|
|
|
|
}
|
|
|
|
commitPoll(b.key)
|
|
|
|
if *verbose {
|
|
|
|
log.Printf("sleep...")
|
|
|
|
}
|
|
|
|
time.Sleep(60e9)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 = `
|
|
|
|
<log>
|
|
|
|
<hash>{node|escape}</hash>
|
|
|
|
<parent>{parent|escape}</parent>
|
|
|
|
<author>{author|escape}</author>
|
|
|
|
<date>{date}</date>
|
|
|
|
<desc>{desc|escape}</desc>
|
|
|
|
</log>
|
|
|
|
`
|
|
|
|
|
|
|
|
// commitPoll pulls any new revisions from the hg server
|
|
|
|
// and tells the server about them.
|
|
|
|
func commitPoll(key string) {
|
|
|
|
// Catch unexpected panics.
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
log.Printf("commitPoll panic: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := run(nil, goroot, "hg", "pull"); err != nil {
|
|
|
|
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
|
|
|
|
|
|
|
data, _, err := runLog(nil, "", goroot, "hg", "log",
|
|
|
|
"--encoding=utf-8",
|
2011-05-19 18:05:35 -06:00
|
|
|
"--limit="+strconv.Itoa(N),
|
|
|
|
"--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
|
|
|
|
}
|
2011-05-19 18:05:35 -06:00
|
|
|
err = xml.Unmarshal(strings.NewReader("<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 != "" {
|
|
|
|
l.Parent, _ = fullHash(l.Parent)
|
|
|
|
}
|
2011-08-23 20:39:39 -06:00
|
|
|
log.Printf("hg log: %s < %s\n", l.Hash, l.Parent)
|
2011-05-12 09:21:34 -06:00
|
|
|
if l.Parent == "" {
|
|
|
|
// Can't create node without parent.
|
|
|
|
continue
|
|
|
|
}
|
2011-05-19 18:05:35 -06:00
|
|
|
|
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]
|
|
|
|
if l.Parent == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addCommit(l.Hash, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func addCommit(hash, key string) bool {
|
|
|
|
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.
|
|
|
|
if dashboardCommit(hash) {
|
|
|
|
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.
|
|
|
|
if !addCommit(l.Parent, key) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create commit.
|
|
|
|
if err := postCommit(key, l); err != nil {
|
2011-05-30 02:02:59 -06:00
|
|
|
log.Printf("failed to add %s to dashboard: %v", key, err)
|
2011-05-12 09:21:34 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// fullHash returns the full hash for the given Mercurial revision.
|
2011-11-01 20:06:05 -06:00
|
|
|
func fullHash(rev string) (hash string, err error) {
|
2011-05-12 09:21:34 -06:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("fullHash: %s: %s", rev, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
s, _, err := runLog(nil, "", goroot,
|
|
|
|
"hg", "log",
|
|
|
|
"--encoding=utf-8",
|
|
|
|
"--rev="+rev,
|
|
|
|
"--limit=1",
|
|
|
|
"--template={node}",
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
var revisionRe = regexp.MustCompile(`^([^ ]+) +[0-9]+:([0-9a-f]+)$`)
|
|
|
|
|
|
|
|
// firstTag returns the hash and tag of the most recent tag matching re.
|
2011-11-01 20:06:05 -06:00
|
|
|
func firstTag(re *regexp.Regexp) (hash string, tag string, err error) {
|
2011-05-12 09:21:34 -06:00
|
|
|
o, _, err := runLog(nil, "", goroot, "hg", "tags")
|
2011-06-27 17:43:14 -06:00
|
|
|
for _, l := range strings.Split(o, "\n") {
|
2011-05-12 09:21:34 -06:00
|
|
|
if l == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s := revisionRe.FindStringSubmatch(l)
|
|
|
|
if s == nil {
|
2011-11-01 20:06:05 -06:00
|
|
|
err = errors.New("couldn't find revision number")
|
2011-05-12 09:21:34 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !re.MatchString(s[1]) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tag = s[1]
|
2011-07-01 22:02:42 -06:00
|
|
|
hash, err = fullHash(s[2])
|
2011-05-12 09:21:34 -06:00
|
|
|
return
|
|
|
|
}
|
2011-11-01 20:06:05 -06:00
|
|
|
err = errors.New("no matching tag found")
|
2011-05-12 09:21:34 -06:00
|
|
|
return
|
|
|
|
}
|