<commit_before>package main

import (
	"flag"
	"fmt"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"reflect"
	"strings"

	"github.com/d4l3k/go-pry/pry"
)

var contexts []pryContext

// ExecuteGoCmd runs the 'go' command with certain parameters.
func ExecuteGoCmd(args []string) {
	binary, lookErr := exec.LookPath("go")
	if lookErr != nil {
		panic(lookErr)
	}

	cmd := exec.Command(binary, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	cmd.Run()
}

// InjectPry walks the scope and replaces pry.Pry with pry.Apply(pry.Scope{...}).
func InjectPry(filePath string) (string, error) {
	Debug("Prying into %s\n", filePath)

	contexts = make([]pryContext, 0)

	fset := token.NewFileSet() // positions are relative to fset

	// Parse the file containing this very example
	// but stop after processing the imports.
	f, err := parser.ParseFile(fset, filePath, nil, 0)
	if err != nil {
		return "", err
	}

	packagePairs := []string{}
	for _, imp := range f.Imports {
		importStr := imp.Path.Value[1 : len(imp.Path.Value)-1]
		if importStr != "../pry" {
			dir := filepath.Dir(filePath)
			pkg, err := build.Import(importStr, dir, build.AllowBinary)
			if err != nil {
				panic(err)
			}
			pkgAst, err := parser.ParseDir(fset, pkg.Dir, nil, parser.ParseComments)
			if err != nil {
				panic(err)
			}
			pair := "\"" + pkg.Name + "\": pry.Package{Name: \"" + pkg.Name + "\", Functions: map[string]interface{}{"
			added := make(map[string]bool)
			for _, nPkg := range pkgAst {
				pair += GetExports(nPkg, added)
			}
			pair += "}}, "
			packagePairs = append(packagePairs, pair)
		}
	}

	var funcs []*ast.FuncDecl
	var vars []string

	// Print the imports from the file's AST.
	for k, v := range f.Scope.Objects {
		switch decl := v.Decl.(type) {
		case *ast.FuncDecl:
			funcs = append(funcs, decl)
		case *ast.ValueSpec:
			vars = append(vars, k)
		}
	}

	for _, f := range funcs {
		vars = append(vars, f.Name.Name)
		extractVariables(vars, f.Body.List)
	}

	fileTextBytes, err := ioutil.ReadFile(filePath)
	if err != nil {
		panic(err)
	}

	fileText := (string)(fileTextBytes)

	offset := 0

	if len(contexts) == 0 {
		return "", nil
	}

	Debug(" :: Found %d pry statements.\n", len(contexts))

	for _, context := range contexts {
		filteredVars := filterVars(context.Vars)
		obj := "&pry.Scope{Vals:map[string]interface{}{ "
		for _, v := range filteredVars {
			obj += "\"" + v + "\": " + v + ", "
		}
		obj += strings.Join(packagePairs, "")
		obj += "}}"
		text := "pry.Apply(" + obj + ")"
		fileText = fileText[0:context.Start+offset] + text + fileText[context.End+offset:]
		offset = len(text) - (context.End - context.Start)
	}

	newPath := filepath.Dir(filePath) + "/." + filepath.Base(filePath) + "pry"

	err = os.Rename(filePath, newPath)
	if err != nil {
		return "", err
	}
	ioutil.WriteFile(filePath, ([]byte)(fileText), 0644)
	return filePath, nil
}

// GenerateFile generates and executes a temp file with the given imports
func GenerateFile(imports []string) error {
	dir, err := ioutil.TempDir("", "pry")
	if err != nil {
		return err
	}
	file := "package main\nimport (\n\t\"github.com/d4l3k/go-pry/pry\"\n\n"
	for _, imp := range imports {
		file += fmt.Sprintf("\t%#v\n", imp)
	}
	file += ")\nfunc main() {\n\tpry.Pry()\n}"

	newPath := dir + "/main.go"
	ioutil.WriteFile(newPath, []byte(file), 0644)
	InjectPry(newPath)
	ExecuteGoCmd([]string{"run", newPath})
	err = os.RemoveAll(dir)
	if err != nil {
		return err
	}
	return nil
}

var debug bool

func main() {
	// Catch Ctrl-C
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for range c {<commit_msg>Fixes #8 and supports go1.3.3<commit_after>package main

import (
	"flag"
	"fmt"
	"go/ast"
	"go/build"
	"go/parser"
	"go/token"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"os/signal"
	"path/filepath"
	"reflect"
	"strings"

	"github.com/d4l3k/go-pry/pry"
)

var contexts []pryContext

// ExecuteGoCmd runs the 'go' command with certain parameters.
func ExecuteGoCmd(args []string) {
	binary, lookErr := exec.LookPath("go")
	if lookErr != nil {
		panic(lookErr)
	}

	cmd := exec.Command(binary, args...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	cmd.Run()
}

// InjectPry walks the scope and replaces pry.Pry with pry.Apply(pry.Scope{...}).
func InjectPry(filePath string) (string, error) {
	Debug("Prying into %s\n", filePath)

	contexts = make([]pryContext, 0)

	fset := token.NewFileSet() // positions are relative to fset

	// Parse the file containing this very example
	// but stop after processing the imports.
	f, err := parser.ParseFile(fset, filePath, nil, 0)
	if err != nil {
		return "", err
	}

	packagePairs := []string{}
	for _, imp := range f.Imports {
		importStr := imp.Path.Value[1 : len(imp.Path.Value)-1]
		if importStr != "../pry" {
			dir := filepath.Dir(filePath)
			pkg, err := build.Import(importStr, dir, build.AllowBinary)
			if err != nil {
				panic(err)
			}
			pkgAst, err := parser.ParseDir(fset, pkg.Dir, nil, parser.ParseComments)
			if err != nil {
				panic(err)
			}
			pair := "\"" + pkg.Name + "\": pry.Package{Name: \"" + pkg.Name + "\", Functions: map[string]interface{}{"
			added := make(map[string]bool)
			for _, nPkg := range pkgAst {
				pair += GetExports(nPkg, added)
			}
			pair += "}}, "
			packagePairs = append(packagePairs, pair)
		}
	}

	var funcs []*ast.FuncDecl
	var vars []string

	// Print the imports from the file's AST.
	for k, v := range f.Scope.Objects {
		switch decl := v.Decl.(type) {
		case *ast.FuncDecl:
			funcs = append(funcs, decl)
		case *ast.ValueSpec:
			vars = append(vars, k)
		}
	}

	for _, f := range funcs {
		vars = append(vars, f.Name.Name)
		extractVariables(vars, f.Body.List)
	}

	fileTextBytes, err := ioutil.ReadFile(filePath)
	if err != nil {
		panic(err)
	}

	fileText := (string)(fileTextBytes)

	offset := 0

	if len(contexts) == 0 {
		return "", nil
	}

	Debug(" :: Found %d pry statements.\n", len(contexts))

	for _, context := range contexts {
		filteredVars := filterVars(context.Vars)
		obj := "&pry.Scope{Vals:map[string]interface{}{ "
		for _, v := range filteredVars {
			obj += "\"" + v + "\": " + v + ", "
		}
		obj += strings.Join(packagePairs, "")
		obj += "}}"
		text := "pry.Apply(" + obj + ")"
		fileText = fileText[0:context.Start+offset] + text + fileText[context.End+offset:]
		offset = len(text) - (context.End - context.Start)
	}

	newPath := filepath.Dir(filePath) + "/." + filepath.Base(filePath) + "pry"

	err = os.Rename(filePath, newPath)
	if err != nil {
		return "", err
	}
	ioutil.WriteFile(filePath, ([]byte)(fileText), 0644)
	return filePath, nil
}

// GenerateFile generates and executes a temp file with the given imports
func GenerateFile(imports []string) error {
	dir, err := ioutil.TempDir("", "pry")
	if err != nil {
		return err
	}
	file := "package main\nimport (\n\t\"github.com/d4l3k/go-pry/pry\"\n\n"
	for _, imp := range imports {
		file += fmt.Sprintf("\t%#v\n", imp)
	}
	file += ")\nfunc main() {\n\tpry.Pry()\n}"

	newPath := dir + "/main.go"
	ioutil.WriteFile(newPath, []byte(file), 0644)
	InjectPry(newPath)
	ExecuteGoCmd([]string{"run", newPath})
	err = os.RemoveAll(dir)
	if err != nil {
		return err
	}
	return nil
}

var debug bool

func main() {
	// Catch Ctrl-C
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	go func() {
		for _ = range c {
{'fraction_non_alphanumeric': 0.17487220877051385, 'fraction_numerical': 0.005918751681463546, 'mean_word_length': 3.3658250146799764, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1926410', 'n_tokens_mistral': 3072, 'n_tokens_neox': 2853, 'n_words': 678}