<commit_before>package main

import (
	"io/ioutil"
	"os"
	"os/exec"
)

func run(cmd string, args ...string) error {
	return exec.Command(cmd, args...).Run()
}

func generateIndex() error {
	return run("index++", "-c", config.swishConfig, config.knowledgeDirectory)
}

func readScroll(id string) (string, error) {
	result, err := ioutil.ReadFile(config.knowledgeDirectory + id + ".tex")
	return string(result), err
}

func readTemplate(filename string) (string, error) {
	result, err := ioutil.ReadFile(config.templateDirectory + filename + ".tex")
	return string(result), err
}

// Write a TeX file with the given name and content to Alexandria's temp
// directory.
func writeTemp(id, data string) error {
	return ioutil.WriteFile(config.tempDirectory+id+".tex", []byte(data), 0644)
}

// Get the time a given file was last modified as a Unix time.
func getModTime(file string) (int64, error) {
	info, err := os.Stat(file)
	if err != nil {
		return -1, err
	}
	return info.ModTime().Unix(), nil
}

// Cache the newest modification of any of the template files as a Unix time
// (i.e. seconds since 1970-01-01).<commit_msg>Add helper functions for printStats<commit_after>package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
)

func LogError(err interface{}) {
	fmt.Fprintln(os.Stderr, err)
}

func TryLogError(err interface{}) {
	if err != nil {
		LogError(err)
	}
}

func run(cmd string, args ...string) error {
	return exec.Command(cmd, args...).Run()
}

func generateIndex() error {
	return run("index++", "-c", config.swishConfig, config.knowledgeDirectory)
}

func readScroll(id string) (string, error) {
	result, err := ioutil.ReadFile(config.knowledgeDirectory + id + ".tex")
	return string(result), err
}

func readTemplate(filename string) (string, error) {
	result, err := ioutil.ReadFile(config.templateDirectory + filename + ".tex")
	return string(result), err
}

// Write a TeX file with the given name and content to Alexandria's temp
// directory.
func writeTemp(id, data string) error {
	return ioutil.WriteFile(config.tempDirectory+id+".tex", []byte(data), 0644)
}

// Get the file size in bytes.
func getFileSize(file string) int64 {
	f, err := os.Open(file)
	if err != nil {
		LogError(err)
		return 0
	}
	defer f.Close()
	stat, err := f.Stat()
	if err != nil {
		LogError(err)
		return 0
	}
	return stat.Size()
}

// Compute the combined size of all files in a given directory.
func getDirSize(dir string) (int, int64) {
	directory, err := os.Open(dir)
	TryLogError(err)
	defer directory.Close()
	fileInfo, err := directory.Readdir(0)
	if err != nil {
		panic(err)
	}
	result := int64(0)
	for _, file := range fileInfo {
		result += file.Size()
	}
	return len(fileInfo), result
}

// Get the time a given file was last modified as a Unix time.
func getModTime(file string) (int64, error) {
	info, err := os.Stat(file)
	if err != nil {
		return -1, err
	}
	return info.ModTime().Unix(), nil
}

// Cache the newest modification of any of the template files as a Unix time
// (i.e. seconds since 1970-01-01).
{'fraction_non_alphanumeric': 0.14182058047493404, 'fraction_numerical': 0.013192612137203167, 'mean_word_length': 3.9884868421052633, 'pattern_counts': {'":': 0, '<': 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': '1226967', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1031, 'n_words': 328}