<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"runtime"
	"sort"
	"strings"
	"syscall"

	"github.com/mitchellh/osext"
)

const envPrefix = "VAGRANT_OLD_ENV"

func main() {
	debug := os.Getenv("VAGRANT_DEBUG_LAUNCHER") != ""

	// Get the path to the executable. This path doesn't resolve symlinks
	// so we have to do that afterwards to find the real binary.
	path, err := osext.Executable()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load Vagrant: %s\n", err)
		os.Exit(1)
	}
	if debug {
		log.Printf("launcher: path = %s", path)
	}
	for {
		fi, err := os.Lstat(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to stat executable: %s\n", err)
			os.Exit(1)
		}
		if fi.Mode()&os.ModeSymlink == 0 {
			break
		}

		// The executable is a symlink, so resolve it
		path, err = os.Readlink(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to load Vagrant: %s\n", err)
			os.Exit(1)
		}
		if debug {
			log.Printf("launcher: resolved symlink = %s", path)
		}
	}

	// Determine some basic directories that we use throughout
	path = filepath.Dir(filepath.Clean(path))
	installerDir := filepath.Dir(path)
	embeddedDir := filepath.Join(installerDir, "embedded")
	if debug {
		log.Printf("launcher: installerDir = %s", installerDir)
		log.Printf("launcher: embeddedDir = %s", embeddedDir)
	}

	// Find the Vagrant gem
	gemPaths, err := filepath.Glob(
		filepath.Join(embeddedDir, "gems", "gems", "vagrant-*"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to find Vagrant: %s\n", err)
		os.Exit(1)
	}
	if debug {
		log.Printf("launcher: gemPaths (initial) = %#v", gemPaths)
	}
	for i := 0; i < len(gemPaths); i++ {
		fullPath := filepath.Join(gemPaths[i], "lib", "vagrant", "version.rb")
		if _, err := os.Stat(fullPath); err != nil {
			if debug {
				log.Printf("launcher: bad gemPath += %s", fullPath)
			}

			gemPaths = append(gemPaths[:i], gemPaths[i+1:]...)
			i--
		}
	}
	if len(gemPaths) == 0 {
		fmt.Fprintf(os.Stderr, "Failed to find Vagrant!\n")
		os.Exit(1)
	}
	gemPath := gemPaths[len(gemPaths)-1]
	vagrantExecutable := filepath.Join(gemPath, "bin", "vagrant")
	if debug {
		log.Printf("launcher: gemPaths (final) = %#v", gemPaths)
		log.Printf("launcher: gemPath = %s", gemPath)
	}

	// Setup the CPP/LDFLAGS so that native extensions can be
	// properly compiled into the Vagrant environment.
	cppflags := ""
	cflags := ""
	ldflags := ""
	mingwArchDir := "x86_64-w64-mingw32"
	mingwDir := "mingw64"
	if runtime.GOOS == "windows" {
		// Check if we are in a 32bit or 64bit install
		mingwTestPath := filepath.Join(embeddedDir, "mingw64")
		if _, err := os.Stat(mingwTestPath); err != nil {
			log.Printf("launcher: detected 32bit Windows installation")
			mingwDir = "mingw32"
			mingwArchDir = "i686-w64-mingw32"
		}
		cflags := "-I" + filepath.Join(embeddedDir, mingwDir, mingwArchDir, "include") +
			" -I" + filepath.Join(embeddedDir, mingwDir, "include") +
			" -I" + filepath.Join(embeddedDir, "usr", "include")
		cppflags := "-I" + filepath.Join(embeddedDir, mingwDir, mingwArchDir, "include") +
			" -I" + filepath.Join(embeddedDir, mingwDir, "include") +
			" -I" + filepath.Join(embeddedDir, "usr", "include")
		ldflags := "-L" + filepath.Join(embeddedDir, mingwDir, mingwArchDir, "lib") +
			" -L" + filepath.Join(embeddedDir, mingwDir, "lib") +
			" -L" + filepath.Join(embeddedDir, "usr", "lib")
		if original := os.Getenv("CFLAGS"); original != "" {
			cflags = original + " " + cflags
		}
		if original := os.Getenv("CPPFLAGS"); original != "" {
			cppflags = original + " " + cppflags
		}
		if original := os.Getenv("LDFLAGS"); original != "" {
			ldflags = original + " " + ldflags
		}
	} else {
		cppflags := "-I" + filepath.Join(embeddedDir, "include") +
			" -I" + filepath.Join(embeddedDir, "include", "libxml2")
		ldflags := "-L" + filepath.Join(embeddedDir, "lib")
		if original := os.Getenv("CPPFLAGS"); original != "" {
			cppflags = original + " " + cppflags
		}
		if original := os.Getenv("LDFLAGS"); original != "" {
			ldflags = original + " " + ldflags
		}
		cflags := "-I" + filepath.Join(embeddedDir, "include") +
			" -I" + filepath.Join(embeddedDir, "include", "libxml2")
		if original := os.Getenv("CFLAGS"); original != "" {
			cflags = original + " " + cflags
		}
	}

	// Set the PATH to include the proper paths into our embedded dir
	path = os.Getenv("PATH")
	if runtime.GOOS == "windows" {
		path = fmt.Sprintf(
			"%s;%s;%s",
			filepath.Join(embeddedDir, mingwDir, "bin"),
			filepath.Join(embeddedDir, "usr", "bin"),
			path)
	} else {
		path = fmt.Sprintf("%s:%s",
			filepath.Join(embeddedDir, "bin"), path)
	}

	// Allow users to specify a custom SSL cert
	sslCertFile := os.Getenv("SSL_CERT_FILE")
	if sslCertFile == "" {
		sslCertFile = filepath.Join(embeddedDir, "cacert.pem")
	}

	newEnv := map[string]string{
		// Setup the environment to prefer our embedded dir over
		// anything the user might have setup on his/her system.
		"CPPFLAGS":      cppflags,
		"CFLAGS":        cflags,
		"GEM_HOME":      filepath.Join(embeddedDir, "gems"),
		"GEM_PATH":      filepath.Join(embeddedDir, "gems"),
		"GEMRC":         filepath.Join(embeddedDir, "etc", "gemrc"),
		"LDFLAGS":       ldflags,
		"PATH":          path,
		"SSL_CERT_FILE": sslCertFile,

		// Instruct nokogiri installations to use libraries provided
		// by the installer
		"NOKOGIRI_USE_SYSTEM_LIBRARIES": "true",

		// Environmental variables used by Vagrant itself
		"VAGRANT_EXECUTABLE":             vagrantExecutable,
		"VAGRANT_INSTALLER_ENV":          "1",
		"VAGRANT_INSTALLER_EMBEDDED_DIR": embeddedDir,
		"VAGRANT_INSTALLER_VERSION":      "2",
	}

	// Unset any RUBYOPT, we don't want this bleeding into our runtime
	newEnv["RUBYOPT"] = ""
	// Unset any RUBYLIB, we don't want this bleeding into our runtime
	newEnv["RUBYLIB"] = ""

	if runtime.GOOS == "darwin" {
		configure_args := "-Wl,rpath," + filepath.Join(embeddedDir, "lib")
		if original_configure_args := os.Getenv("CONFIGURE_ARGS"); original_configure_args != "" {
			configure_args = original_configure_args + " " + configure_args
		}
		newEnv["CONFIGURE_ARGS"] = configure_args
	}

  if runtime.GOOS == "windows" {
		newEnv["PKG_CONFIG_PATH"] = filepath.Join(embeddedDir, mingwDir, "lib", "pkgconfig") +
			":" + filepath.Join(embeddedDir, "usr", "lib", "pkgconfig")
	} else {
		newEnv["PKG_CONFIG_PATH"] = filepath.Join(embeddedDir, "lib", "pkgconfig")
	}

	// Store the "current" environment so Vagrant can restore it when shelling
	// out.
	for _, value := range os.Environ() {
		idx := strings.IndexRune(value, '=')
		key := fmt.Sprintf("%s_%s", envPrefix, value[:idx])
		newEnv[key] = value[idx+1:]
	}
	if debug {
		keys := make([]string, 0, len(newEnv))
		for k, _ := range newEnv {
			keys = append(keys, k)
		}
		sort.Strings(keys)

		for _, k := range keys {
			log.Printf("launcher: env %q = %q", k, newEnv[k])
		}
	}

	// Set all the environmental variables
	for k, v := range newEnv {
		if err := os.Setenv(k, v); err != nil {
			fmt.Fprintf(os.Stderr, "Error setting env var %s: %s\n", k, err)
			os.Exit(1)
		}
	}

	// Determine the path to Ruby and then start the Vagrant process
	rubyPath := filepath.Join(embeddedDir, "bin", "ruby")
	if runtime.GOOS == "windows" {
		rubyPath = filepath.Join(embeddedDir, mingwDir, "bin", "ruby") + ".exe"
	}

	// Prior to starting the command, we ignore interrupts. Vagrant itself
	// handles these, so the launcher should just wait until that exits.
	signal.Ignore(os.Interrupt)

	cmd := exec.Command(rubyPath)
	cmd.Args = make([]string, len(os.Args)+1)
	cmd.Args[0] = "ruby"
	cmd.Args[1] = vagrantExecutable
	copy(cmd.Args[2:], os.Args[1:])
	if debug {
		log.Printf("launcher: rubyPath = %s", rubyPath)
		log.Printf("launcher: args = %#v", cmd.Args)
	}

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr<commit_msg>Wrap process with winpty if required by execution environment on Windows platform<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"runtime"
	"sort"
	"strings"
	"syscall"

	"github.com/mitchellh/osext"
)

const envPrefix = "VAGRANT_OLD_ENV"

func main() {
	debug := os.Getenv("VAGRANT_DEBUG_LAUNCHER") != ""

	// Get the path to the executable. This path doesn't resolve symlinks
	// so we have to do that afterwards to find the real binary.
	path, err := osext.Executable()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to load Vagrant: %s\n", err)
		os.Exit(1)
	}
	if debug {
		log.Printf("launcher: path = %s", path)
	}
	// Retain this path in case we need to re-launch
	launcher_path := path

	for {
		fi, err := os.Lstat(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to stat executable: %s\n", err)
			os.Exit(1)
		}
		if fi.Mode()&os.ModeSymlink == 0 {
			break
		}

		// The executable is a symlink, so resolve it
		path, err = os.Readlink(path)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to load Vagrant: %s\n", err)
			os.Exit(1)
		}
		if debug {
			log.Printf("launcher: resolved symlink = %s", path)
		}
	}

	// Determine some basic directories that we use throughout
	path = filepath.Dir(filepath.Clean(path))
	installerDir := filepath.Dir(path)
	embeddedDir := filepath.Join(installerDir, "embedded")
	if debug {
		log.Printf("launcher: installerDir = %s", installerDir)
		log.Printf("launcher: embeddedDir = %s", embeddedDir)
	}

	// Find the Vagrant gem
	gemPaths, err := filepath.Glob(
		filepath.Join(embeddedDir, "gems", "gems", "vagrant-*"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to find Vagrant: %s\n", err)
		os.Exit(1)
	}
	if debug {
		log.Printf("launcher: gemPaths (initial) = %#v", gemPaths)
	}
	for i := 0; i < len(gemPaths); i++ {
		fullPath := filepath.Join(gemPaths[i], "lib", "vagrant", "version.rb")
		if _, err := os.Stat(fullPath); err != nil {
			if debug {
				log.Printf("launcher: bad gemPath += %s", fullPath)
			}

			gemPaths = append(gemPaths[:i], gemPaths[i+1:]...)
			i--
		}
	}
	if len(gemPaths) == 0 {
		fmt.Fprintf(os.Stderr, "Failed to find Vagrant!\n")
		os.Exit(1)
	}
	gemPath := gemPaths[len(gemPaths)-1]
	vagrantExecutable := filepath.Join(gemPath, "bin", "vagrant")
	if debug {
		log.Printf("launcher: gemPaths (final) = %#v", gemPaths)
		log.Printf("launcher: gemPath = %s", gemPath)
	}

	// Setup the CPP/LDFLAGS so that native extensions can be
	// properly compiled into the Vagrant environment.
	cppflags := ""
	cflags := ""
	ldflags := ""
	mingwArchDir := "x86_64-w64-mingw32"
	mingwDir := "mingw64"
	if runtime.GOOS == "windows" {
		// Check if we are in a 32bit or 64bit install
		mingwTestPath := filepath.Join(embeddedDir, "mingw64")
		if _, err := os.Stat(mingwTestPath); err != nil {
			log.Printf("launcher: detected 32bit Windows installation")
			mingwDir = "mingw32"
			mingwArchDir = "i686-w64-mingw32"
		}
		cflags := "-I" + filepath.Join(embeddedDir, mingwDir, mingwArchDir, "include") +
			" -I" + filepath.Join(embeddedDir, mingwDir, "include") +
			" -I" + filepath.Join(embeddedDir, "usr", "include")
		cppflags := "-I" + filepath.Join(embeddedDir, mingwDir, mingwArchDir, "include") +
			" -I" + filepath.Join(embeddedDir, mingwDir, "include") +
			" -I" + filepath.Join(embeddedDir, "usr", "include")
		ldflags := "-L" + filepath.Join(embeddedDir, mingwDir, mingwArchDir, "lib") +
			" -L" + filepath.Join(embeddedDir, mingwDir, "lib") +
			" -L" + filepath.Join(embeddedDir, "usr", "lib")
		if original := os.Getenv("CFLAGS"); original != "" {
			cflags = original + " " + cflags
		}
		if original := os.Getenv("CPPFLAGS"); original != "" {
			cppflags = original + " " + cppflags
		}
		if original := os.Getenv("LDFLAGS"); original != "" {
			ldflags = original + " " + ldflags
		}
	} else {
		cppflags := "-I" + filepath.Join(embeddedDir, "include") +
			" -I" + filepath.Join(embeddedDir, "include", "libxml2")
		ldflags := "-L" + filepath.Join(embeddedDir, "lib")
		if original := os.Getenv("CPPFLAGS"); original != "" {
			cppflags = original + " " + cppflags
		}
		if original := os.Getenv("LDFLAGS"); original != "" {
			ldflags = original + " " + ldflags
		}
		cflags := "-I" + filepath.Join(embeddedDir, "include") +
			" -I" + filepath.Join(embeddedDir, "include", "libxml2")
		if original := os.Getenv("CFLAGS"); original != "" {
			cflags = original + " " + cflags
		}
	}

	// Set the PATH to include the proper paths into our embedded dir
	path = os.Getenv("PATH")
	if runtime.GOOS == "windows" {
		path = fmt.Sprintf(
			"%s;%s;%s",
			filepath.Join(embeddedDir, mingwDir, "bin"),
			filepath.Join(embeddedDir, "usr", "bin"),
			path)
	} else {
		path = fmt.Sprintf("%s:%s",
			filepath.Join(embeddedDir, "bin"), path)
	}

	// Allow users to specify a custom SSL cert
	sslCertFile := os.Getenv("SSL_CERT_FILE")
	if sslCertFile == "" {
		sslCertFile = filepath.Join(embeddedDir, "cacert.pem")
	}

	newEnv := map[string]string{
		// Setup the environment to prefer our embedded dir over
		// anything the user might have setup on his/her system.
		"CPPFLAGS":      cppflags,
		"CFLAGS":        cflags,
		"GEM_HOME":      filepath.Join(embeddedDir, "gems"),
		"GEM_PATH":      filepath.Join(embeddedDir, "gems"),
		"GEMRC":         filepath.Join(embeddedDir, "etc", "gemrc"),
		"LDFLAGS":       ldflags,
		"PATH":          path,
		"SSL_CERT_FILE": sslCertFile,

		// Instruct nokogiri installations to use libraries provided
		// by the installer
		"NOKOGIRI_USE_SYSTEM_LIBRARIES": "true",

		// Environmental variables used by Vagrant itself
		"VAGRANT_EXECUTABLE":             vagrantExecutable,
		"VAGRANT_INSTALLER_ENV":          "1",
		"VAGRANT_INSTALLER_EMBEDDED_DIR": embeddedDir,
		"VAGRANT_INSTALLER_VERSION":      "2",
	}

	// Unset any RUBYOPT, we don't want this bleeding into our runtime
	newEnv["RUBYOPT"] = ""
	// Unset any RUBYLIB, we don't want this bleeding into our runtime
	newEnv["RUBYLIB"] = ""

	if runtime.GOOS == "darwin" {
		configure_args := "-Wl,rpath," + filepath.Join(embeddedDir, "lib")
		if original_configure_args := os.Getenv("CONFIGURE_ARGS"); original_configure_args != "" {
			configure_args = original_configure_args + " " + configure_args
		}
		newEnv["CONFIGURE_ARGS"] = configure_args
	}

	// Set pkg-config paths
	if runtime.GOOS == "windows" {
		newEnv["PKG_CONFIG_PATH"] = filepath.Join(embeddedDir, mingwDir, "lib", "pkgconfig") +
			":" + filepath.Join(embeddedDir, "usr", "lib", "pkgconfig")
	} else {
		newEnv["PKG_CONFIG_PATH"] = filepath.Join(embeddedDir, "lib", "pkgconfig")
	}

	// Detect custom windows environment (cygwin/msys/etc)
	if runtime.GOOS == "windows" {
		// If VAGRANT_DETECTED_OS is provided by the user let that value
		// take precedence over any discovery.
		if os.Getenv("VAGRANT_DETECTED_OS") != "" {
			newEnv["VAGRANT_DETECTED_OS"] = os.Getenv("VAGRANT_DETECTED_OS")
		} else if os.Getenv("OSTYPE") != "" {
			newEnv["VAGRANT_DETECTED_OS"] = os.Getenv("OSTYPE")
		} else {
			uname, err := exec.Command("uname", "-o").Output()
			if err == nil {
				newEnv["VAGRANT_DETECTED_OS"] = strings.ToLower(strings.Replace(fmt.Sprintf("%s", uname), "\n", "", -1))
			}
		}
		if debug && newEnv["VAGRANT_DETECTED_OS"] != "" {
			log.Printf("launcher: windows detected OS - %s", newEnv["VAGRANT_DETECTED_OS"])
		}
	}

	// Store the "current" environment so Vagrant can restore it when shelling
	// out.
	for _, value := range os.Environ() {
		idx := strings.IndexRune(value, '=')
		key := fmt.Sprintf("%s_%s", envPrefix, value[:idx])
		newEnv[key] = value[idx+1:]
	}
	if debug {
		keys := make([]string, 0, len(newEnv))
		for k, _ := range newEnv {
			keys = append(keys, k)
		}
		sort.Strings(keys)

		for _, k := range keys {
			log.Printf("launcher: env %q = %q", k, newEnv[k])
		}
	}

	// Determine the path to Ruby and then start the Vagrant process
	rubyPath := filepath.Join(embeddedDir, "bin", "ruby")
	if runtime.GOOS == "windows" {
		rubyPath = filepath.Join(embeddedDir, mingwDir, "bin", "ruby") + ".exe"
	}

	// Prior to starting the command, we ignore interrupts. Vagrant itself
	// handles these, so the launcher should just wait until that exits.
	signal.Ignore(os.Interrupt)

	// Check if running within a cygwin or msys type environment on Windows. If
	// we are, then wrap the execution with winpty to properly provide terminal
	// support to Vagrant. Without this we get the ever loved "stdin is not a tty"
	var cmd *exec.Cmd
	if runtime.GOOS == "windows" && os.Getenv("VAGRANT_WINPTY_WRAPPED") != "1" &&
		(newEnv["VAGRANT_DETECTED_OS"] == "msys" || newEnv["VAGRANT_DETECTED_OS"] == "cygwin") {
		os.Setenv("VAGRANT_WINPTY_WRAPPED", "1")
		winptyPath := filepath.Join(embeddedDir, "bin", newEnv["VAGRANT_DETECTED_OS"], "winpty.exe")
		cmd = exec.Command(winptyPath)
		cmd.Args = make([]string, len(os.Args)+1)
		cmd.Args[0] = "winpty"
		cmd.Args[1] = launcher_path
		copy(cmd.Args[2:], os.Args[1:])
		if debug {
			log.Printf("launcher: winpty re-launch (stdin will be a tty!)")
			log.Printf("launcher: winptyPath = %s", winptyPath)
		}
	} else {
		// Set all the environmental variables
		for k, v := range newEnv {
			if err := os.Setenv(k, v); err != nil {
				fmt.Fprintf(os.Stderr, "Error setting env var %s: %s\n", k, err)
				os.Exit(1)
			}
		}

		cmd = exec.Command(rubyPath)
		cmd.Args = make([]string, len(os.Args)+1)
		cmd.Args[0] = "ruby"
		cmd.Args[1] = vagrantExecutable
		copy(cmd.Args[2:], os.Args[1:])
		if debug {
			log.Printf("launcher: rubyPath = %s", rubyPath)
		}
	}

	if debug {
		log.Printf("launcher: args = %#v", cmd.Args)
	}

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
{'fraction_non_alphanumeric': 0.16392040595244883, 'fraction_numerical': 0.00604367409772507, 'mean_word_length': 3.672349493873202, 'pattern_counts': {'":': 28, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4330573', 'n_tokens_mistral': 7176, 'n_tokens_neox': 6521, 'n_words': 1553}