<commit_before>package weed_server

import (
	"code.google.com/p/weed-fs/go/storage"
	"encoding/json"
	"errors"
	"net/http"
	"strconv"
	"strings"
)

func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
	vid := r.FormValue("volumeId")
	collection := r.FormValue("collection") //optional, but can be faster if too many collections
	commaSep := strings.Index(vid, ",")
	if commaSep > 0 {
		vid = vid[0:commaSep]
	}
	volumeId, err := storage.NewVolumeId(vid)
	if err == nil {
		machines := ms.topo.Lookup(collection, volumeId)
		if machines != nil {
			ret := []map[string]string{}
			for _, dn := range machines {
				ret = append(ret, map[string]string{"url": dn.Url(), "publicUrl": dn.PublicUrl})
			}
			writeJsonQuiet(w, r, map[string]interface{}{"locations": ret})
		} else {
			w.WriteHeader(http.StatusNotFound)
			writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
		}
	} else {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": "unknown volumeId format " + vid})
	}
}

func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
	c, e := strconv.Atoi(r.FormValue("count"))
	if e != nil {
		c = 1
	}
	replication := r.FormValue("replication")
	if replication == "" {
		replication = ms.defaultReplicaPlacement
	}
	collection := r.FormValue("collection")
	dataCenter := r.FormValue("dataCenter")
	replicaPlacement, err := storage.NewReplicaPlacementFromString(replication)
	if err != nil {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
		return
	}

	if ms.topo.GetVolumeLayout(collection, replicaPlacement).GetActiveVolumeCount(dataCenter) <= 0 {
		if ms.topo.FreeSpace() <= 0 {
			w.WriteHeader(http.StatusNotFound)
			writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
			return
		} else {
			ms.vgLock.Lock()
			defer ms.vgLock.Unlock()
			if ms.topo.GetVolumeLayout(collection, replicaPlacement).GetActiveVolumeCount(dataCenter) <= 0 {
				if _, err = ms.vg.AutomaticGrowByType(collection, replicaPlacement, dataCenter, ms.topo); err != nil {
					writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
					return
				}
			}
		}
	}
	fid, count, dn, err := ms.topo.PickForWrite(collection, replicaPlacement, c, dataCenter)
	if err == nil {
		writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
	} else {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
	}
}

func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
	init := r.FormValue("init") == "true"
	ip := r.FormValue("ip")
	if ip == "" {
		ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
	}
	port, _ := strconv.Atoi(r.FormValue("port"))
	maxVolumeCount, _ := strconv.Atoi(r.FormValue("maxVolumeCount"))
	s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
	publicUrl := r.FormValue("publicUrl")
	volumes := new([]storage.VolumeInfo)
	if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
		writeJsonQuiet(w, r, map[string]string{"error": "Cannot unmarshal \"volumes\": " + err.Error()})
		return
	}
	debug(s, "volumes", r.FormValue("volumes"))
	ms.topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
	m := make(map[string]interface{})
	m["VolumeSizeLimit"] = uint64(ms.volumeSizeLimitMB) * 1024 * 1024
	writeJsonQuiet(w, r, m)
}

func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
	m := make(map[string]interface{})
	m["Version"] = ms.version
	m["Topology"] = ms.topo.ToMap()
	writeJsonQuiet(w, r, m)
}

func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
	gcThreshold := r.FormValue("garbageThreshold")
	if gcThreshold == "" {
		gcThreshold = ms.garbageThreshold
	}
	debug("garbageThreshold =", gcThreshold)
	ms.topo.Vacuum(gcThreshold)
	ms.dirStatusHandler(w, r)
}

func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
	count := 0
	replicaPlacement, err := storage.NewReplicaPlacementFromString(r.FormValue("replication"))
	if err == nil {
		if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
			if ms.topo.FreeSpace() < count*replicaPlacement.GetCopyCount() {
				err = errors.New("Only " + strconv.Itoa(ms.topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*replicaPlacement.GetCopyCount()))
			} else {
				count, err = ms.vg.GrowByCountAndType(count, r.FormValue("collection"), replicaPlacement, r.FormValue("dataCneter"), ms.topo)
			}
		} else {
			err = errors.New("parameter count is not found")
		}
	}
	if err != nil {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": "parameter replication " + err.Error()})
	} else {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]interface{}{"count": count})
	}
}

func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
	m := make(map[string]interface{})
	m["Version"] = ms.version
	m["Volumes"] = ms.topo.ToVolumeMap()
	writeJsonQuiet(w, r, m)
}

func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
	vid, _, _, _, _ := parseURLPath(r.URL.Path)
	volumeId, err := storage.NewVolumeId(vid)
	if err != nil {
		debug("parsing error:", err, r.URL.Path)
		return
	}
	machines := ms.topo.Lookup("", volumeId)
	if machines != nil && len(machines) > 0 {
		http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
	} else {
		w.WriteHeader(http.StatusNotFound)
		writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
	}
}

func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
	if ms.IsLeader() {
		submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
	} else {
		submitForClientHandler(w, r, ms.raftServer.Leader())
	}
}
<commit_msg>spelling error with parameter name!<commit_after>package weed_server

import (
	"code.google.com/p/weed-fs/go/storage"
	"encoding/json"
	"errors"
	"net/http"
	"strconv"
	"strings"
)

func (ms *MasterServer) dirLookupHandler(w http.ResponseWriter, r *http.Request) {
	vid := r.FormValue("volumeId")
	collection := r.FormValue("collection") //optional, but can be faster if too many collections
	commaSep := strings.Index(vid, ",")
	if commaSep > 0 {
		vid = vid[0:commaSep]
	}
	volumeId, err := storage.NewVolumeId(vid)
	if err == nil {
		machines := ms.topo.Lookup(collection, volumeId)
		if machines != nil {
			ret := []map[string]string{}
			for _, dn := range machines {
				ret = append(ret, map[string]string{"url": dn.Url(), "publicUrl": dn.PublicUrl})
			}
			writeJsonQuiet(w, r, map[string]interface{}{"locations": ret})
		} else {
			w.WriteHeader(http.StatusNotFound)
			writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
		}
	} else {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": "unknown volumeId format " + vid})
	}
}

func (ms *MasterServer) dirAssignHandler(w http.ResponseWriter, r *http.Request) {
	c, e := strconv.Atoi(r.FormValue("count"))
	if e != nil {
		c = 1
	}
	replication := r.FormValue("replication")
	if replication == "" {
		replication = ms.defaultReplicaPlacement
	}
	collection := r.FormValue("collection")
	dataCenter := r.FormValue("dataCenter")
	replicaPlacement, err := storage.NewReplicaPlacementFromString(replication)
	if err != nil {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
		return
	}

	if ms.topo.GetVolumeLayout(collection, replicaPlacement).GetActiveVolumeCount(dataCenter) <= 0 {
		if ms.topo.FreeSpace() <= 0 {
			w.WriteHeader(http.StatusNotFound)
			writeJsonQuiet(w, r, map[string]string{"error": "No free volumes left!"})
			return
		} else {
			ms.vgLock.Lock()
			defer ms.vgLock.Unlock()
			if ms.topo.GetVolumeLayout(collection, replicaPlacement).GetActiveVolumeCount(dataCenter) <= 0 {
				if _, err = ms.vg.AutomaticGrowByType(collection, replicaPlacement, dataCenter, ms.topo); err != nil {
					writeJsonQuiet(w, r, map[string]string{"error": "Cannot grow volume group! " + err.Error()})
					return
				}
			}
		}
	}
	fid, count, dn, err := ms.topo.PickForWrite(collection, replicaPlacement, c, dataCenter)
	if err == nil {
		writeJsonQuiet(w, r, map[string]interface{}{"fid": fid, "url": dn.Url(), "publicUrl": dn.PublicUrl, "count": count})
	} else {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": err.Error()})
	}
}

func (ms *MasterServer) dirJoinHandler(w http.ResponseWriter, r *http.Request) {
	init := r.FormValue("init") == "true"
	ip := r.FormValue("ip")
	if ip == "" {
		ip = r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")]
	}
	port, _ := strconv.Atoi(r.FormValue("port"))
	maxVolumeCount, _ := strconv.Atoi(r.FormValue("maxVolumeCount"))
	s := r.RemoteAddr[0:strings.Index(r.RemoteAddr, ":")+1] + r.FormValue("port")
	publicUrl := r.FormValue("publicUrl")
	volumes := new([]storage.VolumeInfo)
	if err := json.Unmarshal([]byte(r.FormValue("volumes")), volumes); err != nil {
		writeJsonQuiet(w, r, map[string]string{"error": "Cannot unmarshal \"volumes\": " + err.Error()})
		return
	}
	debug(s, "volumes", r.FormValue("volumes"))
	ms.topo.RegisterVolumes(init, *volumes, ip, port, publicUrl, maxVolumeCount, r.FormValue("dataCenter"), r.FormValue("rack"))
	m := make(map[string]interface{})
	m["VolumeSizeLimit"] = uint64(ms.volumeSizeLimitMB) * 1024 * 1024
	writeJsonQuiet(w, r, m)
}

func (ms *MasterServer) dirStatusHandler(w http.ResponseWriter, r *http.Request) {
	m := make(map[string]interface{})
	m["Version"] = ms.version
	m["Topology"] = ms.topo.ToMap()
	writeJsonQuiet(w, r, m)
}

func (ms *MasterServer) volumeVacuumHandler(w http.ResponseWriter, r *http.Request) {
	gcThreshold := r.FormValue("garbageThreshold")
	if gcThreshold == "" {
		gcThreshold = ms.garbageThreshold
	}
	debug("garbageThreshold =", gcThreshold)
	ms.topo.Vacuum(gcThreshold)
	ms.dirStatusHandler(w, r)
}

func (ms *MasterServer) volumeGrowHandler(w http.ResponseWriter, r *http.Request) {
	count := 0
	replicaPlacement, err := storage.NewReplicaPlacementFromString(r.FormValue("replication"))
	if err == nil {
		if count, err = strconv.Atoi(r.FormValue("count")); err == nil {
			if ms.topo.FreeSpace() < count*replicaPlacement.GetCopyCount() {
				err = errors.New("Only " + strconv.Itoa(ms.topo.FreeSpace()) + " volumes left! Not enough for " + strconv.Itoa(count*replicaPlacement.GetCopyCount()))
			} else {
				count, err = ms.vg.GrowByCountAndType(count, r.FormValue("collection"), replicaPlacement, r.FormValue("dataCenter"), ms.topo)
			}
		} else {
			err = errors.New("parameter count is not found")
		}
	}
	if err != nil {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]string{"error": "parameter replication " + err.Error()})
	} else {
		w.WriteHeader(http.StatusNotAcceptable)
		writeJsonQuiet(w, r, map[string]interface{}{"count": count})
	}
}

func (ms *MasterServer) volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
	m := make(map[string]interface{})
	m["Version"] = ms.version
	m["Volumes"] = ms.topo.ToVolumeMap()
	writeJsonQuiet(w, r, m)
}

func (ms *MasterServer) redirectHandler(w http.ResponseWriter, r *http.Request) {
	vid, _, _, _, _ := parseURLPath(r.URL.Path)
	volumeId, err := storage.NewVolumeId(vid)
	if err != nil {
		debug("parsing error:", err, r.URL.Path)
		return
	}
	machines := ms.topo.Lookup("", volumeId)
	if machines != nil && len(machines) > 0 {
		http.Redirect(w, r, "http://"+machines[0].PublicUrl+r.URL.Path, http.StatusMovedPermanently)
	} else {
		w.WriteHeader(http.StatusNotFound)
		writeJsonQuiet(w, r, map[string]string{"error": "volume id " + volumeId.String() + " not found. "})
	}
}

func (ms *MasterServer) submitFromMasterServerHandler(w http.ResponseWriter, r *http.Request) {
	if ms.IsLeader() {
		submitForClientHandler(w, r, "localhost:"+strconv.Itoa(ms.port))
	} else {
		submitForClientHandler(w, r, ms.raftServer.Leader())
	}
}
<|endoftext|>


<commit_before>package external

import (
	"net/http"

	"github.com/bosssauce/ponzu/content"
	"github.com/bosssauce/ponzu/system/db"
)

// Externalable accepts or rejects external POST requests to /external/posts?type=Review
type Externalable interface {
	Accept() bool
}

func init() {
	http.HandleFunc("/api/external/posts", externalPostsHandler)
}

func externalPostsHandler(res http.ResponseWriter, req *http.Request) {
	if req.Method != http.MethodPost {
		res.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	t := req.URL.Query().Get("type")
	if t == "" {
		res.WriteHeader(http.StatusBadRequest)
		return
	}

	p, found := content.Types[t]
	if !found {
		res.WriteHeader(http.StatusNotFound)
		return
	}

	post := p()

	ext, ok := post.(Externalable)
	if !ok {
		res.WriteHeader(http.StatusInternalServerError)
		return
	}

	if ext.Accept() {
		db.SetContent(t+"_external"+":-1", req.Form)
	}
}
<commit_msg>debugging<commit_after>package external

import (
	"fmt"
	"net/http"

	"github.com/bosssauce/ponzu/content"
	"github.com/bosssauce/ponzu/system/api"
	"github.com/bosssauce/ponzu/system/db"
)

// Externalable accepts or rejects external POST requests to /external/posts?type=Review
type Externalable interface {
	Accept() bool
}

func init() {
	http.HandleFunc("/api/external/posts", api.CORS(externalPostsHandler))
}

func externalPostsHandler(res http.ResponseWriter, req *http.Request) {
	if req.Method != http.MethodPost {
		res.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	t := req.URL.Query().Get("type")
	if t == "" {
		res.WriteHeader(http.StatusBadRequest)
		return
	}

	p, found := content.Types[t]
	if !found {
		fmt.Println(t, content.Types, p)
		res.WriteHeader(http.StatusNotFound)
		return
	}

	post := p()

	ext, ok := post.(Externalable)
	if !ok {
		res.WriteHeader(http.StatusInternalServerError)
		return
	}

	if ext.Accept() {
		db.SetContent(t+"_external"+":-1", req.Form)
	}
}
<|endoftext|>


<commit_before>package common

import (
	"math"
	"math/rand"
)

// Distribution provides an interface to model a statistical distribution.
type Distribution interface {
	Advance()
	Get() float64 // should be idempotent
}

// NormalDistribution models a normal distribution.
type NormalDistribution struct {
	Mean   float64
	StdDev float64

	value float64
}

func ND(mean, stddev float64) *NormalDistribution {
	return &NormalDistribution{Mean: mean, StdDev: stddev}
}

// Advance advances this distribution. Since a normal distribution is
// stateless, this is just overwrites the internal cache value.
func (d *NormalDistribution) Advance() {
	d.value = rand.NormFloat64()*d.StdDev + d.Mean
}

// Get returns the last computed value for this distribution.
func (d *NormalDistribution) Get() float64 {
	return d.value
}

// UniformDistribution models a uniform distribution.
type UniformDistribution struct {
	Low  float64
	High float64

	value float64
}

func UD(low, high float64) *UniformDistribution {
	return &UniformDistribution{Low: low, High: high}
}

// Advance advances this distribution. Since a uniform distribution is
// stateless, this is just overwrites the internal cache value.
func (d *UniformDistribution) Advance() {
	x := rand.Float64() // uniform
	x *= d.High - d.Low
	x += d.Low
	d.value = x
}

// Get computes and returns the next value in the distribution.
func (d *UniformDistribution) Get() float64 {
	return d.value
}

// RandomWalkDistribution is a stateful random walk. Initialize it with an
// underlying distribution, which is used to compute the new step value.
type RandomWalkDistribution struct {
	Step Distribution

	State float64 // optional
}

func WD(step Distribution, state float64) *RandomWalkDistribution {
	return &RandomWalkDistribution{Step: step, State: state}
}

// Advance computes the next value of this distribution and stores it.
func (d *RandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += d.Step.Get()
}

// Get returns the last computed value for this distribution.
func (d *RandomWalkDistribution) Get() float64 {
	return d.State
}

// ClampedRandomWalkDistribution is a stateful random walk, with minimum and
// maximum bounds. Initialize it with a Min, Max, and an underlying
// distribution, which is used to compute the new step value.
type ClampedRandomWalkDistribution struct {
	Step Distribution
	Min  float64
	Max  float64

	State float64 // optional
}

func CWD(step Distribution, min, max, state float64) *ClampedRandomWalkDistribution {
	return &ClampedRandomWalkDistribution{
		Step: step,
		Min:  min,
		Max:  max,

		State: state,
	}
}

// Advance computes the next value of this distribution and stores it.
func (d *ClampedRandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += d.Step.Get()
	if d.State > d.Max {
		d.State = d.Max
	}
	if d.State < d.Min {
		d.State = d.Min
	}
}

// Get returns the last computed value for this distribution.
func (d *ClampedRandomWalkDistribution) Get() float64 {
	return d.State
}

// MonotonicRandomWalkDistribution is a stateful random walk that only
// increases. Initialize it with a Start and an underlying distribution,
// which is used to compute the new step value. The sign of any value of the
// u.d. is always made positive.
type MonotonicRandomWalkDistribution struct {
	Step  Distribution
	State float64
}

// Advance computes the next value of this distribution and stores it.
func (d *MonotonicRandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += math.Abs(d.Step.Get())
}

func (d *MonotonicRandomWalkDistribution) Get() float64 {
	return d.State
}

func MWD(step Distribution, state float64) *MonotonicRandomWalkDistribution {
	return &MonotonicRandomWalkDistribution{Step: step, State: state}
}

// MonotonicUpDownRandomWalkDistribution is a stateful random walk that continually
// increases and decreases. Initialize it with State, Min And Max an underlying distribution,
// which is used to compute the new step value.
type MonotonicUpDownRandomWalkDistribution struct {
	Step      Distribution
	State     float64
	Min       float64
	Max       float64
	direction int //1 or -1
}

// Advance computes the next value of this distribution and stores it.
func (d *MonotonicUpDownRandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += d.Step.Get() * float64(d.direction)
	if d.State < d.Min {
		d.State = d.Min
		d.direction = 1
	} else if d.State > d.Max {
		d.State = d.Max
		d.direction = -1
	}
}

func (d *MonotonicUpDownRandomWalkDistribution) Get() float64 {
	return d.State
}

func MUDWD(step Distribution, min float64, max float64, state float64) *MonotonicUpDownRandomWalkDistribution {
	direction := -1
	if state < max {
		direction = 1
	}
	return &MonotonicUpDownRandomWalkDistribution{Step: step, Min: min, Max: max, State: state, direction: direction}
}

type ConstantDistribution struct {
	State float64
}

func (d *ConstantDistribution) Advance() {
}

func (d *ConstantDistribution) Get() float64 {
	return d.State
}

//TwoStateDistribution randomly chooses state from two values
type TwoStateDistribution struct {
	Low   float64
	High  float64
	State float64
}

func (d *TwoStateDistribution) Advance() {
	d.State = d.Low
	if rand.Float64() > 0.5 {
		d.State = d.High
	}
}

func (d *TwoStateDistribution) Get() float64 {
	return d.State
}

func TSD(low float64, high float64, state float64) *TwoStateDistribution {
	return &TwoStateDistribution{Low: low, High: high, State: state}
}

func RandChoice(choices [][]byte) []byte {
	idx := rand.Int63n(int64(len(choices)))
	return choices[idx]
}
<commit_msg>using unsynchronized source random<commit_after>package common

import (
	"math"
	"math/rand"
)

// Distribution provides an interface to model a statistical distribution.
type Distribution interface {
	Advance()
	Get() float64 // should be idempotent
}

// NormalDistribution models a normal distribution.
type NormalDistribution struct {
	Mean   float64
	StdDev float64

	value float64
}

func ND(mean, stddev float64) *NormalDistribution {
	return &NormalDistribution{Mean: mean, StdDev: stddev}
}

// Unsynchronized random source
var localRand = rand.New(rand.NewSource(1))

// Advance advances this distribution. Since a normal distribution is
// stateless, this is just overwrites the internal cache value.
func (d *NormalDistribution) Advance() {
	d.value = localRand.NormFloat64()*d.StdDev + d.Mean
}

// Get returns the last computed value for this distribution.
func (d *NormalDistribution) Get() float64 {
	return d.value
}

// UniformDistribution models a uniform distribution.
type UniformDistribution struct {
	Low  float64
	High float64

	value float64
}

func UD(low, high float64) *UniformDistribution {
	return &UniformDistribution{Low: low, High: high}
}

// Advance advances this distribution. Since a uniform distribution is
// stateless, this is just overwrites the internal cache value.
func (d *UniformDistribution) Advance() {
	x := rand.Float64() // uniform
	x *= d.High - d.Low
	x += d.Low
	d.value = x
}

// Get computes and returns the next value in the distribution.
func (d *UniformDistribution) Get() float64 {
	return d.value
}

// RandomWalkDistribution is a stateful random walk. Initialize it with an
// underlying distribution, which is used to compute the new step value.
type RandomWalkDistribution struct {
	Step Distribution

	State float64 // optional
}

func WD(step Distribution, state float64) *RandomWalkDistribution {
	return &RandomWalkDistribution{Step: step, State: state}
}

// Advance computes the next value of this distribution and stores it.
func (d *RandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += d.Step.Get()
}

// Get returns the last computed value for this distribution.
func (d *RandomWalkDistribution) Get() float64 {
	return d.State
}

// ClampedRandomWalkDistribution is a stateful random walk, with minimum and
// maximum bounds. Initialize it with a Min, Max, and an underlying
// distribution, which is used to compute the new step value.
type ClampedRandomWalkDistribution struct {
	Step Distribution
	Min  float64
	Max  float64

	State float64 // optional
}

func CWD(step Distribution, min, max, state float64) *ClampedRandomWalkDistribution {
	return &ClampedRandomWalkDistribution{
		Step: step,
		Min:  min,
		Max:  max,

		State: state,
	}
}

// Advance computes the next value of this distribution and stores it.
func (d *ClampedRandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += d.Step.Get()
	if d.State > d.Max {
		d.State = d.Max
	}
	if d.State < d.Min {
		d.State = d.Min
	}
}

// Get returns the last computed value for this distribution.
func (d *ClampedRandomWalkDistribution) Get() float64 {
	return d.State
}

// MonotonicRandomWalkDistribution is a stateful random walk that only
// increases. Initialize it with a Start and an underlying distribution,
// which is used to compute the new step value. The sign of any value of the
// u.d. is always made positive.
type MonotonicRandomWalkDistribution struct {
	Step  Distribution
	State float64
}

// Advance computes the next value of this distribution and stores it.
func (d *MonotonicRandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += math.Abs(d.Step.Get())
}

func (d *MonotonicRandomWalkDistribution) Get() float64 {
	return d.State
}

func MWD(step Distribution, state float64) *MonotonicRandomWalkDistribution {
	return &MonotonicRandomWalkDistribution{Step: step, State: state}
}

// MonotonicUpDownRandomWalkDistribution is a stateful random walk that continually
// increases and decreases. Initialize it with State, Min And Max an underlying distribution,
// which is used to compute the new step value.
type MonotonicUpDownRandomWalkDistribution struct {
	Step      Distribution
	State     float64
	Min       float64
	Max       float64
	direction int //1 or -1
}

// Advance computes the next value of this distribution and stores it.
func (d *MonotonicUpDownRandomWalkDistribution) Advance() {
	d.Step.Advance()
	d.State += d.Step.Get() * float64(d.direction)
	if d.State < d.Min {
		d.State = d.Min
		d.direction = 1
	} else if d.State > d.Max {
		d.State = d.Max
		d.direction = -1
	}
}

func (d *MonotonicUpDownRandomWalkDistribution) Get() float64 {
	return d.State
}

func MUDWD(step Distribution, min float64, max float64, state float64) *MonotonicUpDownRandomWalkDistribution {
	direction := -1
	if state < max {
		direction = 1
	}
	return &MonotonicUpDownRandomWalkDistribution{Step: step, Min: min, Max: max, State: state, direction: direction}
}

type ConstantDistribution struct {
	State float64
}

func (d *ConstantDistribution) Advance() {
}

func (d *ConstantDistribution) Get() float64 {
	return d.State
}

//TwoStateDistribution randomly chooses state from two values
type TwoStateDistribution struct {
	Low   float64
	High  float64
	State float64
}

func (d *TwoStateDistribution) Advance() {
	d.State = d.Low
	if rand.Float64() > 0.5 {
		d.State = d.High
	}
}

func (d *TwoStateDistribution) Get() float64 {
	return d.State
}

func TSD(low float64, high float64, state float64) *TwoStateDistribution {
	return &TwoStateDistribution{Low: low, High: high, State: state}
}

func RandChoice(choices [][]byte) []byte {
	idx := rand.Int63n(int64(len(choices)))
	return choices[idx]
}
<|endoftext|>


<commit_before>// Copyright (c) 2014-2015 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import(`github.com/michaeldv/donna/expect`; `testing`)

// Initial position: castles, no en-passant.
func TestPosition000(t *testing.T) {
	p := NewGame(`rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`).start()
	expect.Eq(t, p.color, uint8(White))
	expect.Eq(t, p.castles, uint(0x0F))
	expect.Eq(t, p.enpassant, uint8(0))
	expect.Eq(t, p.king[White], uint8(E1))
	expect.Eq(t, p.king[Black], uint8(E8))
	expect.Eq(t, p.outposts[Pawn], bit[A2]|bit[B2]|bit[C2]|bit[D2]|bit[E2]|bit[F2]|bit[G2]|bit[H2])
	expect.Eq(t, p.outposts[Knight], bit[B1]|bit[G1])
	expect.Eq(t, p.outposts[Bishop], bit[C1]|bit[F1])
	expect.Eq(t, p.outposts[Rook], bit[A1]|bit[H1])
	expect.Eq(t, p.outposts[Queen], bit[D1])
	expect.Eq(t, p.outposts[King], bit[E1])
	expect.Eq(t, p.outposts[BlackPawn], bit[A7]|bit[B7]|bit[C7]|bit[D7]|bit[E7]|bit[F7]|bit[G7]|bit[H7])
	expect.Eq(t, p.outposts[BlackKnight], bit[B8]|bit[G8])
	expect.Eq(t, p.outposts[BlackBishop], bit[C8]|bit[F8])
	expect.Eq(t, p.outposts[BlackRook], bit[A8]|bit[H8])
	expect.Eq(t, p.outposts[BlackQueen], bit[D8])
	expect.Eq(t, p.outposts[BlackKing], bit[E8])
}

// Castles, no en-passant.
func TestPosition010(t *testing.T) {
	p := NewGame(`2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 42 42`).start()
	expect.Eq(t, p.color, uint8(White))
	expect.Eq(t, p.castles, castleKingside[White] | castleKingside[Black])
	expect.Eq(t, p.enpassant, uint8(0))
	expect.Eq(t, p.king[White], uint8(E1))
	expect.Eq(t, p.king[Black], uint8(E8))
	expect.Eq(t, p.outposts[Pawn], bit[A2]|bit[B4]|bit[F2]|bit[G2]|bit[H2])
	expect.Eq(t, p.outposts[Knight], bit[D5]|bit[G1])
	expect.Eq(t, p.outposts[Bishop], bit[G5])
	expect.Eq(t, p.outposts[Rook], bit[D1]|bit[H1])
	expect.Eq(t, p.outposts[Queen], bit[E4])
	expect.Eq(t, p.outposts[King], bit[E1])
	expect.Eq(t, p.outposts[BlackPawn], bit[A7]|bit[B7]|bit[F7]|bit[G7]|bit[H7])
	expect.Eq(t, p.outposts[BlackKnight], bit[C6])
	expect.Eq(t, p.outposts[BlackBishop], bit[E6]|bit[F8])
	expect.Eq(t, p.outposts[BlackRook], bit[C8]|bit[H8])
	expect.Eq(t, p.outposts[BlackQueen], bit[B5])
	expect.Eq(t, p.outposts[BlackKing], bit[E8])
}

// No castles, en-passant.
func TestPosition020(t *testing.T) {
	p := NewGame(`1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 42 42`).start()
	expect.Eq(t, p.color, uint8(White))
	expect.Eq(t, p.castles, uint8(0))
	expect.Eq(t, p.enpassant, uint8(E6))
	expect.Eq(t, p.king[White], uint8(B2))
	expect.Eq(t, p.king[Black], uint8(F8))
	expect.Eq(t, p.outposts[Pawn], bit[B3]|bit[C2]|bit[D5]|bit[F3])
	expect.Eq(t, p.outposts[Knight], bit[E2])
	expect.Eq(t, p.outposts[Bishop], Bitmask(0))
	expect.Eq(t, p.outposts[Rook], bit[D2])
	expect.Eq(t, p.outposts[Queen], bit[G6])
	expect.Eq(t, p.outposts[King], bit[B2])
	expect.Eq(t, p.outposts[BlackPawn], bit[A7]|bit[D6]|bit[E5]|bit[H5])
	expect.Eq(t, p.outposts[BlackKnight], Bitmask(0))
	expect.Eq(t, p.outposts[BlackBishop], Bitmask(0))
	expect.Eq(t, p.outposts[BlackRook], bit[B8]|bit[C8])
	expect.Eq(t, p.outposts[BlackQueen], bit[C7])
	expect.Eq(t, p.outposts[BlackKing], bit[F8])
}

//\\ Position to FEN tests.
// Initial position: castles, no en-passant.
func TestPosition100(t *testing.T) {
	p := NewGame(`rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`).start()
	expect.Eq(t, p.fen(), `rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`)
}

// Castles, no en-passant.
func TestPosition110(t *testing.T) {
	p := NewGame(`2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 42 42`).start()
	expect.Eq(t, p.fen(), `2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 0 1`)
}

// No castles, en-passant.
func TestPosition120(t *testing.T) {
	p := NewGame(`1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 42 42`).start()
	expect.Eq(t, p.fen(), `1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 0 1`)
}

//\\ Donna Chess Format (DCF) tests.
// Initial position: castles, no en-passant.
func TestPosition130(t *testing.T) {
	p := NewGame(`rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`).start()
	expect.Eq(t, p.dcf(), `Ke1,Qd1,Ra1,Rh1,Bc1,Bf1,Nb1,Ng1,a2,b2,c2,d2,e2,f2,g2,h2 : Ke8,Qd8,Ra8,Rh8,Bc8,Bf8,Nb8,Ng8,a7,b7,c7,d7,e7,f7,g7,h7`)
}

// Castles, no en-passant.
func TestPosition140(t *testing.T) {
	p := NewGame(`2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 42 42`).start()
	expect.Eq(t, p.dcf(), `Ke1,Qe4,Rd1,Rh1,Bg5,Ng1,Nd5,Cg1,a2,f2,g2,h2,b4 : Ke8,Qb5,Rc8,Rh8,Be6,Bf8,Nc6,Cg8,a7,b7,f7,g7,h7`)
}

// No castles, en-passant.
func TestPosition150(t *testing.T) {
	p := NewGame(`1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 42 42`).start()
	expect.Eq(t, p.dcf(), `Kb2,Qg6,Rd2,Ne2,Ee6,c2,b3,f3,d5 : Kf8,Qc7,Rb8,Rc8,e5,h5,d6,a7`)

	pp := NewGame(`M,Kb2,Qg6,Rd2,Ne2,Ee6,c2,b3,f3,d5`, `Kf8,Qc7,Rb8,Rc8,e5,h5,d6,a7`).start()
	expect.Eq(t, pp.fen(), `1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 0 1`)
}

// Position status.
func TestPosition200(t *testing.T) {
	p := NewGame().start()
	expect.Eq(t, p.status(NewMove(p, E2, E4), p.Evaluate()), InProgress)
}

// Mate in 1 move.
func TestPosition210(t *testing.T) {
	p := NewGame(`Kf8,Rh1,g6`, `Kh8,Bg8,g7,h7`).start()
	rootNode = node // Reset ply().
	expect.Eq(t, p.status(NewMove(p, H1, H6), Checkmate - ply()), WhiteWinning)
}

// Forced stalemate.
func TestPosition220(t *testing.T) {
	p := NewGame(`Kf7,b2,b4,h6`, `Kh8,Ba4,b3,b5,h7`).start()
	expect.Eq(t, p.status(NewMove(p, F7, F8), 0), Stalemate)
}

// Self-imposed stalemate.
func TestPosition230(t *testing.T) {
	p := NewGame(`Ka1,g3,h2`, `M,Kh5,h3,g4,g5,g6,h7`).start()
	p = p.makeMove(NewMove(p, H7, H6))
	expect.Eq(t, p.status(NewMove(p, A1, B2), 0), Stalemate)
}

// Draw by repetition.
func TestPosition240(t *testing.T) {
	p := NewGame(`Ka1,g3,h2`, `M,Kh5,h3,g4,g5,g6,h7`).start() // Initial.

	p = p.makeMove(NewMove(p, H5, H6))
	p = p.makeMove(NewMove(p, A1, A2))
	p = p.makeMove(NewMove(p, H6, H5))
	p = p.makeMove(NewMove(p, A2, A1)) // Rep #2.
	expect.Eq(t, p.status(NewMove(p, H5, H6), 0), InProgress)

	p = p.makeMove(NewMove(p, H5, H6))
	p = p.makeMove(NewMove(p, A1, A2))
	p = p.makeMove(NewMove(p, H6, H5)) // -- No NewMove(p, A2, A1) here --

	rootNode = node // Reset ply().
	expect.Eq(t, p.status(NewMove(p, A2, A1), 0), Repetition) // <-- Ka2-a1 causes rep #3.
}

// Insufficient material.
func TestPosition250(t *testing.T) {
	p := NewGame(`Ka1,Bb2`, `Kh5`).start()
	expect.True(t, p.insufficient())
}
<commit_msg>Tests for pins/mobility<commit_after>// Copyright (c) 2014-2015 by Michael Dvorkin. All Rights Reserved.
// Use of this source code is governed by a MIT-style license that can
// be found in the LICENSE file.

package donna

import(`github.com/michaeldv/donna/expect`; `testing`)

// Initial position: castles, no en-passant.
func TestPosition000(t *testing.T) {
	p := NewGame(`rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`).start()
	expect.Eq(t, p.color, uint8(White))
	expect.Eq(t, p.castles, uint(0x0F))
	expect.Eq(t, p.enpassant, uint8(0))
	expect.Eq(t, p.king[White], uint8(E1))
	expect.Eq(t, p.king[Black], uint8(E8))
	expect.Eq(t, p.outposts[Pawn], bit[A2]|bit[B2]|bit[C2]|bit[D2]|bit[E2]|bit[F2]|bit[G2]|bit[H2])
	expect.Eq(t, p.outposts[Knight], bit[B1]|bit[G1])
	expect.Eq(t, p.outposts[Bishop], bit[C1]|bit[F1])
	expect.Eq(t, p.outposts[Rook], bit[A1]|bit[H1])
	expect.Eq(t, p.outposts[Queen], bit[D1])
	expect.Eq(t, p.outposts[King], bit[E1])
	expect.Eq(t, p.outposts[BlackPawn], bit[A7]|bit[B7]|bit[C7]|bit[D7]|bit[E7]|bit[F7]|bit[G7]|bit[H7])
	expect.Eq(t, p.outposts[BlackKnight], bit[B8]|bit[G8])
	expect.Eq(t, p.outposts[BlackBishop], bit[C8]|bit[F8])
	expect.Eq(t, p.outposts[BlackRook], bit[A8]|bit[H8])
	expect.Eq(t, p.outposts[BlackQueen], bit[D8])
	expect.Eq(t, p.outposts[BlackKing], bit[E8])
}

// Castles, no en-passant.
func TestPosition010(t *testing.T) {
	p := NewGame(`2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 42 42`).start()
	expect.Eq(t, p.color, uint8(White))
	expect.Eq(t, p.castles, castleKingside[White] | castleKingside[Black])
	expect.Eq(t, p.enpassant, uint8(0))
	expect.Eq(t, p.king[White], uint8(E1))
	expect.Eq(t, p.king[Black], uint8(E8))
	expect.Eq(t, p.outposts[Pawn], bit[A2]|bit[B4]|bit[F2]|bit[G2]|bit[H2])
	expect.Eq(t, p.outposts[Knight], bit[D5]|bit[G1])
	expect.Eq(t, p.outposts[Bishop], bit[G5])
	expect.Eq(t, p.outposts[Rook], bit[D1]|bit[H1])
	expect.Eq(t, p.outposts[Queen], bit[E4])
	expect.Eq(t, p.outposts[King], bit[E1])
	expect.Eq(t, p.outposts[BlackPawn], bit[A7]|bit[B7]|bit[F7]|bit[G7]|bit[H7])
	expect.Eq(t, p.outposts[BlackKnight], bit[C6])
	expect.Eq(t, p.outposts[BlackBishop], bit[E6]|bit[F8])
	expect.Eq(t, p.outposts[BlackRook], bit[C8]|bit[H8])
	expect.Eq(t, p.outposts[BlackQueen], bit[B5])
	expect.Eq(t, p.outposts[BlackKing], bit[E8])
}

// No castles, en-passant.
func TestPosition020(t *testing.T) {
	p := NewGame(`1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 42 42`).start()
	expect.Eq(t, p.color, uint8(White))
	expect.Eq(t, p.castles, uint8(0))
	expect.Eq(t, p.enpassant, uint8(E6))
	expect.Eq(t, p.king[White], uint8(B2))
	expect.Eq(t, p.king[Black], uint8(F8))
	expect.Eq(t, p.outposts[Pawn], bit[B3]|bit[C2]|bit[D5]|bit[F3])
	expect.Eq(t, p.outposts[Knight], bit[E2])
	expect.Eq(t, p.outposts[Bishop], Bitmask(0))
	expect.Eq(t, p.outposts[Rook], bit[D2])
	expect.Eq(t, p.outposts[Queen], bit[G6])
	expect.Eq(t, p.outposts[King], bit[B2])
	expect.Eq(t, p.outposts[BlackPawn], bit[A7]|bit[D6]|bit[E5]|bit[H5])
	expect.Eq(t, p.outposts[BlackKnight], Bitmask(0))
	expect.Eq(t, p.outposts[BlackBishop], Bitmask(0))
	expect.Eq(t, p.outposts[BlackRook], bit[B8]|bit[C8])
	expect.Eq(t, p.outposts[BlackQueen], bit[C7])
	expect.Eq(t, p.outposts[BlackKing], bit[F8])
}

//\\ Position to FEN tests.
// Initial position: castles, no en-passant.
func TestPosition100(t *testing.T) {
	p := NewGame(`rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`).start()
	expect.Eq(t, p.fen(), `rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`)
}

// Castles, no en-passant.
func TestPosition110(t *testing.T) {
	p := NewGame(`2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 42 42`).start()
	expect.Eq(t, p.fen(), `2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 0 1`)
}

// No castles, en-passant.
func TestPosition120(t *testing.T) {
	p := NewGame(`1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 42 42`).start()
	expect.Eq(t, p.fen(), `1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 0 1`)
}

//\\ Donna Chess Format (DCF) tests.
// Initial position: castles, no en-passant.
func TestPosition130(t *testing.T) {
	p := NewGame(`rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`).start()
	expect.Eq(t, p.dcf(), `Ke1,Qd1,Ra1,Rh1,Bc1,Bf1,Nb1,Ng1,a2,b2,c2,d2,e2,f2,g2,h2 : Ke8,Qd8,Ra8,Rh8,Bc8,Bf8,Nb8,Ng8,a7,b7,c7,d7,e7,f7,g7,h7`)
}

// Castles, no en-passant.
func TestPosition140(t *testing.T) {
	p := NewGame(`2r1kb1r/pp3ppp/2n1b3/1q1N2B1/1P2Q3/8/P4PPP/3RK1NR w Kk - 42 42`).start()
	expect.Eq(t, p.dcf(), `Ke1,Qe4,Rd1,Rh1,Bg5,Ng1,Nd5,Cg1,a2,f2,g2,h2,b4 : Ke8,Qb5,Rc8,Rh8,Be6,Bf8,Nc6,Cg8,a7,b7,f7,g7,h7`)
}

// No castles, en-passant.
func TestPosition150(t *testing.T) {
	p := NewGame(`1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 42 42`).start()
	expect.Eq(t, p.dcf(), `Kb2,Qg6,Rd2,Ne2,Ee6,c2,b3,f3,d5 : Kf8,Qc7,Rb8,Rc8,e5,h5,d6,a7`)

	pp := NewGame(`M,Kb2,Qg6,Rd2,Ne2,Ee6,c2,b3,f3,d5`, `Kf8,Qc7,Rb8,Rc8,e5,h5,d6,a7`).start()
	expect.Eq(t, pp.fen(), `1rr2k2/p1q5/3p2Q1/3Pp2p/8/1P3P2/1KPRN3/8 w - e6 0 1`)
}

// Position status.
func TestPosition200(t *testing.T) {
	p := NewGame().start()
	expect.Eq(t, p.status(NewMove(p, E2, E4), p.Evaluate()), InProgress)
}

// Mate in 1 move.
func TestPosition210(t *testing.T) {
	p := NewGame(`Kf8,Rh1,g6`, `Kh8,Bg8,g7,h7`).start()
	rootNode = node // Reset ply().
	expect.Eq(t, p.status(NewMove(p, H1, H6), Checkmate - ply()), WhiteWinning)
}

// Forced stalemate.
func TestPosition220(t *testing.T) {
	p := NewGame(`Kf7,b2,b4,h6`, `Kh8,Ba4,b3,b5,h7`).start()
	expect.Eq(t, p.status(NewMove(p, F7, F8), 0), Stalemate)
}

// Self-imposed stalemate.
func TestPosition230(t *testing.T) {
	p := NewGame(`Ka1,g3,h2`, `M,Kh5,h3,g4,g5,g6,h7`).start()
	p = p.makeMove(NewMove(p, H7, H6))
	expect.Eq(t, p.status(NewMove(p, A1, B2), 0), Stalemate)
}

// Draw by repetition.
func TestPosition240(t *testing.T) {
	p := NewGame(`Ka1,g3,h2`, `M,Kh5,h3,g4,g5,g6,h7`).start() // Initial.

	p = p.makeMove(NewMove(p, H5, H6))
	p = p.makeMove(NewMove(p, A1, A2))
	p = p.makeMove(NewMove(p, H6, H5))
	p = p.makeMove(NewMove(p, A2, A1)) // Rep #2.
	expect.Eq(t, p.status(NewMove(p, H5, H6), 0), InProgress)

	p = p.makeMove(NewMove(p, H5, H6))
	p = p.makeMove(NewMove(p, A1, A2))
	p = p.makeMove(NewMove(p, H6, H5)) // -- No NewMove(p, A2, A1) here --

	rootNode = node // Reset ply().
	expect.Eq(t, p.status(NewMove(p, A2, A1), 0), Repetition) // <-- Ka2-a1 causes rep #3.
}

// Insufficient material.
func TestPosition250(t *testing.T) {
	p := NewGame(`Ka1,Bb2`, `Kh5`).start()
	expect.True(t, p.insufficient())
}

// Restricted mobility for pinned pieces.
func TestPosition300(t *testing.T) {
	p := NewGame(`Ka1,a2,Nc3`, `Kh8,h7,Bg8`).start() // Nc3 vs Bishop, no pin.
	expect.Eq(t, p.Evaluate(), -18)
	p = NewGame(`Ka1,a2,Nc3`, `Kh8,h7,Bg7`).start() // Nc3 vs Bishop, pin on C3-G7 diagonal.
	expect.Eq(t, p.Evaluate(), -67)

}

func TestPosition310(t *testing.T) {
	p := NewGame(`Ka1,a2,Bc3`, `Kg8,h7,Bg6`).start() // Bc3 vs Bishop, no pin.
	expect.Eq(t, p.Evaluate(), 0)
	p = NewGame(`Ka1,a2,Bc3`, `Kg8,h7,Bg7`).start() // Bc3 vs Bishop, pin on C3-G7 diagonal.
	expect.Eq(t, p.Evaluate(), -24)
	p = NewGame(`Ka3,a2,Bc3`, `Kh8,h7,Rh1`).start() // Bc3 vs Rook, no pin.
	expect.Eq(t, p.Evaluate(), -211)
	p = NewGame(`Ka3,a2,Bc3`, `Kh8,h7,Rh3`).start() // Bc3 vs Rook, pin on C3-H3 file.
	expect.Eq(t, p.Evaluate(), -317)

}
<|endoftext|>


<commit_before>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package profiles

import (
	"archive/zip"
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"runtime"
	"strings"

	"v.io/jiri/collect"
	"v.io/jiri/project"
	"v.io/jiri/runutil"
	"v.io/jiri/tool"
)

const (
	DefaultDirPerm  = os.FileMode(0755)
	DefaultFilePerm = os.FileMode(0644)
	targetDefValue  = "<runtime.GOARCH>-<runtime.GOOS>"
)

// RegisterTargetFlag registers the commonly used --target flag with
// the supplied FlagSet.
func RegisterTargetFlag(flags *flag.FlagSet, target *Target) {
	*target = DefaultTarget()
	flags.Var(target, "target", target.Usage())
	flags.Lookup("target").DefValue = targetDefValue
}

// RegisterTargetAndEnvFlags registers the commonly used --target and --env
// flags with the supplied FlagSet
func RegisterTargetAndEnvFlags(flags *flag.FlagSet, target *Target) {
	*target = DefaultTarget()
	flags.Var(target, "target", target.Usage())
	flags.Lookup("target").DefValue = targetDefValue
	flags.Var(&target.commandLineEnv, "env", target.commandLineEnv.Usage())
}

// RegisterManifestFlag registers the commonly used --profiles-manifest
// flag with the supplied FlagSet.
func RegisterManifestFlag(flags *flag.FlagSet, manifest *string, defaultManifest string) {
	root, _ := project.JiriRoot()
	flags.StringVar(manifest, "profiles-manifest", filepath.Join(root, defaultManifest), "specify the profiles XML manifest filename.")
	flags.Lookup("profiles-manifest").DefValue = filepath.Join("$JIRI_ROOT", defaultManifest)
}

// RegisterProfileFlags registers the commonly used --profiles-manifest, --profiles,
// --target and --merge-policies flags with the supplied FlagSet.
func RegisterProfileFlags(flags *flag.FlagSet, profilesMode *ProfilesMode, manifest, profiles *string, defaultManifest string, policies *MergePolicies, target *Target) {
	flags.Var(profilesMode, "skip-profiles", "if set, no profiles will be used")
	RegisterProfilesFlag(flags, profiles)
	RegisterMergePoliciesFlag(flags, policies)
	RegisterManifestFlag(flags, manifest, defaultManifest)
	RegisterTargetFlag(flags, target)
}

// RegisterProfilesFlag registers the --profiles flag
func RegisterProfilesFlag(flags *flag.FlagSet, profiles *string) {
	flags.StringVar(profiles, "profiles", "base,jiri", "a comma separated list of profiles to use")
}

// RegisterMergePoliciesFlag registers the --merge-policies flag
func RegisterMergePoliciesFlag(flags *flag.FlagSet, policies *MergePolicies) {
	flags.Var(policies, "merge-policies", "specify policies for merging environment variables")
}

type AppendJiriProfileMode bool

const (
	AppendJiriProfile      AppendJiriProfileMode = true
	DoNotAppendJiriProfile                       = false
)

// InitProfilesFromFlag splits a comma separated list of profile names into
// a slice and optionally appends the 'jiri' profile if it's not already
// present.
func InitProfilesFromFlag(flag string, appendJiriProfile AppendJiriProfileMode) []string {
	n := strings.Split(flag, ",")
	if appendJiriProfile == AppendJiriProfile && !strings.Contains(flag, "jiri") {
		n = append(n, "jiri")
	}
	return n
}

// AtomicAction performs an action 'atomically' by keeping track of successfully
// completed actions in the supplied completion log and re-running them if they
// are not successfully logged therein after deleting the entire contents of the
// dir parameter. Consequently it does not make sense to apply AtomicAction to
// the same directory in sequence.
func AtomicAction(ctx *tool.Context, installFn func() error, dir, message string) error {
	atomicFn := func() error {
		completionLogPath := filepath.Join(dir, ".complete")
		if dir != "" && ctx.Run().DirectoryExists(dir) {
			// If the dir exists but the completionLogPath doesn't, then it means
			// the previous action didn't finish.
			// Remove the dir so we can perform the action again.
			if !ctx.Run().FileExists(completionLogPath) {
				ctx.Run().RemoveAll(dir)
			} else {
				if ctx.Verbose() {
					fmt.Fprintf(ctx.Stdout(), "AtomicAction: %s already completed in %s\n", message, dir)
				}
				return nil
			}
		}
		if err := installFn(); err != nil {
			if dir != "" {
				ctx.Run().RemoveAll(dir)
			}
			return err
		}
		if err := ctx.Run().WriteFile(completionLogPath, []byte("completed"), DefaultFilePerm); err != nil {
			return err
		}
		return nil
	}
	return ctx.Run().Function(atomicFn, message)
}

// RunCommand runs the specified command with the specified args and environment
// whilst logging the output to ctx.Stdout() on error or if tracing is requested
// via the -v flag.
func RunCommand(ctx *tool.Context, env map[string]string, bin string, args ...string) error {
	var out bytes.Buffer
	opts := ctx.Run().Opts()
	opts.Stdout = &out
	opts.Stderr = &out
	opts.Env = env
	err := ctx.Run().CommandWithOpts(opts, bin, args...)
	if err != nil || tool.VerboseFlag {
		fmt.Fprintf(ctx.Stdout(), "%s", out.String())
	}
	return err
}

func brewList(ctx *tool.Context) (map[string]bool, error) {
	var out bytes.Buffer
	opts := ctx.Run().Opts()
	opts.Stdout = &out
	opts.Stderr = &out
	err := ctx.Run().CommandWithOpts(opts, "brew", "list")
	if err != nil || tool.VerboseFlag {
		fmt.Fprintf(ctx.Stdout(), "%s", out.String())
	}
	scanner := bufio.NewScanner(&out)
	pkgs := map[string]bool{}
	for scanner.Scan() {
		pkgs[scanner.Text()] = true
	}
	return pkgs, err
}

// InstallPackages identifies the packages that need to be installed
// and installs them using the OS-specific package manager.
func InstallPackages(ctx *tool.Context, pkgs []string) error {
	installDepsFn := func() error {
		switch runtime.GOOS {
		case "linux":
			if runutil.IsFNLHost() {
				fmt.Fprintf(ctx.Stdout(), "skipping installation of %v on FNL host", pkgs)
				fmt.Fprintf(ctx.Stdout(), "success\n")
				break
			}
			installPkgs := []string{}
			for _, pkg := range pkgs {
				if err := RunCommand(ctx, nil, "dpkg", "-L", pkg); err != nil {
					installPkgs = append(installPkgs, pkg)
				}
			}
			if len(installPkgs) > 0 {
				args := append([]string{"apt-get", "install", "-y"}, installPkgs...)
				fmt.Fprintf(ctx.Stdout(), "Running: sudo %s: ", strings.Join(args, " "))
				if err := RunCommand(ctx, nil, "sudo", args...); err != nil {
					fmt.Fprintf(ctx.Stdout(), "%v\n", err)
					return err
				}
				fmt.Fprintf(ctx.Stdout(), "success\n")
			}
		case "darwin":
			installPkgs := []string{}
			installedPkgs, err := brewList(ctx)
			if err != nil {
				return err
			}
			for _, pkg := range pkgs {
				if !installedPkgs[pkg] {
					installPkgs = append(installPkgs, pkg)
				}
			}
			if len(installPkgs) > 0 {
				args := append([]string{"install"}, installPkgs...)
				fmt.Fprintf(ctx.Stdout(), "Running: brew %s: ", strings.Join(args, " "))
				if err := RunCommand(ctx, nil, "brew", args...); err != nil {
					fmt.Fprintf(ctx.Stdout(), "%v\n", err)
					return err
				}
				fmt.Fprintf(ctx.Stdout(), "success\n")
			}
		}
		return nil
	}
	return ctx.Run().Function(installDepsFn, "Install dependencies")
}

// ensureAction ensures that the requested profile and target
// is installed/uninstalled, installing/uninstalling it if only if necessary.
func ensureAction(ctx *tool.Context, action Action, profile string, root RelativePath, target Target) error {
	verb := ""
	switch action {
	case Install:
		verb = "install"
	case Uninstall:
		verb = "uninstall"
	default:
		return fmt.Errorf("unrecognised action %v", action)
	}

	if t := LookupProfileTarget(profile, target); t != nil {
		if ctx.Run().Opts().Verbose {
			fmt.Fprintf(ctx.Stdout(), "%v %v is already %sed as %v\n", profile, target, verb, t)
		}
		return nil
	}
	mgr := LookupManager(profile)
	if mgr == nil {
		return fmt.Errorf("profile %v is not supported", profile)
	}
	version, err := mgr.VersionInfo().Select(target.Version())
	if err != nil {
		return err
	}
	target.SetVersion(version)
	if ctx.Run().Opts().Verbose || ctx.Run().Opts().DryRun {
		fmt.Fprintf(ctx.Stdout(), "%s %s %s\n", verb, profile, target.DebugString())
	}
	if action == Install {
		return mgr.Install(ctx, root, target)
	}
	return mgr.Uninstall(ctx, root, target)
}

// EnsureProfileTargetIsInstalled ensures that the requested profile and target
// is installed, installing it if only if necessary.
func EnsureProfileTargetIsInstalled(ctx *tool.Context, profile string, root RelativePath, target Target) error {
	return ensureAction(ctx, Install, profile, root, target)
}

// EnsureProfileTargetIsUninstalled ensures that the requested profile and target
// are no longer installed.
func EnsureProfileTargetIsUninstalled(ctx *tool.Context, profile string, root RelativePath, target Target) error {
	return ensureAction(ctx, Uninstall, profile, root, target)
}

// Fetch downloads the specified url and saves it to dst.
// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
// so it does not pollute the profile package namespace.
func Fetch(ctx *tool.Context, dst, url string) error {
	ctx.Run().Output([]string{"fetching " + url})
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("got non-200 status code while getting %v: %v", url, resp.StatusCode)
	}
	file, err := ctx.Run().Create(dst)
	if err != nil {
		return err
	}
	_, err = ctx.Run().Copy(file, resp.Body)
	return err
}

// GitCloneRepo clones a repo at a specific revision in outDir.
// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
// so it does not pollute the profile package namespace.
func GitCloneRepo(ctx *tool.Context, remote, revision, outDir string, outDirPerm os.FileMode) (e error) {
	cwd, err := os.Getwd()
	if err != nil {
		return err
	}
	defer collect.Error(func() error { return ctx.Run().Chdir(cwd) }, &e)

	if err := ctx.Run().MkdirAll(outDir, outDirPerm); err != nil {
		return err
	}
	if err := ctx.Git().Clone(remote, outDir); err != nil {
		return err
	}
	if err := ctx.Run().Chdir(outDir); err != nil {
		return err
	}
	return ctx.Git().Reset(revision)
}

// Unzip unzips the file in srcFile and puts resulting files in directory dstDir.
// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
// so it does not pollute the profile package namespace.
func Unzip(ctx *tool.Context, srcFile, dstDir string) error {
	r, err := zip.OpenReader(srcFile)
	if err != nil {
		return err
	}
	defer r.Close()

	unzipFn := func(zFile *zip.File) error {
		rc, err := zFile.Open()
		if err != nil {
			return err
		}
		defer rc.Close()

		fileDst := filepath.Join(dstDir, zFile.Name)
		if zFile.FileInfo().IsDir() {
			return ctx.Run().MkdirAll(fileDst, zFile.Mode())
		}
		file, err := ctx.Run().OpenFile(fileDst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zFile.Mode())
		if err != nil {
			return err
		}
		defer file.Close()
		_, err = ctx.Run().Copy(file, rc)
		return err
	}

	ctx.Run().Output([]string{"unzipping " + srcFile})
	for _, zFile := range r.File {
		ctx.Run().Output([]string{"extracting " + zFile.Name})
		if err := unzipFn(zFile); err != nil {
			return err
		}
	}
	return nil
}
<commit_msg>TBR: jiri/profiles: Fix unzip utility.<commit_after>// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package profiles

import (
	"archive/zip"
	"bufio"
	"bytes"
	"flag"
	"fmt"
	"net/http"
	"os"
	"path/filepath"
	"runtime"
	"strings"

	"v.io/jiri/collect"
	"v.io/jiri/project"
	"v.io/jiri/runutil"
	"v.io/jiri/tool"
)

const (
	DefaultDirPerm  = os.FileMode(0755)
	DefaultFilePerm = os.FileMode(0644)
	targetDefValue  = "<runtime.GOARCH>-<runtime.GOOS>"
)

// RegisterTargetFlag registers the commonly used --target flag with
// the supplied FlagSet.
func RegisterTargetFlag(flags *flag.FlagSet, target *Target) {
	*target = DefaultTarget()
	flags.Var(target, "target", target.Usage())
	flags.Lookup("target").DefValue = targetDefValue
}

// RegisterTargetAndEnvFlags registers the commonly used --target and --env
// flags with the supplied FlagSet
func RegisterTargetAndEnvFlags(flags *flag.FlagSet, target *Target) {
	*target = DefaultTarget()
	flags.Var(target, "target", target.Usage())
	flags.Lookup("target").DefValue = targetDefValue
	flags.Var(&target.commandLineEnv, "env", target.commandLineEnv.Usage())
}

// RegisterManifestFlag registers the commonly used --profiles-manifest
// flag with the supplied FlagSet.
func RegisterManifestFlag(flags *flag.FlagSet, manifest *string, defaultManifest string) {
	root, _ := project.JiriRoot()
	flags.StringVar(manifest, "profiles-manifest", filepath.Join(root, defaultManifest), "specify the profiles XML manifest filename.")
	flags.Lookup("profiles-manifest").DefValue = filepath.Join("$JIRI_ROOT", defaultManifest)
}

// RegisterProfileFlags registers the commonly used --profiles-manifest, --profiles,
// --target and --merge-policies flags with the supplied FlagSet.
func RegisterProfileFlags(flags *flag.FlagSet, profilesMode *ProfilesMode, manifest, profiles *string, defaultManifest string, policies *MergePolicies, target *Target) {
	flags.Var(profilesMode, "skip-profiles", "if set, no profiles will be used")
	RegisterProfilesFlag(flags, profiles)
	RegisterMergePoliciesFlag(flags, policies)
	RegisterManifestFlag(flags, manifest, defaultManifest)
	RegisterTargetFlag(flags, target)
}

// RegisterProfilesFlag registers the --profiles flag
func RegisterProfilesFlag(flags *flag.FlagSet, profiles *string) {
	flags.StringVar(profiles, "profiles", "base,jiri", "a comma separated list of profiles to use")
}

// RegisterMergePoliciesFlag registers the --merge-policies flag
func RegisterMergePoliciesFlag(flags *flag.FlagSet, policies *MergePolicies) {
	flags.Var(policies, "merge-policies", "specify policies for merging environment variables")
}

type AppendJiriProfileMode bool

const (
	AppendJiriProfile      AppendJiriProfileMode = true
	DoNotAppendJiriProfile                       = false
)

// InitProfilesFromFlag splits a comma separated list of profile names into
// a slice and optionally appends the 'jiri' profile if it's not already
// present.
func InitProfilesFromFlag(flag string, appendJiriProfile AppendJiriProfileMode) []string {
	n := strings.Split(flag, ",")
	if appendJiriProfile == AppendJiriProfile && !strings.Contains(flag, "jiri") {
		n = append(n, "jiri")
	}
	return n
}

// AtomicAction performs an action 'atomically' by keeping track of successfully
// completed actions in the supplied completion log and re-running them if they
// are not successfully logged therein after deleting the entire contents of the
// dir parameter. Consequently it does not make sense to apply AtomicAction to
// the same directory in sequence.
func AtomicAction(ctx *tool.Context, installFn func() error, dir, message string) error {
	atomicFn := func() error {
		completionLogPath := filepath.Join(dir, ".complete")
		if dir != "" && ctx.Run().DirectoryExists(dir) {
			// If the dir exists but the completionLogPath doesn't, then it means
			// the previous action didn't finish.
			// Remove the dir so we can perform the action again.
			if !ctx.Run().FileExists(completionLogPath) {
				ctx.Run().RemoveAll(dir)
			} else {
				if ctx.Verbose() {
					fmt.Fprintf(ctx.Stdout(), "AtomicAction: %s already completed in %s\n", message, dir)
				}
				return nil
			}
		}
		if err := installFn(); err != nil {
			if dir != "" {
				ctx.Run().RemoveAll(dir)
			}
			return err
		}
		if err := ctx.Run().WriteFile(completionLogPath, []byte("completed"), DefaultFilePerm); err != nil {
			return err
		}
		return nil
	}
	return ctx.Run().Function(atomicFn, message)
}

// RunCommand runs the specified command with the specified args and environment
// whilst logging the output to ctx.Stdout() on error or if tracing is requested
// via the -v flag.
func RunCommand(ctx *tool.Context, env map[string]string, bin string, args ...string) error {
	var out bytes.Buffer
	opts := ctx.Run().Opts()
	opts.Stdout = &out
	opts.Stderr = &out
	opts.Env = env
	err := ctx.Run().CommandWithOpts(opts, bin, args...)
	if err != nil || tool.VerboseFlag {
		fmt.Fprintf(ctx.Stdout(), "%s", out.String())
	}
	return err
}

func brewList(ctx *tool.Context) (map[string]bool, error) {
	var out bytes.Buffer
	opts := ctx.Run().Opts()
	opts.Stdout = &out
	opts.Stderr = &out
	err := ctx.Run().CommandWithOpts(opts, "brew", "list")
	if err != nil || tool.VerboseFlag {
		fmt.Fprintf(ctx.Stdout(), "%s", out.String())
	}
	scanner := bufio.NewScanner(&out)
	pkgs := map[string]bool{}
	for scanner.Scan() {
		pkgs[scanner.Text()] = true
	}
	return pkgs, err
}

// InstallPackages identifies the packages that need to be installed
// and installs them using the OS-specific package manager.
func InstallPackages(ctx *tool.Context, pkgs []string) error {
	installDepsFn := func() error {
		switch runtime.GOOS {
		case "linux":
			if runutil.IsFNLHost() {
				fmt.Fprintf(ctx.Stdout(), "skipping installation of %v on FNL host", pkgs)
				fmt.Fprintf(ctx.Stdout(), "success\n")
				break
			}
			installPkgs := []string{}
			for _, pkg := range pkgs {
				if err := RunCommand(ctx, nil, "dpkg", "-L", pkg); err != nil {
					installPkgs = append(installPkgs, pkg)
				}
			}
			if len(installPkgs) > 0 {
				args := append([]string{"apt-get", "install", "-y"}, installPkgs...)
				fmt.Fprintf(ctx.Stdout(), "Running: sudo %s: ", strings.Join(args, " "))
				if err := RunCommand(ctx, nil, "sudo", args...); err != nil {
					fmt.Fprintf(ctx.Stdout(), "%v\n", err)
					return err
				}
				fmt.Fprintf(ctx.Stdout(), "success\n")
			}
		case "darwin":
			installPkgs := []string{}
			installedPkgs, err := brewList(ctx)
			if err != nil {
				return err
			}
			for _, pkg := range pkgs {
				if !installedPkgs[pkg] {
					installPkgs = append(installPkgs, pkg)
				}
			}
			if len(installPkgs) > 0 {
				args := append([]string{"install"}, installPkgs...)
				fmt.Fprintf(ctx.Stdout(), "Running: brew %s: ", strings.Join(args, " "))
				if err := RunCommand(ctx, nil, "brew", args...); err != nil {
					fmt.Fprintf(ctx.Stdout(), "%v\n", err)
					return err
				}
				fmt.Fprintf(ctx.Stdout(), "success\n")
			}
		}
		return nil
	}
	return ctx.Run().Function(installDepsFn, "Install dependencies")
}

// ensureAction ensures that the requested profile and target
// is installed/uninstalled, installing/uninstalling it if only if necessary.
func ensureAction(ctx *tool.Context, action Action, profile string, root RelativePath, target Target) error {
	verb := ""
	switch action {
	case Install:
		verb = "install"
	case Uninstall:
		verb = "uninstall"
	default:
		return fmt.Errorf("unrecognised action %v", action)
	}

	if t := LookupProfileTarget(profile, target); t != nil {
		if ctx.Run().Opts().Verbose {
			fmt.Fprintf(ctx.Stdout(), "%v %v is already %sed as %v\n", profile, target, verb, t)
		}
		return nil
	}
	mgr := LookupManager(profile)
	if mgr == nil {
		return fmt.Errorf("profile %v is not supported", profile)
	}
	version, err := mgr.VersionInfo().Select(target.Version())
	if err != nil {
		return err
	}
	target.SetVersion(version)
	if ctx.Run().Opts().Verbose || ctx.Run().Opts().DryRun {
		fmt.Fprintf(ctx.Stdout(), "%s %s %s\n", verb, profile, target.DebugString())
	}
	if action == Install {
		return mgr.Install(ctx, root, target)
	}
	return mgr.Uninstall(ctx, root, target)
}

// EnsureProfileTargetIsInstalled ensures that the requested profile and target
// is installed, installing it if only if necessary.
func EnsureProfileTargetIsInstalled(ctx *tool.Context, profile string, root RelativePath, target Target) error {
	return ensureAction(ctx, Install, profile, root, target)
}

// EnsureProfileTargetIsUninstalled ensures that the requested profile and target
// are no longer installed.
func EnsureProfileTargetIsUninstalled(ctx *tool.Context, profile string, root RelativePath, target Target) error {
	return ensureAction(ctx, Uninstall, profile, root, target)
}

// Fetch downloads the specified url and saves it to dst.
// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
// so it does not pollute the profile package namespace.
func Fetch(ctx *tool.Context, dst, url string) error {
	ctx.Run().Output([]string{"fetching " + url})
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("got non-200 status code while getting %v: %v", url, resp.StatusCode)
	}
	file, err := ctx.Run().Create(dst)
	if err != nil {
		return err
	}
	_, err = ctx.Run().Copy(file, resp.Body)
	return err
}

// GitCloneRepo clones a repo at a specific revision in outDir.
// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
// so it does not pollute the profile package namespace.
func GitCloneRepo(ctx *tool.Context, remote, revision, outDir string, outDirPerm os.FileMode) (e error) {
	cwd, err := os.Getwd()
	if err != nil {
		return err
	}
	defer collect.Error(func() error { return ctx.Run().Chdir(cwd) }, &e)

	if err := ctx.Run().MkdirAll(outDir, outDirPerm); err != nil {
		return err
	}
	if err := ctx.Git().Clone(remote, outDir); err != nil {
		return err
	}
	if err := ctx.Run().Chdir(outDir); err != nil {
		return err
	}
	return ctx.Git().Reset(revision)
}

// Unzip unzips the file in srcFile and puts resulting files in directory dstDir.
// TODO(nlacasse, cnicoloau): Move this to a package for profile-implementors
// so it does not pollute the profile package namespace.
func Unzip(ctx *tool.Context, srcFile, dstDir string) error {
	r, err := zip.OpenReader(srcFile)
	if err != nil {
		return err
	}
	defer r.Close()

	unzipFn := func(zFile *zip.File) error {
		rc, err := zFile.Open()
		if err != nil {
			return err
		}
		defer rc.Close()

		fileDst := filepath.Join(dstDir, zFile.Name)
		if zFile.FileInfo().IsDir() {
			return ctx.Run().MkdirAll(fileDst, zFile.Mode())
		}

		// Make sure the parent directory exists.  Note that sometimes files
		// can appear in a zip file before their directory.
		if err := ctx.Run().MkdirAll(filepath.Dir(fileDst), zFile.Mode()); err != nil {
			return err
		}

		file, err := ctx.Run().OpenFile(fileDst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, zFile.Mode())
		if err != nil {
			return err
		}
		defer file.Close()
		_, err = ctx.Run().Copy(file, rc)
		return err
	}

	ctx.Run().Output([]string{"unzipping " + srcFile})
	for _, zFile := range r.File {
		ctx.Run().Output([]string{"extracting " + zFile.Name})
		if err := unzipFn(zFile); err != nil {
			return err
		}
	}
	return nil
}
<|endoftext|>


<commit_before>package condor

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strings"
	"tes/config"
	pbe "tes/ga4gh"
	"tes/logger"
	sched "tes/scheduler"
	pbr "tes/server/proto"
	"text/template"
)

var log = logger.New("condor")

// prefix is a string prefixed to condor worker IDs, so that condor
// workers can be identified by ShouldStartWorker() below.
// TODO move to worker metadata to be consistent with GCE
const prefix = "condor-"

// NewScheduler returns a new HTCondor Scheduler instance.
func NewScheduler(conf config.Config) sched.Scheduler {
	return &scheduler{conf}
}

type scheduler struct {
	conf config.Config
}

// Schedule schedules a job on the HTCondor queue and returns a corresponding Offer.
func (s *scheduler) Schedule(j *pbe.Job) *sched.Offer {
	log.Debug("Running condor scheduler")

	var disk float64
	for _, v := range j.Task.Resources.Volumes {
		disk += v.SizeGb
	}

	// TODO could we call condor_submit --dry-run to test if a job would succeed?
	w := &pbr.Worker{
		Id: prefix + sched.GenWorkerID(),
		Resources: &pbr.Resources{
			Cpus: j.Task.Resources.MinimumCpuCores,
			Ram:  j.Task.Resources.MinimumRamGb,
			Disk: disk,
		},
	}
	return sched.NewOffer(w, j, sched.Scores{})
}

func (s *scheduler) ShouldStartWorker(w *pbr.Worker) bool {
	return strings.HasPrefix(w.Id, prefix) &&
		w.State == pbr.WorkerState_Uninitialized
}

// StartWorker submits a job via "condor_submit" to start a new worker.
func (s *scheduler) StartWorker(w *pbr.Worker) error {
	log.Debug("Starting condor worker")

	// TODO document that these working dirs need manual cleanup
	workdir := path.Join(s.conf.WorkDir, w.Id)
	workdir, _ = filepath.Abs(workdir)
	os.MkdirAll(workdir, 0755)

	c := s.conf.Worker
	c.ID = w.Id
	c.Timeout = 0
	c.Resources.Cpus = w.Resources.Cpus
	c.Resources.Ram = w.Resources.Ram
	c.Resources.Disk = w.Resources.Disk
	c.Storage = s.conf.Storage

	confPath := path.Join(workdir, "worker.conf.yml")
	c.ToYamlFile(confPath)

	workerPath := sched.DetectWorkerPath()

	submitPath := path.Join(workdir, "condor.submit")
	f, _ := os.Create(submitPath)

	submitTpl, _ := template.New("condor.submit").Parse(`
		universe       = vanilla
		executable     = {{.Executable}}
		arguments      = -config worker.conf.yml
		environment    = "PATH=/usr/bin"
		log            = {{.WorkDir}}/condor-event-log
		error          = {{.WorkDir}}/tes-worker-stderr
		output         = {{.WorkDir}}/tes-worker-stdout
    input          = {{.Config}}
    request_cpus   = {{.CPU}}
    request_memory = {{.RAM}}
    request_disk   = {{.Disk}}
    should_transfer_files   = YES
    when_to_transfer_output = ON_EXIT
		queue
	`)
	submitTpl.Execute(f, map[string]string{
		"Executable": workerPath,
		"WorkDir":    workdir,
		"Config":     confPath,
		"CPU":        fmt.Sprintf("%s", w.Resources.Cpus),
		"RAM":        fmt.Sprintf("%s GB", w.Resources.Ram),
		// Convert GB to KiB
		"Disk": fmt.Sprintf("%s GB", w.Resources.Disk*976562),
	})
	f.Close()

	cmd := exec.Command("condor_submit")
	stdin, _ := os.Open(submitPath)
	cmd.Stdin = stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()

	// TODO better error checking
	return nil
}
<commit_msg>make lint<commit_after>package condor

import (
	"fmt"
	"os"
	"os/exec"
	"path"
	"path/filepath"
	"strings"
	"tes/config"
	pbe "tes/ga4gh"
	"tes/logger"
	sched "tes/scheduler"
	pbr "tes/server/proto"
	"text/template"
)

var log = logger.New("condor")

// prefix is a string prefixed to condor worker IDs, so that condor
// workers can be identified by ShouldStartWorker() below.
// TODO move to worker metadata to be consistent with GCE
const prefix = "condor-"

// NewScheduler returns a new HTCondor Scheduler instance.
func NewScheduler(conf config.Config) sched.Scheduler {
	return &scheduler{conf}
}

type scheduler struct {
	conf config.Config
}

// Schedule schedules a job on the HTCondor queue and returns a corresponding Offer.
func (s *scheduler) Schedule(j *pbe.Job) *sched.Offer {
	log.Debug("Running condor scheduler")

	var disk float64
	for _, v := range j.Task.Resources.Volumes {
		disk += v.SizeGb
	}

	// TODO could we call condor_submit --dry-run to test if a job would succeed?
	w := &pbr.Worker{
		Id: prefix + sched.GenWorkerID(),
		Resources: &pbr.Resources{
			Cpus: j.Task.Resources.MinimumCpuCores,
			Ram:  j.Task.Resources.MinimumRamGb,
			Disk: disk,
		},
	}
	return sched.NewOffer(w, j, sched.Scores{})
}

func (s *scheduler) ShouldStartWorker(w *pbr.Worker) bool {
	return strings.HasPrefix(w.Id, prefix) &&
		w.State == pbr.WorkerState_Uninitialized
}

// StartWorker submits a job via "condor_submit" to start a new worker.
func (s *scheduler) StartWorker(w *pbr.Worker) error {
	log.Debug("Starting condor worker")

	// TODO document that these working dirs need manual cleanup
	workdir := path.Join(s.conf.WorkDir, w.Id)
	workdir, _ = filepath.Abs(workdir)
	os.MkdirAll(workdir, 0755)

	c := s.conf.Worker
	c.ID = w.Id
	c.Timeout = 0
	c.Resources.Cpus = w.Resources.Cpus
	c.Resources.Ram = w.Resources.Ram
	c.Resources.Disk = w.Resources.Disk
	c.Storage = s.conf.Storage

	confPath := path.Join(workdir, "worker.conf.yml")
	c.ToYamlFile(confPath)

	workerPath := sched.DetectWorkerPath()

	submitPath := path.Join(workdir, "condor.submit")
	f, _ := os.Create(submitPath)

	submitTpl, _ := template.New("condor.submit").Parse(`
		universe       = vanilla
		executable     = {{.Executable}}
		arguments      = -config worker.conf.yml
		environment    = "PATH=/usr/bin"
		log            = {{.WorkDir}}/condor-event-log
		error          = {{.WorkDir}}/tes-worker-stderr
		output         = {{.WorkDir}}/tes-worker-stdout
    input          = {{.Config}}
    request_cpus   = {{.CPU}}
    request_memory = {{.RAM}}
    request_disk   = {{.Disk}}
    should_transfer_files   = YES
    when_to_transfer_output = ON_EXIT
		queue
	`)
	submitTpl.Execute(f, map[string]string{
		"Executable": workerPath,
		"WorkDir":    workdir,
		"Config":     confPath,
		"CPU":        fmt.Sprintf("%d", w.Resources.Cpus),
		"RAM":        fmt.Sprintf("%f GB", w.Resources.Ram),
		// Convert GB to KiB
		"Disk": fmt.Sprintf("%f", w.Resources.Disk*976562),
	})
	f.Close()

	cmd := exec.Command("condor_submit")
	stdin, _ := os.Open(submitPath)
	cmd.Stdin = stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Run()

	// TODO better error checking
	return nil
}
<|endoftext|>


<commit_before>package entities

import (
	"encoding/json"
	"fmt"
	"github.com/Vladimiroff/vec2d"
	"math"
	"time"
)

type Planet struct {
	coords              []int
	Texture             int
	Size                int
	LastShipCountUpdate int64
	ShipCount           int
	MaxShipCount        int
	Owner               string
}

func (p *Planet) String() string {
	return fmt.Sprintf("Planet[%s, %s]", p.coords[0], p.coords[1])
}

func (p *Planet) GetKey() string {
	return fmt.Sprintf("planet.%d_%d", p.coords[0], p.coords[1])
}

func (p *Planet) GetCoords() []int {
	return p.coords
}

func (p *Planet) Serialize() (string, []byte, error) {
	_ = p.GetShipCount()
	result, err := json.Marshal(p)
	if err != nil {
		return p.GetKey(), nil, err
	}
	return p.GetKey(), result, nil
}

func (p *Planet) GetShipCount() int {
	if len(p.Owner) > 0 {
		p.UpdateShipCount()
	}
	return p.ShipCount
}

func (p *Planet) SetShipCount(count int) {
	p.ShipCount = count
	p.LastShipCountUpdate = time.Now().Unix()
}

func (p *Planet) UpdateShipCount() {
	passedTime := time.Now().Unix() - p.LastShipCountUpdate
	timeModifier := 5 - (int64(p.Size/3) + 1)
	//TODO: To be completed for all planet size types
	//if getobject(Owner.getkey).gethomeplanet == p.getkey
	p.ShipCount += int(passedTime / (timeModifier * 10))
	p.LastShipCountUpdate = time.Now().Unix()
}

/*
TODO: We need to add ship count on new planet creation
TODO: Put all funny numbers in a constans in our config file
NOTE: 5 in ring_offset is the distance between planets
*/
func GeneratePlanets(hash string, sun_position *vec2d.Vector) ([]*Planet, *Planet) {
	hashElement := func(index int) float64 {
		return float64(hash[index]) - 48 // The offset of simbol "1" in the ascii table
	}

	result := []*Planet{}
	ring_offset := float64(PLANETS_RING_OFFSET)
	planet_radius := float64(PLANETS_PLANET_RADIUS)

	for ix := 0; ix < PLANETS_PLANET_COUNT; ix++ {
		planet_in_creation := Planet{[]int{0, 0}, 0, 0, time.Now().Unix(), 10, 0, ""}
		ring_offset += planet_radius + hashElement(4*ix)*5

		planet_in_creation.coords[0] = int(float64(sun_position.X) + ring_offset*math.Cos(
			hashElement(4*ix+1)*40))
		planet_in_creation.coords[1] = int(float64(sun_position.Y) + ring_offset*math.Sin(
			hashElement(4*ix+1)*40))

		planet_in_creation.Texture = int(hashElement(4*ix + 2))
		planet_in_creation.Size = 1 + int(hashElement(4*ix+3))
		//p.LastShipCountUpdate = time.Now()
		result = append(result, &planet_in_creation)
	}
	// + 1 bellow stands for: after all the planet info is read the next element is the user's home planet idx
	return result, result[int(hashElement(PLANETS_PLANET_COUNT*PLANETS_PLANET_HASH_ARGS+1))]
}
<commit_msg>Ship count calculated as intended<commit_after>package entities

import (
	"encoding/json"
	"fmt"
	"github.com/Vladimiroff/vec2d"
	"math"
	"time"
)

type Planet struct {
	coords              []int
	Texture             int
	Size                int
	LastShipCountUpdate int64
	ShipCount           int
	MaxShipCount        int
	Owner               string
}

func (p *Planet) String() string {
	return fmt.Sprintf("Planet[%s, %s]", p.coords[0], p.coords[1])
}

func (p *Planet) GetKey() string {
	return fmt.Sprintf("planet.%d_%d", p.coords[0], p.coords[1])
}

func (p *Planet) GetCoords() []int {
	return p.coords
}

func (p *Planet) Serialize() (string, []byte, error) {
	_ = p.GetShipCount()
	result, err := json.Marshal(p)
	if err != nil {
		return p.GetKey(), nil, err
	}
	return p.GetKey(), result, nil
}

func (p *Planet) GetShipCount() int {
	if len(p.Owner) > 0 {
		p.UpdateShipCount()
	}
	return p.ShipCount
}

func (p *Planet) SetShipCount(count int) {
	p.ShipCount = count
	p.LastShipCountUpdate = time.Now().Unix()
}

func (p *Planet) UpdateShipCount() {
	passedTime := time.Now().Unix() - p.LastShipCountUpdate
	timeModifier := int64(p.Size/3) + 1
	//TODO: To be completed for all planet size types
	//if getobject(Owner.getkey).gethomeplanet == p.getkey
	p.ShipCount += int(passedTime / (timeModifier * 10))
	p.LastShipCountUpdate = time.Now().Unix()
}

/*
TODO: We need to add ship count on new planet creation
TODO: Put all funny numbers in a constans in our config file
NOTE: 5 in ring_offset is the distance between planets
*/
func GeneratePlanets(hash string, sun_position *vec2d.Vector) ([]*Planet, *Planet) {
	hashElement := func(index int) float64 {
		return float64(hash[index]) - 48 // The offset of simbol "1" in the ascii table
	}

	result := []*Planet{}
	ring_offset := float64(PLANETS_RING_OFFSET)
	planet_radius := float64(PLANETS_PLANET_RADIUS)

	for ix := 0; ix < PLANETS_PLANET_COUNT; ix++ {
		planet_in_creation := Planet{[]int{0, 0}, 0, 0, time.Now().Unix(), 10, 0, ""}
		ring_offset += planet_radius + hashElement(4*ix)*5

		planet_in_creation.coords[0] = int(float64(sun_position.X) + ring_offset*math.Cos(
			hashElement(4*ix+1)*40))
		planet_in_creation.coords[1] = int(float64(sun_position.Y) + ring_offset*math.Sin(
			hashElement(4*ix+1)*40))

		planet_in_creation.Texture = int(hashElement(4*ix + 2))
		planet_in_creation.Size = 1 + int(hashElement(4*ix+3))
		//p.LastShipCountUpdate = time.Now()
		result = append(result, &planet_in_creation)
	}
	// + 1 bellow stands for: after all the planet info is read the next element is the user's home planet idx
	return result, result[int(hashElement(PLANETS_PLANET_COUNT*PLANETS_PLANET_HASH_ARGS+1))]
}
<|endoftext|>


<commit_before>package errors

type ConflictError struct{}

func (e *ConflictError) Error() string {
	return "ConflictError"
}
<commit_msg>ConflictError constructor<commit_after>package errors

func NewConflictError() *ConflictError {
	return &ConflictError{}
}

type ConflictError struct{}

func (e *ConflictError) Error() string {
	return "ConflictError"
}
<|endoftext|>


<commit_before>package errorutil

import (
	"errors"
	"fmt"
)

var _ error = &nestedError{}

type nestedError struct {
	message   string
	errorCode string
	cause     error
}

func (this *nestedError) ErrorCode() string {
	return this.errorCode
}

func (this *nestedError) Error() string {
	if this.cause == nil {
		return fmt.Sprintln(this.message)
	} else {
		return fmt.Sprintln(this.message, "\n caused by: ", this.cause.Error())
	}
}

func New(message string) error {
	return errors.New(message)
}

func NewWithErrorCode(errorCode, message string) error {
	return &nestedError{
		message:   message,
		errorCode: errorCode,
		cause:     nil,
	}
}

func NewNested(message string, cause error) error {
	n := &nestedError{
		message: message,
		cause:   cause,
	}
	return n
}
<commit_msg>error msg<commit_after>package errorutil

import (
	"errors"
	"fmt"
)

var _ error = &nestedError{}

type nestedError struct {
	message   string
	errorCode string
	cause     error
}

func (this *nestedError) ErrorCode() string {
	return this.errorCode
}

func (this *nestedError) Error() string {
	if this.cause == nil {
		return fmt.Sprintln(this.message)
	} else {
		return fmt.Sprintln(this.message, "\n  caused by: ", this.cause.Error())
	}
}

func New(message string) error {
	return errors.New(message)
}

func NewWithErrorCode(errorCode, message string) error {
	return &nestedError{
		message:   message,
		errorCode: errorCode,
		cause:     nil,
	}
}

func NewNested(message string, cause error) error {
	n := &nestedError{
		message: message,
		cause:   cause,
	}
	return n
}
<|endoftext|>


<commit_before>// Simple example for the gosmart libraries.
//
// This is a simple demonstration of how to obtain a token from the smartthings
// API using Oauth2 authorization, and how to request the status of some of your
// sensors (in this case, temperature).
//
// This file is part of gosmart, a set of libraries to communicate with
// the Samsumg SmartThings API using Go (golang).
//
// http://github.com/marcopaganini/gosmart
// (C) 2016 by Marco Paganini <paganini@paganini.net>

package main

import (
	"flag"
	"fmt"
	"github.com/marcopaganini/gosmart"
	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"io/ioutil"
	"log"
)

const (
	defaultPort     = 4567
	tokenFilePrefix = ".example_st_token"
)

var (
	flagClient = flag.String("client", "", "OAuth Client ID")
	flagSecret = flag.String("secret", "", "OAuth Secret")

	config *oauth2.Config
)

func main() {
	flag.Parse()

	// No date on log messages
	log.SetFlags(0)

	// We need client and secret to fetch a new token.
	if *flagClient == "" || *flagSecret == "" {
		log.Fatalf("Must specify Client ID (--client) and Secret (--secret)")
	}

	// Attempt to load token from the local storage. If an error occurs
	// of the token is invalid (expired, etc), trigger the OAuth process.
	tFile := tokenFilePrefix + "_" + *flagClient

	token, err := gosmart.LoadToken(tFile)
	if err != nil || !token.Valid() {
		// Create an OAuth2.Config object and use it to retrieve
		// the token from the SmartThings website.
		config = gosmart.NewOAuthConfig(*flagClient, *flagSecret)
		gst, err := gosmart.NewAuth(defaultPort, config)
		if err != nil {
			log.Fatalln(err)
		}

		fmt.Printf("Please login by visiting http://localhost:%d\n", defaultPort)
		token, err = gst.GetOAuthToken()
		if err != nil {
			log.Fatalln(err)
		}

		// Save new token.
		err = gosmart.SaveToken(tFile, token)
		if err != nil {
			log.Fatalln(err)
		}
	}

	// Create a client with token
	ctx := context.Background()
	client := config.Client(ctx, token)

	// Retrieve Endpoints URI. All future accesses to the smartthings API
	// for this session should use this URL, followed by the desired URL path.
	endpoint, err := gosmart.GetEndPointsURI(client)
	if err != nil {
		log.Fatalln(err)
		return
	}

	// Fetch temperature
	resp, err := client.Get(endpoint + "/temperature")
	if err != nil {
		log.Fatalln()
		return
	}
	contents, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	fmt.Printf("Temperature content: %s\n", contents)

	// Fetch batttery
	resp, err = client.Get(endpoint + "/battery")
	if err != nil {
		log.Fatalln()
		return
	}
	contents, err = ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	fmt.Printf("Battery content: %s\n", contents)
}
<commit_msg>Users can now specify an explicit token file in example.go<commit_after>// Simple example for the gosmart libraries.
//
// This is a simple demonstration of how to obtain a token from the smartthings
// API using Oauth2 authorization, and how to request the status of some of your
// sensors (in this case, temperature).
//
// This file is part of gosmart, a set of libraries to communicate with
// the Samsumg SmartThings API using Go (golang).
//
// http://github.com/marcopaganini/gosmart
// (C) 2016 by Marco Paganini <paganini@paganini.net>

package main

import (
	"flag"
	"fmt"
	"github.com/marcopaganini/gosmart"
	"golang.org/x/net/context"
	"golang.org/x/oauth2"
	"io/ioutil"
	"log"
)

const (
	defaultPort     = 4567
	tokenFilePrefix = ".example_st_token"
)

var (
	flagClient    = flag.String("client", "", "OAuth Client ID")
	flagSecret    = flag.String("secret", "", "OAuth Secret")
	flagTokenFile = flag.String("tokenfile", "", "Token filename")

	config *oauth2.Config
)

func main() {
	flag.Parse()

	// No date on log messages
	log.SetFlags(0)

	// If we have a token file from the command line, use that directly.
	// Otherwise, form the name from tokenFilePrefix and the Client ID.
	tfile := *flagTokenFile
	if tfile == "" {
		if *flagClient == "" {
			log.Fatalf("Must specify Client ID (--client) or Token File (--tokenfile)")
		}
		tfile = tokenFilePrefix + "_" + *flagClient
	}

	// Attempt to load token from the local storage. If an error occurs
	// of the token is invalid (expired, etc), trigger the OAuth process.
	token, err := gosmart.LoadToken(tfile)
	if err != nil || !token.Valid() {
		// Create an OAuth2.Config object and use it to retrieve
		// the token from the SmartThings website. At this point,
		// we need both the ClientID and the Secret.
		if *flagClient == "" || *flagSecret == "" {
			log.Fatal("Need both Client ID (--client) and Secret (--secret) to generate new Token")
		}

		config = gosmart.NewOAuthConfig(*flagClient, *flagSecret)
		gst, err := gosmart.NewAuth(defaultPort, config)
		if err != nil {
			log.Fatalln(err)
		}

		fmt.Printf("Please login by visiting http://localhost:%d\n", defaultPort)
		token, err = gst.GetOAuthToken()
		if err != nil {
			log.Fatalln(err)
		}

		// Save new token.
		err = gosmart.SaveToken(tfile, token)
		if err != nil {
			log.Fatalln(err)
		}
	}

	// Create a client with token
	ctx := context.Background()
	client := config.Client(ctx, token)

	// Retrieve Endpoints URI. All future accesses to the smartthings API
	// for this session should use this URL, followed by the desired URL path.
	endpoint, err := gosmart.GetEndPointsURI(client)
	if err != nil {
		log.Fatalln(err)
		return
	}

	// Fetch temperature
	resp, err := client.Get(endpoint + "/temperature")
	if err != nil {
		log.Fatalln()
		return
	}
	contents, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	fmt.Printf("Temperature content: %s\n", contents)

	// Fetch batttery
	resp, err = client.Get(endpoint + "/battery")
	if err != nil {
		log.Fatalln()
		return
	}
	contents, err = ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	fmt.Printf("Battery content: %s\n", contents)
}
<|endoftext|>


<commit_before>// Copyright 2012 Harry de Boer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"github.com/harrydb/go/img/pnm"
	"image"
	"log"
	"os"
)

func Main() {
	file, err := os.Open("in.ppm")
	if err != nil {
		log.Fatal(err)
	}

	img, format, err := image.Decode(file)

	log.Println("Format: ", format)

	if err != nil {
		log.Fatal(err)
	}

	outFile, err := os.Create("out.ppm")
	if err != nil {
		log.Fatal(err)
	}

	err = pnm.Encode(outFile, img, pnm.PPM)
	if err != nil {
		log.Fatal(err)
	}

}
<commit_msg>Fix build for example -- bad case.<commit_after>// Copyright 2012 Harry de Boer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"github.com/jbuchbinder/gopnm"
	"image"
	"log"
	"os"
)

func main() {
	file, err := os.Open("in.ppm")
	if err != nil {
		log.Fatal(err)
	}

	img, format, err := image.Decode(file)

	log.Println("Format: ", format)

	if err != nil {
		log.Fatal(err)
	}

	outFile, err := os.Create("out.ppm")
	if err != nil {
		log.Fatal(err)
	}

	err = pnm.Encode(outFile, img, pnm.PPM)
	if err != nil {
		log.Fatal(err)
	}

}
<|endoftext|>


<commit_before>package main

import (
	"github.com/scale-it/go-log"
	"os"
)

var Log = log.New()

func main() {
	Log.AddHandler(os.Stderr, log.Levels.Info, log.StdFormatter{"[root]", log.Lshortfile, false})
	Log.AddHandler(os.Stderr, log.Levels.Info, log.StdFormatter{"[with time]", log.Lshortfile | log.Ldate | log.Ltime, false})
	x := 1.23
	Log.Info("x is", x)
	Log.Infof("x is %f and is positive: %v", x, x > 0)
	Log.Debug("This won't be printed")

	Log = log.NewStd(os.Stderr, log.Levels.Debug, log.Ldate|log.Lmicroseconds, true)
	Log.Log(15, "raw message\n")
	Log.Debug("some message")
	Log.Info("some message")
	Log.Warning("some message")
	Log.Error("some message")
	Log.Critical("some message")
}
<commit_msg>Update the examply by adding call source info.<commit_after>package main

import (
	"github.com/scale-it/go-log"
	"os"
)

var Log = log.New()

func main() {
	Log.AddHandler(os.Stderr, log.Levels.Info, log.StdFormatter{"[root]", log.Lshortfile, false})
	Log.AddHandler(os.Stderr, log.Levels.Info, log.StdFormatter{"[with time]", log.Lshortfile | log.Ldate | log.Ltime, false})
	x := 1.23
	Log.Info("x is", x)
	Log.Infof("x is %f and is positive: %v", x, x > 0)
	Log.Debug("This won't be printed")

	Log = log.NewStd(os.Stderr, log.Levels.Debug, log.Ldate|log.Lmicroseconds|log.Lshortfile, true)
	Log.Log(15, "raw message\n")
	Log.Debug("some message")
	Log.Info("some message")
	Log.Warning("some message")
	Log.Error("some message")
	Log.Critical("some message")
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	_ "github.com/couchbaselabs/go_n1ql"
	"log"
	"os"
)

func main() {

	n1ql, err := sql.Open("n1ql", "localhost:8093")
	if err != nil {
		log.Fatal(err)
	}

	err = n1ql.Ping()
	if err != nil {
		log.Fatal(err)
	}

	// Set query parameters
	os.Setenv("n1ql_timeout", "10s")
	ac := []byte(`[{"user": "admin:Administrator", "pass": "asdasd"}]`)
	os.Setenv("n1ql_creds", string(ac))

	name := "dave"
	rows, err := n1ql.Query("select * from contacts unnest contacts.children where contacts.name = ? and children.age > ?", name, 10)
	if err != nil {
		log.Fatal(err)
	}

	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	for rows.Next() {
		var beer string
		if err := rows.Scan(&beer); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned %s : \n", beer)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	// prepared statements with positional args

	stmt, err := n1ql.Prepare("select personal_details, shipped_order_history from users_with_orders where doc_type=? and personal_details.age = ?")

	rows, err = stmt.Query("user_profile", 60)
	if err != nil {
		log.Fatal(err)
	}

	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	for rows.Next() {
		var personal, shipped string
		if err := rows.Scan(&personal, &shipped); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned personal_details: %s shipped_order_history %s : \n", personal, shipped)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	// Exec examples
	result, err := n1ql.Exec("Upsert INTO contacts KEY \"irish3\" VALUES {\"name\":\"irish\", \"type\":\"contact\"}")
	if err != nil {
		log.Fatal(err)
	}

	rowsAffected, err := result.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Rows affected %d", rowsAffected)

	stmt, err = n1ql.Prepare("Upsert INTO contacts KEY ?  VALUES ?")
	if err != nil {
		log.Fatal(err)
	}

	// Map Values need to be marshaled
	value, _ := json.Marshal(map[string]interface{}{"name": "irish", "type": "contact"})
	result, err = stmt.Exec("irish4", value)
	if err != nil {
		log.Fatal(err)
	}

	rowsAffected, err = result.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Rows affected %d", rowsAffected)

	for i := 0; i < 20; i++ {
		key := fmt.Sprintf("irish%d", i)
		result, err = stmt.Exec(key, value)
		if err != nil {
			log.Fatal(err)
		}

		ra, err := result.RowsAffected()
		if err != nil {
			log.Fatal(err)
		}
		rowsAffected += ra
	}

	log.Printf("Total Rows Affected %d", rowsAffected)

	stmt.Close()
	result, err = stmt.Exec("test", "this shouldn't work")
	if err == nil {
		log.Fatal("Statement not closed")
	}

	result, err = n1ql.Exec("delete from contacts use keys ? ", "irish")
	if err != nil {
		log.Fatal(err)
	}

	keys := make([]string, 0)
	for i := 0; i < 20; i++ {
		keys = append(keys, fmt.Sprintf("irish%d", i))
	}

	value, _ = json.Marshal(keys)
	result, err = n1ql.Exec("delete from contacts use keys ?", value)
	if err != nil {
		log.Fatal(err)
	}

}
<commit_msg>Remove redundant code in example<commit_after>package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	_ "github.com/couchbaselabs/go_n1ql"
	"log"
	"os"
)

func main() {

	n1ql, err := sql.Open("n1ql", "localhost:8093")
	if err != nil {
		log.Fatal(err)
	}

	err = n1ql.Ping()
	if err != nil {
		log.Fatal(err)
	}

	// Set query parameters
	os.Setenv("n1ql_timeout", "10s")
	ac := []byte(`[{"user": "admin:Administrator", "pass": "asdasd"}]`)
	os.Setenv("n1ql_creds", string(ac))

	name := "dave"
	rows, err := n1ql.Query("select * from contacts unnest contacts.children where contacts.name = ? and children.age > ?", name, 10)

	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	for rows.Next() {
		var contacts string
		if err := rows.Scan(&contacts); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned %s : \n", contacts)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	// prepared statements with positional args

	stmt, err := n1ql.Prepare("select personal_details, shipped_order_history from users_with_orders where doc_type=? and personal_details.age = ?")

	rows, err = stmt.Query("user_profile", 60)
	if err != nil {
		log.Fatal(err)
	}

	defer rows.Close()
	for rows.Next() {
		var personal, shipped string
		if err := rows.Scan(&personal, &shipped); err != nil {
			log.Fatal(err)
		}
		log.Printf("Row returned personal_details: %s shipped_order_history %s : \n", personal, shipped)
	}

	if err := rows.Err(); err != nil {
		log.Fatal(err)
	}

	// Exec examples
	result, err := n1ql.Exec("Upsert INTO contacts KEY \"irish3\" VALUES {\"name\":\"irish\", \"type\":\"contact\"}")
	if err != nil {
		log.Fatal(err)
	}

	rowsAffected, err := result.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Rows affected %d", rowsAffected)

	stmt, err = n1ql.Prepare("Upsert INTO contacts KEY ?  VALUES ?")
	if err != nil {
		log.Fatal(err)
	}

	// Map Values need to be marshaled
	value, _ := json.Marshal(map[string]interface{}{"name": "irish", "type": "contact"})
	result, err = stmt.Exec("irish4", value)
	if err != nil {
		log.Fatal(err)
	}

	rowsAffected, err = result.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Rows affected %d", rowsAffected)

	for i := 0; i < 20; i++ {
		key := fmt.Sprintf("irish%d", i)
		result, err = stmt.Exec(key, value)
		if err != nil {
			log.Fatal(err)
		}

		ra, err := result.RowsAffected()
		if err != nil {
			log.Fatal(err)
		}
		rowsAffected += ra
	}

	log.Printf("Total Rows Affected %d", rowsAffected)

	stmt.Close()
	result, err = stmt.Exec("test", "this shouldn't work")
	if err == nil {
		log.Fatal("Statement not closed")
	}

	result, err = n1ql.Exec("delete from contacts use keys ? ", "irish")
	if err != nil {
		log.Fatal(err)
	}

	keys := make([]string, 0)
	for i := 0; i < 20; i++ {
		keys = append(keys, fmt.Sprintf("irish%d", i))
	}

	value, _ = json.Marshal(keys)
	result, err = n1ql.Exec("delete from contacts use keys ?", value)
	if err != nil {
		log.Fatal(err)
	}

}
<|endoftext|>