text:
package main

import (
	"fmt"
	"io/ioutil"
	"path/filepath"
	"strings"
	"testing"

	"github.com/blang/semver/v4"
	"github.com/giantswarm/microerror"
	"github.com/giantswarm/release-operator/v3/api/v1alpha1"
	"github.com/giantswarm/versionbundle"
	"sigs.k8s.io/yaml"
)

type kustomizationFile struct {
	CommonAnnotations map[string]string `yaml:"commonAnnotations"`
	Resources         []string          `yaml:"resources"`
	Transformers      []string          `yaml:"transformers"`
}

// requestException represents a single release exception to a request.
type requestException struct {
	Version string `yaml:"releaseVersion" json:"releaseVersion"`
	Reason  string `yaml:"reason"`
}

// versionRequest represents a specific requested component name and version.
type versionRequest struct {
	Issue      string             `yaml:"issue"`
	Name       string             `yaml:"name"`
	Version    string             `yaml:"version"`
	Exceptions []requestException `yaml:"except,omitempty" json:"except,omitempty"`
}

// releaseRequest is one release pattern with associated requests.
type releaseRequest struct {
	Name     string           `yaml:"name"`
	Requests []versionRequest `yaml:"requests"`
}

type requestsFile struct {
	Releases []releaseRequest `yaml:"releases"`
}

// appListSatisfiesRequest determines whether the given request is satisfied in the given app list.
// It returns a boolean value for whether the request is satisfied as well as
// a string containing the actual app version which satisfies the request.
func appListSatisfiesRequest(request versionRequest, appList []v1alpha1.ReleaseSpecApp) (bool, string, error) {
	var actual string
	for _, app := range appList {
		if app.Name == request.Name {
			actual = app.Version
			actualMatchesRequested, err := versionMatches(actual, request.Version)
			if err != nil {
				return false, actual, microerror.Mask(err)
			}

			if actualMatchesRequested {
				return true, actual, nil
			}

			break // No need to keep searching for this component.
		}
	}
	return false, actual, nil
}

// componentListSatisfiesRequest determines whether the given request is satisfied in the given component list.
// It returns a boolean value for whether the request is satisfied as well as
// a string containing the actual component version which satisfies the request.
func componentListSatisfiesRequest(request versionRequest, componentList []v1alpha1.ReleaseSpecComponent) (bool, string, error) {
	var actual string
	for _, component := range componentList {
		if component.Name == request.Name {
			actual = component.Version
			actualMatchesRequested, err := versionMatches(actual, request.Version)
			if err != nil {
				return false, actual, microerror.Mask(err)
			}

			if actualMatchesRequested {
				return true, actual, nil
			}

			break // No need to keep searching for this component.
		}
	}
	return false, actual, nil
}

// findMatchingRequests searches the given array of releaseRequests
// for requests which apply to the given release version.
func findMatchingRequests(release string, requests []releaseRequest) ([]versionRequest, error) {
	var requestList []versionRequest
	for _, request := range requests {

		// See whether this request applies to the current release version.
		match, err := versionMatches(release, request.Name)
		if err != nil {
			return nil, microerror.Mask(err)
		}

		if match {
			for _, component := range request.Requests {
				releaseIsExcluded := false
				if component.Exceptions != nil {
					// Check the excluded releases for this component to see if our release is there.
					for _, e := range component.Exceptions {
						releaseIsExcluded, err = versionMatches(e.Version, request.Name)
						if err != nil {
							return nil, microerror.Mask(err)
						}
					}
				}

				if !releaseIsExcluded {
					requestList = append(requestList, component)
				}
			}
		}
	}
	return requestList, nil
}

func findReleases(provider string, archived bool) ([]v1alpha1.Release, error) {
	path := provider
	if archived {
		path = filepath.Join(provider, "archived")
	}
	releaseDirectories, err := ioutil.ReadDir(path)
	if err != nil {
		return nil, microerror.Mask(err)
	}
	var releases []v1alpha1.Release
	for _, releaseDirectory := range releaseDirectories {
		if !releaseDirectory.IsDir() || releaseDirectory.Name() == "archived" {
			continue
		}
		releaseFile := filepath.Join(path, releaseDirectory.Name(), releaseFilename)
		data, err := ioutil.ReadFile(releaseFile)
		if err != nil {
			return nil, microerror.Mask(err)
		}
		var release v1alpha1.Release
		err = yaml.Unmarshal(data, &release)
		if err != nil {
			return nil, microerror.Mask(err)
		}
		if releaseDirectory.Name() != release.Name {
			return nil, fmt.Errorf("%s release %s is in directory %s which doesn't match its name", provider, release.Name, releaseDirectory)
		}
		releases = append(releases, release)
	}
	return releases, nil
}

// To reuse versionbundle.ValidateIndexReleases, the slice of Releases must first be
// converted into a slice of versionbundle.IndexRelease.
func releasesToIndex(releases []v1alpha1.Release) []versionbundle.IndexRelease {
	var indexReleases []versionbundle.IndexRelease
	for _, release := range releases {
		var apps []versionbundle.App
		for _, app := range release.Spec.Apps {
			indexApp := versionbundle.App{
				App:              app.Name,
				ComponentVersion: app.ComponentVersion,
				Version:          app.Version,
			}
			apps = append(apps, indexApp)
		}
		var authorities []versionbundle.Authority
		for _, component := range release.Spec.Components {
			indexAuthority := versionbundle.Authority{
				Name:    component.Name,
				Version: component.Version,
			}
			authorities = append(authorities, indexAuthority)
		}
		indexRelease := versionbundle.IndexRelease{
			Active:      release.Spec.State == "active",
			Apps:        apps,
			Authorities: authorities,
			Date:        release.Spec.Date.Time,
			Version:     release.Name,
		}
		indexReleases = append(indexReleases, indexRelease)
	}
	return indexReleases
}

// versionMatches compares the given version with the given semver
// constraint pattern and returns whether it matches.
func versionMatches(version string, pattern string) (bool, error) {
	validator, err := semver.ParseRange(pattern)
	if err != nil {
		return false, fmt.Errorf("release names for requests must be valid semver constraints: %s", err)
	}

	version = strings.TrimPrefix(version, "v")

	v, err := semver.Parse(version)
	if err != nil {
		return false, fmt.Errorf("release names must be valid semver: %s: %s", err, version)
	}

	return validator(v), nil
}

func Test_Releases(t *testing.T) {
	testCases := []struct {
		provider string
		name     string
	}{
		{
			provider: "aws",
			name:     "case 1: aws releases are valid",
		},
		{
			provider: "azure",
			name:     "case 2: azure releases are valid",
		},
		{
			provider: "kvm",
			name:     "case 3: kvm releases are valid",
		},
	}

	// Load the README so we can check links for each release.
	var readmeContent string
	{
		readmeContentBytes, err := ioutil.ReadFile(readmeFilename)
		if err != nil {
			t.Fatal(err)
		}
		readmeContent = string(readmeContentBytes)
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			providerResources := map[string]bool{}
			{
				var providerKustomization kustomizationFile
				providerKustomizationData, err := ioutil.ReadFile(filepath.Join(tc.provider, kustomizationFilename))
				if err != nil {
					t.Fatal(err)
				}
				err = yaml.UnmarshalStrict(providerKustomizationData, &providerKustomization)
				if err != nil {
					t.Fatal(err)
				}
				for _, resource := range providerKustomization.Resources {
					providerResources[resource] = false
				}
			}

			providerRequests := []releaseRequest{}
			{
				var providerRequestsFile requestsFile
				providerRequestsData, err := ioutil.ReadFile(filepath.Join(tc.provider, requestsFilename))
				if err != nil {
					t.Fatal(err)
				}
				err = yaml.UnmarshalStrict(providerRequestsData, &providerRequestsFile)
				if err != nil {
					t.Fatal(err)
				}
				for _, release := range providerRequestsFile.Releases {
					providerRequests = append(providerRequests, release)
				}
			}

			releases, err := findReleases(tc.provider, false)
			if err != nil {
				t.Fatal(microerror.Mask(err))
			}

			// Ensure that releases satisfy OpenAPI validation.
			for _, release := range releases {
				// Check that the release is registered in the main provider kustomization.yaml resources.
				if _, ok := providerResources[release.Name]; !ok {
					t.Errorf("release %s not registered in %s/%s", release.Name, tc.provider, kustomizationFilename)
				}
				providerResources[release.Name] = true
			}

			for _, release := range releases {
				// Check that the release-specific kustomization.yaml file points to the release manifest.
				{
					releaseKustomizationData, err := ioutil.ReadFile(filepath.Join(tc.provider, release.Name, kustomizationFilename))
					if err != nil {
						t.Errorf("missing file for %s release %s: %s", tc.provider, release.Name, err)
					}
					var releaseKustomization kustomizationFile
					err = yaml.UnmarshalStrict(releaseKustomizationData, &releaseKustomization)
					if len(releaseKustomization.Resources) != 1 || releaseKustomization.Resources[0] != releaseFilename {
						t.Errorf("%s for %s release %s should contain only one resource, \"%s\"", kustomizationFilename, tc.provider, release.Name, releaseFilename)
					}
				}

				// Check that the version in the first line of the release notes is correct.
				{
					releaseNotesData, err := ioutil.ReadFile(filepath.Join(tc.provider, release.Name, readmeFilename))
					if err != nil {
						t.Errorf("missing file for %s release %s: %s", tc.provider, release.Name, err)
					}
					releaseNotesLines := strings.Split(string(releaseNotesData), "\n")
					if len(releaseNotesLines) == 0 || !strings.Contains(releaseNotesLines[0], strings.TrimPrefix(release.Name, "v")) {
						t.Errorf("expected release notes for %s release %s to contain the release version on the first line", tc.provider, release.Name)
					}
				}

				// Check that the README links to the release.
				if !strings.Contains(readmeContent, fmt.Sprintf("https://github.com/giantswarm/releases/tree/master/%s/%s", tc.provider, release.Name)) {
					t.Errorf("expected link in %s to %s release %s", readmeFilename, tc.provider, release.Name)
				}

				// Check that all active releases contain all requested component versions.
				if release.Spec.State == "active" {
					requests, err := findMatchingRequests(release.Name, providerRequests)
					if err != nil {
						t.Fatal(microerror.Mask(err))
					}

					unsatisfiedRequests := []string{}
					for _, request := range requests {
						componentsSatisfied, actualComponentVersion, err := componentListSatisfiesRequest(request, release.Spec.Components)
						if err != nil {
							t.Fatal(microerror.Mask(err))
						}

						appsSatisfied, actualAppVersion, err := appListSatisfiesRequest(request, release.Spec.Apps)
						if err != nil {
							t.Fatal(microerror.Mask(err))
						}

						if !componentsSatisfied && !appsSatisfied {
							// Either components or apps were not satisfied. Use the 'actual' version which isn't empty.
							actual := actualComponentVersion
							if actual == "" {
								actual = actualAppVersion
							}

							unsatisfied := fmt.Sprintf("requested: %s: %s \tactual: %s", request.Name, request.Version, actual)
							unsatisfiedRequests = append(unsatisfiedRequests, unsatisfied)
						}
					}

					if len(unsatisfiedRequests) > 0 {
						msg := fmt.Sprintf("Release %s does not meet the requested version requirements:\n%s", release.Name, strings.Join(unsatisfiedRequests, ",\n"))
						t.Errorf(msg)
					}
				}

			}

			archived, err := findReleases(tc.provider, true)
			if err != nil {
				t.Fatal(microerror.Mask(err))
			}

			for _, release := range archived {
				// Check that the README links to the release.
				if !strings.Contains(readmeContent, fmt.Sprintf("https://github.com/giantswarm/releases/tree/master/%s/archived/%s", tc.provider, release.Name)) {
					t.Errorf("expected link in %s to archived %s release %s", readmeFilename, tc.provider, release.Name)
				}
			}

			// Check for extra resources in provider kustomization.yaml that don't have a corresponding release.
			for release, processed := range providerResources {
				if !processed {
					t.Errorf("release %s registered in %s/%s resources but not found", release, tc.provider, kustomizationFilename)
				}
			}

			// Ensure that releases are unique.
			indexReleases := releasesToIndex(releases)
			err = versionbundle.ValidateIndexReleases(indexReleases)
			if err != nil {
				t.Fatal(microerror.Mask(err))
			}
		})
	}
}

repository: giantswarm/releases

repo_name: giantswarm/releases


text:
LICENSE="MIT"

require test.inc

do_compile() {
    bbplain "PN=\"${PN}\""
    bbplain "PV=\"${PV}\""
    bbplain "S=\"${S}\""
    bbplain "MACHINE=\"${MACHINE}\""
    bbplain "value: $A"
    bbplain "shell: $SHELL"
    bbwarn "test warning msg"
    bbplain "test plain msg"
    bbnote "test note msg"
    bbdebug 1 "test log lvl 1 msg"
    bbdebug 2 "test log lvl 2 msg"
    bbwarn "number of CPU: ${@oe.utils.cpu_count()}"
}

license: MIT

repository: XDeschuyteneer/meta-xade

license_type: permissive

repo_name: XDeschuyteneer/meta-xade

branch_name: refs/heads/master

gha_license_id: MIT

gha_language: BitBake

extension: bb


text:
// +build linux,unit

// Copyright 2014-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
//	http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package task

import (
	"encoding/json"
	"testing"
	"time"

	apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
	apicontainerstatus "github.com/aws/amazon-ecs-agent/agent/api/container/status"
	apieni "github.com/aws/amazon-ecs-agent/agent/api/eni"
	"github.com/aws/amazon-ecs-agent/agent/config"
	"github.com/aws/amazon-ecs-agent/agent/dockerclient"
	"github.com/aws/amazon-ecs-agent/agent/taskresource"
	"github.com/aws/amazon-ecs-agent/agent/taskresource/cgroup/control/mock_control"
	"github.com/aws/amazon-ecs-agent/agent/utils/ioutilwrapper/mocks"
	"github.com/golang/mock/gomock"

	docker "github.com/fsouza/go-dockerclient"
	specs "github.com/opencontainers/runtime-spec/specs-go"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

const (
	validTaskArn   = "arn:aws:ecs:region:account-id:task/task-id"
	invalidTaskArn = "invalid:task::arn"

	expectedCgroupRoot = "/ecs/task-id"

	taskVCPULimit             = 2.0
	taskMemoryLimit           = 512
	minDockerClientAPIVersion = dockerclient.Version_1_17
)

func TestAddNetworkResourceProvisioningDependencyNop(t *testing.T) {
	testTask := &Task{
		Containers: []*apicontainer.Container{
			{
				Name: "c1",
			},
		},
	}
	testTask.addNetworkResourceProvisioningDependency(nil)
	assert.Equal(t, 1, len(testTask.Containers))
}

func TestAddNetworkResourceProvisioningDependencyWithENI(t *testing.T) {
	testTask := &Task{
		ENI: &apieni.ENI{},
		Containers: []*apicontainer.Container{
			{
				Name: "c1",
				TransitionDependenciesMap: make(map[apicontainerstatus.ContainerStatus]apicontainer.TransitionDependencySet),
			},
		},
	}
	cfg := &config.Config{
		PauseContainerImageName: "pause-container-image-name",
		PauseContainerTag:       "pause-container-tag",
	}
	testTask.addNetworkResourceProvisioningDependency(cfg)
	assert.Equal(t, 2, len(testTask.Containers),
		"addNetworkResourceProvisioningDependency should add another container")
	pauseContainer, ok := testTask.ContainerByName(NetworkPauseContainerName)
	require.True(t, ok, "Expected to find pause container")
	assert.Equal(t, apicontainer.ContainerCNIPause, pauseContainer.Type, "pause container should have correct type")
	assert.True(t, pauseContainer.Essential, "pause container should be essential")
	assert.Equal(t, cfg.PauseContainerImageName+":"+cfg.PauseContainerTag, pauseContainer.Image,
		"pause container should use configured image")
}

// TestBuildCgroupRootHappyPath builds cgroup root from valid taskARN
func TestBuildCgroupRootHappyPath(t *testing.T) {
	task := Task{
		Arn: validTaskArn,
	}

	cgroupRoot, err := task.BuildCgroupRoot()

	assert.NoError(t, err)
	assert.Equal(t, expectedCgroupRoot, cgroupRoot)
}

// TestBuildCgroupRootErrorPath validates the cgroup path build error path
func TestBuildCgroupRootErrorPath(t *testing.T) {
	task := Task{
		Arn: invalidTaskArn,
	}

	cgroupRoot, err := task.BuildCgroupRoot()

	assert.Error(t, err)
	assert.Empty(t, cgroupRoot)
}

// TestBuildLinuxResourceSpecCPUMem validates the linux resource spec builder
func TestBuildLinuxResourceSpecCPUMem(t *testing.T) {
	taskMemoryLimit := int64(taskMemoryLimit)

	task := &Task{
		Arn:    validTaskArn,
		CPU:    float64(taskVCPULimit),
		Memory: taskMemoryLimit,
	}

	expectedTaskCPUPeriod := uint64(defaultCPUPeriod / time.Microsecond)
	expectedTaskCPUQuota := int64(taskVCPULimit * float64(expectedTaskCPUPeriod))
	expectedTaskMemory := taskMemoryLimit * bytesPerMegabyte
	expectedLinuxResourceSpec := specs.LinuxResources{
		CPU: &specs.LinuxCPU{
			Quota:  &expectedTaskCPUQuota,
			Period: &expectedTaskCPUPeriod,
		},
		Memory: &specs.LinuxMemory{
			Limit: &expectedTaskMemory,
		},
	}

	linuxResourceSpec, err := task.BuildLinuxResourceSpec()

	assert.NoError(t, err)
	assert.EqualValues(t, expectedLinuxResourceSpec, linuxResourceSpec)
}

// TestBuildLinuxResourceSpecCPU validates the linux resource spec builder
func TestBuildLinuxResourceSpecCPU(t *testing.T) {
	task := &Task{
		Arn: validTaskArn,
		CPU: float64(taskVCPULimit),
	}

	expectedTaskCPUPeriod := uint64(defaultCPUPeriod / time.Microsecond)
	expectedTaskCPUQuota := int64(taskVCPULimit * float64(expectedTaskCPUPeriod))
	expectedLinuxResourceSpec := specs.LinuxResources{
		CPU: &specs.LinuxCPU{
			Quota:  &expectedTaskCPUQuota,
			Period: &expectedTaskCPUPeriod,
		},
	}

	linuxResourceSpec, err := task.BuildLinuxResourceSpec()

	assert.NoError(t, err)
	assert.EqualValues(t, expectedLinuxResourceSpec, linuxResourceSpec)
}

// TestBuildLinuxResourceSpecWithoutTaskCPULimits validates behavior of CPU Shares
func TestBuildLinuxResourceSpecWithoutTaskCPULimits(t *testing.T) {
	task := &Task{
		Arn: validTaskArn,
		Containers: []*apicontainer.Container{
			{
				Name: "C1",
			},
		},
	}
	expectedCPUShares := uint64(minimumCPUShare)
	expectedLinuxResourceSpec := specs.LinuxResources{
		CPU: &specs.LinuxCPU{
			Shares: &expectedCPUShares,
		},
	}

	linuxResourceSpec, err := task.BuildLinuxResourceSpec()

	assert.NoError(t, err)
	assert.EqualValues(t, expectedLinuxResourceSpec, linuxResourceSpec)
}

// TestBuildLinuxResourceSpecWithoutTaskCPUWithContainerCPULimits validates behavior of CPU Shares
func TestBuildLinuxResourceSpecWithoutTaskCPUWithContainerCPULimits(t *testing.T) {
	task := &Task{
		Arn: validTaskArn,
		Containers: []*apicontainer.Container{
			{
				Name: "C1",
				CPU:  uint(512),
			},
		},
	}
	expectedCPUShares := uint64(512)
	expectedLinuxResourceSpec := specs.LinuxResources{
		CPU: &specs.LinuxCPU{
			Shares: &expectedCPUShares,
		},
	}

	linuxResourceSpec, err := task.BuildLinuxResourceSpec()

	assert.NoError(t, err)
	assert.EqualValues(t, expectedLinuxResourceSpec, linuxResourceSpec)
}

// TestBuildLinuxResourceSpecInvalidMem validates the linux resource spec builder
func TestBuildLinuxResourceSpecInvalidMem(t *testing.T) {
	taskMemoryLimit := int64(taskMemoryLimit)

	task := &Task{
		Arn:    validTaskArn,
		CPU:    float64(taskVCPULimit),
		Memory: taskMemoryLimit,
		Containers: []*apicontainer.Container{
			{
				Name:   "C1",
				Memory: uint(2048),
			},
		},
	}

	expectedLinuxResourceSpec := specs.LinuxResources{}
	linuxResourceSpec, err := task.BuildLinuxResourceSpec()

	assert.Error(t, err)
	assert.EqualValues(t, expectedLinuxResourceSpec, linuxResourceSpec)
}

// TestOverrideCgroupParent validates the cgroup parent override
func TestOverrideCgroupParentHappyPath(t *testing.T) {
	task := &Task{
		Arn:                    validTaskArn,
		CPU:                    float64(taskVCPULimit),
		Memory:                 int64(taskMemoryLimit),
		MemoryCPULimitsEnabled: true,
	}

	hostConfig := &docker.HostConfig{}

	assert.NoError(t, task.overrideCgroupParent(hostConfig))
	assert.NotEmpty(t, hostConfig)
	assert.Equal(t, expectedCgroupRoot, hostConfig.CgroupParent)
}

// TestOverrideCgroupParentErrorPath validates the error path for
// cgroup parent update
func TestOverrideCgroupParentErrorPath(t *testing.T) {
	task := &Task{
		Arn:                    invalidTaskArn,
		CPU:                    float64(taskVCPULimit),
		Memory:                 int64(taskMemoryLimit),
		MemoryCPULimitsEnabled: true,
	}

	hostConfig := &docker.HostConfig{}

	assert.Error(t, task.overrideCgroupParent(hostConfig))
	assert.Empty(t, hostConfig.CgroupParent)
}

// TestPlatformHostConfigOverride validates the platform host config overrides
func TestPlatformHostConfigOverride(t *testing.T) {
	task := &Task{
		Arn:                    validTaskArn,
		CPU:                    float64(taskVCPULimit),
		Memory:                 int64(taskMemoryLimit),
		MemoryCPULimitsEnabled: true,
	}

	hostConfig := &docker.HostConfig{}

	assert.NoError(t, task.platformHostConfigOverride(hostConfig))
	assert.NotEmpty(t, hostConfig)
	assert.Equal(t, expectedCgroupRoot, hostConfig.CgroupParent)
}

// TestPlatformHostConfigOverride validates the platform host config overrides
func TestPlatformHostConfigOverrideErrorPath(t *testing.T) {
	task := &Task{
		Arn:                    invalidTaskArn,
		CPU:                    float64(taskVCPULimit),
		Memory:                 int64(taskMemoryLimit),
		MemoryCPULimitsEnabled: true,
		Containers: []*apicontainer.Container{
			{
				Name: "c1",
			},
		},
	}

	dockerHostConfig, err := task.DockerHostConfig(task.Containers[0], dockerMap(task), defaultDockerClientAPIVersion)
	assert.Error(t, err)
	assert.Empty(t, dockerHostConfig)
}

func TestDockerHostConfigRawConfigMerging(t *testing.T) {
	// Use a struct that will marshal to the actual message we expect; not
	// docker.HostConfig which will include a lot of zero values.
	rawHostConfigInput := struct {
		Privileged  bool     `json:"Privileged,omitempty" yaml:"Privileged,omitempty"`
		SecurityOpt []string `json:"SecurityOpt,omitempty" yaml:"SecurityOpt,omitempty"`
	}{
		Privileged:  true,
		SecurityOpt: []string{"foo", "bar"},
	}

	rawHostConfig, err := json.Marshal(&rawHostConfigInput)
	if err != nil {
		t.Fatal(err)
	}

	testTask := &Task{
		Arn:     "arn:aws:ecs:us-east-1:012345678910:task/c09f0188-7f87-4b0f-bfc3-16296622b6fe",
		Family:  "myFamily",
		Version: "1",
		Containers: []*apicontainer.Container{
			{
				Name:        "c1",
				Image:       "image",
				CPU:         50,
				Memory:      100,
				VolumesFrom: []apicontainer.VolumeFrom{{SourceContainer: "c2"}},
				DockerConfig: apicontainer.DockerConfig{
					HostConfig: strptr(string(rawHostConfig)),
				},
			},
			{
				Name: "c2",
			},
		},
	}

	hostConfig, configErr := testTask.DockerHostConfig(testTask.Containers[0], dockerMap(testTask), minDockerClientAPIVersion)
	assert.Nil(t, configErr)

	expected := docker.HostConfig{
		Privileged:       true,
		SecurityOpt:      []string{"foo", "bar"},
		VolumesFrom:      []string{"dockername-c2"},
		MemorySwappiness: memorySwappinessDefault,
		CPUPercent:       minimumCPUPercent,
	}

	assertSetStructFieldsEqual(t, expected, *hostConfig)
}

// TestSetConfigHostconfigBasedOnAPIVersion tests the docker hostconfig was correctly
// set based on the docker client version
func TestSetConfigHostconfigBasedOnAPIVersion(t *testing.T) {
	memoryMiB := 500
	testTask := &Task{
		Containers: []*apicontainer.Container{
			{
				Name:   "c1",
				CPU:    uint(10),
				Memory: uint(memoryMiB),
			},
		},
	}

	hostconfig, err := testTask.DockerHostConfig(testTask.Containers[0], dockerMap(testTask), minDockerClientAPIVersion)
	assert.Nil(t, err)

	config, cerr := testTask.DockerConfig(testTask.Containers[0], defaultDockerClientAPIVersion)
	assert.Nil(t, cerr)

	assert.Equal(t, int64(memoryMiB*1024*1024), config.Memory)
	assert.Equal(t, int64(10), config.CPUShares)
	assert.Empty(t, hostconfig.CPUShares)
	assert.Empty(t, hostconfig.Memory)

	hostconfig, err = testTask.DockerHostConfig(testTask.Containers[0], dockerMap(testTask), dockerclient.Version_1_18)
	assert.Nil(t, err)

	config, cerr = testTask.DockerConfig(testTask.Containers[0], dockerclient.Version_1_18)
	assert.Nil(t, err)
	assert.Equal(t, int64(memoryMiB*1024*1024), hostconfig.Memory)
	assert.Equal(t, int64(10), hostconfig.CPUShares)

	assert.Empty(t, config.CPUShares)
	assert.Empty(t, config.Memory)
}

func TestInitCgroupResourceSpecHappyPath(t *testing.T) {
	taskMemoryLimit := int64(taskMemoryLimit)
	task := &Task{
		Arn:    validTaskArn,
		CPU:    float64(taskVCPULimit),
		Memory: taskMemoryLimit,
		Containers: []*apicontainer.Container{
			{
				Name: "c1",
				TransitionDependenciesMap: make(map[apicontainerstatus.ContainerStatus]apicontainer.TransitionDependencySet),
			},
		},
		MemoryCPULimitsEnabled: true,
		ResourcesMapUnsafe:     make(map[string][]taskresource.TaskResource),
	}
	ctrl := gomock.NewController(t)
	defer ctrl.Finish()
	mockControl := mock_control.NewMockControl(ctrl)
	mockIO := mock_ioutilwrapper.NewMockIOUtil(ctrl)
	assert.NoError(t, task.initializeCgroupResourceSpec("cgroupPath", &taskresource.ResourceFields{
		Control: mockControl,
		ResourceFieldsCommon: &taskresource.ResourceFieldsCommon{
			IOUtil: mockIO,
		},
	}))
	assert.Equal(t, 1, len(task.GetResources()))
	assert.Equal(t, 1, len(task.Containers[0].TransitionDependenciesMap))
}

func TestInitCgroupResourceSpecInvalidARN(t *testing.T) {
	task := &Task{
		Arn:     "arn", // malformed arn
		Family:  "testFamily",
		Version: "1",
		Containers: []*apicontainer.Container{
			{
				Name: "c1",
				TransitionDependenciesMap: make(map[apicontainerstatus.ContainerStatus]apicontainer.TransitionDependencySet),
			},
		},
		MemoryCPULimitsEnabled: true,
		ResourcesMapUnsafe:     make(map[string][]taskresource.TaskResource),
	}
	assert.Error(t, task.initializeCgroupResourceSpec("", nil))
	assert.Equal(t, 0, len(task.GetResources()))
	assert.Equal(t, 0, len(task.Containers[0].TransitionDependenciesMap))
}

func TestInitCgroupResourceSpecInvalidMem(t *testing.T) {
	taskMemoryLimit := int64(taskMemoryLimit)
	task := &Task{
		Arn:    validTaskArn,
		CPU:    float64(taskVCPULimit),
		Memory: taskMemoryLimit,
		Containers: []*apicontainer.Container{
			{
				Name:   "C1",
				Memory: uint(2048), // container memory > task memory
				TransitionDependenciesMap: make(map[apicontainerstatus.ContainerStatus]apicontainer.TransitionDependencySet),
			},
		},
		MemoryCPULimitsEnabled: true,
		ResourcesMapUnsafe:     make(map[string][]taskresource.TaskResource),
	}
	assert.Error(t, task.initializeCgroupResourceSpec("", nil))
	assert.Equal(t, 0, len(task.GetResources()))
	assert.Equal(t, 0, len(task.Containers[0].TransitionDependenciesMap))
}

func TestPostUnmarshalWithCPULimitsFail(t *testing.T) {
	task := &Task{
		Arn:     "arn", // malformed arn
		Family:  "testFamily",
		Version: "1",
		Containers: []*apicontainer.Container{
			{
				Name: "c1",
				TransitionDependenciesMap: make(map[apicontainerstatus.ContainerStatus]apicontainer.TransitionDependencySet),
			},
		},
		ResourcesMapUnsafe: make(map[string][]taskresource.TaskResource),
	}
	cfg := config.Config{
		TaskCPUMemLimit: config.ExplicitlyEnabled,
	}
	assert.Error(t, task.PostUnmarshalTask(&cfg, nil, nil, nil, nil))
	assert.Equal(t, 0, len(task.GetResources()))
	assert.Equal(t, 0, len(task.Containers[0].TransitionDependenciesMap))
}

repository: suneyz/amazon-ecs-agent

repo_name: suneyz/amazon-ecs-agent

gha_language: Go

extension: go


text:
@InProceedings{azizzadenesheli16a,
  author = {Azizzadenesheli, Kamyar and Lazaric, Alessandro and Anandkumar, Animashree},
  title = {Reinforcement Learning of POMDPs using Spectral Methods},
  pages = {193-256},
  abstract = {We propose a new reinforcement learning algorithm for partially observable Markov decision processes (POMDP) based on spectral decomposition methods.
While spectral methods have been previously employed for consistent learning of (passive) latent variable models such as hidden Markov models, POMDPs are more challenging  since the learner interacts with the environment and possibly changes the future observations in the process. We devise a learning algorithm running through episodes, in each episode we employ spectral techniques to learn the POMDP parameters from a trajectory generated by a fixed policy. At the end of the episode, an optimization oracle returns the optimal memoryless planning policy which maximizes the expected reward based on the estimated POMDP model. We prove an order-optimal regret bound w.r.t.\ the optimal memoryless policy and efficient scaling with respect to the dimensionality of observation and action spaces.},
}

repository: mreid/papersite

repo_name: mreid/papersite

gha_language: TeX

extension: bib


text:
package seriesgen

import (
	"math/rand"
	"time"

	"github.com/prometheus/prometheus/model/labels"
)

type sample struct {
	T int64
	V float64
}

// SeriesSet contains a set of series.
type SeriesSet interface {
	Next() bool
	At() Series
	Err() error
}

// Series exposes a single time series.
type Series interface {
	// Labels returns the complete set of labels identifying the series.
	Labels() labels.Labels

	// Iterator returns a new iterator of the data of the series.
	// TODO(bwplotka): Consider moving this to tsdb.SeriesIterator if required. This means adding `Seek` method.
	Iterator() SeriesIterator
}

// SeriesIterator iterates over the data of a time series.
// Simplified version of github.com/prometheus/prometheus/tsdb.SeriesIterator
type SeriesIterator interface {
	// At returns the current timestamp/value pair.
	At() (t int64, v float64)
	// Next advances the iterator by one.
	Next() bool
	// Err returns current error.
	Err() error
}

type SeriesGen struct {
	SeriesIterator

	lset labels.Labels
}

func NewSeriesGen(lset labels.Labels, si SeriesIterator) *SeriesGen {
	return &SeriesGen{
		SeriesIterator: si,
		lset:           lset,
	}
}
func (s *SeriesGen) Labels() labels.Labels { return s.lset }

func (s *SeriesGen) Iterator() SeriesIterator { return s }

var _ SeriesIterator = &GaugeGen{}
var _ SeriesIterator = &CounterGen{}
var _ SeriesIterator = &ValGen{}

type Characteristics struct {
	Jitter         float64       `yaml:"jitter"`
	ScrapeInterval time.Duration `yaml:"scrapeInterval"`
	ChangeInterval time.Duration `yaml:"changeInterval"`
	Max            float64       `yaml:"max"`
	Min            float64       `yaml:"min"`
}

type GaugeGen struct {
	changeInterval   time.Duration
	interval         time.Duration
	maxTime, minTime int64

	min, max, jitter float64

	v       float64
	mod     float64
	init    bool
	elapsed int64

	random *rand.Rand
}

func NewGaugeGen(random *rand.Rand, mint, maxt int64, opts Characteristics) *GaugeGen {
	return &GaugeGen{
		changeInterval: opts.ChangeInterval,
		interval:       opts.ScrapeInterval,
		max:            opts.Max,
		min:            opts.Min,
		minTime:        mint,
		maxTime:        maxt,
		jitter:         opts.Jitter,
		random:         random,
	}
}

func (g *GaugeGen) Next() bool {
	if g.minTime > g.maxTime {
		return false
	}
	defer func() {
		g.minTime += int64(g.interval.Seconds() * 1000)
		g.elapsed += int64(g.interval.Seconds() * 1000)
	}()

	if !g.init {
		g.v = g.min + g.random.Float64()*((g.max-g.min)+1)
		g.init = true
	}

	// Technically only mod changes.
	if g.jitter > 0 && g.elapsed >= int64(g.changeInterval.Seconds()*1000) {
		g.mod = (g.random.Float64() - 0.5) * g.jitter
		g.elapsed = 0
	}
	return true
}

func (g *GaugeGen) At() (t int64, v float64) {
	return g.minTime, g.v + g.mod
}

func (g *GaugeGen) Err() error { return nil }

// TODO(bwplotka): Improve. Does not work well (: Too naive.
// Add resets etc.
type CounterGen struct {
	maxTime, minTime int64

	min, max, jitter float64
	interval         time.Duration
	changeInterval   time.Duration
	rateInterval     time.Duration

	v    float64
	init bool
	buff []sample

	lastVal float64
	elapsed int64

	random *rand.Rand
}

func NewCounterGen(random *rand.Rand, mint, maxt int64, opts Characteristics) *CounterGen {
	return &CounterGen{
		changeInterval: opts.ChangeInterval,
		interval:       opts.ScrapeInterval,
		max:            opts.Max,
		min:            opts.Min,
		minTime:        mint,
		maxTime:        maxt,
		jitter:         opts.Jitter,
		random:         random,
		rateInterval:   5 * time.Minute,
	}
}

func (g *CounterGen) Next() bool {
	defer func() { g.elapsed += int64(g.interval.Seconds() * 1000) }()

	if g.init && len(g.buff) == 0 {
		return false
	}

	if len(g.buff) > 0 {
		// Pop front.
		g.buff = g.buff[1:]

		if len(g.buff) > 0 {
			return true
		}
	}

	if !g.init {
		g.v = g.min + g.random.Float64()*((g.max-g.min)+1)
		g.init = true
	}

	var mod float64
	if g.jitter > 0 && g.elapsed >= int64(g.changeInterval.Seconds()*1000) {
		mod = (g.random.Float64() - 0.5) * g.jitter

		if mod > g.v {
			mod = g.v
		}

		g.elapsed = 0
	}

	// Distribute goalV into multiple rateInterval/interval increments.
	comps := make([]float64, int64(g.rateInterval/g.interval))
	var sum float64
	for i := range comps {
		comps[i] = g.random.Float64()
		sum += comps[i]
	}

	// That's the goal for our rate.
	x := g.v + mod/sum
	for g.minTime <= g.maxTime && len(comps) > 0 {
		g.lastVal += x * comps[0]
		comps = comps[1:]

		g.minTime += int64(g.interval.Seconds() * 1000)
		g.buff = append(g.buff, sample{T: g.minTime, V: g.lastVal})
	}

	return len(g.buff) > 0
}

func (g *CounterGen) At() (int64, float64) { return g.buff[0].T, g.buff[0].V }

func (g *CounterGen) Err() error { return nil }

type ValGen struct {
	interval         time.Duration
	maxTime, minTime int64

	min, max float64

	v      float64
	random *rand.Rand
}

func NewValGen(random *rand.Rand, mint, maxt int64, opts Characteristics) *ValGen {
	return &ValGen{
		interval: opts.ScrapeInterval,
		max:      opts.Max,
		min:      opts.Min,
		minTime:  mint,
		maxTime:  maxt,
		random:   random,
	}
}

func (g *ValGen) Next() bool {
	if g.minTime > g.maxTime {
		return false
	}

	g.minTime += int64(g.interval.Seconds() * 1000)
	g.v = g.min + g.random.Float64()*((g.max-g.min)+1)

	return true
}

func (g *ValGen) At() (t int64, v float64) {
	return g.minTime, g.v
}

func (g *ValGen) Err() error { return nil }

repository: thanos-io/thanosbench

repo_name: thanos-io/thanosbench


text:
%Beispiel BibTeX-Datenbank für die Verwendung von biblatex und biber

@online{imrad,
	title = {IMRAD},
	url = {https://en.wikipedia.org/wiki/IMRAD},
	organization = {Wikipedia},
	urldate = {2018-12-20},
}

@online{scrguide,
	title={KOMA-Script},
	subtitle={Die Anleitung},
	author={Kohm, Markus},
	date={2019-01-14},
	url={http://mirrors.ctan.org/macros/latex/contrib/koma-script/doc/scrguide.pdf},
	urldate={2019-02-05}
	}

@online{leaflet,
	title={The document class leaflet},
	author={Niepraschk, Rolf and Schmidt, Walter and Gäßlein, Hubert},
	date={2015-12-27},
	urldate={2020-02-12},
	url={http://mirrors.ctan.org/macros/latex/contrib/leaflet/leaflet-manual.pdf}
}


text:
# Copyright (C) 2017 Khem Raj <raj.khem@gmail.com>
# Released under the MIT license (see COPYING.MIT for the terms)

DESCRIPTION = "Lightweight cross platform C++ GUID/UUID library"
HOMEPAGE = "https://github.com/graeme-hill/crossguid"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=1373274bc8d8001edc54933919f36f68"
DEPENDS += "util-linux"

SRCREV = "ca1bf4b810e2d188d04cb6286f957008ee1b7681"
SRC_URI = "git://github.com/graeme-hill/crossguid;protocol=https;branch=master \
           file://0001-include-missing-cstdint.patch"

S = "${WORKDIR}/git"

inherit cmake

repository: openembedded/meta-openembedded

repo_name: openembedded/meta-openembedded


text:
require linux-omap3.inc

COMPATIBLE_MACHINE = "ti814x"

KVER = "2.6.37"
PSPREL = "04.01.00.03"

BRANCH = "ti81xx-master"
# Use literal tags in SRCREV, when available, instead of commit IDs
SRCREV = "v2.6.37_TI814XPSP_04.01.00.03"
require recipes/ti/ti-paths.inc

DEPENDS += "ti-ipc"

SYSLINKPV = "02_00_00_68"
SYSLINKPVwithdots = "02.00.00.68"
SYSLINKPVextra = "_beta1"

SRC_URI[syslinktarball.md5sum] = "468034372124f70f82b60cfb5f11c8e8"
SRC_URI[syslinktarball.sha256sum] = "ed574dcb3a5477cfbc69a1c9e768d5197291cb057d19fd791e16e1c89af3e8e1"

SRC_URI += "http://software-dl.ti.com/dsps/dsps_public_sw/SysLink/${SYSLINKPV}${SYSLINKPVextra}/exports/syslink_${SYSLINKPV}${SYSLINKPVextra}.tar.gz;name=syslinktarball \
            file://defconfig.vpss \
"

SYSLINK_ROOT = "${WORKDIR}/syslink_${SYSLINKPV}${SYSLINKPVextra}"
export SYSLINK_ROOT

export IPCDIR = "${IPC_INSTALL_DIR}/packages"

export KBUILD_EXTRA_SYMBOLS=$SYSLINK_ROOT/ti/syslink/utils/hlos/knl/Linux/Module.symvers

SYSLINKPLATFORM_omap     = "OMAP3530"
SYSLINKPLATFORM_omapl137  = "OMAPL1XX"
SYSLINKPLATFORM_omapl138  = "OMAPL1XX"
SYSLINKPLATFORM_ti816x    = "TI81XX"
SYSLINKPLATFORM_ti814x    = "TI81XX"
SYSLINKPLATFORM          ?= "<UNDEFINED_SYSLINKPLATFORM>"

SYSLINKLOADER_omap3       = "COFF"
SYSLINKLOADER_omapl137    = "COFF"
SYSLINKLOADER_omapl138    = "COFF"
SYSLINKLOADER_ti816x      = "ELF"
SYSLINKLOADER_ti814x      = "ELF"
SYSLINKLOADER            ?= "<UNDEFINED_SYSLINKLOADER>"

SYSLINKVARIANT_omap3     = "OMAP3530"
SYSLINKVARIANT_omapl137  = "OMAPL1XX"
SYSLINKVARIANT_omapl138  = "OMAPL1XX"
SYSLINKVARIANT_ti816x    = "TI816X"
SYSLINKVARIANT_ti814x    = "TI814X"
SYSLINKVARIANT          ?= "<UNDEFINED_SYSLINKVARIANT>"

do_compilesyslinkkernelmodule() {

    unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS

    # Build the gpp (hlos) kernel space
    cd ${SYSLINK_ROOT}/ti/syslink/utils/hlos/knl/Linux && make \
        ARCH="${TARGET_ARCH}" \
        CROSS_COMPILE="${TARGET_PREFIX}" \
        SYSLINK_PLATFORM="${SYSLINKPLATFORM}" \
        SYSLINK_VARIANT="${SYSLINKVARIANT}" \
        SYSLINK_LOADER="${SYSLINKLOADER}" \
        SYSLINK_PKGPATH="${IPC_INSTALL_DIR}/packages" \
        KDIR="${S}"
}
addtask compilesyslinkkernelmodule after do_compile before do_setup_defconfigvpss

do_setup_defconfigvpss() {
        cp ${WORKDIR}/defconfig.vpss ${S}/.config
}

addtask setup_defconfigvpss after do_compilesyslinkkernelmodule before do_compile_kernelmodules

do_compile_kernelmodules() {
        unset CFLAGS CPPFLAGS CXXFLAGS LDFLAGS MACHINE
        oe_runmake modules  CC="${KERNEL_CC}" LD="${KERNEL_LD}" KBUILD_EXTRA_SYMBOLS="${KBUILD_EXTRA_SYMBOLS}"
}

addtask compile_kernelmodules after do_setup_defconfigvpss before do_sizecheck


text:
// problem 15.12

package chapter15

func WordBreaking(dict map[string]bool, s string) bool {
	suffix := make([]int, len(s))
	for i := range suffix {
		suffix[i] = -1
	}

	for i := len(s); i >= 0; i-- {
		for j := i + 1; j <= len(s); j++ {
			if dict[s[i:j]] && (j == len(s) || suffix[j] != -1) {
				suffix[i] = j
			}
		}
	}

	return suffix[0] != -1
}


text:
@article{Authors01,
  author    = {Mehdi Mirza and
  Simon Osindero},
  title     = {Conditional Generative Adversarial Nets},
  journal   = {CoRR},
  volume    = {abs/1411.1784},
  year      = {2014},
  url       = {http://arxiv.org/abs/1411.1784},
  archivePrefix = {arXiv},
  eprint    = {1411.1784},
  timestamp = {Wed, 07 Jun 2017 14:40:37 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/MirzaO14},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}

@article{Authors02,
 author = {Chinese Language Education Section},
 title = {Lexical Items with Eng. Explanations for Fundamental Chin. Learning in HK Schools},
 note = {www.edbchinese.hk}
}

@article{Authors03,
  author    = {Leon A. Gatys and
  Alexander S. Ecker and
  Matthias Bethge},
  title     = {A Neural Algorithm of Artistic Style},
  journal   = {CoRR},
  volume    = {abs/1508.06576},
  year      = {2015},
  url       = {http://arxiv.org/abs/1508.06576},
  archivePrefix = {arXiv},
  eprint    = {1508.06576},
  timestamp = {Wed, 07 Jun 2017 14:41:58 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/GatysEB15a},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}

@article{Authors04,
  author    = {Dmitry Ulyanov and
  Vadim Lebedev and
  Andrea Vedaldi and
  Victor S. Lempitsky},
  title     = {Texture Networks: Feed-forward Synthesis of Textures and Stylized
  Images},
  journal   = {CoRR},
  volume    = {abs/1603.03417},
  year      = {2016},
  url       = {http://arxiv.org/abs/1603.03417},
  archivePrefix = {arXiv},
  eprint    = {1603.03417},
  timestamp = {Wed, 07 Jun 2017 14:42:13 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/UlyanovLVL16},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}

@article{Authors05,
  author    = {Phillip Isola and
  Jun{-}Yan Zhu and
  Tinghui Zhou and
  Alexei A. Efros},
  title     = {Image-to-Image Translation with Conditional Adversarial Networks},
  journal   = {CoRR},
  volume    = {abs/1611.07004},
  year      = {2016},
  url       = {http://arxiv.org/abs/1611.07004},
  archivePrefix = {arXiv},
  eprint    = {1611.07004},
  timestamp = {Wed, 07 Jun 2017 14:41:37 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/IsolaZZE16},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}

@article{Authors06,
  author    = {Jun{-}Yan Zhu and
  Taesung Park and
  Phillip Isola and
  Alexei A. Efros},
  title     = {Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial
  Networks},
  journal   = {CoRR},
  volume    = {abs/1703.10593},
  year      = {2017},
  url       = {http://arxiv.org/abs/1703.10593},
  archivePrefix = {arXiv},
  eprint    = {1703.10593},
  timestamp = {Wed, 07 Jun 2017 14:40:17 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/ZhuPIE17},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}

repository: fendaq/CharTrans-GAN

repo_name: fendaq/CharTrans-GAN


text:
@article{repeated,
  author={a},
  title={t},
  journal={j},
  year={2017},
}
@article{repeated,
  author={a},
  title={t},
  journal={j},
  year={2017},
}

repository: isakhammer/vimrc

repo_name: isakhammer/vimrc

gha_language: Vim script


text:
include neutrino-plugins-tuxbox-env.inc

DESCRIPTION = "Get rc-signals for scripts..."

SRC_URI += " \
	file://${PLUGIN_NAME}/Makefile.am \
"

repository: neutrino-hd/meta-neutrino

repo_name: neutrino-hd/meta-neutrino


text:
package main

import (
	//h "./internal"
	"fmt"
	"log"
	"net/http"
	"regexp"
	//"net/http/httptest"
	// "encoding/json"
	"io/ioutil"
	"net/url"
	"strings"
	"testing"
)

func TestIndexPage(t *testing.T) {
	formData := url.Values{
		"bannerSelectControl": {"standard.txt"},
		"inputText":           {"123asd;as"},
	}
	req, err := http.NewRequest("POST", "http://localhost:8181/", strings.NewReader(formData.Encode()))
	req.Header.Set("Content-type", "application/x-www-form-urlencoded")
	if err != nil {
		log.Fatalln(err)
	}
	client := http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatalln(err)
	}
	re := regexp.MustCompile(`<pre>(.|\n)*?</pre>`)

	fmt.Println(re.FindString(string(body)))
}

repository: innovember/ascii-art-web-export-file

repo_name: innovember/ascii-art-web-export-file


text:
include westeros.inc

SUMMARY = "This receipe compiles the westeros compositor gstreamer sink element"

S = "${WORKDIR}/git"

SRC_URI += " \
           file://0004-Dispatch-custom-queue-instead-flushing-display.patch \
"

inherit autotools pkgconfig

DEPENDS += "wayland-native wayland westeros-simpleshell westeros-simplebuffer westeros"

do_compile_prepend() {
	oe_runmake -C ${S}/protocol
}

CFLAGS += "-I${S}/protocol"
CFLAGS_append_toolchain-clang = " -Wno-error=c++11-narrowing"

repository: KrishnakumarPathivuraj/meta-wpe

repo_name: KrishnakumarPathivuraj/meta-wpe

gha_license_id: NOASSERTION


text:
require oprofile.inc

PR = "${INC_PR}.0"

SRC_URI[tarball.md5sum] = "4e407093ac06200185d5a5e6437d7242"
SRC_URI[tarball.sha256sum] = "3f0dd40a7749fc650d25d79d42ebbff8f3b6db310c36e7c3839696cc09077880"

repository: overo/overo-oe-natty

repo_name: overo/overo-oe-natty

branch_name: HEAD


text:
SUMMARY = "This is the PAM to RADIUS authentication module."
LICENSE = "GPLv2+ & Livingston"
LIC_FILES_CHKSUM = "file://debian/copyright;md5=fe546348781fcbc12a5e7b8ae6414e60"
NO_GENERIC_LICENSE[Livingston] = "debian/copyright"

# Set DEBIAN_UNPACK_DIR to the path to the extracted Debian source package.
DEBIAN_UNPACK_DIR = "${WORKDIR}/pam_radius-release_1_4_0"

inherit autotools-brokensep debian-package
require recipes-debian/sources/libpam-radius-auth.inc

DEPENDS += "libpam"
REQUIRED_DISTRO_FEATURES = "pam"

FILES_${PN} += "${base_libdir}/security/* ${sysconfdir}/* ${datadir}/* ${docdir}/*"

do_install() {
    install -d ${D}${sysconfdir}
    install -m 600 ${S}/pam_radius_auth.conf ${D}${sysconfdir}
    install -d ${D}${base_libdir}/security
    install -m 755 ${S}/pam_radius_auth.so ${D}${base_libdir}/security

    install -d ${D}${docdir}/libpam-radius-auth/examples
    install -m 644 ${S}/debian/README.Debian ${D}${docdir}/libpam-radius-auth
    install -m 644 ${S}/README.rst ${D}${docdir}/libpam-radius-auth
    install -m 644 ${S}/USAGE ${D}${docdir}/libpam-radius-auth
    install -m 644 ${S}/debian/changelog ${D}${docdir}/libpam-radius-auth/changelog.Debian
    install -m 644 ${S}/Changelog ${D}${docdir}/libpam-radius-auth/changelog
    install -m 644 ${S}/debian/copyright ${D}${docdir}/libpam-radius-auth
    install -m 644 ${S}/debian/index.html ${D}${docdir}/libpam-radius-auth

    install -m 644 ${S}/debian/pam_example ${D}${docdir}/libpam-radius-auth/examples
    install -m 644 ${S}/pam_radius_auth.conf ${D}${docdir}/libpam-radius-auth/examples
}

repository: miraclelinux/meta-debian-extended

repo_name: miraclelinux/meta-debian-extended

branch_name: refs/heads/warrior


text:
package middlewares

import (
	"context"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/purusah/cxtnxbr/pkg/config"
	"github.com/purusah/cxtnxbr/pkg/globals"
)

func DummyHandler(_ http.ResponseWriter, _ *http.Request) {}

func TestCounterMiddleware(t *testing.T) {
	url := "/api"
	conf, _ := config.GetConfig()
	gl := globals.NewCache(conf)
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		t.Fatal(err)
	}

	res := httptest.NewRecorder()

	CounterMiddleware(gl.C, http.HandlerFunc(DummyHandler)).ServeHTTP(res, req)

	if http.StatusOK != res.Code {
		t.Errorf("expected %d got %d code", http.StatusOK, res.Code)
	}
	val, _ := gl.C.Get(context.Background(), GetUriCounterKey(url))
	if val != 1 {
		t.Errorf("expected %d value, got %d", 1, val)
	}
}

repository: Purusah/cxtnxbr

repo_name: Purusah/cxtnxbr


text:
SUMMARY = "SDK for developing enclaves"
DESCRIPTION = "The Open Enclave SDK is a hardware-agnostic open source library for developing applications that utilize Hardware-based Trusted Execution Environments, also known as Enclaves."
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${S}/LICENSE;md5=ecf7715ae2f3910d1baf6908a6725b09"

DEPENDS = "cmake-native ninja-native python3-native python3-pycryptodome-native optee-os-msiot openssl"

SRC_URI = " \
    gitsm://github.com/openenclave/openenclave.git;protocol=https;nobranch=1;tag=v${PV} \
    file://0001-Fix-libgcc-path-and-cmake-byproducts.patch \
"

S = "${WORKDIR}/git"
B = "${WORKDIR}/build"

PACKAGES = "${PN} ${PN}-dev ${PN}-dbg ${PN}-staticdev"

do_configure() {

    # Add CFLAGS to 3rd party part of build flow
    # We modify it in the source directory so we first revert it to make sure we dont have
    # multiple modifications to is when configure is run multiple times
    cd ${S}
    git checkout 3rdparty/CMakeLists.txt
    sed -i '91i\                 -DCMAKE_C_FLAGS='\''--sysroot=${STAGING_DIR_TARGET}'\''' 3rdparty/CMakeLists.txt

    # Get the target compilers without arguments
    TARGET_CC_BASE=`echo ${CC} | cut -d " " -f1 | sed 's/-gcc//g'`
    TARGET_CC=${TARGET_CC_BASE}-gcc
    TARGET_CXX=${TARGET_CC_BASE}-g++
    TARGET_CXX_VERSION=`${CXX} -dumpversion`

    # modify cmake crosscompile toolchain file to match yocto build config
    git checkout cmake/arm-cross.cmake
    echo "set(CMAKE_C_COMPILER ${TARGET_CC})" >> cmake/arm-cross.cmake
    echo "set(CMAKE_CXX_COMPILER ${TARGET_CXX})" >> cmake/arm-cross.cmake
    echo "set(CMAKE_SYSROOT "${PKG_CONFIG_SYSROOT_DIR}")" >> cmake/arm-cross.cmake
    echo "set(CMAKE_CXX_LINK_FLAGS \"-L${PKG_CONFIG_SYSROOT_DIR}/usr/lib/${TARGET_CC_BASE}/${TARGET_CXX_VERSION}/\")" >> cmake/arm-cross.cmake

    # Help cmake to find the correct compiler for the compilation of oeedger
    BUILD_CC_WITH_PATH=`which ${BUILD_CC}`
    BUILD_CXX_WITH_PATH=`which ${BUILD_CXX}`

    cd ${B}

    cmake ${S} \
        -G Ninja \
        -DCMAKE_INSTALL_PREFIX=${D}/opt/oe/enclave/openenclave \
        -DOE_TA_DEV_KIT_DIR=${STAGING_DIR_HOST}/usr/include/optee/export-user_ta/ \
        -DCMAKE_BUILD_TYPE=Debug \
        -DCMAKE_TOOLCHAIN_FILE=${S}/cmake/arm-cross.cmake \
        -DENABLE_REFMAN=OFF \
        -DEDGER8R_C_COMPILER=${BUILD_CC_WITH_PATH} \
        -DEDGER8R_CXX_COMPILER=${BUILD_CXX_WITH_PATH}
}

do_compile() {
    ninja -v ${PARALLEL_MAKE}
}

FILES_${PN}-staticdev =" \
    /opt/oe/ \
"

SYSROOT_DIRS += "/opt"
PROVIDES = "${PN}-staticdev"

do_install_append () {
    SDKDIR="${D}/opt/oe"

    #install -d -m0644 ${SDKDIR}/3rdparty/libc
    #install -d -m0644 ${SDKDIR}/3rdparty/libcxx
    #install -d -m0644 ${SDKDIR}/3rdparty/libmbedtls
    #install -d -m0644 ${SDKDIR}/enclave/openenclave

    # Assemble openeclave SDK compatible export
    install -d -m0644 ${SDKDIR}/
    install -m0644 ${S}/3rdparty/optee/optee_os/ta/arch/arm/ta.ld.S             ${SDKDIR}/
    install -D -m0544 ${S}/3rdparty/optee/optee_os/scripts/sign.py              ${SDKDIR}/sign.py
    sed -i 's|#!/usr/bin/env python|#!/usr/bin/env python3|g'                   ${SDKDIR}/sign.py
    install -m0644 ${S}/3rdparty/optee/optee_os/keys/default_ta.pem             ${SDKDIR}/default_ta.pem

    install -d -m0644 ${SDKDIR}/${MACHINE}/bin
    install -d -m0644 ${SDKDIR}/${MACHINE}/include/openenclave/bits/
    install -d -m0644 ${SDKDIR}/${MACHINE}/include/openenclave/3rdparty/optee/liboeutee
    install -d -m0644 ${SDKDIR}/${MACHINE}/include/openenclave/corelibc/
    install -d -m0644 ${SDKDIR}/${MACHINE}/include/openenclave/corelibc/bits
    install -d -m0644 ${SDKDIR}/${MACHINE}/share
    install -d -m0644 ${SDKDIR}/${MACHINE}/lib/openenclave/cmake
    install -d -m0644 ${SDKDIR}/${MACHINE}/lib/openenclave/host
    install -d -m0644 ${SDKDIR}/${MACHINE}/lib/openenclave/enclave

    # Install OPTEE TA
    cp -R --no-dereference --preserve=mode,links -v ${PKG_CONFIG_SYSROOT_DIR}/usr/include/optee/export-user_ta/include/* ${SDKDIR}/${MACHINE}/include/openenclave/3rdparty/

    install -m0644 ${PKG_CONFIG_SYSROOT_DIR}/usr/include/optee/export-user_ta/host_include/conf.h ${SDKDIR}/${MACHINE}/include/openenclave/3rdparty/optee/

    install -D -m0644 ${S}/include/openenclave/host.h                           ${SDKDIR}/${MACHINE}/include/openenclave/host.h
    install -D -m0644 ${S}/include/openenclave/host_verify.h                    ${SDKDIR}/${MACHINE}/include/openenclave/host_verify.h
    install -D -m0644 ${S}/include/openenclave/enclave.h                        ${SDKDIR}/${MACHINE}/include/openenclave/enclave.h
    install -D -m0644 ${S}/include/openenclave/edger8r/host.h                   ${SDKDIR}/${MACHINE}/include/openenclave/edger8r/host.h

    for F in ${B}/cmake/* ; do
        install -m0644 $F ${SDKDIR}/${MACHINE}/lib/openenclave/cmake/
    done

    for F in ${B}/CMakeFiles/Export/lib/openenclave/cmake/* ; do
        install -m0644 $F ${SDKDIR}/${MACHINE}/lib/openenclave/cmake/
    done

    for F in ${B}/3rdparty/optee/libutee/liboeutee/*.h ; do
        install -m0644 $F ${SDKDIR}/${MACHINE}/include/openenclave/3rdparty/optee/liboeutee
    done

    for F in ${S}/include/openenclave/corelibc/*.h ; do
        install -m0644 $F ${SDKDIR}/${MACHINE}/include/openenclave/corelibc
    done

    for F in ${S}/include/openenclave/corelibc/bits/*.h ; do
        install -m0644 $F ${SDKDIR}/${MACHINE}/include/openenclave/corelibc/bits
    done

    cp -R --no-dereference --preserve=mode,links -v ${B}/output/include/openenclave/libc ${SDKDIR}/${MACHINE}/include/openenclave/3rdparty/libc

    # Include files
    install -D -m0644 ${B}/output/include/openenclave/bits/time.h              ${SDKDIR}/${MACHINE}/include/openenclave/bits/time.h
    install -D -m0644 ${B}/output/include/openenclave/bits/asym_keys.h         ${SDKDIR}/${MACHINE}/include/openenclave/bits/asym_keys.h
    install -D -m0644 ${S}/include/openenclave/bits/defs.h                     ${SDKDIR}/${MACHINE}/include/openenclave/bits/defs.h
    install -D -m0644 ${S}/include/openenclave/bits/eeid.h                     ${SDKDIR}/${MACHINE}/include/openenclave/bits/eeid.h
    install -D -m0644 ${S}/include/openenclave/bits/evidence.h                 ${SDKDIR}/${MACHINE}/include/openenclave/bits/evidence.h
    install -D -m0644 ${S}/include/openenclave/bits/exception.h                ${SDKDIR}/${MACHINE}/include/openenclave/bits/exception.h
    install -D -m0644 ${S}/include/openenclave/bits/fs.h                       ${SDKDIR}/${MACHINE}/include/openenclave/bits/fs.h
    install -D -m0644 ${S}/include/openenclave/bits/types.h                    ${SDKDIR}/${MACHINE}/include/openenclave/bits/types.h
    install -D -m0644 ${S}/include/openenclave/bits/module.h                   ${SDKDIR}/${MACHINE}/include/openenclave/bits/module.h
    install -D -m0644 ${S}/include/openenclave/bits/properties.h               ${SDKDIR}/${MACHINE}/include/openenclave/bits/properties.h
    install -D -m0644 ${S}/include/openenclave/bits/report.h                   ${SDKDIR}/${MACHINE}/include/openenclave/bits/report.h
    install -D -m0644 ${S}/include/openenclave/bits/result.h                   ${SDKDIR}/${MACHINE}/include/openenclave/bits/result.h
    install -D -m0644 ${S}/include/openenclave/bits/optee/opteeproperties.h    ${SDKDIR}/${MACHINE}/include/openenclave/bits/optee/opteeproperties.h
    install -D -m0644 ${S}/include/openenclave/edger8r/enclave.h               ${SDKDIR}/${MACHINE}/include/openenclave/edger8r/enclave.h
    install -D -m0644 ${S}/include/openenclave/edger8r/common.h                ${SDKDIR}/${MACHINE}/include/openenclave/edger8r/common.h

    # Static libraries
    install -D -m0644 ${B}/output/lib/openenclave/host/liboehost.a             ${SDKDIR}/${MACHINE}/lib/openenclave/host/liboehost.a
    install -D -m0644 ${B}/output/lib/openenclave/enclave/liboeenclave.a       ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboeenclave.a
    install -D -m0644 ${B}/output/lib/openenclave/enclave/liboecore.a          ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboecore.a
    install -D -m0644 ${B}/output/lib/openenclave/enclave/liboelibc.a          ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboelibc.a
    install -D -m0644 ${B}/output/lib/openenclave/enclave/liboecryptombedtls.a ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboecryptombedtls.a
    install -D -m0644 ${B}/output/lib/openenclave/enclave/liboedebugmalloc.a   ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboedebugmalloc.a
    install -D -m0644 ${B}/output/lib/enclave/liboelibcxx.a                    ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboelibcxx.a
    install -D -m0644 ${B}/syscall/liboesyscall.a                              ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboesyscall.a
    install -D -m0644 ${B}/syscall/devices/hostfs/liboehostfs.a                ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboehostfs.a
    install -D -m0644 ${B}/syscall/devices/hostresolver/liboehostresolver.a    ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboehostresolver.a
    install -D -m0644 ${B}/syscall/devices/hostsock/liboehostsock.a            ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboehostsock.a
    install -D -m0644 ${B}/syscall/devices/hostepoll/liboehostepoll.a          ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboehostepoll.a

    install -D -m0644 ${B}/3rdparty/mbedtls/libmbedcrypto.a                    ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/libmbedcrypto.a
    install -D -m0644 ${B}/3rdparty/mbedtls/libmbedtls.a                       ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/libmbedtls.a
    install -D -m0644 ${B}/3rdparty/mbedtls/libmbedx509.a                      ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/libmbedx509.a

    # TODO, make sure these match the acual running optee version
    install -D -m0644 ${B}/3rdparty/optee/libutee/liboeutee.a                  ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboeutee.a
    install -D -m0644 ${B}/3rdparty/optee/libutee/liboeuteeasm.a               ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboeuteeasm.a
    install -D -m0644 ${B}/3rdparty/optee/libutee/liboeutee.a                  ${SDKDIR}/${MACHINE}/lib/openenclave/enclave/liboeutee.a

    install -D -m0644 ${B}/3rdparty/optee_client-wrap-prefix/src/optee_client-wrap-build/libteec/libteec.a ${SDKDIR}/${MACHINE}/lib/openenclave/optee/libteec/libteec.a

    #Remove paths from commands, as we assume everything external to be put in place by yocto
    sed -i 's|${OE_BINDIR}/||g' ${SDKDIR}/${MACHINE}/lib/openenclave/cmake/openenclave-config.cmake

}

repository: Scalys/meta-scalys-openenclave

repo_name: Scalys/meta-scalys-openenclave

branch_name: refs/heads/main


text:
// Copyright 2019 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package apis

import (
	nginxApis "github.com/tsuru/nginx-operator/pkg/apis"
	"github.com/tsuru/rpaas-operator/pkg/apis/extensions/v1alpha1"
	"k8s.io/apimachinery/pkg/runtime"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
	"sigs.k8s.io/controller-runtime/pkg/manager"
)

// AddToSchemes may be used to add all resources defined in the project to a Scheme
var AddToSchemes runtime.SchemeBuilder

// AddToScheme adds all Resources to the Scheme
func AddToScheme(s *runtime.Scheme) error {
	return AddToSchemes.AddToScheme(s)
}

// AddFieldIndexes adds some indexes on fieldIndexer so their objects can
// later be fetched by a field selector.
func AddFieldIndexes(fieldIndexer client.FieldIndexer) error {
	return fieldIndexer.IndexField(
		&v1alpha1.RpaasInstance{},
		"metadata.name",
		func(o runtime.Object) []string { return []string{o.(*v1alpha1.RpaasInstance).Name} },
	)
}

func NewManager() (manager.Manager, error) {
	cfg, err := config.GetConfig()
	if err != nil {
		return nil, err
	}
	mgr, err := manager.New(cfg, manager.Options{})
	if err != nil {
		return nil, err
	}
	if err = AddToScheme(mgr.GetScheme()); err != nil {
		return nil, err
	}
	if err = nginxApis.AddToScheme(mgr.GetScheme()); err != nil {
		return nil, err
	}
	if err = AddFieldIndexes(mgr.GetFieldIndexer()); err != nil {
		return nil, err
	}
	return mgr, nil
}

repository: arthurcgc/rpaas-operator

repo_name: arthurcgc/rpaas-operator


text:
LICENSE = "GPL"
inherit gpe autotools pkgconfig

DESCRIPTION = "GPE to-do list"
DEPENDS = "gtk+ libgpewidget libgpepimc libtododb"
SECTION = "gpe"
PRIORITY = "optional"

SRC_URI="${GPE_MIRROR}/${PN}-${PV}.tar.bz2"

SRC_URI[md5sum] = "b96e83e2e02ef55474878500587fd2a2"
SRC_URI[sha256sum] = "b9b7a165ffd1998ac09ddf9359d2005f4f2dbc995887b9c9a4fec959e3f32646"


text:
DESCRIPTION = "A docutils-compatibility bridge to CommonMark"
HOMEPAGE = "https://github.com/rtfd/recommonmark"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://license.md;md5=5e65ef1f0d4e1a71254a98b0ed7e9101"

SRC_URI = "git://github.com/rtfd/recommonmark;protocol=https"
SRCREV = "e81b0a0de13be3c5effe26e531df17c16e87329a"
S = "${WORKDIR}/git"

PYPI_PACKAGE_HASH = "aa1085573adf3dc7b164ae8569d57b1af5e98922e40345bb7efffed5ad2e"

inherit setuptools

BBCLASSEXTEND = "native"

repository: billyjbryant/ops-build

repo_name: billyjbryant/ops-build

gha_language: Python


text:
%CP3-10-15 (lorenz)%
@Article{Lorenz:2010vf,
     author    = "Lorenz, Larissa and Martin, Jerome and Yokoyama, Jun'ichi",
     title     = "{Geometrically Consistent Approach to Stochastic DBI Inflation}",
     journal   = "Phys. Rev.",
     volume    = "D82",
     year      = "2010",
     pages     = "023515",
     eprint    = "1004.3734",
     archivePrefix = "arXiv",
     primaryClass  =  "hep-th",
     doi       = "10.1103/PhysRevD.82.023515",
     SLACcitation  = "%%CITATION = 1004.3734;%%"
}

%CP3-10-21 (ringeval)%
@Article{Lorenz:2010sm,
     author    = "Lorenz, Larissa and Ringeval, Christophe and Sakellariadou,
                  Mairi",
     title     = "{Cosmic string loop distribution on all length scales and
                  at any redshift}",
     journal   = "JCAP",
     volume    = "1010",
     year      = "2010",
     pages     = "003",
     eprint    = "1006.0931",
     archivePrefix = "arXiv",
     primaryClass  =  "astro-ph.CO",
     doi       = "10.1088/1475-7516/2010/10/003",
     SLACcitation  = "%%CITATION = 1006.0931;%%"
}

%CP3-11-16 (lorenz)%
@Article{Lilley:2011ag,
     author    = "Lilley, Marc and Lorenz, Larissa and Clesse, Sebastien",
     title     = "{Observational signatures of a non-singular bouncing
                  cosmology}",
     journal   = "JCAP",
     volume    = "1106",
     year      = "2011",
     pages     = "004",
     eprint    = "1104.3494",
     archivePrefix = "arXiv",
     primaryClass  =  "gr-qc",
     doi       = "10.1088/1475-7516/2011/06/004",
     SLACcitation  = "%%CITATION = 1104.3494;%%"
}

repository: eatdust/curl

repo_name: eatdust/curl

branch_name: refs/heads/gh-pages

gha_language: HTML


text:
package main

import (
	"context"
	"net"
	"strings"
	"syscall"
)

type multiListener struct {
	listeners []*net.TCPListener
	connChan  chan acceptResult
	ctx       context.Context
	cancel    context.CancelFunc
}

type acceptResult struct {
	conn net.Conn
	err  error
}

func newListener(addr string) (net.Listener, error) {
	host, port, err := net.SplitHostPort(addr)
	if err != nil {
		return nil, err
	}
	iPort, err := net.LookupPort("tcp", port)
	if err != nil {
		return nil, err
	}

	var ips []net.IP
	if host == "" {
		return net.Listen("tcp", addr)
	} else if host == "localhost" {
		ips, err = getLocalhostIp()
		if err != nil {
			return nil, err
		}
	} else {
		ips, err = net.LookupIP(host)
		if err != nil {
			return nil, err
		}
	}
	if len(ips) == 0 {
		return net.Listen("tcp", addr)
	} else if len(ips) == 1 {
		return net.ListenTCP("tcp", &net.TCPAddr{IP: ips[0], Port: iPort})
	}
	return newMultiListener(ips, iPort)
}

func getLocalhostIp() ([]net.IP, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}
	rt := make([]net.IP, 0, 2)
	for _, addr := range addrs {
		if ip, ok := addr.(*net.IPNet); ok && ip.IP.IsLoopback() {
			rt = append(rt, ip.IP)
		}
	}
	return rt, nil
}

func newMultiListener(ips []net.IP, port int) (lis net.Listener, err error) {
	listeners := make([]*net.TCPListener, 0, len(ips))
	defer func() {
		if err != nil {
			for _, l := range listeners {
				l.Close()
			}
		}
	}()
	for _, ip := range ips {
		l, err := net.ListenTCP("tcp", &net.TCPAddr{IP: ip, Port: port})
		if err != nil {
			return nil, err
		}
		listeners = append(listeners, l)
	}
	ctx, cancel := context.WithCancel(context.Background())
	rt := &multiListener{
		listeners: listeners,
		connChan:  make(chan acceptResult),
		ctx:       ctx,
		cancel:    cancel,
	}
	for _, l := range listeners {
		l := l
		go func() {
			for {
				conn, err := l.AcceptTCP()
				select {
				case rt.connChan <- acceptResult{conn: conn, err: err}:
				case <-ctx.Done():
					return
				}

			}
		}()
	}

	return rt, nil
}

func (ml *multiListener) Accept() (net.Conn, error) {
	select {
	case ar := <-ml.connChan:
		return ar.conn, ar.err
	case <-ml.ctx.Done():
		return nil, syscall.EINVAL
	}
}

func (ml *multiListener) Close() error {
	ml.cancel()
	for _, l := range ml.listeners {
		l.Close()
	}
	return nil
}

func (ml *multiListener) Addr() net.Addr {
	return ml.listeners[0].Addr()
}

func printListener(lis net.Listener) string {
	switch l := lis.(type) {
	case *multiListener:
		addrs := make([]string, 0, len(l.listeners))
		for _, l := range l.listeners {
			addrs = append(addrs, l.Addr().String())
		}
		return strings.Join(addrs, ",")
	default:
		return lis.Addr().String()
	}
}

repository: criyle/go-judge

repo_name: criyle/go-judge


text:
// Package rtmp provides client and server implementation for RTMP message and chunk streams
package rtmp

import (
	"context"

	"github.com/eientei/videostreamer/pkg/rtmp/message"
)

// Connection interface provides RTMP connection methods for terminating connection and writing and reading messages
type Connection interface {
	Recv(ctx context.Context) (*message.Raw, error)
	Send(ctx context.Context, msg *message.Raw) error
	Close() error
}

repository: eientei/videostreamer

repo_name: eientei/videostreamer

branch_name: refs/heads/wip


text:
package main

import "fmt"

func trap(height []int) int {
	if len(height) == 0 {
		return 0
	}
	res := 0
	lmax, rmax := make([]int, len(height)), make([]int, len(height))
	lmax[0] = height[0]
	for i := 1; i < len(height); i++ {
		lmax[i] = max(lmax[i-1], height[i])
	}
	rmax[len(height)-1] = height[len(height)-1]
	for i := len(height) - 2; i >= 0; i-- {
		rmax[i] = max(rmax[i+1], height[i])
	}

	for i := 0; i < len(height); i++ {
		if min(lmax[i], rmax[i])-height[i] > 0 {
			res += min(lmax[i], rmax[i]) - height[i]
		}

	}

	return res
}

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func max(a, b int) int {
	if a > b {
		return a
	}

	return b
}

func main() {
	fmt.Println(trap([]int{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}))
}

repository: amistyrain/leetcode

repo_name: amistyrain/leetcode


text:
@ARTICLE{4663680,
author={J. Ruiz-Amaya and M. Delgado-Restituto and Á. Rodriguez-Vazquez},
journal={IEEE Transactions on Circuits and Systems I: Regular Papers},
title={Accurate Settling-Time Modeling and Design Procedures for Two-Stage Miller-Compensated Amplifiers for Switched-Capacitor Circuits},
year={2009},
volume={56},
number={6},
pages={1077-1087},
keywords={capacitors;operational amplifiers;switched capacitor networks;ENOB;SNDR;electrical simulation;full-fledged simulations;operational transconductance amplifiers;settling-time modeling;small-signal approach;switched-capacitor circuits;system-level specifications;two-stage miller-compensated amplifiers;Analog circuit design;data converters;design methodology;operational amplifiers;switched-capacitor (SC) circuits},
doi={10.1109/TCSI.2008.2008509},
ISSN={1549-8328},
month={June},}


text:
@InProceedings{huang:naacl16,
    title = {Visual Storytelling},
    author = {Huang, Ting-Hao (Kenneth) and Ferraro, Francis and Mostafazadeh, Nasrin and Misra, Ishan and Agrawal, Aishwarya and Devlin, Jacob and Girshick, Ross and He, Xiaodong and Kohli, Pushmeet and Batra, Dhruv and Zitnick, C. Lawrence and Parikh, Devi and Vanderwende, Lucy and Galley, Michel and Mitchell, Margaret},
    booktitle = {Proceedings of NAACL-HLT 2016},
    year = {2016}
}

@InProceedings{roemmele:aaai16,
	title = {Writing Stories with Help from Recurrent Neural Networks},
	author = {Roemmele, Melissa},
	booktitle = {Proceedings of AAAI 2016},
	year = {2016}
}

@InProceedings{jain:sigkdd17,
	title = {Story Generation from Sequence of Independent Short Descriptions},
	author = {Jain, Parag and Agrawal, Priyanka and Mishra, Abhijit and Sukhwani, Mohak and Laha, Anirban and Sankaranarayanan, Karthik},
	booktitle = {Proceedings of SIGKDD 2017 Workshop on Machine Learning for Creativity (ML4Creativity)},
	year = {2017}
}

@InProceedings{liu:aaai18,
	title = {Table-to-text Generation by Structure-aware Seq2seq Learning},
	author = {Liu, Tianyu and Wang, Kexiang and Sha, Lei and Chang, Baobao and Sui, Zhifang},
	booktitle = {Proceedings of AAAI 2018},
	year = {2018}
}

@InProceedings{fan:acl18,
	title = {Hierarchical Neural Story Generation},
	author = {Fan, Angela and Lewis, Mike and Dauphin, Yann},
	booktitle = {Proceedings of ACL 2018},
	year = {2018}
}

@InProceedings{song:acl18,
	title = {A Graph-to-Sequence Model for AMR-to-Text Generation},
	author = {Song, Linfeng and Zhang, Yue and Wang, Zhiguo and Gildea, Daniel},
	booktitle = {Proceedings of ACL 2018},
	year = {2018}
}

@InProceedings{clark:aaai18,
	title = {Event Representations for Automated Story Generation with Deep Neural Nets},
	author = {Clark, Elizabeth and Ji, Yangfeng and Smith, Noah A.},
	booktitle = {Proceedings of AAAI 2018},
	year = {2018}
}

@InProceedings{wiseman:emnlp18,
	title = {Learning Neural Templates for Text Generation},
	author = {Wiseman, Sam and Shieber, Stuart and Rush, Alexander},
	booktitle = {Proceedings of EMNLP 2018},
	year = {2018}
}

@InProceedings{chaturvedi:emnlp18,
	title = {Story Comprehension for Predicting What Happens Next},
	author = {Chaturvedi, Snigdha and Peng, Haoruo and Roth, Dan},
	booktitle = {Proceedings of EMNLP 2018},
	year = {2018}
}

@InProceedings{zhang:acl18,
	title = {Sentence-State LSTM for Text Representation},
	author = {Zhang, Yue and Liu, Qi and Song, Linfeng},
	booktitle = {Proceedings of ACL 2018},
	year = {2018}
}

@InProceedings{kezar:acl18,
	title = {Mixed Feelings: Natural Text Generation with Variable, Coexistent Affective Categories},
	author = {Kezar, Lee},
	booktitle = {Proceedings of ACL 2018},
	year = {2018}
}

@InProceedings{welleck:icml19,
	title = {Non-Monotonic Sequential Text Generation},
	author = {Welleck, Sean and Brantley, Kianté and Daumé III, Hal and Cho, Kyunghyun},
	booktitle = {Proceedings of ICML 2019},
	year = {2019}
}

@InProceedings{nikolaos:icml19,
	title = {Deep Residual Output Layers for Neural Language Generation},
	author = {Nikolaos, Pappas and James, Henderson},
	booktitle = {Proceedings of ICML 2019},
	year = {2019}
}

@InProceedings{amit:naacl19,
	title = {Step-by-Step: Separating Planning from Realization in Neural Data to Text Generation},
	author = {Amit, Moryossef and Yoav, Goldberg and Ido, Dagan},
	booktitle = {Proceedings of NAACL-HLT 2019},
	year = {2019}
}

@InProceedings{sheng:naacl19,
	title = {Pragmatically Informative Text Generation},
	author = {Sheng, Shen and Daniel, Fried and Jacob, Andreas and Dan, Klein},
	booktitle = {Proceedings of NAACL-HLT 2019},
	year = {2019}
}

@InProceedings{fan:acl19,
	title = {Strategies for Structuring Story Generation},
	author = {Fan, Angela and Lewis, Mike and Dauphin, Yann. 2019},
	booktitle = {Proceedings of ACL 2019},
	year = {2019}
}

@InProceedings{wang:emnlp19,
	title = {Denoising based Sequence-to-Sequence Pre-training for Text Generation},
	author = {Wang, Liang and Zhao, Wei and Jia, Ruoyu and Li, Sujian and Liu, Jingming},
	booktitle = {Proceedings of EMNLP 2019},
	year = {2019}
}


text:
# base recipe: meta/recipes-multimedia/alsa/alsa-utils_1.1.8.bb
# base branch: warrior
# base commit: 640b062451eec0d0ee4f972a4749c580c41f9b1f

SUMMARY = "ALSA sound utilities"
HOMEPAGE = "http://www.alsa-project.org"
BUGTRACKER = "http://alsa-project.org/main/index.php/Bug_Tracking"
SECTION = "console/utils"
LICENSE = "GPLv2+"
LIC_FILES_CHKSUM = "file://COPYING;md5=59530bdf33659b29e73d4adb9f9f6552 \
                    file://alsactl/utils.c;beginline=1;endline=20;md5=2ce7f7b6739487fb72c689d46521f958"

inherit debian-package
require recipes-debian/sources/alsa-utils.inc

DEPENDS = "alsa-lib ncurses libsamplerate0"

PACKAGECONFIG ??= "udev"

# alsabat can be built also without fftw support (with reduced functionality).
# It would be better to always enable alsabat, but provide an option for
# enabling/disabling fftw. The configure script doesn't support that, however
# (at least in any obvious way), so for now we only support alsabat with fftw
# or no alsabat at all.
PACKAGECONFIG[bat] = "--enable-bat,--disable-bat,fftwf"

PACKAGECONFIG[udev] = "--with-udev-rules-dir=`pkg-config --variable=udevdir udev`/rules.d,--with-udev-rules-dir=/unwanted/rules.d,udev"
PACKAGECONFIG[manpages] = "--enable-xmlto, --disable-xmlto, xmlto-native docbook-xml-dtd4-native docbook-xsl-stylesheets-native"

# On build machines with python-docutils (not python3-docutils !!) installed
# rst2man (not rst2man.py) is detected and compile fails with
# | make[1]: *** No rule to make target 'alsaucm.1', needed by 'all-am'.  Stop.
# Avoid this by disabling expicitly
EXTRA_OECONF = "--disable-rst2man"

inherit autotools gettext pkgconfig manpages

# This are all packages that we need to make. Also, the now empty alsa-utils
# ipk depends on them.

ALSA_UTILS_PKGS = "\
             ${@bb.utils.contains('PACKAGECONFIG', 'bat', 'alsa-utils-alsabat', '', d)} \
             alsa-utils-alsamixer \
             alsa-utils-alsatplg \
             alsa-utils-midi \
             alsa-utils-aplay \
             alsa-utils-amixer \
             alsa-utils-aconnect \
             alsa-utils-iecset \
             alsa-utils-speakertest \
             alsa-utils-aseqnet \
             alsa-utils-aseqdump \
             alsa-utils-alsactl \
             alsa-utils-alsaloop \
             alsa-utils-alsaucm \
            "

PACKAGES += "${ALSA_UTILS_PKGS}"
RDEPENDS_${PN} += "${ALSA_UTILS_PKGS}"

FILES_${PN} = ""
FILES_alsa-utils-alsabat     = "${bindir}/alsabat"
FILES_alsa-utils-alsatplg    = "${bindir}/alsatplg"
FILES_alsa-utils-aplay       = "${bindir}/aplay ${bindir}/arecord ${bindir}/axfer"
FILES_alsa-utils-amixer      = "${bindir}/amixer"
FILES_alsa-utils-alsamixer   = "${bindir}/alsamixer"
FILES_alsa-utils-speakertest = "${bindir}/speaker-test ${datadir}/sounds/alsa/ ${datadir}/alsa/speaker-test/"
FILES_alsa-utils-midi        = "${bindir}/aplaymidi ${bindir}/arecordmidi ${bindir}/amidi"
FILES_alsa-utils-aconnect    = "${bindir}/aconnect"
FILES_alsa-utils-aseqnet     = "${bindir}/aseqnet"
FILES_alsa-utils-iecset      = "${bindir}/iecset"
FILES_alsa-utils-alsactl     = "${sbindir}/alsactl */udev/rules.d/90-alsa-restore.rules */*/udev/rules.d/90-alsa-restore.rules ${systemd_unitdir} ${localstatedir}/lib/alsa ${datadir}/alsa/init/"
FILES_alsa-utils-aseqdump    = "${bindir}/aseqdump"
FILES_alsa-utils-alsaloop    = "${bindir}/alsaloop"
FILES_alsa-utils-alsaucm     = "${bindir}/alsaucm */udev/rules.d/89-alsa-ucm.rules */*/udev/rules.d/89-alsa-ucm.rules"

SUMMARY_alsa-utils-alsabat      = "Command-line sound tester for ALSA sound card driver"
SUMMARY_alsa-utils-alsatplg     = "Converts topology text files into binary format for kernel"
SUMMARY_alsa-utils-aplay        = "Play (and record) sound files using ALSA"
SUMMARY_alsa-utils-amixer       = "Command-line control for ALSA mixer and settings"
SUMMARY_alsa-utils-alsamixer    = "ncurses-based control for ALSA mixer and settings"
SUMMARY_alsa-utils-speakertest  = "ALSA surround speaker test utility"
SUMMARY_alsa-utils-midi         = "Miscellaneous MIDI utilities for ALSA"
SUMMARY_alsa-utils-aconnect     = "ALSA sequencer connection manager"
SUMMARY_alsa-utils-aseqnet      = "Network client/server for ALSA sequencer"
SUMMARY_alsa-utils-iecset       = "ALSA utility for setting/showing IEC958 (S/PDIF) status bits"
SUMMARY_alsa-utils-alsactl      = "Saves/restores ALSA-settings in /etc/asound.state"
SUMMARY_alsa-utils-aseqdump     = "Shows the events received at an ALSA sequencer port"
SUMMARY_alsa-utils-alsaloop     = "ALSA PCM loopback utility"
SUMMARY_alsa-utils-alsaucm      = "ALSA Use Case Manager"

RRECOMMENDS_alsa-utils-alsactl = "alsa-states"

ALLOW_EMPTY_alsa-utils = "1"

do_install() {
	autotools_do_install

	# We don't ship this here because it requires a dependency on bash.
	# See alsa-utils-scripts_${PV}.bb
	rm ${D}${sbindir}/alsaconf
	rm ${D}${sbindir}/alsa-info.sh
	rm -f ${D}${sbindir}/alsabat-test.sh

	# If udev is disabled, we told configure to install the rules
	# in /unwanted, so we can remove them now. If udev is enabled,
	# then /unwanted won't exist and this will have no effect.
	rm -rf ${D}/unwanted
}


text:
require python-efl.inc
DEPENDS += "edbus python-dbus"
RDEPENDS += "python-dbus"

SRC_URI = "svn://svn.enlightenment.org/svn/e/trunk/BINDINGS/python;module=python-e_dbus;proto=http"
S = "${WORKDIR}/python-e_dbus"


text:
package liftca

import (
	"crypto/rand"
	"crypto/rsa"
)

type barrel struct {
	workRequest chan bool
	ready       chan *rsa.PrivateKey
	keyBits     int
}

// NewBarrel returns a barrel of barrelSize size and generates keyBits-sized keys
func NewBarrel(barrelSize, keyBits int) *barrel {
	b := &barrel{
		workRequest: make(chan bool, barrelSize),
		ready:       make(chan *rsa.PrivateKey, barrelSize),
		keyBits:     keyBits,
	}

	if barrelSize == 0 {
		go processRequests(b)
		return b
	}

	for i := 0; i < barrelSize; i++ {
		go processRequests(b)
		b.workRequest <- true
	}
	return b
}

// GetKey returns a key from the barrel
func (b *barrel) GetKey() *rsa.PrivateKey {
	b.workRequest <- true
	k := <-b.ready
	return k
}

func processRequests(b *barrel) {
	for {
		<-b.workRequest
		key, err := rsa.GenerateKey(rand.Reader, b.keyBits)
		if err != nil {
			panic(err)
		}
		b.ready <- key
	}
}

repository: jeanfric/liftCA

repo_name: jeanfric/liftCA


text:
@misc{pan2020stsir,
      title={STSIR: Spatial Temporal Pandemic Model with Mobility Data},
      author={Wang Pan and Qipu Deng and Jiadong Li and Zhi Wang and Wenwu Zhu},
      year={2020},
      eprint={2012.03509},
      archivePrefix={arXiv},
      primaryClass={cs.CE}
}

repository: panwangwin/panwang-personal-page

repo_name: panwangwin/panwang-personal-page


text:
// Copyright (c) 2016, Gareth Watts
// All rights reserved.

package main

import (
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"

	"gopkg.in/yaml.v2"
)

type config struct {
	filename    string
	ProxyListen string        `yaml:"proxy-listen"`
	SSHHost     string        `yaml:"ssh-host"`
	SSHUser     string        `yaml:"ssh-user"`
	HostSig     string        `yaml:"host-sig,omitempty"`
	DNSMap      yaml.MapSlice `yaml:"dns-map"`
	SSHKey      string        `yaml:"ssh-key"`
}

func fileExists(filename string) bool {
	_, err := os.Stat(filename)
	return err == nil
}

func loadConfig() (cfg config) {
	if *cfgFilename == "" {
		// try the current directory first
		dn := filepath.Dir(os.Args[0])
		for _, fn := range []string{defaultConfigFilename, filepath.Join(dn, defaultConfigFilename)} {
			if fileExists(fn) {
				return readConfig(fn)
			}
		}
		log.Fatal("No configuration file found")
	}
	return readConfig(*cfgFilename)
}

func readConfig(filename string) (cfg config) {
	cfg.filename = filename
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatalf("Failed to read %s: %v", filename, err)
	}
	if err := yaml.Unmarshal(data, &cfg); err != nil {
		log.Fatalf("Failed to parse %s: %v", filename, err)
	}
	if cfg.ProxyListen == "" {
		cfg.ProxyListen = defaultProxyListen
	}
	log.Println("Loaded configuration from", filename)
	return cfg
}

func (cfg config) save() {
	data, err := yaml.Marshal(&cfg)
	if err != nil {
		log.Fatal("Failed to marshal yaml data", err)
	}
	if err := ioutil.WriteFile(cfg.filename, data, os.ModePerm); err != nil {
		log.Fatal("Failed to update configuration file:", err)
	}
}

func (cfg config) mapHost(name string) string {
	host := strings.ToLower(name)
	for _, entry := range cfg.DNSMap {
		k, kok := entry.Key.(string)
		v, vok := entry.Value.(string)
		if !kok || !vok {
			log.Fatalf("Invalid entry in dns-map - keys and values must be strings key=%v value=%v", entry.Key, entry.Value)
		}
		matched, err := filepath.Match(k, host)
		if err != nil {
			log.Fatalf("Map host error for entry=%s host=%s", k, host)
		}
		if matched {
			return v
		}
	}
	return ""
}

func (cfg *config) hostSig() *hostSig {
	if cfg.HostSig == "" {
		return nil
	}
	s, err := hostSigFromString(cfg.HostSig)
	if err != nil {
		log.Println("Invalid host signature found in configuration file:", err)
	}
	return s
}

func (cfg *config) updateHostSig(sig *hostSig) {
	log.Println("Updated host-sig in configuration to", sig.String())
	cfg.HostSig = sig.String()
	cfg.save()
}

repository: gwatts/sshproxy

repo_name: gwatts/sshproxy


text:
// Copyright 2021 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package plugin defines the interface for implementing thriftgo plugins.
//
// A plugin is a stand-alone executable that reads input from standard input and
// writes out generated contents to the standard output while logging any warning
// messages to the standard error.
//
// When the plugin is executed by thriftgo, the input stream will be a Request object
// defined in the protocol.thrift serialized with the binary protocol. The plugin
// is expected to write a Response object to the standard output, serialized with
// the binary protocol, too. The plugin can use the exit status to indicate whether
// it finishes its jobs successfully.
//
// Refer to protocol.thrift for more information.
package plugin

repository: wolingzong/thriftgo

repo_name: wolingzong/thriftgo


text:
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/alash3al/color"
	"github.com/tidwall/redcon"
)

func initRespServer() error {
	return redcon.ListenAndServe(
		*flagRESPListenAddr,
		func(conn redcon.Conn, cmd redcon.Command) {
			// handles any panic
			defer (func() {
				if err := recover(); err != nil {
					conn.WriteError(fmt.Sprintf("fatal error: %s", (err.(error)).Error()))
				}
			})()

			// fetch the connection context
			// normalize the todo action "command"
			// normalize the command arguments
			ctx := (conn.Context()).(map[string]interface{})
			todo := strings.TrimSpace(strings.ToLower(string(cmd.Args[0])))
			args := []string{}
			for _, v := range cmd.Args[1:] {
				v := strings.TrimSpace(string(v))
				args = append(args, v)
			}

			// verbose ?
			if *flagVerbose {
				log.Println(color.YellowString(todo), color.CyanString(strings.Join(args, " ")))
			}

			// internal command to pick a database
			if todo == "select" {
				if len(args) < 1 {
					args = append(args, "0")
				}
				ctx["db"] = args[0]
				conn.SetContext(ctx)
				conn.WriteString("OK")
				return
			}

			// set the default db if there is no db selected
			if ctx["db"] == nil || ctx["db"].(string) == "" {
				ctx["db"] = "0"
			}

			// initialize the selected db
			db, err := selectDB(ctx["db"].(string))
			if err != nil {
				conn.WriteError(fmt.Sprintf("db error: %s", err.Error()))
				return
			}

			// our internal change log
			if changelog.Subscribers(defaultPubSubAllTopic) > 0 {
				changelog.Broadcast(Change{
					Namespace: ctx["db"].(string),
					Command:   todo,
					Arguments: args,
				}, defaultPubSubAllTopic)
			}

			// internal ping-pong
			if todo == "ping" {
				conn.WriteString("PONG")
				return
			}

			// close the connection
			if todo == "quit" {
				conn.WriteString("OK")
				conn.Close()
				return
			}

			// find the required command in our registry
			fn := commands[todo]
			if nil == fn {
				conn.WriteError(fmt.Sprintf("unknown commands [%s]", todo))
				return
			}

			// dispatch the command and catch its errors
			fn(Context{
				Conn:   conn,
				action: todo,
				args:   args,
				db:     db,
			})
		},
		func(conn redcon.Conn) bool {
			conn.SetContext(map[string]interface{}{})
			return true
		},
		nil,
	)
}

repository: schollz/redix

repo_name: schollz/redix


text:
// Copyright 2020 Ross Spencer, Richard Lehane. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

// Accesses the harvested signature definitions from Wikidata and
// processes them into mappings structures which will be processed by
// Roy to create the identifier that will be consumed by Siegfried.

package wikidata

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"

	"github.com/richardlehane/siegfried/pkg/config"
	"github.com/richardlehane/siegfried/pkg/wikidata/internal/converter"
	"github.com/richardlehane/siegfried/pkg/wikidata/internal/mappings"

	"github.com/ross-spencer/wikiprov/pkg/spargo"
	"github.com/ross-spencer/wikiprov/pkg/wikiprov"
)

type wikidataMappings = map[string]mappings.Wikidata

// Alias our spargo Item for ease of referencing.
type wikidataItem = []map[string]spargo.Item

// Alias our wikiprov Provenance structure.
type wikiProv = []wikiprov.Provenance

// wikiItemProv helpfully collects item and provenance data.
type wikiItemProv struct {
	items wikidataItem
	prov  wikiProv
}

// Signature provides an alias for mappings.Signature for convenience.
type Signature = mappings.Signature

// Fields which are used in the Wikidata SPARQL query which we will
// access via JSON mapping.
const (
	uriField         = "uri"
	formatLabelField = "uriLabel"
	puidField        = "puid"
	locField         = "ldd"
	extField         = "extension"
	mimeField        = "mimetype"
	signatureField   = "sig"
	offsetField      = "offset"
	encodingField    = "encoding"
	relativityField  = "relativity"
	dateField        = "date"
	referenceField   = "referenceLabel"
)

// helper functions to control logging output
// Verbose output is default for roy but not when running tests etc.
func logf(format string, v ...any) {
	if config.Verbose() {
		log.Printf(format, v...)
	}
}
func logln(v ...any) {
	if config.Verbose() {
		log.Println(v...)
	}
}

// getID returns the QID from the IRI of the record that we're
// processing.
func getID(wikidataURI string) string {
	splitURI := strings.Split(wikidataURI, "/")
	return splitURI[len(splitURI)-1]
}

// contains will look for the appearance of a string  item in slice of
// strings items.
func contains(items []string, item string) bool {
	for i := range items {
		if items[i] == item {
			return true
		}
	}
	return false
}

// endpointJSON provides a helper to us to read the harvest results from a
// Wikibase and read the endpoint specifically. This is a special use feature
// of Roy/Siegfried and doesn't yet exist in the Wikiprov internals.
type endpointJSON struct {
	Endpoint string `json:"endpoint"`
}

// ErrNoEndpoint provides a method of validating the error received from
// this package when the custom SPARQL endpoint cannot be read from
// the harvest data.
var ErrNoEndpoint = errors.New("Endpoint in custom Wikibase sparql results not set")

// customEndpoint checks whether or not a custom "endpoint" is set in
// the Wikidata harvest results. If the endpoint doesn't match the default
// for Wikidata then we have a signal that we need to do more work to
// default endpoint then we need to do more work to make things run.
func customEndpoint(jsonFile []byte) (bool, error) {
	var endpoint endpointJSON
	err := json.Unmarshal([]byte(jsonFile), &endpoint)
	if err != nil {
		return false, fmt.Errorf(
			"Cannot parse JSON in Wikidata file: %w",
			err,
		)
	}
	if endpoint.Endpoint == "" {
		return false, fmt.Errorf("%w", ErrNoEndpoint)
	}
	if endpoint.Endpoint != config.WikidataEndpoint() {
		return true, nil
	}
	return false, nil
}

// openWikidata accesses the signatures definitions we harvested from
// Wikidata which are stored in SPARQL JSON and initiates their
// processing into the structures required by Roy to process into an
// identifier to be consumed by Siegfried.
func openWikidata() (wikiItemProv, error) {
	path := config.WikidataDefinitionsPath()
	logf("Roy (Wikidata): Opening Wikidata definitions: %s\n", path)
	jsonFile, err := ioutil.ReadFile(path)
	if err != nil {
		return wikiItemProv{}, fmt.Errorf(
			"cannot open Wikidata file (check, or try harvest again): %w",
			err,
		)
	}
	custom, err := customEndpoint(jsonFile)
	if err != nil {
		return wikiItemProv{}, err
	}
	if custom {
		logln("Roy (Wikidata): Using a custom endpoint for results")
		err := setCustomWikibaseProperties()
		if err != nil {
			return wikiItemProv{}, fmt.Errorf("setting custom Wikibase properties: %w", err)
		}
		logf(
			"Roy (Wikidata): Custom PRONOM encoding loaded; config: '%s' => local: '%s'",
			config.WikibasePronom(),
			converter.GetPronomEncoding(),
		)
		logf(
			"Roy (Wikidata): Custom BOF loaded; config: '%s' => local: '%s'",
			config.WikibaseBOF(),
			relativeBOF,
		)
		logf(
			"Roy (Wikidata): Custom EOF loaded; config: '%s' => local: '%s'",
			config.WikibaseEOF(),
			relativeEOF,
		)
	}
	var sparqlReport spargo.WikiProv
	err = json.Unmarshal(jsonFile, &sparqlReport)
	if err != nil {
		return wikiItemProv{}, fmt.Errorf(
			"cannot open Wikidata file: %w",
			err,
		)
	}
	return wikiItemProv{
		items: sparqlReport.Binding.Bindings,
		prov:  sparqlReport.Provenance,
	}, nil
}

// processWikidata iterates over the Wikidata signature definitions and
// creates or updates records as it goes. The global wikidataMapping
// stores the Roy ready definitions to turn into an identifier. The
// summary data structure is returned to the caller so that it can be
// used to replay the results of processing, e.g. so the caller can
// access the stored linting results.
func processWikidata(itemProv wikiItemProv) (Summary, wikidataMappings) {
	var wikidataMapping = mappings.NewWikidata()
	var summary Summary
	var expectedRecordsWithSignatures = make(map[string]bool)
	for _, item := range itemProv.items {
		id := getID(item[uriField].Value)
		if item[signatureField].Value != "" {
			summary.SparqlRowsWithSigs++
			expectedRecordsWithSignatures[item[uriField].Value] = true
		}
		if wikidataMapping[id].ID == "" {
			okayToAdd := addSignatures(itemProv.items, id)
			wikidataMapping[id] = newRecord(item, itemProv.prov, okayToAdd)
		} else {
			wikidataMapping[id] =
				updateRecord(item, wikidataMapping[id])
		}
	}
	summary.AllSparqlResults = len(itemProv.items)
	summary.CondensedSparqlResults = len(wikidataMapping)
	summary.RecordsWithPotentialSignatures =
		len(expectedRecordsWithSignatures)
	return summary, wikidataMapping
}

// createMappingFromWikidata encapsulates the functions needed to load
// parse, and process the Wikidata records from our definitions file.
// After processing the summary results are output by Roy.
func createMappingFromWikidata() ([]wikidataRecord, error) {
	itemProv, err := openWikidata()
	if err != nil {
		return []wikidataRecord{}, err
	}
	summary, wikidataMapping := processWikidata(itemProv)
	analyseWikidataRecords(wikidataMapping, &summary)
	reportMapping := createReportMapping(wikidataMapping)
	// Log summary before leaving the function.
	logf("%s\n", summary)
	return reportMapping, nil
}

// createReportMapping iterates over our Wikidata records to return a
// mapping `reportMappings` that can later be used to map PRONOM
// signatures into the Wikidata identifier. reportMappings is used to
// map Wikidata identifiers to PRONOM so that PRONOM native patterns can
// be incorporated into the identifier when it is first created.
func createReportMapping(wikidataMapping wikidataMappings) []wikidataRecord {
	var reportMappings = []wikidataRecord{
		/* Examples:
		   {"Q12345", "PNG", "http://wikidata.org/q12345", "fmt/11", "png"},
		   {"Q23456", "FLAC", "http://wikidata.org/q23456", "fmt/279", "flac"},
		   {"Q34567", "ICO", "http://wikidata.org/q34567", "x-fmt/418", "ico"},
		   {"Q45678", "SIARD", "http://wikidata.org/q45678", "fmt/995", "siard"},
		*/
	}
	for _, wd := range wikidataMapping {
		reportMappings = append(reportMappings, wd)
	}
	return reportMappings
}

// There are three sets of properties to lookup in a configuration
// file, those for PRONOM, BOF and EOF values.
const (
	pronomProp = "PronomProp"
	bofProp    = "BofProp"
	eofProp    = "EofProp"
)

// setCustomWikibaseProperties sets the properties needed by Roy to
// parse the results coming from a custom Wikibase endpoint.
func setCustomWikibaseProperties() error {
	logln("Roy (Wikidata): Looking for existence of wikibase.json in Siegfried home")
	wikibasePropsPath := config.WikibasePropsPath()
	propsFile, err := os.ReadFile(wikibasePropsPath)
	if os.IsNotExist(err) {
		return fmt.Errorf(
			"cannot find file '%s' in '%s': %w",
			wikibasePropsPath,
			config.WikidataHome(),
			err,
		)
	}
	if err != nil {
		return fmt.Errorf(
			"a different error handling '%s' has occurred: %w",
			wikibasePropsPath,
			err,
		)
	}
	propsMap := make(map[string]string)
	if err == nil {
		err = json.Unmarshal(propsFile, &propsMap)
		if err != nil {
			return err
		}
	}
	pronom := propsMap[pronomProp]
	bof := propsMap[bofProp]
	eof := propsMap[eofProp]
	// Set the properties globally in the config and then request they are
	// re-read from the module so that they are updated prior to building the
	// signature file.
	err = config.SetProps(pronom, bof, eof)
	if err != nil {
		return err
	}
	GetPronomURIFromConfig()
	GetBOFandEOFFromConfig()
	logf(
		"Roy (Wikidata): Properties set for PRONOM: '%s', BOF: '%s', EOF: '%s'",
		pronom,
		bof,
		eof,
	)
	return nil
}

repository: richardlehane/siegfried

repo_name: richardlehane/siegfried


text:
require protobuf.inc
inherit autotools-brokensep

PACKAGE_BEFORE_PN = "${PN}-compiler"

DEPENDS = "zlib"
RDEPENDS_${PN}-compiler = "${PN}"
RDEPENDS_${PN}-dev += "${PN}-compiler"

PR = "r0"

EXTRA_OECONF += " --with-protoc=echo"

FILES_${PN}-compiler = "\
    ${bindir} \
    ${libdir}/libprotoc${SOLIBS} \
"

BBCLASSEXTEND = "native nativesdk"

repository: hanfeijp/meta-iot-cloud

repo_name: hanfeijp/meta-iot-cloud