text:
package dataset

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path"
	"sort"
	"testing"
	"time"

	// Caltech Library packages
	"github.com/caltechlibrary/dotpath"
)

var (
	contentType        = "application/json"
	dName              = "testout"
	testCollectionsSrc = []byte(`{
	"attachment_test.ds": {
		"one": {
			"one": 1,
			"two": "too",
			"three": true
		}
	},
	"objects_test.ds": {
		"Miller-A": {
			"id":     "Miller-A",
			"given":  "Arthor",
			"family": "Miller",
			"genre":  ["plays", "film"]
		},
		"Lopez-T": {
			"id":     "Lopez-T",
			"given":  "Tom",
			"family": "Lopez",
			"genre":  ["plays", "radio-theater"]
		}
	},
	"frames_test.ds": {
		"Miller-A": {
			"id":        "Miller-A",
			"given":     "Arthor",
			"family":    "Miller",
			"character": false,
			"vocations": ["playright", "writer", "author"]
		},
		"Lopez-T": {
			"id":        "Lopez-T",
			"given":     "Tom",
			"family":    "Lopez",
			"character": false,
			"vocations": ["playright", "producer", "director", "sound-engineer", "voice actor", "disc jockey"]
		},
		"Flanders-J": {
			"id":          "Flanders-J",
			"given":       "Jack",
			"family":      "Flanders",
			"played-by":   "Robert Lorick",
			"character":   true,
			"description": "Metaphysical Detective"
		},
		"Freda-L": {
			"id":          "Freda-L",
			"given":       "Little",
			"family":      "Freda",
			"played-by":   "P.J. O'Rorke",
			"character":   true,
			"description": "A wise Venusian"
		},
		"Sam-M": {
			"id":          "Sam-M",
			"given":       "Mojo",
			"family":      "Sam",
			"played-by":   "Dave Adams",
			"character":   true,
			"description": "The wise You-do man"
		}
	}
}`)
)

// sameStrings compares one slice of strings to another by converting
// them to a JSON represention and compariing the representation.
func sameStrings(expected []string, got []string) bool {
	src1, _ := json.MarshalIndent(expected, "", "    ")
	src2, _ := json.MarshalIndent(got, "", "    ")
	return bytes.Compare(src1, src2) == 0
}

// sameMapping compares one map to another by converting
// them to a JSON representation and comparing the representation.
func sameMapping(expected map[string]interface{}, got map[string]interface{}) bool {
	src1, _ := json.MarshalIndent(expected, "", "    ")
	src2, _ := json.MarshalIndent(got, "", "    ")
	return bytes.Compare(src1, src2) == 0
}

// sameObjects compares one slice of objects to an another
// by converting them to JSON and comparing the representation.
func sameObjects(expected []map[string]interface{}, got []map[string]interface{}) bool {
	if len(expected) != len(got) {
		return false
	}
	for i := 0; i < len(expected); i++ {
		if !sameMapping(expected[i], got[i]) {
			return false
		}
	}
	return true
}

func makePayload(src []byte) (io.Reader, error) {
	return bytes.NewBuffer(src), nil
}

func makeObjectPayload(o map[string]interface{}) (io.Reader, error) {
	src, err := json.MarshalIndent(o, "", "    ")
	if err != nil {
		return nil, err
	}
	return makePayload(src)
}

func makeRequest(u string, method string, payload io.Reader) (*http.Response, error) {
	req, err := http.NewRequest(method, u, payload)
	if err != nil {
		return nil, err
	}
	req.Header.Set("content-type", contentType)
	client := new(http.Client)
	return client.Do(req)
}

func assertHTTPStatus(expected int, got int) error {
	if got == http.StatusNotImplemented {
		return fmt.Errorf("expected status %s, got %s <- %s",
			http.StatusText(expected), http.StatusText(got), http.StatusText(http.StatusNotImplemented))
	}
	if expected != got {
		return fmt.Errorf("expected http status %q, got %q", http.StatusText(expected), http.StatusText(got))
	}
	return nil
}

func sameObjectSrc(expected []byte, got []byte) bool {
	expectedO := map[string]interface{}{}
	gotO := map[string]interface{}{}
	if err := json.Unmarshal(expected, &expectedO); err != nil {
		return false
	}
	if err := json.Unmarshal(got, &gotO); err != nil {
		return false
	}
	if len(expectedO) != len(gotO) {
		return false
	}
	for k, v := range expectedO {
		gotV, ok := gotO[k]
		if !ok {
			return false
		}
		//FIXME: assumes simple type, i.e. not object or array
		eT := fmt.Sprintf("%T", v)
		gT := fmt.Sprintf("%T", gotV)
		if eT != gT {
			return false
		}
		if v != gotV {
			return false
		}
	}
	return true
}

func clientTestVersion(t *testing.T, settings *Settings) {
	fmt.Printf("starting client test verisons\n")
	// Run through a set of the
	u := fmt.Sprintf("http://%s/api/version", settings.Host)
	res, err := http.Get(u)
	if err != nil {
		t.Errorf("failed to get version of api, %s", err)
		t.FailNow()
	}
	defer res.Body.Close()
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	body, err := io.ReadAll(res.Body)
	if err != nil {
		t.Errorf("%s", err)
		t.FailNow()
	}
	if len(body) == 0 {
		t.Errorf("expected a response body, got %q", body)
		t.FailNow()
	}
}

func clientTestKeys(t *testing.T, settings *Settings) {
	fmt.Printf("starting client test keys\n")

	for _, cfg := range settings.Collections {
		c, err := Open(cfg.CName)
		if err != nil {
			t.Errorf("Open(%q) failed, %s", cfg.CName, err)
			t.FailNow()
		}
		expectedKeys, err := c.Keys()
		if err != nil {
			t.Errorf("c.Key() %s", err)
			t.FailNow()
		}
		cName := path.Base(cfg.CName)
		// Run through a set of the
		u := fmt.Sprintf("http://%s/api/%s/keys", settings.Host, cName)
		res, err := http.Get(u)
		if err != nil {
			t.Errorf("failed to get keys from api, %s", err)
			t.FailNow()
		}
		defer res.Body.Close()
		if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
			t.Error(err)
			t.FailNow()
		}
		body, err := io.ReadAll(res.Body)
		if err != nil {
			t.Errorf("%s", err)
			t.FailNow()
		}
		if len(body) == 0 {
			t.Errorf("expected a response body, got %q", body)
			t.FailNow()
		}
		keys := []string{}
		err = json.Unmarshal(body, &keys)
		if err != nil {
			t.Errorf("failed to unmarshal\n%s\n%s", body, err)
			t.FailNow()
		}
		if len(expectedKeys) != len(keys) {
			t.Errorf("expected %d keys, got %d", len(expectedKeys), len(keys))
			t.FailNow()
		}
		sort.Strings(expectedKeys)
		sort.Strings(keys)
		for i := 0; i < len(expectedKeys); i++ {
			if expectedKeys[i] != keys[i] {
				t.Errorf("expected key %q, got %q", expectedKeys[i], keys[i])
				t.FailNow()
			}
		}
	}
}

func clientTestObjects(t *testing.T, settings *Settings) {
	fmt.Printf("starting client test add objects\n")

	newRecords := map[string]map[string]interface{}{
		"Flanders-J": {
			"name":        "Jack Flanders",
			"played-by":   "Robert Lorick",
			"character":   true,
			"description": "Metaphysical Detective",
		},
		"Freda-L": {
			"name":        "Little Freda",
			"played-by":   "P.J. O'Rorke",
			"character":   true,
			"description": "A wise Venusian",
		},
		"Sam-M": {
			"name":        "Mojo Sam",
			"played-by":   "Dave Adams",
			"character":   true,
			"description": "The wise You-do man",
		},
	}
	newPost := make(map[string][]byte)
	for k, o := range newRecords {
		src, _ := json.MarshalIndent(o, "", "    ")
		newPost[k] = src
	}

	for _, cfg := range settings.Collections {
		cName := path.Base(cfg.CName)
		if cName == "objects_test.ds" {
			//FIXME: only work with object_tests.ds
			for k, v := range newPost {
				// Add an Object
				u := fmt.Sprintf("http://%s/api/%s/object/%s", settings.Host, cName, k)
				body := bytes.NewBuffer(v)
				res, err := makeRequest(u, http.MethodPost, body)
				if err != nil {
					t.Errorf("http.Post(%q, %q, %s) error %s", u, contentType, body, err)
					t.FailNow()
				}
				if err := assertHTTPStatus(http.StatusCreated, res.StatusCode); err != nil {
					t.Error(err)
					t.FailNow()
				}
				// Read Back Object
				res, err = makeRequest(u, http.MethodGet, nil)
				if err != nil {
					t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
					t.FailNow()
				}
				if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
					t.Error(err)
					t.FailNow()
				}
				src, err := ioutil.ReadAll(res.Body)
				res.Body.Close()
				if !sameObjectSrc(v, src) {
					t.Errorf("expected %s, got %s", v, src)
				}

				// We lost Robert Lorick and Dave Adams.
				if k == "Flanders-J" || k == "Sam-M" {
					o := map[string]interface{}{}
					json.Unmarshal(v, &o)
					o["active"] = false
					src, _ := json.Marshal(o)
					body = bytes.NewBuffer(src)
					res, err := makeRequest(u, http.MethodPut, body)
					if err != nil {
						t.Errorf("makeRequest(%q, %q, %s) %s", u, http.MethodPut, body, err)
					}
					if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
						t.Error(err)
						t.FailNow()
					}
				} else if k == "Freda-L" {
					// Little Freda went on to a different plain of existence
					res, _ := makeRequest(u, http.MethodDelete, nil)
					if err != nil {
						t.Errorf("delete failed, %s", err)
					}
					if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
						t.Error(err)
						t.FailNow()
					}
				}
			}
		}
	}
}

func clientTestAttachments(t *testing.T, settings *Settings) {
	cPath := path.Join(dName, "attachment_test.ds")
	cName := path.Base(cPath)
	/*
		c, err := Open(cPath)
		if err != nil {
			t.Errorf("Failed to open test collection %q, %s", cPath, err)
		}
		defer c.Close()
	*/

	// Write out a file to attach, attach it and then test "attachments"
	// route.
	key := "123"
	src := []byte(`one,two,three
1,3,2
4,7,6
10,100,50
131,313,113
`)
	aName := path.Join(dName, "numbers.csv")
	if _, err := os.Stat(aName); err == nil {
		os.RemoveAll(aName)
	}
	if err := ioutil.WriteFile(aName, src, 0664); err != nil {
		t.Errorf("failed to write %q, %s", aName, err)
		t.FailNow()
	}

	// Retrieve the attachments (should be no attachments first time)
	u := fmt.Sprintf("http://%s/api/%s/attachments/%s", settings.Host, cName, key)
	res, err := makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
		t.FailNow()
	}
	defer res.Body.Close()
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	body, err := io.ReadAll(res.Body)
	if err != nil {
		t.Errorf("%s", err)
		t.FailNow()
	}
	if len(body) == 0 {
		t.Errorf("expected a response body, got %q", body)
		t.FailNow()
	}
	l := []string{}
	if err := json.Unmarshal(body, &l); err != nil {
		t.Errorf("expected a list of attachments %q, %s", len(l), err)
		t.FailNow()
	}
	if len(l) > 0 {
		t.Errorf("expected a no attachments, got %+v", l)
		t.FailNow()
	}

	// Adding a new document
	fName := "numbers.csv"
	u = fmt.Sprintf("http://%s/api/%s/attachment/%s/%s", settings.Host, cName, key, fName)
	payload, err := makePayload(src)
	res, err = makeRequest(u, http.MethodPost, payload)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, %q) -> %s", u, http.MethodPost, payload, err)
		t.FailNow()
	}
	defer res.Body.Close()
	if err := assertHTTPStatus(http.StatusCreated, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	body, err = io.ReadAll(res.Body)
	if err != nil {
		t.Errorf("%s", err)
		t.FailNow()
	}
	if len(body) == 0 {
		t.Errorf("expected a response body, got %q", body)
		t.FailNow()
	}

	// Retrieve the attachments (should be one attachment second time)
	u = fmt.Sprintf("http://%s/api/%s/attachments/%s", settings.Host, cName, key)
	res, err = makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
		t.FailNow()
	}
	defer res.Body.Close()
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	body, err = io.ReadAll(res.Body)
	if err != nil {
		t.Errorf("%s", err)
		t.FailNow()
	}
	if len(body) == 0 {
		t.Errorf("expected a response body, got %q", body)
		t.FailNow()
	}
	l = []string{}
	if err := json.Unmarshal(body, &l); err != nil {
		t.Errorf("expected a list of attachments %q, %s", len(l), err)
		t.FailNow()
	}
	if len(l) != 1 {
		t.Errorf("expected a no attachments, got %+v", l)
		t.FailNow()
	}
	if l[0] != path.Base(aName) {
		t.Errorf("expected %q, got, %q", path.Base(aName), l[0])
		t.FailNow()
	}

	// Get the file we added and make sure it looks OK
	for _, filename := range l {
		u = fmt.Sprintf("http://%s/api/%s/attachment/%s/%s", settings.Host, cName, key, filename)
		res, err = makeRequest(u, http.MethodGet, nil)
		if err != nil {
			t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
			t.FailNow()
		}
		defer res.Body.Close()
		if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
			t.Error(err)
			t.FailNow()
		}
		body, err = io.ReadAll(res.Body)
		if err != nil {
			t.Errorf("%s", err)
			t.FailNow()
		}
		if len(body) == 0 {
			t.Errorf("expected a response body, got %q", body)
			t.FailNow()
		}
		outName := path.Join(dName, "download-"+filename)
		if err := ioutil.WriteFile(outName, body, 0664); err != nil {
			t.Errorf("unable to write requested file %q, %s", filename, err)
		}
	}
	for _, filename := range l {
		u = fmt.Sprintf("http://%s/api/%s/attachment/%s/%s", settings.Host, cName, key, filename)
		res, err = makeRequest(u, http.MethodDelete, nil)
		if err != nil {
			t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
			t.FailNow()
		}
		defer res.Body.Close()
		if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
			t.Error(err)
			t.FailNow()
		}
	}
	// Now make sure attachments were all removed.
	u = fmt.Sprintf("http://%s/api/%s/attachments/%s", settings.Host, cName, key)
	res, err = makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) -> %s", u, http.MethodGet, err)
		t.FailNow()
	}
	defer res.Body.Close()
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	body, err = io.ReadAll(res.Body)
	if err != nil {
		t.Errorf("%s", err)
		t.FailNow()
	}
	if len(body) == 0 {
		t.Errorf("expected a response body, got %q", body)
		t.FailNow()
	}
	l = []string{}
	if err := json.Unmarshal(body, &l); err != nil {
		t.Errorf("expected a list of attachments %q, %s", len(l), err)
		t.FailNow()
	}
	if len(l) > 0 {
		t.Errorf("expected a no attachments, got %+v", l)
		t.FailNow()
	}
}

func clientTestFrames(t *testing.T, settings *Settings) {
	cPath := path.Join(dName, "frames_test.ds")
	cName := path.Base(cPath)
	c, err := Open(cPath)
	if err != nil {
		t.Errorf("Failed to open test collection %q, %s", cPath, err)
	}
	defer c.Close()

	// Double check loaded test data
	frmKeys := []string{"Sam-M", "Freda-L", "Flanders-J"}
	attributes := []string{"family", "given"}
	dotPaths := []string{".family", ".given"}
	labels := []string{"Family Name", "Given Name"}
	for _, key := range frmKeys {
		m := map[string]interface{}{}
		if err := c.Read(key, m); err != nil {
			t.Errorf("could not open %q, %s", key, err)
			t.FailNow()
		}
		for _, attr := range attributes {
			if given, ok := m[attr]; !ok {
				t.Errorf("failed to find .%s in %s, %+v", attr, key, m)
			} else if given == "" {
				t.Errorf("failed to find .%s is empty string %s, %+v", attr, key, m)
			}
			expr := fmt.Sprintf(".%s", attr)
			val, err := dotpath.Eval(expr, m)
			if err != nil {
				t.Errorf("dotpath.Eval(%q, %+v) failed, %s", expr, m, err)
			}
			if val == nil {
				t.Errorf("dotpath.Eval(%q, %+v) should not return nil value", expr, m)
				t.FailNow()
			}
		}
	}
	exFrameName := "expected"
	expectedFrm, err := c.FrameCreate(exFrameName, frmKeys, dotPaths, labels, true)
	if err != nil {
		t.Errorf("should be able to created the %q frame, %s", exFrameName, err)
	}
	if expectedFrm == nil {
		t.Errorf("something went really wrong, should have an %q frame", exFrameName)
	}

	// Check to make sure frame does not exist
	frameName := "test_names"
	u := fmt.Sprintf("http://%s/api/%s/has-frame/%s", settings.Host, cName, frameName)
	res, err := makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusNotFound, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	if _, err := ioutil.ReadAll(res.Body); err != nil {
		t.Errorf("expected to read the body of reqest, %s", err)
		t.FailNow()
	}
	res.Body.Close()

	// Create the frame "test_names"
	frameDef := map[string]interface{}{
		"keys":      frmKeys,
		"dot_paths": dotPaths,
		"labels":    labels,
	}
	payload, err := makeObjectPayload(frameDef)
	u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
	res, err = makeRequest(u, http.MethodPost, payload)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, %s) -> %s", u, http.MethodPost, payload, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusCreated, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	// Attempt to read the frame that was just created.
	frm, err := c.FrameRead(frameName)
	if err != nil {
		t.Errorf("frame failed to be created %q, %s", frameName, err)
		t.FailNow()
	}
	if len(frm.ObjectMap) == 0 {
		t.Errorf("something went wrong, frm.ObjectMap should have objects, %+v", frm)
		t.FailNow()
	}
	hasError := false
	for k, v := range frm.ObjectMap {
		if v == nil || len(v.(map[string]interface{})) == 0 {
			t.Errorf("something went wrong, frm.ObjectMap[%q] should have a populated map[string]interface{}, %+v", k, frm.ObjectMap)
			hasError = true
		}
	}
	if hasError {
		t.FailNow()
	}

	// Check if we have the "names" frame.
	u = fmt.Sprintf("http://%s/api/%s/has-frame/%s", settings.Host, cName, frameName)
	res, err = makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	if _, err := ioutil.ReadAll(res.Body); err != nil {
		t.Errorf("expected to read the body of request, %s", err)
	}
	res.Body.Close()

	// List the frames in a collection
	u = fmt.Sprintf("http://%s/api/%s/frames/", settings.Host, cName)
	res, err = makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	src, err := ioutil.ReadAll(res.Body)
	if err != nil {
		t.Errorf("expected to read the body of the request, %s", err)
	}
	res.Body.Close()
	frameList := []string{}
	if err := json.Unmarshal(src, &frameList); err != nil {
		t.Errorf("Unmarshal(%s, %+v) -> %s", src, frameList, err)
	}
	if len(frameList) != 2 {
		t.Errorf("expected two frames defined, got %d", len(frameList))
		t.FailNow()
	}
	foundFrame := false
	for _, name := range frameList {
		if name == frameName {
			foundFrame = true
			break
		}
	}
	if !foundFrame {
		t.Errorf("expected frame %q, it was missing from %+v", frameName, frameList)
		t.FailNow()
	}

	// Get the frame's def
	expectedDef, err := c.FrameDef(frameName)
	if err != nil {
		t.Errorf("expected to find frame def for %q, got %s", frameName, err)
		t.FailNow()
	}
	u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
	res, err = makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	src, err = ioutil.ReadAll(res.Body)
	if err != nil {
		t.Errorf("expected to read the body of the request, %s", err)
	}
	res.Body.Close()
	def := map[string]interface{}{}
	if err := json.Unmarshal(src, &def); err != nil {
		t.Errorf("json.Unmarshal(%s, def) failed, %s", src, err)
		t.FailNow()
	}
	if !sameMapping(expectedDef, def) {
		exSrc, _ := json.MarshalIndent(expectedDef, "", "    ")
		t.Errorf("expected map %s, got %s", exSrc, src)
	}

	expectedKeys := c.FrameKeys(frameName)
	u = fmt.Sprintf("http://%s/api/%s/frame-keys/%s", settings.Host, cName, frameName)
	res, err = makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	src, err = ioutil.ReadAll(res.Body)
	if err != nil {
		t.Errorf("expected to read the body of the request, %s", err)
	}
	res.Body.Close()
	gotKeys := []string{}
	if err := json.Unmarshal(src, &gotKeys); err != nil {
		t.Errorf("json.Unmarshal(%s, gotKeys) failed, %s", src, err)
		t.FailNow()
	}
	if !sameStrings(expectedKeys, gotKeys) {
		exSrc, _ := json.MarshalIndent(expectedKeys, "", "    ")
		t.Errorf("expected map %s, got %s", exSrc, src)
	}

	expectedObjects, err := c.FrameObjects(frameName)
	if err != nil {
		t.Errorf("Could get get %q objects, %s", frameName, err)
		t.FailNow()
	}
	u = fmt.Sprintf("http://%s/api/%s/frame-objects/%s", settings.Host, cName, frameName)
	res, err = makeRequest(u, http.MethodGet, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodGet, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	src, err = ioutil.ReadAll(res.Body)
	if err != nil {
		t.Errorf("expected to read the body of the request, %s", err)
	}
	res.Body.Close()
	gotObjects := []map[string]interface{}{}
	if err := json.Unmarshal(src, &gotObjects); err != nil {
		t.Errorf("json.Unmarshal(%s, gotObjects) failed, %s", src, err)
		t.FailNow()
	}
	if !sameObjects(expectedObjects, gotObjects) {
		exSrc, _ := json.MarshalIndent(expectedObjects, "", "    ")
		t.Errorf("expected map %s, got %s", exSrc, src)
	}

	expectedObjects, err = c.FrameObjects(frameName)
	if err != nil {
		t.Errorf("Could get get %q objects, %s", frameName, err)
		t.FailNow()
	}

	// save old object list
	oldObjects := gotObjects[:]
	// Update a record so we can then try frame-refresh
	key := "Flanders-J"
	buf := bytes.NewBuffer([]byte(`{
    "id":        "Flanders-J",
    "given":     "Capt. Marcelle",
    "family":    "Du Champ",
	"character":   true,
	"description": "Sky Pirate Captain"
}`))
	u = fmt.Sprintf("http://%s/api/%s/object/%s", settings.Host, cName, key)
	res, err = makeRequest(u, http.MethodPut, buf)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodPut, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}

	// Make a refresh call
	u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
	res, err = makeRequest(u, http.MethodPut, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodPut, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}

	// Verify records changed.
	gotObjects, err = c.FrameObjects(frameName)
	if err != nil {
		t.Errorf("Could get get %q objects, %s", frameName, err)
		t.FailNow()
	}
	if sameObjects(oldObjects, gotObjects) {
		t.Errorf("FrameRefresh failed to update objects")
		t.FailNow()
	}

	//FIXME: need tests for reframe
	newKeys, err := c.Keys()
	if err != nil {
		t.Errorf("failed to get keys from %q, %s", cName, err)
		t.FailNow()
	}
	src, err = json.MarshalIndent(newKeys, "", "    ")
	if err != nil {
		t.Errorf("failed to marshal key list, %s", err)
		t.FailNow()
	}
	u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
	buf = bytes.NewBuffer(src)
	res, err = makeRequest(u, http.MethodPut, buf)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodPut, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	updatedKeys := c.FrameKeys(frameName)
	if !sameStrings(newKeys, updatedKeys) {
		t.Errorf("expected keys %+v, got %+v", newKeys, updatedKeys)
		t.FailNow()
	}
	objects, err := c.FrameObjects(frameName)
	if len(objects) != len(newKeys) {
		t.Errorf("expected %d objects, got %d", len(newKeys), len(objects))
		t.FailNow()
	}

	frameName = "expected"
	u = fmt.Sprintf("http://%s/api/%s/frame/%s", settings.Host, cName, frameName)
	res, err = makeRequest(u, http.MethodDelete, nil)
	if err != nil {
		t.Errorf("makeRequest(%q, %q, nil) %s", u, http.MethodDelete, err)
		t.FailNow()
	}
	if err := assertHTTPStatus(http.StatusOK, res.StatusCode); err != nil {
		t.Error(err)
		t.FailNow()
	}
	if c.HasFrame(frameName) {
		t.Errorf("expected frame %q to be deleted, found it", frameName)
		t.FailNow()
	}
}

func TestRunAPI(t *testing.T) {
	if _, err := os.Stat(dName); os.IsNotExist(err) {
		os.MkdirAll(dName, 0775)
	}
	// Setup up a test collection
	fName := path.Join(dName, "settings.json")
	if _, err := os.Stat(fName); err == nil {
		os.RemoveAll(fName)
	}
	settings := new(Settings)
	settings.Host = "localhost:8585"
	testCollections := map[string]interface{}{}
	if err := json.Unmarshal(testCollectionsSrc, &testCollections); err != nil {
		t.Errorf("Failed to unpack text data to populate collections, %s", err)
		t.FailNow()
	}
	for name, testRecords := range testCollections {
		records := map[string]map[string]interface{}{}
		for k, v := range testRecords.(map[string]interface{}) {
			records[k] = v.(map[string]interface{})
		}
		pName := path.Join(dName, name)
		if _, err := os.Stat(pName); err == nil {
			os.RemoveAll(pName)
		}

		dbName := path.Join(pName, "collection.db")
		dsnURI := "sqlite://" + dbName
		if err := SetupApiTestCollection(pName, dsnURI, records); err != nil {
			t.Errorf("Failed to setup test collection %q, %s", pName, err)
			t.FailNow()
		}
		// Setup a test configuration
		cfg := new(Config)
		cfg.CName = pName
		cfg.DsnURI = dsnURI
		cfg.Keys = true
		cfg.Create = true
		cfg.Read = true
		cfg.Update = true
		cfg.Delete = true
		cfg.Attachments = true
		cfg.Attach = true
		cfg.Retrieve = true
		cfg.Prune = true
		cfg.FrameRead = true
		cfg.FrameWrite = true
		settings.Collections = append(settings.Collections, cfg)
	}
	if err := settings.WriteFile(fName, 0664); err != nil {
		t.Errorf("failed to save config %q, %s", fName, err)
		t.FailNow()
	}

	setupWait := "5s"
	wait, _ := time.ParseDuration(setupWait)
	fmt.Printf(`
Launching API Tests at http://%s

Press Ctr-C if tests hang, you should see requests log output

Client testings starts in %s (s = seconds)
`, settings.Host, setupWait)
	appName := os.Args[0]

	// Add some records to our test collection.

	// Send our web service into the back ground so we can run
	// a client test.
	go func() {
		if err := RunAPI(appName, fName); err != nil {
			t.Errorf("Expected API to setup and run, %s", err)
		}
	}()
	time.Sleep(wait)
	clientTestVersion(t, settings)
	clientTestKeys(t, settings)
	clientTestObjects(t, settings)
	clientTestKeys(t, settings)
	clientTestFrames(t, settings)
	clientTestAttachments(t, settings)
}

repository: caltechlibrary/dataset

repo_name: caltechlibrary/dataset


text:
SUMMARY = "maintain multipath block device access"
DESCRIPTION = "\
	These tools are in charge of maintaining the disk multipath device maps and \
	react to path and map events. \
	If you install this package you may have to change the way you address block \
	devices. See README.Debian for details. \
"
HOMEPAGE = "http://christophe.varoqui.free.fr/"
PR = "r0"
inherit debian-package

LICENSE = "LGPLv2+ & GPLv2+"
LIC_FILES_CHKSUM = "\
	file://COPYING;md5=7be2873b6270e45abacc503abbe2aa3d \
	file://libmultipath/parser.c;beginline=1;endline=18;md5=4207fa3fb9902e3dd287cd489545c80b"
inherit autotools-brokensep
DEPENDS += "libaio lvm2 readline"

#follow debian/rules
export DISABLE_SYSTEMD = "1"

#install follow Debian jessie
do_install() {
	oe_runmake install LIB=${base_libdir} DESTDIR=${D}

	install -m 0755 ${S}/debian/multipath-tools.init \
		${D}${sysconfdir}/init.d/multipath-tools
	install -m 0755 ${S}/debian/multipath-tools.multipath-tools-boot.init \
		${D}${sysconfdir}/init.d/multipath-tools-boot
	install -m 0755 ${S}/debian/dmsetup_env ${D}${base_libdir}/udev/
	install -D -m 0644 ${S}/kpartx/kpartx.rules \
		${D}${base_libdir}/udev/rules.d/60-kpartx.rules
	install -m 0644 ${S}/debian/multipath.udev \
		${D}${base_libdir}/udev/rules.d/60-multipath.rules

	# initramfs stuff:
	install -D -m 755 ${S}/debian/initramfs/hooks \
		${D}${datadir}/initramfs-tools/hooks/multipath
	install -D -m 755 ${S}/debian/initramfs/local-top \
		${D}${datadir}/initramfs-tools/scripts/local-top/multipath
	install -D -m 755 ${S}/debian/initramfs/init-top \
		${D}${datadir}/initramfs-tools/scripts/init-top/multipath

	# reportbug:
	for pkg in "multipath-tools" "multipath-tools-boot"; do \
		install -D -m 755 ${S}/debian/reportbug/script \
			${D}${datadir}/bug/$pkg/script; \
	done

	rm ${D}${sysconfdir}/udev/rules.d/kpartx.rules
	LINKLIB=$(basename $(readlink ${D}${base_libdir}/libmpathpersist.so))
	rm ${D}${base_libdir}/libmpathpersist.so
	ln -sf $LINKLIB ${D}${base_libdir}/libmpathpersist.so
}
PACKAGES =+ "kpartx ${PN}-boot"

FILES_kpartx = "\
	${base_libdir}/udev/kpartx ${base_libdir}/udev/kpartx_id \
	${base_libdir}/udev/rules.d/60-kpartx.rules ${base_sbindir}/kpartx \
	${base_libdir}/udev/dmsetup_env \
"
FILES_${PN} += "\
	${datadir}/bug/multipath-tools ${base_libdir}/multipath \
"
FILES_${PN}-boot = "\
	${datadir}/bug/multipath-tools-boot ${datadir}/initramfs-tools \
"
FILES_${PN}-dbg += "${base_libdir}/multipath/.debug/*"

#follow debian/control
RDEPENDS_${PN} += "sysvinit-initscripts udev"
RDEPENDS_kpartx += "dmsetup udev"
RDEPENDS_${PN}-boot += "${PN}"

license: MIT

repository: nghiaht-tsdv/meta-debian

license_type: permissive

repo_name: nghiaht-tsdv/meta-debian

branch_name: refs/heads/jethro

gha_language: BitBake

extension: bb


text:
@inproceedings{calliess_towards_2012,
	title = {Towards auction-based multi-agent collision-avoidance under continuous stochastic dynamics},
	abstract = {We describe an approach to multi-agent planning under continuous stochastic dynamics. The approach yields collision-free state tra jectories with adjustably high certainty, while aiming for low social cost. To this end we describe a collision-detection module based on a distribution-independent probabilistic bound. We employ an optimization-based approach to incrementally alter plans until all collisions are avoided with sufficiently high confidence. We consider the case of feedback controlled agents, and alter plans by introducing new setpoints to the agents’ controllers. In the context of a simple stochastic path planning scenario, we compare two alternative strategies for finding such setpoints.
Due to their practical importance, multi-agent collision avoidance and control have been extensively studied across different communities including AI, robotics and control. However, these works typically assume linear and discrete dynamic models; by contrast, our work intends to overcome these limitations and to present solutions for continuous state space. While our current experiments were conducted with linear stochastic differential equation (SDE) models with state-independent noise (yielding Gaussian processes) our method is also applicable to non-Gaussian cases with state-dependent uncertainties.
To ensure collision avoidance yields low social cost across the entire agent collective, we compare two different coordination mechanisms. Firstly, we consider a simple fixed-priority scheme Erdmann \& Lozano-Perez (1987), and secondly, we modify an auction-based coordination protocol Calliess et al. (2011) to work in our continuous setting. In contrast to pre-existing work in auction-style multi-agent planning (e.g. Tovey et al. (2005); Lagoudakis et al. (2005); Calliess et al. (2011)) and multi-agent collision avoidance (e.g. Kostic et al. (2010); Ayanian \& Kumar (2010)), we avoid a priori discretizations of space and time and present solutions for continuous time and state space.},
	booktitle = { {ICML}-2012, {Workshop} on {Markets}, {Mechanisms}, and {Multi}-{Agent} {Models}-{Examining} the {Interaction} of {Machine} {Learning} and {Economics}},
	author = {Calliess, Jan-P. and Osborne, Michael A. and Roberts, Stephen J.},
	year = {2012},
	keywords = {workshop},
	file = {1668/calliess_ICML_workshop_2012.pdf}
}

@inproceedings{calliess_towards_2012-1,
	title = {Towards optimization-based multi-agent collision-avoidance under continuous stochastic dynamics},
	abstract = {In our ongoing work, we aim to control a team of agents so as to achieve a prescribed goal state while being confident that collisions with other agents are avoided. Each agent is associated with a feedback controlled plant, whose continuous state trajectories follow some stochastic differential dynamics. To this end we describe a collision-detection module based on a distribution-independent probabilistic bound and employ a fixed priority method to resolve collisions. Due to their practical importance, multi-agent collision avoidance and control have been extensively studied across different communities including AI, robotics and control. However, these works typically assume linear and discrete dynamic models; by contrast, our work intends to overcome these limitations and to present solutions for continuous state space. While our current experiments were conducted with linear stochastic differential equation (SDE) models with state-independent noise (yielding Gaussian processes) we believe that our approach could also be applicable to nonGaussian cases with state-dependent uncertainties.},
	booktitle = {Workshops at the {Twenty}-{Sixth} {AAAI} {Conference} on {Artificial} {Intelligence}},
	author = {Calliess, Jan-Peter and Osborne, Michael A. and Roberts, Stephen},
	year = {2012},
	keywords = {workshop},
	file = {1935/TowardsOptimization.pdf}
}

@inproceedings{osborne_machine_2011,
	title = {A machine learning approach to pattern detection and prediction for environmental monitoring and water sustainability},
	abstract = {We describe one of the successful products of a research partnership among several academic institutions (CMU, Oxford and UBC) and a water monitoring company (Aquatic Informatics). Water monitoring sensors are very diverse and remotely distributed. They produce vast quantities of data. The data itself is nonlinear and nonstationary. In addition, unanticipated environmental conditions and limitations in the sensing and communications hardware cause the data to be corrupted by previously uncharacterized nonlinearities, missing observations, spikes and multiple discontinuities. To improve the quality of the data and the monitoring process, this paper introduces an approach that uses Gaussian processes and a general “fault bucket” to capture a priori uncharacterized faults, along with an approximate method for marginalizing the potential faultiness of all observations. This gives rise to an efficient, flexible algorithm for the detection and automatic correction of faults. The probabilistic nature of the method is ideal for reporting uncertainty estimates to human operators. The approach can also be applied to detect patterns, other than faults, which are of great environmental significance. We present a fish sustainability example, where specific patterns in water level need to be detected so that fish don’t get trapped and die in shallow pools.},
	booktitle = {Proc. {Workshop} on {Machine} {Learning} for {Global} {Challenges}},
	author = {Osborne, Michael A. and Garnett, Roman and Swersky, Kevin and de Freitas, Nando},
	year = {2011},
	note = {https://is.gd/9M0ZYE},
	keywords = {workshop},
	file = {1628/osborne_et_al_mlgc_2011.pdf}
}

@inproceedings{rainforth_bayesian_2015,
	title = {Bayesian {Optimization} for {Probabilistic} {Programs}},
	url = {http://www.robots.ox.ac.uk/~twgr/assets/pdf/rainforth2015BOPP.pdf},
	abstract = {We outline a general purpose framework for black-box marginal maximum a pos- teriori estimation of probabilistic program variables using Bayesian optimization with Gaussian processes.  We introduce the concept of an optimization query, whereby a probabilistic program returns an infinite lazy sequence of increasingly optimal estimates, and explain how a general purpose program transformation would allow the evidence of any probabilistic program, and therefore any graphical model, to be optimized with respect to an arbitrary subset of its variables.},
	booktitle = {Workshop on "{Black} {Box} {Learning} and {Inference}" at {NIPS} 2015},
	author = {Rainforth, Tom and van de Meent, Jan-Willem and Osborne, Michael A. and Wood, Frank},
	year = {2015},
	keywords = {workshop},
	file = {1088/rainforth2015BOPP.pdf}
}

@inproceedings{gibson_nir_2011,
	title = { {NIR} {Transmission} {Spectra} of {HD}189733: {Application} of {Gaussian} {Processes} for {Removing} {Systematics}},
	volume = {2},
	url = {http://goo.gl/4DmFW4},
	abstract = {The interpretation of HST transmission spectroscopy signals has recently
been the subject of much debate, in particular the NIR NICMOS data of HD
189733. At optical wavelengths, a high-altitude haze has been confirmed
with both STIS and ACS, whereas the presence of molecules has been
claimed with NICMOS. However, this detection of molecules has been
disputed based on the ad hoc model used to remove the systematics, the
choice of which changes the interpretation of the transmission signal.
Here, we introduce a powerful new technique, Gaussian Processes (GPs),
to model the systematics and simultaneously extract the transmission
spectrum, and demonstrate its application to the NICMOS data. GPs are a
Bayesian technique widely used in the machine learning community, which
allow us to define a distribution over functions. Rather than impose a
strict, functional form of systematics correction, we marginalise over
potentially infinite numbers of basis functions, effectively inferring
the form of the systematics correction from the data itself. This
results in a more robust interpretation of the signal. We also present
similar analyses of HST/WFC3 observations of HD 189733, which bridge the
gap between the current optical and NIR spectrum.},
	booktitle = { {AAS}/{Division} for {Extreme} {Solar} {Systems} {Abstracts}},
	author = {Gibson, Neale and Aigrain, Suzanne and Roberts, Stephen J. and Evans, Tom and Osborne, Michael A. and Pont, Frederic and Sing, David K.},
	year = {2011},
	keywords = {workshop},
	pages = {1106}
}

@inproceedings{nyikosa_adaptive_2015,
	title = {Adaptive {Bayesian} {Optimisation} for {Online} {Portfolio} {Selection}},
	abstract = {We present a Bayesian approach for online portfolio selection, a fundamental problem in computational finance. We pose the problem as the global optimi- sation of an expensive, time-varying, black-box function. As the optimum is itself dynamic, we use a model that allows us to capture time-dependent patterns of the function and to provide sequential decision processes that enable us to select optimal portfolios to invest in an online manner.},
	booktitle = {Workshop on {Bayesian} {Optimization} at {NIPS} 2015},
	author = {Nyikosa, Favour and Osborne, Michael A. and Roberts, Stephen J.},
	year = {2015},
	keywords = {workshop},
	file = {1566/NyikosaOsborneRobertsNipsBayesopt2015.pdf}
}

@inproceedings{swersky_raiders_2013,
	title = {Raiders of the lost architecture\_ {Kernels} for {Bayesian} optimization in conditional parameter spaces},
	abstract = {In practical Bayesian optimization, we must often search over structures with differing numbers of parameters. For instance, we may wish to search over neural network architectures with an unknown number of layers. To relate performance data gathered for different architectures, we define a new kernel for conditional parameter spaces that explicitly includes information about which parameters are relevant in a given structure. We show that this kernel improves model quality and Bayesian optimization results over several simpler baseline kernels.},
	booktitle = { {NIPS} workshop on {Bayesian} {Optimization} in theory and practice ({BayesOpt}’13)},
	author = {Swersky, Kevin and Duvenaud, David and Snoek, Jasper and Hutter, Frank and Osborne, Michael A.},
	year = {2013},
	keywords = {preprint},
	file = {1173/hier-kern-workshop.pdf}
}

@inproceedings{aigrain_gaussian_2011,
	title = {Gaussian {Processes} : the {Next} {Step} in {Exoplanet} {Data} {Analysis}},
	volume = {2},
	abstract = {When searching for or characterising exoplanets, we typically need to isolate a deterministic signal from stochastic processes-astrophysical or instrumental" noise"-in time-series data. Gaussian processes (GPs) enable us to construct distributions over random functions, and to infer the properties of" signal" and" noise" in a way that is both flexible and robust.},
	booktitle = { {AAS}/{Division} for {Extreme} {Solar} {Systems} {Abstracts}},
	author = {Aigrain, Suzanne and Gibson, Neale P. and Roberts, Stephen J. and Evans, Tom and McQuillan, Amy and Reece, Steve and Osborne, Michael A.},
	year = {2011},
	keywords = {workshop},
	pages = {1105},
	file = {2053/algrain_suzanne.pdf}
}

@inproceedings{fruehwirt_gaussian_2017,
	title = {Gaussian process classification from multivariate spatio-temporal brain potential patterns in {Alzheimer}’s disease},
	abstract = {The diagnosis of Alzheimer’s disease (AD) in routine clinical practice is most commonly based on subjective clinical interpretations. Event-related potentials (ERPs) have been shown to reflect neu- rodegenerative processes in AD and might qualify as affordable and thereby widely available markers to facilitate the objectivization of AD assessment. Here, we present a novel method combining multivariate pattern analysis (MVPA) and Gaussian process classification (GP) and aim to de- velop ERP markers for two crucial AD classification problems, i.e., the prediction of rapid cognitive decline (RCD) and the distinction between carriers and non-carriers of the ε4 allele of the apolipoprotein E gene, the main genetic risk factor for AD.},
	booktitle = { {IJCAI} {BOOM} workshop},
	author = {Fruehwirt, Wolfgang and Zhang, Pengfei and Gerstgrasser, Matthias and Osborne, Michael A and Grossegger, Dieter and Schmidt, Reinhold and Benke, Thomas and Dal-Bianco, Peter and Ransmayr, Gerhard and Weydemann, Leonard and Garn, Heinrich and Waser, Markus and Roberts, Stephen J. and Dorffner, Georg},
	year = {2017},
	file = {2576/GPC_from_multivariate_spatio-temporal_brain_potential_patterns_in_AD.pdf}
}

@inproceedings{zhang_sensor_2017,
	title = {Sensor {Selection} and {Random} {Field} {Reconstruction} for {Robust} and {Cost}-effective {Heterogeneous} {Weather} {Sensor} {Networks} for the {Developing} {World}},
	url = {http://arxiv.org/abs/1711.04308},
	abstract = {We address the two fundamental problems of spatial field reconstruction and sensor selection in heterogeneous sensor networks: (i) how to efficiently perform spatial field reconstruction based on measurements obtained simultaneously from networks with both high and low quality sensors; and (ii) how to perform query based sensor set selection with predictive MSE performance guarantee. For the first problem, we developed a low complexity algorithm based on the spatial best linear unbiased estimator (S-BLUE). Next, building on the S-BLUE, we address the second problem, and develop an efficient algorithm for query based sensor set selection with performance guarantee. Our algorithm is based on the Cross Entropy method which solves the combinatorial optimization problem in an efficient manner.},
	urldate = {2017-11-17},
	booktitle = { {NIPS} 2017 {Workshop} on {Machine} {Learning} for the {Developing} {World}},
	author = {Zhang, Pengfei and Nevat, Ido and Peters, Gareth W. and Fruehwirt, Wolfgang and Huang, Yongchao and Anders, Ivonne and Osborne, Michael},
	month = nov,
	year = {2017},
	keywords = {Statistics - Machine Learning, Electrical Engineering and Systems Science - Signal Processing},
	file = {2734/Zhang et al. - 2017 - Sensor Selection and Random Field Reconstruction f.pdf}
}

repository: mosb/robots-www

repo_name: mosb/robots-www

branch_name: refs/heads/master

gha_license_id: NOASSERTION

gha_language: TeX

extension: bib


text:
@inproceedings{PlancherParallelDDP,
  title={A performance analysis of parallel differential dynamic programming on a GPU},
  author={Plancher, Brian and Kuindersma, Scott},
  booktitle={International Workshop on the Algorithmic Foundations of Robotics (WAFR)},
  pages={656--672},
  year={2018},
  address = {Merida, Mexico},
  month={Dec.},
  organization={Springer},
  isbn={978-3-030-44051-0}
}

gha_license_id: MIT


text:
package main

import (
	"image/color"

	oak "github.com/oakmound/oak/v2"
	"github.com/oakmound/oak/v2/entities"
	"github.com/oakmound/oak/v2/render"
	"github.com/oakmound/oak/v2/scene"
)

func main() {
	oak.Add("platformer", func(string, interface{}) {

		char := entities.NewMoving(100, 100, 16, 32,
			render.NewColorBox(16, 32, color.RGBA{255, 0, 0, 255}),
			nil, 0, 0)

		render.Draw(char.R)

	}, func() bool {
		return true
	}, func() (string, *scene.Result) {
		return "platformer", nil
	})
	oak.Init("platformer")
}

repository: lolbinarycat/oak

repo_name: lolbinarycat/oak

gha_language: Go

extension: go


text:
package db

import (
	"github.com/decentralized-identity/kerigo/pkg/event"
)

type DB interface {
	Put(k string, v []byte) error
	Get(k string) ([]byte, error)

	LogEvent(e *event.Message, first bool) error
	LogTransferableReceipt(vrc *event.Receipt) error
	LogNonTransferableReceipt(rct *event.Receipt) error

	EscrowPendingEvent(e *event.Message) error
	RemovePendingEscrow(prefix string, sn int, dig string) error

	EscrowOutOfOrderEvent(e *event.Message) error
	EscrowLikelyDuplicitiousEvent(e *event.Message) error

	LogSize(pre string) int
	StreamEstablisment(pre string, handler func(*event.Message) error) error
	StreamAsFirstSeen(pre string, handler func(*event.Message) error) error
	StreamBySequenceNo(pre string, handler func(*event.Message) error) error
	StreamPending(pre string, handler func(*event.Message) error) error
	StreamTransferableReceipts(pre string, sn int, handler func(quadlet []byte) error) error

	Seen(pre string) bool
	Inception(pre string) (*event.Message, error)
	CurrentEvent(pre string) (*event.Message, error)
	CurrentEstablishmentEvent(pre string) (*event.Message, error)
	EventAt(prefix string, sequence int) (*event.Message, error)

	LastAcceptedDigest(pre string, seq int) ([]byte, error)

	Close() error
}

repository: decentralized-identity/kerigo

repo_name: decentralized-identity/kerigo


text:
DESCRIPTION = "Icecast source client"
LICENSE = "GPLv2"

DEPENDS = "libvorbis libogg libxml2 libshout"

PR = "r1"

SRC_URI = "http://downloads.us.xiph.org/releases/ices/ices-${PV}.tar.gz"

EXTRA_OECONF = "--with-ogg=${STAGING_LIBDIR} --with-vorbis=${STAGING_LIBDIR}"

S = "${WORKDIR}/ices-${PV}"
inherit autotools

repository: overo/overo-oe-natty

repo_name: overo/overo-oe-natty

branch_name: HEAD


text:
// (C) Copyright 2018 Hewlett Packard Enterprise Development LP.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

package onesphere

import (
	"encoding/json"
	"github.com/HewlettPackard/hpe-onesphere-go/rest"
)

type ServiceType = NamedUriIdentifier

type ServiceTypeList struct {
	Total   int           `json:"total"`
	Members []ServiceType `json:"members"`
}

// GetServiceTypes returns a list of all Service Types
func (c *Client) GetServiceTypes() (ServiceTypeList, error) {
	var (
		uri          = "/rest/service-types"
		serviceTypes ServiceTypeList
	)

	response, err := c.RestAPICall(rest.GET, uri, nil, nil)

	if err != nil {
		return serviceTypes, err
	}

	if err := json.Unmarshal([]byte(response), &serviceTypes); err != nil {
		return serviceTypes, apiResponseError(response, err)
	}

	return serviceTypes, err
}

// GetServiceTypeByID returns a Service Type by ID
func (c *Client) GetServiceTypeByID(id string) (ServiceType, error) {
	var (
		uri     = "/rest/service-types/" + id
		service ServiceType
	)

	response, err := c.RestAPICall(rest.GET, uri, nil, nil)

	if err != nil {
		return service, err
	}

	if err := json.Unmarshal([]byte(response), &service); err != nil {
		return service, apiResponseError(response, err)
	}

	return service, err
}

repository: HewlettPackard/hpe-onesphere-go

repo_name: HewlettPackard/hpe-onesphere-go


text:
package ntt

import (
	"context"
	"fmt"
	"sort"

	"github.com/nokia/ntt/internal/errors"
	"github.com/nokia/ntt/internal/loc"
	"github.com/nokia/ntt/internal/ttcn3/ast"
	"github.com/nokia/ntt/internal/ttcn3/token"
)

// Symbols
func (suite *Suite) symbols(syntax *ParseInfo) *Module {
	syntax.handle = suite.store.Bind(syntax.ID(), func(ctx context.Context) interface{} {

		b := &builder{
			fset:   syntax.FileSet,
			scopes: make(map[*ast.Ident]Scope),
			types:  make(map[ast.Expr]Type),
		}
		b.currScope = &b.scope

		b.define(syntax.Module)
		b.resolve(syntax.Module)

		// This should not happen, so a panic is fine, I guess.
		if len(b.mods) != 1 {
			panic("exptecting only one module")
		}

		b.mods[0].Scopes = b.scopes
		b.mods[0].Types = b.types

		return b.mods[0]
	})

	v := syntax.handle.Get(context.TODO())
	return v.(*Module)
}

type Scope interface {

	// Insert attemps to insert an object obj into the Scope. If the scope
	// already contains an alternative object alt with the same name, Insert
	// leaves the scope unchanged and returns altnative object. Otherwise it
	// inserts obj, sets the object's parent scope, if not already set, and
	// returns nil.
	Insert(obj Object) Object

	// Lookup returns the object for a given name. Lookup may follow scope chains.
	Lookup(name string) Object

	// Names lists all names defined in this scope.
	Names() []string
}

// Object describes a named language entity, such as a function or const.
type Object interface {
	Name() string // Object name.

	// Parent returns the (lexical) scope the object is defined in.
	Parent() Scope

	// Type returns the type of the object.
	Type() Type

	// setParent sets the scope the object is defined in.
	setParent(s Scope)

	// Node returns the representing AST node of the object.
	Node() ast.Node

	Range
}

// Range interface is identical to ast.Node interface and helps handling source
// code locations.
type Range interface {
	Pos() loc.Pos
	End() loc.Pos
}

// Module describes a Module.
type Module struct {
	object
	scope

	Imports []string
	Scopes  map[*ast.Ident]Scope
	Types   map[ast.Expr]Type

	fset *loc.FileSet
}

func NewModule(n *ast.Module, name string) *Module {
	return &Module{
		object: object{
			node: n,
			name: name,
		},
		Scopes: make(map[*ast.Ident]Scope),
		Types:  make(map[ast.Expr]Type),
	}
}

func (m *Module) Lookup(name string) Object {
	// m.scope.Lookup does not climb up scope chains. When obj != nil we know
	// the scope is m.scope.
	// However we must return m to make sure clients can a type assertions, like
	// 		scp.(*ntt.Module).Name()
	if obj := m.scope.Lookup(name); obj != nil {
		return obj
	}
	return Universe.Lookup(name)
}

// Func describes testcases, altsteps, functions and external functions.
type Func struct {
	object
	external bool
}

func NewFunc(n *ast.FuncDecl, name string) *Func {
	return &Func{
		object: object{
			node: n,
			name: name,
		},
	}
}

// Import describes the view to an imported module.
type Import struct {
	object
	module string
}

func NewImport(n *ast.ImportDecl, name string, module string) *Import {
	return &Import{
		object: object{
			node: n,
			name: name,
		},
		module: module,
	}
}

// Var describes an object, which can hold an value. This could be a local
// variable, a constant, a module parameter or a template.
type Var struct {
	object
}

func NewVar(n ast.Node, name string) *Var {
	return &Var{
		object: object{
			node: n,
			name: name,
		},
	}
}

type TypeName struct {
	object
}

func NewTypeName(n ast.Node, name string, typ Type) *TypeName {
	return &TypeName{
		object: object{
			node: n,
			name: name,
			typ:  typ,
		},
	}
}

// object implements the common parts of an Object
type object struct {
	node   ast.Node
	name   string
	parent Scope
	typ    Type
}

// Object interface

func (obj *object) Name() string      { return obj.name }
func (obj *object) Parent() Scope     { return obj.parent }
func (obj *object) Type() Type        { return obj.typ }
func (obj *object) setParent(s Scope) { obj.parent = s }

func (obj *object) Node() ast.Node { return obj.node }

// Range interface

func (obj *object) Pos() loc.Pos { return obj.node.Pos() }
func (obj *object) End() loc.Pos { return obj.node.End() }

type LocalScope struct {
	pos, end loc.Pos
	parent   Scope
	scope
}

func NewLocalScope(rng Range, parent Scope) *LocalScope {
	return &LocalScope{
		pos:    rng.Pos(),
		end:    rng.End(),
		parent: parent,
	}
}

func (ls *LocalScope) Lookup(name string) Object {
	if obj := ls.scope.Lookup(name); obj != nil {
		return obj
	}

	// Ascend into parent scopes.
	if ls.parent != nil {
		return ls.parent.Lookup(name)
	}

	return nil
}

// scope implements the common parts of Scope
type scope struct {
	elems map[string]Object
}

func (s *scope) Insert(obj Object) Object {
	name := obj.Name()
	if alt := s.elems[name]; alt != nil {
		return alt
	}
	if s.elems == nil {
		s.elems = make(map[string]Object)
	}

	s.elems[name] = obj
	return nil
}

func (s *scope) Lookup(name string) Object {
	if obj := s.elems[name]; obj != nil {
		return obj
	}

	return nil
}

func (s *scope) Names() []string {
	names := make([]string, len(s.elems))
	i := 0
	for name := range s.elems {
		names[i] = name
		i++
	}
	sort.Strings(names)
	return names
}

type builder struct {
	fset *loc.FileSet
	errs errors.ErrorList
	mods []*Module

	scope
	currScope Scope

	// scopes associates every referencing identifier with its current scope.
	scopes map[*ast.Ident]Scope

	// types assiciates every expression with its (calculated) type.
	types map[ast.Expr]Type
}

// define builds a scope tree. ~efinitionis are inserted in their enclosing
// scope. All referencing identifiers will be associated with current scope for
// later resolving.
func (b *builder) define(n ast.Node) {
	ast.Apply(n, b.defineEnter, nil)
}

func (b *builder) defineEnter(c *ast.Cursor) bool {
	switch n := c.Node().(type) {

	case *ast.Ident:
		b.scopes[n] = b.currScope
		return true

	case *ast.BlockStmt:
		b.currScope = NewLocalScope(n, b.currScope)
		for i := range n.Stmts {
			b.define(n.Stmts[i])
		}
		b.currScope = b.currScope.(*LocalScope).parent
		return false

	case *ast.ForStmt:
		b.currScope = NewLocalScope(n, b.currScope)
		b.define(n.Init)
		b.define(n.Cond)
		b.define(n.Post)
		b.define(n.Body)
		b.currScope = b.currScope.(*LocalScope).parent
		return false

	case *ast.Module:
		mod := NewModule(n, n.Name.String())
		b.mods = append(b.mods, mod)
		b.insert(mod)
		b.currScope = mod
		for i := range n.Defs {
			b.define(n.Defs[i])
		}
		b.define(n.With)
		b.currScope = mod.Parent()
		return false

	case *ast.ImportDecl:
		mod := b.mods[len(b.mods)-1]
		mod.Imports = append(mod.Imports, n.Module.String())
		return false

	case *ast.ValueDecl:
		b.define(n.Type)
		err := ast.Declarators(n.Decls, b.fset, func(decl ast.Expr, name ast.Node, arrays []ast.Expr, value ast.Expr) {
			v := NewVar(decl, identName(name))
			b.insert(v)
			for i := range arrays {
				b.define(arrays[i])
			}
			b.define(value)
		})

		// Add syntax errors to the error list
		if err != nil {
			for _, e := range err.List() {
				b.errs = append(b.errs, e)
			}
		}

		b.define(n.With)
		return false

	case *ast.TemplateDecl:
		sym := NewVar(n, n.Name.String())
		b.insert(sym)
		if n.TypePars != nil {
			b.currScope = NewLocalScope(n.TypePars, b.currScope)
			b.define(n.TypePars)
		}
		b.define(n.Type)
		b.define(n.Base)
		if n.Params != nil {
			b.currScope = NewLocalScope(n.Params, b.currScope)
			b.define(n.Params)
		}
		b.define(n.Value)
		b.define(n.With)

		if n.Params != nil {
			b.currScope = b.currScope.(*LocalScope).parent
		}

		if n.TypePars != nil {
			b.currScope = b.currScope.(*LocalScope).parent
		}
		return false

	case *ast.FuncDecl:
		sym := NewFunc(n, n.Name.String())
		b.insert(sym)
		if n.TypePars != nil {
			b.currScope = NewLocalScope(n.TypePars, b.currScope)
			b.define(n.TypePars)
		}
		b.define(n.RunsOn)
		b.define(n.Mtc)
		b.define(n.System)
		b.define(n.Return)
		if n.Params != nil {
			b.currScope = NewLocalScope(n.Params, b.currScope)
			b.define(n.Params)
		}
		b.define(n.Body)
		b.define(n.With)

		if n.Params != nil {
			b.currScope = b.currScope.(*LocalScope).parent
		}

		if n.TypePars != nil {
			b.currScope = b.currScope.(*LocalScope).parent
		}
		return false

	case *ast.StructTypeDecl:
		name := NewTypeName(n.Name, n.Name.String(), nil)
		b.insert(name)

		s := NewStruct(n, name.Parent())
		name.typ = s
		b.currScope = s

		for i := range n.Fields {
			b.define(n.Fields[i])
		}

		b.currScope = name.Parent()
		return false

	case *ast.ComponentTypeDecl:
		c := NewComponentType(n, n.Name.String())
		b.insert(c)
		if n.TypePars != nil {
			b.currScope = NewLocalScope(n.TypePars, b.currScope)
			b.define(n.TypePars)
		}
		if n.Extends != nil {
			for i := range n.Extends {
				b.define(n.Extends[i])
			}
		}

		b.currScope = c
		b.define(n.Body)
		b.currScope = c.parent
		return false

	case *ast.Field:
		err := ast.Declarators([]ast.Expr{n.Name}, b.fset, func(decl ast.Expr, name ast.Node, arrays []ast.Expr, value ast.Expr) {
			v := NewVar(decl, identName(name))
			b.insert(v)
			for i := range arrays {
				b.define(arrays[i])
			}
			b.define(value)
		})

		// Add syntax errors to the error list
		if err != nil {
			for _, e := range err.List() {
				b.errs = append(b.errs, e)
			}
		}
		b.define(n.Type)
		return false

	case *ast.FormalPar:
		b.define(n.Type)
		err := ast.Declarators([]ast.Expr{n.Name}, b.fset, func(decl ast.Expr, name ast.Node, arrays []ast.Expr, value ast.Expr) {
			v := NewVar(decl, identName(name))
			b.insert(v)
			for i := range arrays {
				b.define(arrays[i])
			}
			b.define(value)
		})

		// Add syntax errors to the error list
		if err != nil {
			for _, e := range err.List() {
				b.errs = append(b.errs, e)
			}
		}
		return false

	case *ast.PortMapAttribute:
		b.currScope = NewLocalScope(n, b.currScope)
		b.define(n.Params)
		b.currScope = b.currScope.(*LocalScope).parent
		return false

	default:
		return true
	}
}

// resolve resolves all references and types.
func (b *builder) resolve(n ast.Node) {
	ast.Apply(n, b.resolveEnter, b.resolveExit)
}

func (b *builder) resolveEnter(c *ast.Cursor) bool {
	switch n := c.Node().(type) {
	case *ast.ValueDecl:
		b.resolveVar(n)
		return false
	}
	return true
}

func (b *builder) resolveVar(n *ast.ValueDecl) {
	b.resolve(n.Type)
	ast.Declarators(n.Decls, b.fset, func(decl ast.Expr, name ast.Node, arrays []ast.Expr, value ast.Expr) {
		//v := NewVar(decl, identName(name))
		//b.insert(v)
		//for i := range arrays {
		//	b.define(arrays[i])
		//}
		b.resolve(value)
	})
	b.resolve(n.With)
}

func (b *builder) resolveExit(c *ast.Cursor) bool {
	switch n := c.Node().(type) {
	case *ast.ValueLiteral:
		b.types[n] = literalType(n.Tok.Kind)

	case *ast.Ident:
		scp := b.scopes[n]

		// Identifier which to not have a scope are part of declarations and can
		// be skipped.
		if scp == nil {
			break
		}

		def := scp.Lookup(n.String())
		if def == nil {
			b.errorf(n, "unknown identifier %q", n.String())
			break
		}

		// In local scopes, check if declaration comes after
		if _, ok := def.Parent().(*LocalScope); ok {
			if def.End() >= n.Pos() {
				b.errorf(n, "unknown identifier %q", n.String())
			}
		}
		b.types[n] = def.Type()

	case *ast.ParametrizedIdent:
		b.types[n] = b.types[n.Ident]

	case *ast.SelectorExpr:
		typ := b.types[n.X]
		if typ == nil {
			break
		}

		id := identName(n.Sel)
		if scp, ok := typ.(Scope); ok {
			if obj := scp.Lookup(id); obj != nil {
				b.types[n] = obj.Type()
			}
		}
		b.errorf(n.Sel, "unknown identifer %q", id)

	case *ast.UnaryExpr:
		b.types[n] = b.types[n.X]

	case *ast.BinaryExpr:
		switch n.Op.Kind {
		case token.ASSIGN:
			b.types[n] = b.types[n.X]
		case token.COLON:
			b.types[n] = b.types[n.X]
		case token.RANGE:
			b.types[n] = Typ[Integer]
		case token.OR:
			b.types[n] = Typ[Boolean]
		case token.XOR:
			b.types[n] = Typ[Boolean]
		case token.AND:
			b.types[n] = Typ[Boolean]
		case token.NOT:
			b.types[n] = Typ[Boolean]
		case token.EQ, token.NE:
			b.types[n] = Typ[Boolean]
		case token.LT, token.LE, token.GT, token.GE:
			b.types[n] = Typ[Boolean]
		case token.SHR, token.SHL, token.ROR, token.ROL:
			b.types[n] = Typ[String]
		case token.OR4B:
			b.types[n] = Typ[String]
		case token.XOR4B:
			b.types[n] = Typ[String]
		case token.AND4B:
			b.types[n] = Typ[String]
		case token.NOT4B:
			b.types[n] = Typ[String]
		case token.CONCAT:
			b.types[n] = Typ[String]
		case token.ADD, token.SUB:
			b.types[n] = Typ[Numerical]
		case token.MUL, token.DIV, token.REM, token.MOD:
			b.types[n] = Typ[Numerical]
		}
	}
	return true
}

// error records errors during definition phase, such like ErrRedefined, ...
func (b *builder) errorf(rng Range, format string, args ...interface{}) {
	b.errs.Add(b.fset.Position(rng.Pos()), fmt.Sprintf(format, args...))
}

// insert object into current scope.
func (b *builder) insert(obj Object) {
	if alt := b.currScope.Insert(obj); alt != nil {
		// TODO(5nord) Make nicer errors: On what location is which object
		// defined.
		b.errorf(obj, "redefinition of %q", obj.Name())
		return
	}
	if obj.Parent() == nil {
		obj.setParent(b.currScope)
	}
}

func identName(n ast.Node) string {
	switch n := n.(type) {
	case *ast.Ident:
		return n.String()
	case *ast.ParametrizedIdent:
		return n.Ident.String()
	}
	return "_"
}

func literalType(tok token.Kind) Type {
	switch tok {
	case token.INT:
		return Typ[Integer]

	case token.FLOAT:
		return Typ[Float]

	case token.NAN:
		return Typ[Float]

	case token.ANY, token.MUL:
		return Typ[Template]

	case token.NULL:
		return Typ[Component]

	case token.OMIT:
		return Typ[Omit]

	case token.FALSE, token.TRUE:
		return Typ[Boolean]

	case token.NONE, token.INCONC, token.PASS, token.FAIL, token.ERROR:
		return Typ[Verdict]

	case token.BSTRING:
		// TODO(5nord) Implement hexstring, octetstring, ...
		return Typ[Bitstring]

	case token.STRING:
		// TODO(5nord) Implement universal charstring
		return Typ[Charstring]

	default:
		return Typ[Invalid]
	}
}

repository: xufangqing/ntt

repo_name: xufangqing/ntt


text:
@article{sikes2009je,
 abstract = {1. A major benefit of the mycorrhizal symbiosis is that it can protect plants from below-ground enemies, such as pathogens. Previous studies have indicated that plant identity (particularly plants that differ in root system architecture) or fungal identity (fungi from different families within the Glomeromycota) can determine the degree of protection from infection by pathogens. Here, we test the combined effects of plant and fungal identity to assess if there is a strong interaction between these two factors. 2. We paired one of two plants (Setaria glauca, a plant with a finely branched root system and Allium cepa, which has a simple root system) with one of six different fungal species from two families within the Glomeromycota. We assessed the degree to which plant identity, fungal identity and their interaction determined infection by Fusarium oxysporum, a common plant pathogen. 3. Our results show that the interaction between plant and fungal identity can be an important determinant of root infection by the pathogen. Infection by Fusarium was less severe in Allium (simple root system) or when Setaria (complex root system) was associated with a fungus from the family Glomeraceae. We also detected significant plant growth responses to the treatments; the fine-rooted Setaria benefited more from associating with a member of the family Glomeraceae, while Allium benefited more from associating with a member of the family Gigasporaceae. 4. Synthesis. This study supports previous claims that plants with complex root systems are more susceptible to infection by pathogens, and that the arbuscular mycorrhizal symbiosis can reduce infection in such plants – provided that the plant is colonized by a mycorrhizal fungus that can offer protection, such as the isolates of Glomus used here.},
 author = {Sikes, Benjamin A. and Cottenie, Karl and Klironomos, John N.},
 copyright = {© 2009 The Authors. Journal compilation © 2009 British Ecological Society},
 doi = {10.1111/j.1365-2745.2009.01557.x},
 file = {sikes_2009_plant_and_fungal_identity_determines_pathogen_protection_of_plant_roots_by.pdf:/Users/karlcottenie/Google Drive/zotero-library/2009/sikes_2009_plant_and_fungal_identity_determines_pathogen_protection_of_plant_roots_by.pdf:application/pdf},
 issn = {1365-2745},
 journal = {Journal of Ecology},
 keywords = {arbuscular mycorrhizal fungi, Fusarium oxysporum, mycorrhizal function, mycorrhizal identity, pathogen protection, plant-soil interactions, root architecture},
 language = {en},
 number = {6},
 pages = {1274--1280},
 title = {Plant and fungal identity determines pathogen protection of plant roots by arbuscular mycorrhizas},
 url = {https://besjournals.onlinelibrary.wiley.com/doi/abs/10.1111/j.1365-2745.2009.01557.x},
 urldate = {2019-03-28},
 volume = {97},
 year = {2009}
}

repository: karl-cottenie/academic-kickstart

repo_name: karl-cottenie/academic-kickstart


text:
package api

import (
	"encoding/json"
	"fmt"
	"io"
)

type Error struct {
	Object  string `json:"object"`
	Status  int    `json:"status"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

func (err *Error) Error() string {
	return fmt.Sprintf("[%d] %s: %s", err.Status, err.Code, err.Message)
}

func (err *Error) String() string {
	return err.Error()
}

func ReadError(r io.Reader) error {
	err := &Error{}

	d := json.NewDecoder(r)
	if decodeError := d.Decode(err); decodeError != nil {
		return decodeError
	}

	return err
}

func ParseError(buf []byte) error {
	err := &Error{}

	if jsonError := json.Unmarshal(buf, err); jsonError != nil {
		return jsonError
	}

	return err
}

repository: hunydev/notion

repo_name: hunydev/notion

branch_name: refs/heads/main


text:
PR = "${INC_PR}.0"

require klibc.inc
require klibc-checksums_${PV}.inc
DEPENDS = "klibc"

FILESPATHPKG =. "klibc-${PV}:"

export KLCC_INST=${STAGING_DIR_TARGET}/lib/klibc

inherit cross

do_install() {
         install -d ${STAGING_BINDIR_CROSS}/
         install -m 0755 klcc/klcc ${STAGING_BINDIR_CROSS}/klcc
}

PACKAGES = "${PN}"
FILES_${PN} = "${STAGING_BINDIR_CROSS}/klcc"

repository: libo/openembedded

repo_name: libo/openembedded


text:
.spectacle-slide {
  -webkit-transform-origin: center center;
          transform-origin: center center;
}

* {
  -webkit-font-smoothing: antialiased;
}

a {
    text-decoration: none;
}

/* Firefox */

_:-moz-tree-row(hover), .spectacle-deck {
  perspective: 1000px;
}

_:-moz-tree-row(hover), ul .appear {
  display: inline;
}

.suevalovComponent a {
  color: #49848E;
  display: inline-block;
  margin: 0 20px;
  text-decoration: none;
  font-size: 2em;
  font-weight: 100;
  border: none;
}

.thanksSlide__links {
  margin-top: 20px;
}

.thanksSlide__links a {
  font-family: 'Source Sans Pro', 'Open Sans', sans-serif;
  font-weight: 300;
  font-size: 1.6em;
  margin-left: 10px;
  display: block;
  line-height: 1.9em;
  text-decoration: none;
  color: #A9ECF8;
}

extension: css


text:
@InProceedings{Hardt13,
  pdf = {http://jmlr.org/proceedings/papers/v30/Hardt13.pdf},
  title = {Algorithms and Hardness for Robust Subspace Recovery},
  author = {Moritz Hardt and Ankur Moitra},
  pages = {354-375},
  abstract = {We consider a fundamental problem in unsupervised learning called subspace recovery: given a collection of m points in $R^n$, if many but not necessarily all of these points are contained in a $d$-dimensional subspace $T$ can we find it? The points contained in T are called  inliers and the remaining points are  outliers. This problem has received considerable attention in computer science and in statistics. Yet efficient algorithms from computer science are not robust to  adversarial outliers, and the estimators from robust statistics are hard to compute in high dimensions. This is a serious and persistent issue not just in this application, but for many other problems in unsupervised learning. Are there algorithms for subspace recovery that are both robust to outliers and efficient?  We give an algorithm that finds $T$ when it contains more than a $d/n$ fraction of the points.  Hence, for say $d = n/2$ this estimator is both easy to compute and well-behaved when there are a constant fraction of outliers. We prove that it is small set expansion hard to find $T$ when the fraction of errors is any larger and so our estimator is an  optimal compromise between efficiency and robustness. In fact, this basic problem has a surprising number of connections to other areas including small set expansion, matroid theory and functional analysis that we make use of here.},
}

repository: mreid/papersite

repo_name: mreid/papersite


text:
package block_test

import (
	"fmt"
	"strings"
	"testing"

	"github.com/harrybrwn/vine/internal/mockblock"
	"github.com/harrybrwn/vine/internal/testutil"
	"github.com/harrybrwn/vine/key"
)

func init() {
	testutil.Deterministic = true
}

func TestRandomized(t *testing.T) {
	t.Skip()
	users := []key.Sender{
		testutil.Wallet(t, 1),
		testutil.Wallet(t, 2),
		testutil.Wallet(t, 3),
		testutil.Wallet(t, 4),
		testutil.Wallet(t, 5),
		testutil.Wallet(t, 6),
		testutil.Wallet(t, 7),
		testutil.Wallet(t, 8),
		testutil.Wallet(t, 9),
		testutil.Wallet(t, 10),
		testutil.Wallet(t, 11),
		testutil.Wallet(t, 12),
	}
	e := mockblock.NewEngine(t, mockblock.NewChain(users[0]), users)
	err := e.Step(10)
	if err != nil {
		t.Error(err)
	}
	// e.SetSeed(1)
	// dbg := mockblock.NewChainDebugger(os.Stdout, users)
	// dbg.PrintChain(e.Chain.Iter())
	fmt.Println(strings.Repeat("-", 80))
	for _, u := range users {
		fmt.Println(e.UTXO.Bal(u))
	}
}

repository: harrybrwn/vine

repo_name: harrybrwn/vine


text:
@article{balachandran2021fat,
  title={Investigating the Effect of Background Knowledge on Natural Questions},
  author={Balachandran, Vidhisha and Dhingra, Bhuwan and Sun, Haitian and Collins, Michael and Cohen, William},
  journal={DeeLIO},
  year={2021}
}

repository: vidhishanair/academic-kickstart

repo_name: vidhishanair/academic-kickstart


text:
DESCRIPTION = "Python parser for the CommonMark Markdown spec"
HOMEPAGE = "https://github.com/rtfd/CommonMark-py"
LICENSE = "BSD"
LIC_FILES_CHKSUM = "file://LICENSE;md5=37e127eb75a030780aefcfc584e78523"

SRC_URI = "https://pypi.python.org/packages/4d/93/3808cbcebe94d205f55a9a32857df733a603339d32c46cd32669d808d964/CommonMark-0.5.4.tar.gz"
SRC_URI[md5sum] = "8eaa7602f8086344ae9027d9a7b1d4b9"
SRC_URI[sha256sum] = "34d73ec8085923c023930dfc0bcd1c4286e28a2a82de094bb72fabcc0281cbe5"
S = "${WORKDIR}/CommonMark-0.5.4"

PYPI_PACKAGE_HASH = "e976be0d4ff0b85b44e7e669af8224e0f80c11042015154667a2d9d61a33"

inherit setuptools

BBCLASSEXTEND = "native"

repository: billyjbryant/ops-build

repo_name: billyjbryant/ops-build

gha_language: Python


text:
package cap

import (
	"encoding/json"
	"encoding/xml"
	"errors"
	"strings"
)

const (
	CategoryUnknown = iota
	CategoryGeological
	CategoryMeteorological
	CategorySafety
	CategorySecurity
	CategoryRescue
	CategoryFire
	CategoryHealth
	CategoryEnvironment
	CategoryTransport
	CategoryInfrastructure
	CategoryCBRNE
	CategoryOther
)

type Category int

// UnmarshalString unmarshals the string into a Category value
func (category *Category) UnmarshalString(value string) error {
	value = strings.ToLower(value)

	if value == "geo" || value == "geological" {
		*category = CategoryGeological
	} else if value == "met" || value == "meteorological" {
		*category = CategoryMeteorological
	} else if value == "safety" {
		*category = CategorySafety
	} else if value == "security" {
		*category = CategorySecurity
	} else if value == "rescue" {
		*category = CategoryRescue
	} else if value == "fire" {
		*category = CategoryFire
	} else if value == "health" {
		*category = CategoryHealth
	} else if value == "env" || value == "environment" {
		*category = CategoryEnvironment
	} else if value == "transport" {
		*category = CategoryTransport
	} else if value == "infra" || value == "infrastructure" {
		*category = CategoryInfrastructure
	} else if value == "cbrne" {
		*category = CategoryCBRNE
	} else if value == "other" {
		*category = CategoryOther
	} else {
		return errors.New("Unknown Category value: " + value)
	}

	return nil
}

// UnmarshalXML unmarshals the XML into a Category value.
func (category *Category) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var str string
	if err := d.DecodeElement(&str, &start); err != nil {
		return err
	}
	return category.UnmarshalString(str)
}

// UnmarshalJSON unmarshals the JSON into a Category value.
func (category *Category) UnmarshalJSON(b []byte) error {
	var str string
	if err := json.Unmarshal(b, &str); err != nil {
		return err
	}
	return category.UnmarshalString(str)
}

// MarshalJSON returns the string version of the category.
func (category *Category) MarshalJSON() ([]byte, error) {
	return json.Marshal(category.String())
}

// MarshalXML returns the string version of the status.
func (category *Category) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	str := category.String()

	// Some of the values are shortened in the xml version
	if str == "Geological" {
		str = "Geo"
	} else if str == "Meteorological" {
		str = "Met"
	} else if str == "Environment" {
		str = "Env"
	} else if str == "Infrastructure" {
		str = "Infra"
	}

	return e.EncodeElement(str, start)
}

// String returns a Category as a string
func (category Category) String() string {
	if category == CategoryGeological {
		return "Geological"
	} else if category == CategoryMeteorological {
		return "Meteorological"
	} else if category == CategorySafety {
		return "Safety"
	} else if category == CategorySecurity {
		return "Security"
	} else if category == CategoryRescue {
		return "Rescue"
	} else if category == CategoryFire {
		return "Fire"
	} else if category == CategoryHealth {
		return "Health"
	} else if category == CategoryEnvironment {
		return "Environment"
	} else if category == CategoryTransport {
		return "Transport"
	} else if category == CategoryInfrastructure {
		return "Infrastructure"
	} else if category == CategoryCBRNE {
		return "CBRNE"
	} else if category == CategoryOther {
		return "Other"
	} else if category == CategoryUnknown {
		return "Unknown"
	}

	return ""
}

repository: alerting/go-cap

repo_name: alerting/go-cap


text:
@article{tokovenko2007,
  title={In silico approach to study and functionally analyze interferon regulated genes},
  author={Tokovenko, BT and El’skaya, AV and Obolenskaya, M Yu},
  journal={Biopolymers and Cell},
  volume={23},
  number={4},
  pages={368--375},
  year={2007}
}


text:
@inproceedings{FSS149111,
  author = {Aijun Bai and Reid Simmons and Manuela Veloso and Xiaoping Chen},
  title = {Intention-Aware Multi-Human Tracking for Human-Robot Interaction via Particle Filtering over Sets},
  booktitle = {AAAI Fall Symposium Series},
  year = {2014},
}


text:
package array

import (
	"fmt"
	"math/bits"

	proto "github.com/golang/protobuf/proto"
	"github.com/openacid/slim/benchhelper"
)

const (
	BMFlagDenseRank = 0x00000001
)

// Bitmapper defines behavior of a bitmap.
//
// Since 0.5.4
type Bitmapper interface {

	// Stat returns a map describing memory usage.
	//
	//    bits/one  :9
	//    mem_total :1195245
	//
	// Since 0.5.4
	Stat() map[string]int32

	// Has returns a bool indicating whether a bit is set.
	//
	// Since 0.5.4
	Has(int32) bool

	// Len returns number of bits in it.
	//
	// Since 0.5.4
	Len() int32

	// Bits returns all uint64 words in it.
	//
	// Since 0.5.4
	Bits() []uint64

	// Rand returns the count of 1 up to i(exclude i)
	//
	// Since 0.5.4
	Rank(int32) int32

	proto.Message
}

// NewBitmap creates a new Bitmapper instance from a serias of int32.
// The input specifies what bit to set to 1.
//
// Since 0.5.4
func NewBitmap(nums []int32) Bitmapper {

	n, words, index := newBitmapData(nums)

	bm := &Bitmap{
		Flags:     0,
		N:         n,
		Words:     words,
		RankIndex: index,
	}
	return bm
}

// NewDenseBitmap creates a new Bitmapper instance from a serias of int32.
// The input specifies what bit to set to 1.
//
// It compress rand index to reduce memory cost.
// But increase query time.
//
// Since 0.5.4
func NewDenseBitmap(nums []int32) Bitmapper {

	n, words, index := newBitmapData(nums)

	d := NewPolyArray(index)

	bm := &Bitmap{
		Flags:          BMFlagDenseRank,
		N:              n,
		Words:          words,
		RankIndexDense: d,
	}
	return bm
}

func newBitmapData(nums []int32) (int32, []uint64, []int32) {

	n := int32(0)
	if len(nums) > 0 {
		n = nums[len(nums)-1] + 1
	}

	nWords := (n + 63) >> 6
	words := make([]uint64, nWords)

	for _, i := range nums {
		iWord := i >> 6
		i = i & 63
		words[iWord] |= 1 << uint(i)
	}

	index := newRankIndex(words)

	return n, words, index
}

// Stat returns a map describing memory usage.
//
//    bits/one  :9
//    mem_total :1195245
//
// Since 0.5.4
func (b *Bitmap) Stat() map[string]int32 {

	totalmem := benchhelper.SizeOf(b)

	st := map[string]int32{
		"mem_total": int32(totalmem),
		"bits/one":  int32(totalmem) * 8 / b.Rank(b.N),
	}

	return st
}

// Has return a bool indicating whether a bit is set.
//
// Since 0.5.4
func (b *Bitmap) Has(i int32) bool {
	if i < 0 || i >= b.N {
		panic(fmt.Sprintf("i=%d out of range, n=%d", i, b.N))
	}
	iWord := i >> 6
	j := uint(i & 63)
	return ((b.Words[iWord] >> j) & 1) != 0
}

// Len returns number of bits in it.
//
// Since 0.5.4
func (b *Bitmap) Len() int32 {
	return b.N
}

// Bits returns all uint64 words in it.
//
// Since 0.5.4
func (b *Bitmap) Bits() []uint64 {
	return b.Words
}

// Rand returns the count of 1 up to i(exclude i)
//
// It takes about 4 ns/op with uncompressed rank,
// and 15 ns/op with dense rank
//
// Since 0.5.4
func (b *Bitmap) Rank(i int32) int32 {

	// An precaculated count serves for two 64-bit word on the left and right.
	//
	// Idx[1] = OnesCount(bits[0:128])
	// Idx[1] serves for rank query from 64 to 192
	//
	//	   0       64      128     192     256
	//	   |-------+-------+-------+-------+
	//	   Idx[0]          Idx[1]          Idx[2]
	//
	// let j = i % 128
	//
	//	   If 0 <= j < 64:   use index at i/128
	//	   If 64 <= j < 128: use index at i/128 + 1

	if i < 0 || i > b.N {
		panic(fmt.Sprintf("i=%d out of range, n=%d", i, b.N))
	}

	iWord := uint64(i >> 6)

	// Get Idx[j]
	var n int32
	if b.Flags&BMFlagDenseRank == 0 {
		n = b.RankIndex[(i+64)>>7]
	} else {
		n = b.RankIndexDense.Get((i + 64) >> 7)
	}

	// j <  64: 000000...
	// j >= 64: 111111...
	//          <-- less significant
	all1 := -(iWord & 1)

	// j <  64: 11111000.....
	//               ^
	//               ` j is here
	//
	//	   0       64      128     192     256
	//	   |-------+-------+1111---+-------+
	//	   Idx[0]          Idx[1]          Idx[2]
	//
	// j >= 64: 00000111.....
	//               ^
	//               ` j is here
	//
	//	   0       64      128     192     256
	//	   |-------+----111+-------+-------+
	//	   Idx[0]          Idx[1]          Idx[2]
	mask := (uint64(1) << uint(i&63)) - 1
	mask = all1 ^ mask

	// j <  64: word of [128, 192]
	//	   0       64      128     192     256
	//	   |-------+-------+1111---+-------+
	//	   Idx[0]          Idx[1]          Idx[2]
	//
	// j >= 64:  word of [64, 128]
	//	   0       64      128     192     256
	//	   |-------+----111+-------+-------+
	//	   Idx[0]          Idx[1]          Idx[2]
	word := b.Words[iWord] & mask
	d := int32(bits.OnesCount64(word))

	// j <  64:  d
	// j >= 64: -d
	diff := (int32(all1) ^ d) - int32(all1)

	return n + diff
}

func newRankIndex(words []uint64) []int32 {

	// two uint64 words share one index

	idx := make([]int32, 0)
	n := int32(0)
	for i := 0; i < len(words); i += 2 {
		idx = append(idx, n)
		n += int32(bits.OnesCount64(words[i]))
		if i < len(words)-1 {
			n += int32(bits.OnesCount64(words[i+1]))
		}
	}

	// Need a last index to let distance from every bit to its closest index
	// <=64
	if len(words)&1 == 0 {
		idx = append(idx, n)
	}

	// clone to reduce cap to len
	idx = append(idx[:0:0], idx...)

	return idx
}


text:
const multer = require('multer');
const express = require('express');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const app = express();
const port = 3000;

global.__basedir = __dirname;

let uploads = __dirname + '/uploads';
if (!fs.existsSync(uploads)) {
    console.log('trying for create the directory "upload"');
    fs.mkdirSync(uploads);
}

const storage = multer.diskStorage({
    destination: (req, file, callback) => {
        callback(null, __basedir + '/uploads/');
    },
    filename: (req, file, callback) => {
        callback(null, file.fieldname + "-" + Date.now() + "-" + file.originalname);
    }
});

const filterCSV = (req, file, callback) => {
    let ext = path.extname(file.originalname);
    if(ext === '.csv') {
        callback(null, true);
    } else {
        callback('File format is not a CSV', false);
    }
}

const upload = multer({
    storage: storage,
    fileFilter: filterCSV
});

app.use(cors())

app.post('/upload/', upload.single('uploadFile'), (req, res) => {
    try{
        if(req.file == undefined) {
            res.status(400).send({
                message: "Please upload a CSV file: "
            });
        } else {
            console.log('File upload');
            res.status(200).send({
                message: "File Upload: "
            });
        }
    } catch(error) {
        res.status(500).send({
            message: "Could not upload the file: " + req.file.originalname
        });
    }
});

app.get('/upload/', (req, res) => {
    res.send("Hello from Node") ;
});

require('dns').lookup(require('os').hostname(), (err, add, fam) => {
    console.log('addr: ' + add);
});

app.listen(port, '0.0.0.0', () => {
    console.log(`App listening port ${port}`);
});

repository: nayosx/SimpleCSVUpload

repo_name: nayosx/SimpleCSVUpload

extension: js


text:
package main

import (
	"encoding/json"
	"strings"
	"testing"
)

func TestWithInvalidJSON(t *testing.T) {
	invalidJSONDecoder := json.NewDecoder(strings.NewReader("I'm no json"))

	if mm := MakeMap(invalidJSONDecoder); mm == nil {
		t.Errorf("Json is invalid. Empty map should be returned. Value: %v", mm)
	}
	if qm := MakeQueueMap(invalidJSONDecoder); qm == nil {
		t.Errorf("Json is invalid. Empty map should be returned. Value: %v", qm)
	}

	if mm := MakeMap(nil); mm == nil {
		t.Errorf("Empty map should be returned. Value: %v", mm)
	}
	if qm := MakeQueueMap(nil); qm == nil {
		t.Errorf("Empty map should be returned.. Value: %v", qm)
	}
}

func checkMap(flMap map[string]float64, t *testing.T, addValue float64) {
	if flMap == nil {
		t.Error("Map should not be nil")
	}

	if v := flMap["FloatKey"]; v != addValue+4 {
		t.Errorf("Map should contain FloatKey but found '%v'", v)
	}

	if v := flMap["nes.ted"]; v != addValue+5 {
		t.Errorf("Map should contain nes.ted key but found '%v'", v)
	}

	if v, ok := flMap["st"]; ok {
		t.Errorf("key 'st' should not be included in map as it contains a string. Value: %v", v)
	}
}

func TestMakeMap(t *testing.T) {
	jsonObject := strings.NewReader(`{"FloatKey":4, "st":"string","nes":{"ted":5}}`)
	decoder := json.NewDecoder(jsonObject)

	flMap := MakeMap(decoder)
	checkMap(flMap, t, 0)
}

func TestMakeQueueMap(t *testing.T) {
	jsonArray := strings.NewReader(`[{"name":"q1", "FloatKey":14,"nes":{"ted":15}},{"name":"q2", "FloatKey":24,"nes":{"ted":25}}]`)
	decoder := json.NewDecoder(jsonArray)

	qmap := MakeQueueMap(decoder)
	if _, ok := qmap["q1"]; !ok {
		t.Error("map should contain queue map. queue=q1")
	}
	if _, ok := qmap["q2"]; !ok {
		t.Error("map should contain queue map. queue=q2")
	}
	checkMap(qmap["q1"], t, 10)
	checkMap(qmap["q2"], t, 20)
}


text:
/*
 * RMS
 *
 * Resource Manager Api
 *
 */

package model

import (
	"encoding/json"

	"strings"
)

// 合规状态
type PolicyState struct {
	// 合规状态所属用户ID
	DomainId *string `json:"domain_id,omitempty"`
	// 合规状态所属资源ID
	ResourceId *string `json:"resource_id,omitempty"`
	// 合规状态所属资源名字
	ResourceName *string `json:"resource_name,omitempty"`
	// 合规状态所属资源provider
	ResourceProvider *string `json:"resource_provider,omitempty"`
	// 合规状态所属资源类型
	ResourceType *string `json:"resource_type,omitempty"`
	// 合规状态
	ComplianceState *string `json:"compliance_state,omitempty"`
	// 合规状态所属规则ID
	PolicyAssignmentId *string `json:"policy_assignment_id,omitempty"`
	// 合规状态所属策略ID
	PolicyDefinitionId *string `json:"policy_definition_id,omitempty"`
	// 合规状态评估时间
	EvaluationTime *string `json:"evaluation_time,omitempty"`
}

func (o PolicyState) String() string {
	data, _ := json.Marshal(o)
	return strings.Join([]string{"PolicyState", string(data)}, " ")
}


text:
package parse

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"sort"
	"strings"

	"github.com/thought-machine/please/rules"
	"github.com/thought-machine/please/rules/bazel"
	"github.com/thought-machine/please/src/cli"
	"github.com/thought-machine/please/src/core"
	"github.com/thought-machine/please/src/parse/asp"
)

// InitParser initialises the parser engine. This is guaranteed to be called exactly once before any calls to Parse().
func InitParser(state *core.BuildState) *core.BuildState {
	if state.Parser == nil {
		p := &aspParser{parser: newAspParser(state)}
		state.Parser = p
	}
	return state
}

// aspParser implements the core.Parser interface around our parser package.
type aspParser struct {
	parser *asp.Parser
}

// newAspParser returns a asp.Parser object with all the builtins loaded
func newAspParser(state *core.BuildState) *asp.Parser {
	p := asp.NewParser(state)
	log.Debug("Loading built-in build rules...")
	dir, _ := rules.AllAssets()
	sort.Strings(dir)
	for _, filename := range dir {
		src, _ := rules.ReadAsset(filename)
		p.MustLoadBuiltins(filename, src)
	}

	for _, preload := range state.Config.Parse.PreloadBuildDefs {
		log.Debug("Preloading build defs from %s...", preload)
		p.MustLoadBuiltins(preload, nil)
	}

	if state.Config.Bazel.Compatibility {
		// Add a subrepo for @bazel_tools which appears to be one of their builtins.
		// Mostly we only include build defs in there.
		createBazelSubrepo(state)
	}

	log.Debug("parser initialised")
	return p
}

func (p *aspParser) ParseFile(pkg *core.Package, forLabel, dependent *core.BuildLabel, mode core.ParseMode, filename string) error {
	return p.parser.ParseFile(pkg, forLabel, dependent, mode, filename)
}

func (p *aspParser) ParseReader(pkg *core.Package, reader io.ReadSeeker, forLabel, dependent *core.BuildLabel, mode core.ParseMode) error {
	_, err := p.parser.ParseReader(pkg, reader, forLabel, dependent, mode)
	return err
}

func (p *aspParser) RunPreBuildFunction(state *core.BuildState, target *core.BuildTarget) error {
	return p.runBuildFunction(state, target, "pre", func() error {
		return target.PreBuildFunction.Call(target)
	})
}

func (p *aspParser) RunPostBuildFunction(state *core.BuildState, target *core.BuildTarget, output string) error {
	return p.runBuildFunction(state, target, "post", func() error {
		log.Debug("Running post-build function for %s. Build output:\n%s", target.Label, output)
		return target.PostBuildFunction.Call(target, output)
	})
}

// BuildRuleArgOrder returns a map of the arguments to build rule and the order they appear in the source file
func (p *aspParser) BuildRuleArgOrder() map[string]int {
	return p.parser.BuildRuleArgOrder()
}

// RegisterPreload pre-registers a preload, forcing us to build any transitive preloads before we move on
func (p *aspParser) RegisterPreload(label core.BuildLabel) error {
	return p.parser.RegisterPreload(label)
}

// runBuildFunction runs either the pre- or post-build function.
func (p *aspParser) runBuildFunction(state *core.BuildState, target *core.BuildTarget, callbackType string, f func() error) error {
	state.LogBuildResult(target, core.PackageParsing, fmt.Sprintf("Running %s-build function for %s", callbackType, target.Label))
	state.SyncParsePackage(target.Label)
	if err := f(); err != nil {
		state.LogBuildError(target.Label, core.ParseFailed, err, "Failed %s-build function for %s", callbackType, target.Label)
		return err
	}
	state.LogBuildResult(target, core.TargetBuilding, fmt.Sprintf("Finished %s-build function for %s", callbackType, target.Label))
	return nil
}

func createBazelSubrepo(state *core.BuildState) {
	if sr := state.Graph.Subrepo("bazel_tools"); sr != nil {
		return
	}
	dir := filepath.Join(core.OutDir, "bazel_tools")
	state.Graph.AddSubrepo(core.NewSubrepo(state, "bazel_tools", dir, nil, cli.HostArch(), false))
	// TODO(peterebden): This is a bit yuck... would be nice if we could avoid hardcoding all
	//                   this upfront and add a build target to do it for us.
	dir = filepath.Join(dir, "tools/build_defs/repo")
	if err := os.MkdirAll(dir, core.DirPermissions); err != nil {
		log.Fatalf("%s", err)
	}
	for filename, data := range bazel.AllFiles() {
		if err := os.WriteFile(filepath.Join(dir, strings.ReplaceAll(filename, ".build_defs", ".bzl")), data, 0644); err != nil {
			log.Fatalf("%s", err)
		}
	}
}

repository: thought-machine/please

repo_name: thought-machine/please


text:
package libpod

import (
	"time"

	"github.com/containers/podman/v4/libpod/define"
	"github.com/containers/podman/v4/libpod/lock"
	"github.com/containers/podman/v4/libpod/plugin"
	"github.com/containers/storage/pkg/directory"
)

// Volume is a libpod named volume.
// Named volumes may be shared by multiple containers, and may be created using
// more complex options than normal bind mounts. They may be backed by a mounted
// filesystem on the host.
type Volume struct {
	config *VolumeConfig
	state  *VolumeState

	ignoreIfExists bool
	valid          bool
	plugin         *plugin.VolumePlugin
	runtime        *Runtime
	lock           lock.Locker
}

// VolumeConfig holds the volume's immutable configuration.
type VolumeConfig struct {
	// Name of the volume.
	Name string `json:"name"`
	// ID of the volume's lock.
	LockID uint32 `json:"lockID"`
	// Labels for the volume.
	Labels map[string]string `json:"labels"`
	// The volume driver. Empty string or local does not activate a volume
	// driver, all other values will.
	Driver string `json:"volumeDriver"`
	// The location the volume is mounted at.
	MountPoint string `json:"mountPoint"`
	// Time the volume was created.
	CreatedTime time.Time `json:"createdAt,omitempty"`
	// Options to pass to the volume driver. For the local driver, this is
	// a list of mount options. For other drivers, they are passed to the
	// volume driver handling the volume.
	Options map[string]string `json:"volumeOptions,omitempty"`
	// Whether this volume is anonymous (will be removed on container exit)
	IsAnon bool `json:"isAnon"`
	// UID the volume will be created as.
	UID int `json:"uid"`
	// GID the volume will be created as.
	GID int `json:"gid"`
	// Size maximum of the volume.
	Size uint64 `json:"size"`
	// Inodes maximum of the volume.
	Inodes uint64 `json:"inodes"`
	// DisableQuota indicates that the volume should completely disable using any
	// quota tracking.
	DisableQuota bool `json:"disableQuota,omitempty"`
	// Timeout allows users to override the default driver timeout of 5 seconds
	Timeout *uint `json:"timeout,omitempty"`
	// StorageName is the name of the volume in c/storage. Only used for
	// image volumes.
	StorageName string `json:"storageName,omitempty"`
	// StorageID is the ID of the volume in c/storage. Only used for image
	// volumes.
	StorageID string `json:"storageID,omitempty"`
	// StorageImageID is the ID of the image the volume was based off of.
	// Only used for image volumes.
	StorageImageID string `json:"storageImageID,omitempty"`
	// MountLabel is the SELinux label to assign to mount points
	MountLabel string `json:"mountlabel,omitempty"`
}

// VolumeState holds the volume's mutable state.
// Volumes are not guaranteed to have a state. Only volumes using the Local
// driver that have mount options set will create a state.
type VolumeState struct {
	// Mountpoint is the location where the volume was mounted.
	// This is only used for volumes using a volume plugin, which will mount
	// at non-standard locations.
	MountPoint string `json:"mountPoint,omitempty"`
	// MountCount is the number of times this volume has been requested to
	// be mounted.
	// It is incremented on mount() and decremented on unmount().
	// On incrementing from 0, the volume will be mounted on the host.
	// On decrementing to 0, the volume will be unmounted on the host.
	MountCount uint `json:"mountCount"`
	// NeedsCopyUp indicates that the next time the volume is mounted into
	// a container, the container will "copy up" the contents of the
	// mountpoint into the volume.
	// This should only be done once. As such, this is set at container
	// create time, then cleared after the copy up is done and never set
	// again.
	NeedsCopyUp bool `json:"notYetMounted,omitempty"`
	// NeedsChown indicates that the next time the volume is mounted into
	// a container, the container will chown the volume to the container process
	// UID/GID.
	NeedsChown bool `json:"notYetChowned,omitempty"`
	// UIDChowned is the UID the volume was chowned to.
	UIDChowned int `json:"uidChowned,omitempty"`
	// GIDChowned is the GID the volume was chowned to.
	GIDChowned int `json:"gidChowned,omitempty"`
}

// Name retrieves the volume's name
func (v *Volume) Name() string {
	return v.config.Name
}

// Returns the size on disk of volume
func (v *Volume) Size() (uint64, error) {
	size, err := directory.Size(v.config.MountPoint)
	return uint64(size), err
}

// Driver retrieves the volume's driver.
func (v *Volume) Driver() string {
	return v.config.Driver
}

// Scope retrieves the volume's scope.
// Libpod does not implement volume scoping, and this is provided solely for
// Docker compatibility. It returns only "local".
func (v *Volume) Scope() string {
	return "local"
}

// Labels returns the volume's labels
func (v *Volume) Labels() map[string]string {
	labels := make(map[string]string)
	for key, value := range v.config.Labels {
		labels[key] = value
	}
	return labels
}

// MountPoint returns the volume's mountpoint on the host
func (v *Volume) MountPoint() (string, error) {
	// For the sake of performance, avoid locking unless we have to.
	if v.UsesVolumeDriver() || v.config.Driver == define.VolumeDriverImage {
		v.lock.Lock()
		defer v.lock.Unlock()

		if err := v.update(); err != nil {
			return "", err
		}
	}

	return v.mountPoint(), nil
}

// MountCount returns the volume's mountcount on the host from state
// Useful in determining if volume is using plugin or a filesystem mount and its mount
func (v *Volume) MountCount() (uint, error) {
	v.lock.Lock()
	defer v.lock.Unlock()
	if err := v.update(); err != nil {
		return 0, err
	}
	return v.state.MountCount, nil
}

// Internal-only helper for volume mountpoint
func (v *Volume) mountPoint() string {
	if v.UsesVolumeDriver() || v.config.Driver == define.VolumeDriverImage {
		return v.state.MountPoint
	}

	return v.config.MountPoint
}

// Options return the volume's options
func (v *Volume) Options() map[string]string {
	options := make(map[string]string)
	for k, v := range v.config.Options {
		options[k] = v
	}
	return options
}

// Anonymous returns whether this volume is anonymous. Anonymous volumes were
// created with a container, and will be removed when that container is removed.
func (v *Volume) Anonymous() bool {
	return v.config.IsAnon
}

// UID returns the UID the volume will be created as.
func (v *Volume) UID() (int, error) {
	v.lock.Lock()
	defer v.lock.Unlock()

	if err := v.update(); err != nil {
		return -1, err
	}

	return v.uid(), nil
}

// Internal, unlocked accessor for UID.
func (v *Volume) uid() int {
	if v.state.UIDChowned > 0 {
		return v.state.UIDChowned
	}
	return v.config.UID
}

// GID returns the GID the volume will be created as.
func (v *Volume) GID() (int, error) {
	v.lock.Lock()
	defer v.lock.Unlock()

	if err := v.update(); err != nil {
		return -1, err
	}

	return v.gid(), nil
}

// Internal, unlocked accessor for GID.
func (v *Volume) gid() int {
	if v.state.GIDChowned > 0 {
		return v.state.GIDChowned
	}
	return v.config.GID
}

// CreatedTime returns the time the volume was created at. It was not tracked
// for some time, so older volumes may not contain one.
func (v *Volume) CreatedTime() time.Time {
	return v.config.CreatedTime
}

// Config returns the volume's configuration.
func (v *Volume) Config() (*VolumeConfig, error) {
	config := VolumeConfig{}
	err := JSONDeepCopy(v.config, &config)
	return &config, err
}

// VolumeInUse goes through the container dependencies of a volume
// and checks if the volume is being used by any container.
func (v *Volume) VolumeInUse() ([]string, error) {
	v.lock.Lock()
	defer v.lock.Unlock()

	if !v.valid {
		return nil, define.ErrVolumeRemoved
	}
	return v.runtime.state.VolumeInUse(v)
}

// IsDangling returns whether this volume is dangling (unused by any
// containers).
func (v *Volume) IsDangling() (bool, error) {
	ctrs, err := v.VolumeInUse()
	if err != nil {
		return false, err
	}
	return len(ctrs) == 0, nil
}

// UsesVolumeDriver determines whether the volume uses a volume driver. Volume
// drivers are pluggable backends for volumes that will manage the storage and
// mounting.
func (v *Volume) UsesVolumeDriver() bool {
	if v.config.Driver == define.VolumeDriverImage {
		if _, ok := v.runtime.config.Engine.VolumePlugins[v.config.Driver]; ok {
			return true
		}
		return false
	}
	return !(v.config.Driver == define.VolumeDriverLocal || v.config.Driver == "")
}

func (v *Volume) Mount() (string, error) {
	v.lock.Lock()
	defer v.lock.Unlock()
	err := v.mount()
	return v.config.MountPoint, err
}

func (v *Volume) Unmount() error {
	v.lock.Lock()
	defer v.lock.Unlock()
	return v.unmount(false)
}

func (v *Volume) NeedsMount() bool {
	return v.needsMount()
}

repository: containers/podman

repo_name: containers/podman