<commit_before>package gatherrun

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"os"
	"strings"
	"time"
)

var (
	abuseInterval     = time.Minute * 30
	analyticsInterval = time.Hour * 24
)

type GatherRun struct {
	DestFolder string
	Exporter   Exporter
	Fetcher    Fetcher
	Output     *Gather
	ScriptType string
}

func Run(env, username string) {
	fetcher := &S3Fetcher{
		AccessKey:  "<KEY>",
		SecretKey:  "<KEY>",
		BucketName: "gather-vm",
		FileName:   "gather.tar",
		Region:     "us-east-1",
	}

	exporter := NewKodingExporter()

	opts := &Gather{Env: env, Username: username}

	go func() {
		New(fetcher, exporter, opts, "abuse").Run()
		New(fetcher, exporter, opts, "analytics").Run()
	}()

	abuseTimer := time.NewTimer(abuseInterval)
	analyticsTimer := time.NewTimer(analyticsInterval)

	for {
		select {
		case <-abuseTimer.C:
			New(fetcher, exporter, opts, "abuse").Run()
		case <-analyticsTimer.C:
			New(fetcher, exporter, opts, "analytics").Run()
		}
	}
}

func New(fetcher Fetcher, exporter Exporter, output *Gather, scriptType string) *GatherRun {
	tmpDir, err := ioutil.TempDir("/tmp", "gather")
	if err != nil {
		// TODO: how to deal with errs
	}

	return &GatherRun{
		Fetcher:    fetcher,
		Exporter:   exporter,
		DestFolder: tmpDir,
		Output:     output,
		ScriptType: scriptType,
	}
}

func (c *GatherRun) Run() error {
	defer c.Cleanup()

	binary, err := c.GetGatherBinary()
	if err != nil {
		return err
	}

	if err := c.Export(binary.Run()); err != nil {
		return err
	}

	return nil
}

func (c *GatherRun) GetGatherBinary() (*GatherBinary, error) {
	if err := os.MkdirAll(c.DestFolder, 0777); err != nil {
		return nil, err
	}

	if err := c.DownloadScripts(c.DestFolder); err != nil {
		return nil, err
	}

	tarFile := c.DestFolder + "/" + c.Fetcher.GetFileName()
	if err := untarFile(tarFile, c.DestFolder); err != nil {
		return nil, err
	}

	binaryPath := strings.TrimSuffix(tarFile, tarSuffix)
	return &GatherBinary{Path: binaryPath, ScriptType: c.ScriptType}, nil
}

func (c *GatherRun) CreateDestFolder() error {
	folderExists, err := exists(c.DestFolder)
	if err != nil {
		return err
	}
<commit_msg>gather: use path/filepath to avoid windows related issues<commit_after>package gatherrun

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"time"
)

var (
	abuseInterval     = time.Minute * 30
	analyticsInterval = time.Hour * 24
)

type GatherRun struct {
	DestFolder string
	Exporter   Exporter
	Fetcher    Fetcher
	Output     *Gather
	ScriptType string
}

func Run(env, username string) {
	fetcher := &S3Fetcher{
		AccessKey:  "<KEY>",
		SecretKey:  "<KEY>",
		BucketName: "gather-vm",
		FileName:   "gather.tar",
		Region:     "us-east-1",
	}

	exporter := NewKodingExporter()

	opts := &Gather{Env: env, Username: username}

	go func() {
		New(fetcher, exporter, opts, "abuse").Run()
		New(fetcher, exporter, opts, "analytics").Run()
	}()

	abuseTimer := time.NewTimer(abuseInterval)
	analyticsTimer := time.NewTimer(analyticsInterval)

	for {
		select {
		case <-abuseTimer.C:
			New(fetcher, exporter, opts, "abuse").Run()
		case <-analyticsTimer.C:
			New(fetcher, exporter, opts, "analytics").Run()
		}
	}
}

func New(fetcher Fetcher, exporter Exporter, output *Gather, scriptType string) *GatherRun {
	tmpDir, err := ioutil.TempDir("/tmp", "gather")
	if err != nil {
		// TODO: how to deal with errs
	}

	return &GatherRun{
		Fetcher:    fetcher,
		Exporter:   exporter,
		DestFolder: tmpDir,
		Output:     output,
		ScriptType: scriptType,
	}
}

func (c *GatherRun) Run() error {
	defer c.Cleanup()

	binary, err := c.GetGatherBinary()
	if err != nil {
		return err
	}

	if err := c.Export(binary.Run()); err != nil {
		return err
	}

	return nil
}

func (c *GatherRun) GetGatherBinary() (*GatherBinary, error) {
	if err := os.MkdirAll(c.DestFolder, 0777); err != nil {
		return nil, err
	}

	if err := c.DownloadScripts(c.DestFolder); err != nil {
		return nil, err
	}

	tarFile := filepath.Join(c.DestFolder, c.Fetcher.GetFileName())
	if err := untarFile(tarFile, c.DestFolder); err != nil {
		return nil, err
	}

	binaryPath := strings.TrimSuffix(tarFile, tarSuffix)
	return &GatherBinary{Path: binaryPath, ScriptType: c.ScriptType}, nil
}

func (c *GatherRun) CreateDestFolder() error {
	folderExists, err := exists(c.DestFolder)
	if err != nil {
		return err
	}

{'fraction_non_alphanumeric': 0.14426832069836895, 'fraction_numerical': 0.004594532506317482, 'mean_word_length': 3.8004410143329657, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3181571', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1646, 'n_words': 400}