<commit_before>// Copyright (c) 2015 SUSE LLC. 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.

package main

import (
	"crypto/tls"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/SUSE/dockerclient"
	"github.com/docker/docker/pkg/tlsconfig"
)

// dockerError encapsulates a dockerclient.WaitResult that has an exit status
// different than 0. This is done this way because, for some commands, zypper
// might set an exit code different than 0, even if there was no error. For
// example, the patch-check command can set the exit code 100, to determine
// that there are patches to be installed. In this case, the caller can decide
// what to do depending on the error returned by zypper. Therefore, the caller
// of functions such as `startContainer` should only care about this type if
// the command being implemented has this kind of behavior.
type dockerError struct {
	dockerclient.WaitResult
}

func (de dockerError) Error() string {
	return fmt.Sprintf("Command exited with status %d", de.ExitCode)
}

// The DockerClient interface lists all the functions that we use from Docker clients. Take
// a look at http://godoc.org/github.com/samalba/dockerclient if you want to
// read the documentation for each function.
type DockerClient interface {
	ListImages(all bool, filter string, filters *dockerclient.ListFilter) ([]*dockerclient.Image, error)
	ListContainers(all bool, size bool, filters string) ([]dockerclient.Container, error)

	CreateContainer(config *dockerclient.ContainerConfig, name string) (string, error)
	StartContainer(id string, config *dockerclient.HostConfig) error
	RemoveContainer(id string, force, volume bool) error
	KillContainer(id, signal string) error
	ResizeContainer(id string, isExec bool, width, height int) error
	Commit(id string, c *dockerclient.ContainerConfig, repo, tag, comment, author string) (string, error)

	Wait(id string) <-chan dockerclient.WaitResult

	ContainerLogs(id string, options *dockerclient.LogOptions) (io.ReadCloser, error)
}

// This global variable holds the instance to the docker client.
var dockerClient DockerClient

const (
	// The path in which the Docker client is listening to.
	dockerSocket = "unix:///var/run/docker.sock"

	// The timeout in which the container is allowed to run a command as given
	// to the `startContainer` function.
	containerTimeout = 2 * time.Second

	// tls-related files, code taken from docker/common.go
	defaultTrustKeyFile = "key.json"
	defaultCaFile       = "ca.pem"
	defaultKeyFile      = "key.pem"
	defaultCertFile     = "cert.pem"
)

// Use this function to safely retrieve the singleton instance of the Docker
// client. In order to guarantee such safety, the instance has to be
// initialized when no goroutines are being executed concurrently (e.g. the
// `init` or the `main` function).
func getDockerClient() DockerClient {
	if dockerClient != nil {
		return dockerClient
	}

	// We can safely discard the error. The connection will be started
	// successfully because internally `NewDockerClientTimeout` will handle the
	// connection as a dial for the http package. Therefore, it won't fail even
	// if the given URL does not exist. This is ok, since this possible error
	// will appear later on with subsequent commands.
	//
	// The only time it will return an error will be if the given URL has a bad
	// format, which won't happen.

	daemonUrl := dockerSocket
	if os.Getenv("DOCKER_HOST") != "" {
		daemonUrl = os.Getenv("DOCKER_HOST")
	}
<commit_msg>Increased the timeout for containers<commit_after>// Copyright (c) 2015 SUSE LLC. 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.

package main

import (
	"crypto/tls"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/SUSE/dockerclient"
	"github.com/docker/docker/pkg/tlsconfig"
)

// dockerError encapsulates a dockerclient.WaitResult that has an exit status
// different than 0. This is done this way because, for some commands, zypper
// might set an exit code different than 0, even if there was no error. For
// example, the patch-check command can set the exit code 100, to determine
// that there are patches to be installed. In this case, the caller can decide
// what to do depending on the error returned by zypper. Therefore, the caller
// of functions such as `startContainer` should only care about this type if
// the command being implemented has this kind of behavior.
type dockerError struct {
	dockerclient.WaitResult
}

func (de dockerError) Error() string {
	return fmt.Sprintf("Command exited with status %d", de.ExitCode)
}

// The DockerClient interface lists all the functions that we use from Docker clients. Take
// a look at http://godoc.org/github.com/samalba/dockerclient if you want to
// read the documentation for each function.
type DockerClient interface {
	ListImages(all bool, filter string, filters *dockerclient.ListFilter) ([]*dockerclient.Image, error)
	ListContainers(all bool, size bool, filters string) ([]dockerclient.Container, error)

	CreateContainer(config *dockerclient.ContainerConfig, name string) (string, error)
	StartContainer(id string, config *dockerclient.HostConfig) error
	RemoveContainer(id string, force, volume bool) error
	KillContainer(id, signal string) error
	ResizeContainer(id string, isExec bool, width, height int) error
	Commit(id string, c *dockerclient.ContainerConfig, repo, tag, comment, author string) (string, error)

	Wait(id string) <-chan dockerclient.WaitResult

	ContainerLogs(id string, options *dockerclient.LogOptions) (io.ReadCloser, error)
}

// This global variable holds the instance to the docker client.
var dockerClient DockerClient

const (
	// The path in which the Docker client is listening to.
	dockerSocket = "unix:///var/run/docker.sock"

	// The timeout in which the container is allowed to run a command as given
	// to the `startContainer` function.
	containerTimeout = 15 * time.Second

	// tls-related files, code taken from docker/common.go
	defaultTrustKeyFile = "key.json"
	defaultCaFile       = "ca.pem"
	defaultKeyFile      = "key.pem"
	defaultCertFile     = "cert.pem"
)

// Use this function to safely retrieve the singleton instance of the Docker
// client. In order to guarantee such safety, the instance has to be
// initialized when no goroutines are being executed concurrently (e.g. the
// `init` or the `main` function).
func getDockerClient() DockerClient {
	if dockerClient != nil {
		return dockerClient
	}

	// We can safely discard the error. The connection will be started
	// successfully because internally `NewDockerClientTimeout` will handle the
	// connection as a dial for the http package. Therefore, it won't fail even
	// if the given URL does not exist. This is ok, since this possible error
	// will appear later on with subsequent commands.
	//
	// The only time it will return an error will be if the given URL has a bad
	// format, which won't happen.

	daemonUrl := dockerSocket
	if os.Getenv("DOCKER_HOST") != "" {
		daemonUrl = os.Getenv("DOCKER_HOST")
	}

{'fraction_non_alphanumeric': 0.08737623762376237, 'fraction_numerical': 0.0035891089108910893, 'mean_word_length': 3.9790511398644486, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3561663', 'n_tokens_mistral': 2344, 'n_tokens_neox': 2209, 'n_words': 998}